-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-chatbot.py
More file actions
35 lines (28 loc) · 860 Bytes
/
my-chatbot.py
File metadata and controls
35 lines (28 loc) · 860 Bytes
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
33
34
35
from openai import OpenAI
import os
# Load the API key from an environment variable
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("No OpenAI API key found. Set the OPENAI_API_KEY environment variable.")
client = OpenAI(api_key=api_key)
def chat_with_gpt(prompt):
response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="gpt-4",
)
return response.choices[0].message.content.strip()
if __name__ == "__main__":
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit", "bye"]:
break
try:
response = chat_with_gpt(user_input)
print("Chatbot:", response)
except Exception as e:
print("An error occurred:", e)