Last active
September 28, 2024 19:02
-
-
Save jetnew/83e08c330f7bbf0e8d6ea227c32a4c0c to your computer and use it in GitHub Desktop.
Super simple zero-shot text classifier using OpenAI's function calling API.
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
import json | |
import openai | |
openai.api_key = "sk-" | |
def classifier(description, labels, label_descriptions): | |
def classify(text): | |
function = { | |
"name": "Classify", | |
"description": description, | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"prediction": { | |
"type": "string", | |
"description": label_descriptions, | |
"enum": labels | |
}, | |
}, | |
"required": ["prediction"] | |
}, | |
} | |
response = openai.ChatCompletion.create( | |
model="gpt-4-0613", # "gpt-3.5-turbo-0613", | |
messages=[{"role": "system", "content": "Only use the functions you have been provided with."}, | |
{"role": "user", "content": text}], | |
functions=[function], | |
function_call="auto", | |
temperature=0 | |
) | |
return json.loads(response["choices"][0]["message"]["function_call"]["arguments"])["prediction"] | |
return classify | |
task_complexity = classifier(description="Complexity of a task", | |
labels=("Simple", "Tool", "Complex", "Impossible"), | |
label_descriptions=("Simple tasks can be completed with ChatGPT without the use of any tools. " | |
"Tool tasks can be completed with a single tool. " | |
"Complex tasks require planning over multiple tools. " | |
"Impossible tasks cannot be completed with the available tools.")) | |
print(task_complexity("Summarize my emails.")) # Tool | |
print(task_complexity("Get events from my calendar for today.")) # Tool | |
print(task_complexity("Who is the president of the United States?")) # Simple | |
print(task_complexity("What is the weather like today?")) # Tool | |
print(task_complexity("Sync my calendar from my emails.")) # Complex | |
print(task_complexity("Help me collect my laundry.")) # Impossible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment