Skip to content

Instantly share code, notes, and snippets.

@reedlaw
Last active April 9, 2023 00:07
Show Gist options
  • Save reedlaw/5f84c801c20ad5f1e6b16c6ae193b4a7 to your computer and use it in GitHub Desktop.
Save reedlaw/5f84c801c20ad5f1e6b16c6ae193b4a7 to your computer and use it in GitHub Desktop.
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)
@reedlaw
Copy link
Author

reedlaw commented Apr 9, 2023

Example from Category column of output.csv:

Books:Textbooks
Computer:Accessories
Grocery:Cereal
Grocery:Produce
Grocery:Organic Produce
Health:Dental
Electronics:Audio
Electronics:Headphones

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment