|
|
import gradio as gr |
|
|
from fastapi import FastAPI |
|
|
import openai |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
openai.api_key = "" |
|
|
|
|
|
chat_history = [] |
|
|
|
|
|
|
|
|
@app.get("/chat") |
|
|
async def chatbot(prompt: str, history: list): |
|
|
global chat_history |
|
|
chat_history.append({"role": "user", "content": f"{prompt}"}) |
|
|
|
|
|
messages= chat_history.copy() |
|
|
|
|
|
messages.insert(0, {"role": "system", "content": "You are a helpful assistant"}) |
|
|
|
|
|
completion = openai.chat.completions.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages=messages, |
|
|
|
|
|
) |
|
|
|
|
|
reply = completion.choices[0].message.content |
|
|
|
|
|
chat_history.append({"role": "assistant", "content": reply}) |
|
|
|
|
|
return reply |
|
|
|
|
|
async def chatinterface(message, history): |
|
|
response = await chatbot(message, history) |
|
|
return response |
|
|
|
|
|
|
|
|
gr.ChatInterface(chatinterface).launch(share=True) |