-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_utils.py
49 lines (45 loc) · 1.16 KB
/
openai_utils.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
45
46
47
48
49
from dotenv import load_dotenv
from openai import OpenAI
import json
load_dotenv()
openai_client = OpenAI()
def system_prompt(func):
def wrapper(*args, **kwargs):
text = func(*args, **kwargs)
return {
"role": "system",
"content": [
{
"type": "text",
"text": text
}
]
}
return wrapper
def user_prompt(func):
def wrapper(*args, **kwargs):
text = func(*args, **kwargs)
return {
"role": "user",
"content": [
{
"type": "text",
"text": text
}
]
}
return wrapper
def openai_json_response(messages, model="gpt-4o-mini", temp=1, max_tokens=1024):
response = openai_client.chat.completions.create(
model=model,
messages=messages,
temperature=temp,
max_tokens=max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
response_format={
"type": "json_object"
}
)
return json.loads(response.choices[0].message.content)