-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenaiHandler.py
28 lines (24 loc) · 939 Bytes
/
OpenaiHandler.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
import openai
class openaiHandler:
def __init__(self, api_key: str):
"""
Initialize the OpenAI API handler with the necessary API key.
:param api_key: Your OpenAI API key.
"""
self.api_key = api_key
openai.api_key = self.api_key
def gpt_chat(self, conversation: str, model: str) -> str:
"""
Handle a chat requests for GPT-3.5 and GPT-4.
:param conversation: The conversation history as a single string. Includes system instructions.
:param model: The chat completion model to use.
:return: The response from GPT3.5 or GPT-4.
"""
messages = [{"role": "user", "content": conversation}]
response = openai.chat.completions.create(
model=model,
messages=messages,
max_tokens=800,
temperature=0.7
)
return response.choices[0].message.content