Skip to content

Instantly share code, notes, and snippets.

@rohitrajiit
Created November 26, 2023 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohitrajiit/7dc393abce26f0b2f6134bdc1f09dcbc to your computer and use it in GitHub Desktop.
Save rohitrajiit/7dc393abce26f0b2f6134bdc1f09dcbc to your computer and use it in GitHub Desktop.
import gradio as gr
from openai import OpenAI
api_key = "sk-" # Replace with your key
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
history_openai_format = []
for human, assistant in chat_history:
history_openai_format.append({"role": "user", "content": human })
history_openai_format.append({"role": "assistant", "content":assistant})
history_openai_format.append({"role": "user", "content": message})
client = OpenAI(
api_key=api_key,)
response = client.chat.completions.create(
messages=history_openai_format,
# model="gpt-3.5-turbo" # gpt 3.5 turbo
# model="gpt-4",
model = "gpt-4-1106-preview", #gpt-4 turbo
stream = False
)
responsetext = response.choices[0].message.content
chat_history.append((message, responsetext))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment