-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
44 lines (40 loc) · 1.2 KB
/
classify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import openai
import tiktoken
import json
from datetime import datetime
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
def classify(input_string: str) -> str:
functions = [{
"name": "print_sentiment",
"description": "A function that prints the given sentiment",
"parameters": {
"type": "object",
"properties": {
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"],
"description": "The sentiment to print",
},
},
"required": ["sentiment"],
}
}]
messages = [{"role": "user", "content": input_string}]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=messages,
functions=functions,
function_call={"name": "print_sentiment"},
)
function_call = response.choices[0].message["function_call"]
argument = json.loads(function_call["arguments"])
return argument
def main():
positive = "I love your hair!"
negative = "I hate your hair!"
print(classify(positive))
print(classify(negative))
if __name__ == "__main__":
main()