├── README.md ├── bank_statement.csv ├── main.py ├── output ├── claude_solution.py ├── gemini_solution.py └── openai_solution.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # langchain-custom-llms 2 | -------------------------------------------------------------------------------- /bank_statement.csv: -------------------------------------------------------------------------------- 1 | Date,Description,Amount (USD) 2 | 2024-01-01,Walmart,35.20 3 | 2024-01-02,Shell Gas Station,45.00 4 | 2024-01-03,Netflix,15.99 5 | 2024-01-04,Starbucks,4.50 6 | 2024-01-05,Verizon Wireless,60.00 7 | 2024-01-06,Target,85.00 8 | 2024-01-07,Whole Foods,70.30 9 | 2024-01-08,AMC Theatres,25.00 10 | 2024-01-09,Spotify,9.99 11 | 2024-01-10,Chick-fil-A,18.40 12 | 2024-01-11,ExxonMobil,50.00 13 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | from langchain.llms.openai import OpenAI 3 | from langchain.llms.bedrock import Bedrock 4 | from langchain_google_genai import GoogleGenerativeAI 5 | from langchain.prompts import PromptTemplate 6 | from langchain.chains import SimpleSequentialChain 7 | from langchain.chains import LLMChain 8 | 9 | 10 | PROMPT_TEMPLATE_TEXT = """ 11 | Generate a table in CSV format from the following bank statement data. 12 | 13 | Add a column called "Category" and populate it with one of the following values: 14 | [Groceries, Transport, Entertainment, Shopping, Utilities, Eating Out, Unknown] 15 | 16 | {statement} 17 | """ 18 | 19 | CODING_TEMPLATE_TEXT = """ 20 | First, hard-code this data as a Python variable called 'items', with the category name and value. 21 | Then write a Python script to sum this data by 'Category' and print the results. 22 | 23 | {categorized_transactions} 24 | """ 25 | 26 | 27 | # OpenAI (GPT-3.5) LLM 28 | llm_open_ai = OpenAI(max_tokens=1024) 29 | 30 | # AWS Bedrock LLM 31 | BEDROCK_CLAUDE_MODEL = "anthropic.claude-v2" 32 | BEDROCK_LLAMA_MODEL = "meta.llama2-70b-chat-v1" 33 | llm_bedrock = Bedrock( 34 | credentials_profile_name="default", 35 | model_id=BEDROCK_CLAUDE_MODEL, 36 | model_kwargs={"max_tokens_to_sample": 1024}, 37 | ) 38 | 39 | # Google Gemini LLM 40 | llm_gemini = GoogleGenerativeAI( 41 | model="gemini-pro", 42 | max_output_tokens=1024, 43 | google_api_key=os.environ["GOOGLE_AI_API_KEY"], 44 | ) 45 | 46 | llm = llm_gemini # Or llm_bedrock or llm_open_ai. 47 | 48 | # Create the individual prompt templates. 49 | categorization_template = PromptTemplate.from_template(PROMPT_TEMPLATE_TEXT) 50 | coding_template = PromptTemplate.from_template(CODING_TEMPLATE_TEXT) 51 | 52 | # Create the chains. 53 | categorization_chain = LLMChain(llm=llm, prompt=categorization_template) 54 | coding_chain = LLMChain(llm=llm, prompt=coding_template) 55 | 56 | # Join them into a sequential chain. 57 | overall_chain = SimpleSequentialChain( 58 | chains=[categorization_chain, coding_chain], verbose=True 59 | ) 60 | 61 | # Load the bank statement data. 62 | with open("bank_statement.csv", "r") as f: 63 | bank_statement_data = f.read() 64 | 65 | # Run the chain using the bank statement data as input. 66 | overall_chain.run(bank_statement_data) 67 | -------------------------------------------------------------------------------- /output/claude_solution.py: -------------------------------------------------------------------------------- 1 | items = [ 2 | ["Groceries", 35.20], 3 | ["Transport", 45.00], 4 | ["Entertainment", 15.99], 5 | ["Eating Out", 4.50], 6 | ["Utilities", 60.00], 7 | ["Shopping", 85.00], 8 | ["Groceries", 70.30], 9 | ["Entertainment", 25.00], 10 | ["Entertainment", 9.99], 11 | ["Eating Out", 18.40], 12 | ["Transport", 50.00], 13 | ] 14 | 15 | category_totals = {} 16 | for item in items: 17 | category = item[0] 18 | amount = item[1] 19 | 20 | if category not in category_totals: 21 | category_totals[category] = 0 22 | 23 | category_totals[category] += amount 24 | 25 | print(category_totals) 26 | -------------------------------------------------------------------------------- /output/gemini_solution.py: -------------------------------------------------------------------------------- 1 | # Convert the data into a Python variable called 'items' 2 | items = [ 3 | ("Groceries", 35.20), 4 | ("Transport", 45.00), 5 | ("Entertainment", 15.99), 6 | ("Eating Out", 4.50), 7 | ("Utilities", 60.00), 8 | ("Shopping", 85.00), 9 | ("Groceries", 70.30), 10 | ("Entertainment", 25.00), 11 | ("Entertainment", 9.99), 12 | ("Eating Out", 18.40), 13 | ("Transport", 50.00), 14 | ] 15 | 16 | # Create a dictionary to store the sum of expenses for each category 17 | category_totals = {} 18 | 19 | # Iterate over the items and add the amount to the corresponding category 20 | for category, amount in items: 21 | category_totals[category] = category_totals.get(category, 0) + amount 22 | 23 | # Print the results 24 | for category, total in category_totals.items(): 25 | print(f"{category}: ${total:.2f}") 26 | -------------------------------------------------------------------------------- /output/openai_solution.py: -------------------------------------------------------------------------------- 1 | items = [ 2 | ("Groceries", 35.20), 3 | ("Transport", 45.00), 4 | ("Entertainment", 15.99), 5 | ("Eating Out", 4.50), 6 | ("Utilities", 60.00), 7 | ("Shopping", 85.00), 8 | ("Groceries", 70.30), 9 | ("Entertainment", 25.00), 10 | ("Entertainment", 9.99), 11 | ("Eating Out", 18.40), 12 | ("Transport", 50.00), 13 | ] 14 | 15 | category_totals = {} 16 | 17 | # Loop through items and add the amounts to the respective categories in category_totals 18 | for item in items: 19 | category = item[0] 20 | amount = item[1] 21 | if category in category_totals: 22 | category_totals[category] += amount 23 | else: 24 | category_totals[category] = amount 25 | 26 | # Print the results 27 | for category, total in category_totals.items(): 28 | print(category + ": $" + str(round(total, 2))) 29 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | boto3 # For AWS Bedrock 4 | langchain-google-genai # For Google AI --------------------------------------------------------------------------------