openai_demo / app.py
xiao8961's picture
Update app.py
253636c verified
import gradio as gr
import time
import openai
import os
openai.api_type = os.environ["OPENAI_API_TYPE"]
openai.api_key = os.environ["OPENAI_API_KEY"]
openai.api_base = os.environ["OPENAI_API_BASE"]
openai.api_version = os.environ["OPENAI_API_VERSION"]
systemMessageContent = "You are a master of a Java programming language. Answer users's question on Java programming only"
systemMessage = {"role": "system", "content": systemMessageContent}
userMessageContent = ""
chatbotMessageContent = ""
temperature = 0.8
top_p = 0.95
max_tokens = 800
numOfHistory = 10
with gr.Blocks() as simpleChatDemo:
# this is the place we put in the state session information, every session has different variables on this.
inputMessages = gr.State([systemMessage])
# Chatbot interface
chatbot = gr.Chatbot()
# Message is a Text Box
msg = gr.Textbox()
# Clear Button on to clear up the msg and chatbot
clear = gr.ClearButton([msg, chatbot])
def respond(userMessageInput, inputMessagesHistory, chatbot_history):
userMessageContent = userMessageInput
userMessage = {"role": "user", "content": userMessageContent}
inputMessagesHistory.append(userMessage)
if len(inputMessagesHistory) > numOfHistory + 1:
numOutstandingMessages = len(inputMessagesHistory) - (numOfHistory + 1)
inputMessagesHistory = [
inputMessagesHistory[0],
*inputMessagesHistory[(1 + numOutstandingMessages) :],
]
completion = openai.ChatCompletion.create(
engine="chatgpt",
messages=inputMessagesHistory,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
)
chatbotMessageContent = completion.choices[0].message.content
chatbotMessage = {"role": "assistant", "content": chatbotMessageContent}
inputMessagesHistory.append(chatbotMessage)
# chat history is main list of [(user message string, bot message string)]
chatbot_history.append((userMessageContent, chatbotMessageContent))
time.sleep(2)
# return with clear up the message box, and put the new messages into the chat_history
return (
"",
inputMessagesHistory,
chatbot_history,
)
# when the textbox click submit, i.e., enter, the function will be called (function, [input parameters], [output response])
msg.submit(respond, [msg, inputMessages, chatbot], [msg, inputMessages, chatbot])
simpleChatDemo.launch(share=True)