Created
April 16, 2024 20:19
-
-
Save alongubkin/baa5d3c2778da30e477f9081b76cf5ae to your computer and use it in GitHub Desktop.
How to build LLM-based Classifiers (full tutorial: https://youtu.be/l7NPMiyuh1M?si=bs8V-whzHwdQ63Iv)
This file contains hidden or 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
import pandas as pd | |
from sklearn.metrics import accuracy_score | |
data = pd.read_csv("data.csv") | |
print(accuracy_score(data["actual"], data["sentiment"])) |
This file contains hidden or 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 openai import OpenAI | |
import json | |
client = OpenAI() | |
product_reviews = [ | |
"This product is good", | |
"The product is not so good", | |
"This product is okay", | |
] | |
def analyze_sentiment(product_review): | |
prompt = f""" | |
Analyze the sentiment of the following product review: | |
{product_review} | |
""" | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo-1106", | |
messages=[{ | |
"role": "system", | |
"content": prompt | |
}], | |
temperature=0, | |
top_p=0.2, | |
seed=42, | |
tools=[{ | |
"type": "function", | |
"function": { | |
"name": "report_sentiment", | |
"description": "Report the analyzed sentiment with the explanation for it", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"explanation": { | |
"type": "string", | |
"description": "Explanation of the review sentiment.", | |
}, | |
"sentiment": { | |
"type": "string", | |
"enum": ["positive", "negative", "neutral"], | |
"description": "The sentiment of the review..", | |
}, | |
}, | |
"required": ["explanation", "sentiment"], | |
}, | |
} | |
}], | |
) | |
return json.loads(response.choices[0].message.tool_calls[0].function.arguments) | |
data = [ | |
{"review": review, "actual": None, **analyze_sentiment(review)} | |
for review in product_reviews | |
] | |
import pandas as pd | |
pd.DataFrame(data).to_csv("data.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment