Spaces:
Sleeping
Sleeping
File size: 2,585 Bytes
52669de d178c3a 52669de c3b0c1f 52669de 253636c 52669de 253636c 52669de d178c3a 52669de d178c3a 52669de d178c3a 52669de ec12f38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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) |