1import subprocess
2import sys
3import os
4import openai
5
6
7subprocess.check_call([sys.executable, "-m", "pip", "install", "openai==0.28"])
8
9import openai
10
11
12openai.api_key = os.environ.get("OPENAI_API_KEY")
13
14def generate_response(prompt):
15 """Generate a response using the OpenAI ChatCompletion API"""
16 if not openai.api_key:
17 raise ValueError("OpenAI API key not found in environment variable 'OPENAI_API_KEY'")
18
19 response = openai.ChatCompletion.create(
20 model="gpt-3.5-turbo-0125",
21 messages=[{"role": "user", "content": prompt}],
22 max_tokens=2048,
23 n=1,
24 stop=None,
25 temperature=0.2,
26 top_p=1,
27 frequency_penalty=0,
28 presence_penalty=0
29 )
30 return response.choices[0].message.content.strip()
31
32while True:
33 user_input = input("User: ")
34 if user_input.lower() == "exit":
35 break
36 assistant_response = generate_response(user_input)
37 print(f"Assistant: {assistant_response}")