-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
37 lines (31 loc) · 1003 Bytes
/
chatbot.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
29
30
31
32
33
34
35
36
37
# importing openai library for AI chatbot program
import openai
# importing os module for environment variables
import os
# importing necessary functions from dotenv library
from dotenv import load_dotenv, dotenv_values
# loading variables from .env file
load_dotenv()
# OpenAI API Key
openai.api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI()
# Function to generate a response from the chatbot
def generate_response(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=100,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].message.content.strip()
# Main loop for the chatbot
while True:
user_input = input("User: ")
if user_input.lower() in ["bye", "goodbye", "exit"]:
print("Chatbot: Goodbye!")
break
prompt = user_input
response = generate_response(prompt)
print(f"Chatbot: {response}")