-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_manager.py
32 lines (27 loc) · 1.09 KB
/
openai_manager.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
from openai import OpenAI
import os
from dotenv import load_dotenv
import _G
load_dotenv()
class OpenAIManager:
def __init__(self):
self.api_key = os.getenv("OPENAI_API_KEY")
self.clientOpenAI = OpenAI(api_key=self.api_key)
def _get_chat_completion(self, messages): # Add 'self' here and make sure 'messages' is a list
try:
response = self.clientOpenAI.chat.completions.create(
messages=messages, # Ensure 'messages' is a list of dicts
model="gpt-4-turbo-preview",
max_tokens=int(_G.MAX_TOKENS/2),
)
part_response = response.choices[0].message.content
finish_reason = response.choices[0].finish_reason
need_user_trigger = finish_reason not in ["stop", "eos"]
return {"content": part_response, "need_user_trigger": need_user_trigger}
except Exception as e:
return {"error": str(e), "need_user_trigger": False}
if __name__ == "__main__":
openai_manager = OpenAIManager()
messages = ["Hello, how can I help you?"]
response = openai_manager._get_chat_completion(messages) # Pass 'messages' as a list
print(response)