-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_client.py
More file actions
22 lines (20 loc) · 792 Bytes
/
Copy pathopenai_client.py
File metadata and controls
22 lines (20 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import openai
from config import OPENAI_API_KEY
# Initialize OpenAI API key
openai.api_key = OPENAI_API_KEY
def get_openai_response(prompt: str) -> str:
"""Send a query to OpenAI's ChatCompletion endpoint and return the response."""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Or use "gpt-4" if enabled for your account
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=100,
)
# Extract the content of the AI response
return response['choices'][0]['message']['content'].strip()
except Exception as e:
return f"Error: {str(e)}"