|
|
import gradio as gr |
|
|
import openai |
|
|
|
|
|
|
|
|
openai.api_key = "YOUR_API_KEY" |
|
|
|
|
|
|
|
|
MAX_HISTORY = 10 |
|
|
|
|
|
def chat_fn(message, history): |
|
|
|
|
|
messages = [] |
|
|
for user_msg, bot_msg in history[-MAX_HISTORY:]: |
|
|
messages.append({"role": "user", "content": user_msg}) |
|
|
messages.append({"role": "assistant", "content": bot_msg}) |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
response = openai.ChatCompletion.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages=messages, |
|
|
max_tokens=300 |
|
|
) |
|
|
|
|
|
|
|
|
bot_reply = response.choices[0].message["content"] |
|
|
|
|
|
|
|
|
history.append((message, bot_reply)) |
|
|
|
|
|
|
|
|
return "", history |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# 💬 JuriX Chatbot") |
|
|
chatbot = gr.Chatbot() |
|
|
msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...") |
|
|
msg.submit(chat_fn, [msg, chat]()_ |
|
|
|