Last active
April 9, 2023 00:07
-
-
Save reedlaw/5f84c801c20ad5f1e6b16c6ae193b4a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain import OpenAI, LLMChain | |
from langchain import PromptTemplate, FewShotPromptTemplate | |
from langchain.llms import LlamaCpp | |
import csv | |
examples = [ | |
{"product": "Toothpaste", | |
"category": "Health:Dental"}, | |
{"product": "Toilet Flapper", | |
"category": "Home:Maintenance"}, | |
{"product": "Laptop Stand", | |
"category": "Computer:Accessories"}, | |
{"product": "Pressure Cooker", | |
"category": "Kitchen:Appliances"}, | |
{"product": "T-shirt", | |
"category": "Clothing"}, | |
{"product": "Bananas", | |
"category": "Grocery"}, | |
] | |
example_formatter_template = """ | |
Product: {product} | |
Category: {category}\n | |
""" | |
example_prompt = PromptTemplate( | |
input_variables=["product", "category"], | |
template=example_formatter_template, | |
) | |
few_shot_prompt = FewShotPromptTemplate( | |
examples=examples, | |
example_prompt=example_prompt, | |
prefix="Give the category of every product", | |
suffix="Product: {product}\nCategory:", | |
input_variables=["product"], | |
example_separator="\n\n", | |
) | |
llm_chain = LLMChain( | |
llm=OpenAI(), | |
prompt=few_shot_prompt, | |
verbose=False, | |
) | |
with open('../../my/finances/amz.csv', 'r') as input_file: | |
reader = csv.reader(input_file) | |
header = next(reader) | |
header.append('Category') | |
transformed_data = [header] | |
for row in reader: | |
print(row[23]) | |
output = llm_chain.predict(product=row[23]) | |
row.append(output.lstrip()) | |
transformed_data.append(row) | |
with open('output.csv', mode='w', newline='') as output_file: | |
writer = csv.writer(output_file) | |
writer.writerows(transformed_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example from Category column of output.csv: