docubot / docubot.py
DrunkenBerger's picture
Update docubot.py
1bde2f2
import gradio as gr
import openai
import time
openai.api_key = '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'
# Start with a system message
with open('data.txt', 'r') as file:
data = file.read().replace('\n', '')
# Open documents
with open('./docsprueba/doc1.txt', 'r') as file:
doc1 = file.read().replace('\n', '')
with open('./docsprueba/doc2.txt', 'r') as file:
doc2 = file.read().replace('\n', '')
with open('./docsprueba/doc3.txt', 'r') as file:
doc3 = file.read().replace('\n', '')
with open('./docsprueba/doc4.txt', 'r') as file:
doc4 = file.read().replace('\n', '')
with open('./docsprueba/doc5.txt', 'r') as file:
doc5 = file.read().replace('\n', '')
history = [{"role": "system", "content": data + doc1 + doc2 + doc3 + doc4 + doc5}]
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
global history
# Add the user's message to the conversation
history.append({"role": "user", "content": message})
# Get a response from the model
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history,
temperature=0.8,
max_tokens=225,
top_p=0.80,
frequency_penalty=0.6,
presence_penalty=0.5,
stop=["thank you", "good bye", "shut up", "thanks"]
)
# Extract the assistant's message from the response
bot_message = response['choices'][0]['message']['content']
# Add the assistant's message to the conversation
history.append({"role": "assistant", "content": bot_message})
chat_history.append((message, bot_message))
time.sleep(2)
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.launch()