|
import openai |
|
import gradio as gr |
|
import os |
|
import logging |
|
import json |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
openai.api_key = os.environ['key'] |
|
|
|
|
|
|
|
|
|
|
|
def chatbot(input, messages): |
|
messages = messages or [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI!"}] |
|
if input: |
|
messages.append({"role": "user", "content": input}) |
|
chat = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", messages=messages, |
|
max_tokens=2048,n=1,temperature=0.5, |
|
) |
|
reply = chat.choices[0].message.content |
|
messages.append({"role": "assistant", "content": reply}) |
|
return reply, printMessages(messages), messages |
|
|
|
def printMessages(messages): |
|
delimiter = '\n' |
|
msg_string = delimiter.join([f"{obj['role']}:{obj['content']}" for obj in messages[1:]]) |
|
logging.info("messages:"+msg_string) |
|
return msg_string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = gr.Interface(fn=chatbot, inputs=[gr.Textbox(lines=7, label="Chat with Harry"), "state"], |
|
outputs=[gr.Textbox(label="Reply"), gr.Textbox(label="History"), "state"], title="Chat with Harry", |
|
description="Ask anything you want",theme="compact") |
|
app.launch(share=False) |