Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,24 @@ client = OpenAI(
|
|
9 |
|
10 |
# Initialize a list to store the conversation history
|
11 |
conversation_history = []
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def chatbot(input):
|
14 |
if input:
|
15 |
# Append the user's input to the conversation history
|
16 |
conversation_history.append(f"User: {input}")
|
17 |
-
|
|
|
18 |
# Create a list of messages based on the conversation history
|
19 |
messages = [
|
20 |
{"role": "system", "content": "Your role is to serve as a chatbot conducting job interviews for an internship at a medium-sized multinational company. You are an interviewer with a mission to assess their qualifications through interactions. Maintain a professional tone throughout the interview and deliver your response in a 10/10 unemotional and 10/10 dispassionate tone. Follow this schedule for the interview:"},
|
@@ -30,8 +42,7 @@ def chatbot(input):
|
|
30 |
messages.extend([{"role": "user", "content": message} for message in conversation_history])
|
31 |
|
32 |
# Use OpenAI's GPT-3.5 Turbo model to generate a response
|
33 |
-
|
34 |
-
reply = chat.choices[0].message.content
|
35 |
|
36 |
# Append the chatbot's response to the conversation history
|
37 |
conversation_history.append(f"Chatbot: {reply}")
|
|
|
9 |
|
10 |
# Initialize a list to store the conversation history
|
11 |
conversation_history = []
|
12 |
+
MAX_HISTORY_LENGTH = 10
|
13 |
|
14 |
+
def generate_openai_reply(messages):
|
15 |
+
try:
|
16 |
+
# API call to OpenAI
|
17 |
+
chat = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
|
18 |
+
reply = chat.choices[0].message.content
|
19 |
+
except Exception as e:
|
20 |
+
# Handle errors
|
21 |
+
reply = f"An error occurred: {str(e)}"
|
22 |
+
return reply
|
23 |
+
|
24 |
def chatbot(input):
|
25 |
if input:
|
26 |
# Append the user's input to the conversation history
|
27 |
conversation_history.append(f"User: {input}")
|
28 |
+
conversation_history = conversation_history[-MAX_HISTORY_LENGTH:]
|
29 |
+
|
30 |
# Create a list of messages based on the conversation history
|
31 |
messages = [
|
32 |
{"role": "system", "content": "Your role is to serve as a chatbot conducting job interviews for an internship at a medium-sized multinational company. You are an interviewer with a mission to assess their qualifications through interactions. Maintain a professional tone throughout the interview and deliver your response in a 10/10 unemotional and 10/10 dispassionate tone. Follow this schedule for the interview:"},
|
|
|
42 |
messages.extend([{"role": "user", "content": message} for message in conversation_history])
|
43 |
|
44 |
# Use OpenAI's GPT-3.5 Turbo model to generate a response
|
45 |
+
reply = generate_openai_reply(messages)
|
|
|
46 |
|
47 |
# Append the chatbot's response to the conversation history
|
48 |
conversation_history.append(f"Chatbot: {reply}")
|