Spaces:
Sleeping
Sleeping
Commit ·
851947c
1
Parent(s): 16268c2
Update app.py
Browse filesImplemented chat 'history' functionality
app.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
# Set up OpenAI credentials
|
| 6 |
-
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 7 |
|
| 8 |
# Define the OpenAI chat function
|
| 9 |
def chat(text):
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Call the OpenAI API to generate a response
|
| 14 |
response = openai.Completion.create(
|
|
@@ -23,16 +25,8 @@ def chat(text):
|
|
| 23 |
# Extract the response text from the OpenAI API result
|
| 24 |
message = response.choices[0].text.strip()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Return the response to the user
|
| 27 |
return message
|
| 28 |
-
|
| 29 |
-
# Set up the Gradio interface
|
| 30 |
-
inputs = gr.inputs.Textbox(label="User input")
|
| 31 |
-
outputs = gr.outputs.Textbox(label="AI response")
|
| 32 |
-
title = "OpenAI Chatbot"
|
| 33 |
-
description = "Talk to an AI trained on a large corpus of text using OpenAI's GPT-3 API."
|
| 34 |
-
examples = [["Hello, how are you?"]]
|
| 35 |
-
interface = gr.Interface(fn=chat, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
|
| 36 |
-
|
| 37 |
-
# Start the Gradio interface
|
| 38 |
-
interface.launch()
|
|
|
|
| 1 |
+
# Define the conversation history variable
|
| 2 |
+
conversation_history = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Define the OpenAI chat function
|
| 5 |
def chat(text):
|
| 6 |
+
# Access the global conversation history variable
|
| 7 |
+
global conversation_history
|
| 8 |
+
|
| 9 |
+
# Update the conversation history with the latest input
|
| 10 |
+
conversation_history += f"User: {text}\nAI: "
|
| 11 |
+
|
| 12 |
+
# Set up the OpenAI prompt with the full conversation history
|
| 13 |
+
prompt = conversation_history
|
| 14 |
|
| 15 |
# Call the OpenAI API to generate a response
|
| 16 |
response = openai.Completion.create(
|
|
|
|
| 25 |
# Extract the response text from the OpenAI API result
|
| 26 |
message = response.choices[0].text.strip()
|
| 27 |
|
| 28 |
+
# Update the conversation history with the latest response
|
| 29 |
+
conversation_history += f"{message}\n"
|
| 30 |
+
|
| 31 |
# Return the response to the user
|
| 32 |
return message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|