-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
27 lines (23 loc) · 921 Bytes
/
test.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
import openai
from dotenv import load_dotenv
import os
# Load the API key from the .env file
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY") # Make sure this is correctly named in your .env file
# Create an instance of the OpenAI client
client = openai.OpenAI(api_key=api_key)
def generate_response(prompt):
try:
# Using the client to create a chat completion with the specified model
response = client.chat.completions.create(
model="gpt-4o-mini", # Make sure this model is available in your API plan
messages=[{"role": "user", "content": prompt}]
)
# Correctly accessing the text content of the response
return response.choices[0].message.content
except Exception as e:
return str(e)
if __name__ == "__main__":
prompt = "Is this working?"
result = generate_response(prompt)
print("Response from OpenAI GPT:", result)