[Bugfix] Update Gradio OpenAI Chatbot Webserver example to new Gradio message history format (#29249)
Signed-off-by: joshiemoore <joshiemoore98@gmail.com>
This commit is contained in:
@@ -25,25 +25,17 @@ import gradio as gr
|
|||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
|
|
||||||
def format_history_to_openai(history):
|
|
||||||
history_openai_format = [
|
|
||||||
{"role": "system", "content": "You are a great AI assistant."}
|
|
||||||
]
|
|
||||||
for human, assistant in history:
|
|
||||||
history_openai_format.append({"role": "user", "content": human})
|
|
||||||
history_openai_format.append({"role": "assistant", "content": assistant})
|
|
||||||
return history_openai_format
|
|
||||||
|
|
||||||
|
|
||||||
def predict(message, history, client, model_name, temp, stop_token_ids):
|
def predict(message, history, client, model_name, temp, stop_token_ids):
|
||||||
# Format history to OpenAI chat format
|
messages = [
|
||||||
history_openai_format = format_history_to_openai(history)
|
{"role": "system", "content": "You are a great AI assistant."},
|
||||||
history_openai_format.append({"role": "user", "content": message})
|
*history,
|
||||||
|
{"role": "user", "content": message},
|
||||||
|
]
|
||||||
|
|
||||||
# Send request to OpenAI API (vLLM server)
|
# Send request to OpenAI API (vLLM server)
|
||||||
stream = client.chat.completions.create(
|
stream = client.chat.completions.create(
|
||||||
model=model_name,
|
model=model_name,
|
||||||
messages=history_openai_format,
|
messages=messages,
|
||||||
temperature=temp,
|
temperature=temp,
|
||||||
stream=True,
|
stream=True,
|
||||||
extra_body={
|
extra_body={
|
||||||
|
|||||||
Reference in New Issue
Block a user