victoriaono commited on
Commit
daeb6ed
·
verified ·
1 Parent(s): 1cc6224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -11,6 +11,10 @@ retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
 
12
  openai.api_key = os.environ["OPENAI_API_KEY"]
13
 
 
 
 
 
14
  # Attempt to load the necessary models and provide feedback on success or failure
15
  try:
16
  retrieval_model = SentenceTransformer(retrieval_model_name)
@@ -63,12 +67,12 @@ def generate_response(user_query, relevant_segment):
63
  Generate a response emphasizing the bot's capability in providing chess information.
64
  """
65
  try:
66
- system_message = "You are a chess chatbot specialized in providing information on chess rules, strategies, and terminology."
67
  user_message = f"Here's the information on chess: {relevant_segment}"
68
- messages = [
69
- {"role": "system", "content": system_message},
70
- {"role": "user", "content": user_message}
71
- ]
72
  response = openai.ChatCompletion.create(
73
  model="gpt-3.5-turbo",
74
  messages=messages,
@@ -78,7 +82,15 @@ def generate_response(user_query, relevant_segment):
78
  frequency_penalty=0,
79
  presence_penalty=0
80
  )
81
- return response['choices'][0]['message']['content'].strip()
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
  print(f"Error in generating response: {e}")
84
  return f"Error in generating response: {e}"
 
11
 
12
  openai.api_key = os.environ["OPENAI_API_KEY"]
13
 
14
+ system_message = "You are a chess chatbot specialized in providing information on chess rules, strategies, and terminology."
15
+ # Initial system message to set the behavior of the assistant
16
+ messages = [{"role": "system", "content": system_message}]
17
+
18
  # Attempt to load the necessary models and provide feedback on success or failure
19
  try:
20
  retrieval_model = SentenceTransformer(retrieval_model_name)
 
67
  Generate a response emphasizing the bot's capability in providing chess information.
68
  """
69
  try:
70
+
71
  user_message = f"Here's the information on chess: {relevant_segment}"
72
+
73
+ # Append user's message to messages list
74
+ messages.append({"role": "user", "content": user_message})
75
+
76
  response = openai.ChatCompletion.create(
77
  model="gpt-3.5-turbo",
78
  messages=messages,
 
82
  frequency_penalty=0,
83
  presence_penalty=0
84
  )
85
+
86
+ # Extract the response text
87
+ output_text = response['choices'][0]['message']['content'].strip()
88
+
89
+ # Append assistant's message to messages list for context
90
+ messages.append({"role": "assistant", "content": output_text})
91
+
92
+ return output_text
93
+
94
  except Exception as e:
95
  print(f"Error in generating response: {e}")
96
  return f"Error in generating response: {e}"