Caerii commited on
Commit
455621c
·
1 Parent(s): d89a126

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -13,40 +13,43 @@ client = openai.OpenAI(api_key=api_key)
13
 
14
  def openai_chatbot(message, history=None):
15
  try:
16
- conversation_history = []
17
- if history:
18
- for human, assistant in history:
19
- conversation_history.append({"role": "user", "content": human})
20
- conversation_history.append({"role": "assistant", "content": assistant})
21
- conversation_history.append({"role": "user", "content": message})
22
-
23
  chat_completion = client.chat.completions.create(
24
  model="gpt-3.5-turbo",
25
  messages=conversation_history,
26
  )
27
-
 
28
  response_text = chat_completion.choices[0].message.content
29
- new_history = history if history else []
30
- new_history.append((message, response_text))
31
 
32
- return response_text, new_history
 
 
 
 
 
33
  except Exception as e:
34
  print(f"An error occurred: {e}")
35
- return "Sorry, there was an error processing your request.", history
 
36
 
37
  chat_interface = gr.Interface(
38
  fn=openai_chatbot,
39
  inputs=[
40
  gr.Textbox(label="Your Message"),
41
- gr.State() # State input
42
  ],
43
  outputs=[
44
- gr.Textbox(label="OpenAI Response"),
45
- gr.State() # Corresponding State output
46
  ],
47
  title="OpenAI Chatbot",
48
  description="Talk to a bot powered by OpenAI GPT-3."
49
  )
50
 
51
  if __name__ == "__main__":
52
- chat_interface.launch(debug=True)
 
13
 
14
  def openai_chatbot(message, history=None):
15
  try:
16
+ conversation_history = history or []
17
+ if message: # Only append if there is a message to send
18
+ conversation_history.append({"role": "user", "content": message})
19
+
 
 
 
20
  chat_completion = client.chat.completions.create(
21
  model="gpt-3.5-turbo",
22
  messages=conversation_history,
23
  )
24
+
25
+ # Append the new assistant message to the conversation history
26
  response_text = chat_completion.choices[0].message.content
27
+ conversation_history.append({"role": "assistant", "content": response_text})
 
28
 
29
+ # Format the history for display
30
+ history_formatted = "\n".join(
31
+ f"{m['role'].title()}: {m['content']}" for m in conversation_history
32
+ )
33
+
34
+ return history_formatted, conversation_history # Return the formatted history and the raw history
35
  except Exception as e:
36
  print(f"An error occurred: {e}")
37
+ # Return the error message and the existing history so the error message doesn't get appended
38
+ return "Sorry, there was an error processing your request.", history
39
 
40
  chat_interface = gr.Interface(
41
  fn=openai_chatbot,
42
  inputs=[
43
  gr.Textbox(label="Your Message"),
44
+ gr.State() # State input to hold the history
45
  ],
46
  outputs=[
47
+ gr.Textbox(label="OpenAI Response", lines=10), # More lines for displaying the history
48
+ gr.State(hidden=True) # State output to pass the history back to the function
49
  ],
50
  title="OpenAI Chatbot",
51
  description="Talk to a bot powered by OpenAI GPT-3."
52
  )
53
 
54
  if __name__ == "__main__":
55
+ chat_interface.launch(debug=True)