Skip to content

Commit c309bb5

Browse files
authored
[Bugfix] Update Gradio OpenAI Chatbot Webserver example to new Gradio message history format (#29249)
Signed-off-by: joshiemoore <[email protected]>
1 parent 3e1ad40 commit c309bb5

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

examples/online_serving/gradio_openai_chatbot_webserver.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,17 @@
2525
from openai import OpenAI
2626

2727

28-
def format_history_to_openai(history):
29-
history_openai_format = [
30-
{"role": "system", "content": "You are a great AI assistant."}
31-
]
32-
for human, assistant in history:
33-
history_openai_format.append({"role": "user", "content": human})
34-
history_openai_format.append({"role": "assistant", "content": assistant})
35-
return history_openai_format
36-
37-
3828
def predict(message, history, client, model_name, temp, stop_token_ids):
39-
# Format history to OpenAI chat format
40-
history_openai_format = format_history_to_openai(history)
41-
history_openai_format.append({"role": "user", "content": message})
29+
messages = [
30+
{"role": "system", "content": "You are a great AI assistant."},
31+
*history,
32+
{"role": "user", "content": message},
33+
]
4234

4335
# Send request to OpenAI API (vLLM server)
4436
stream = client.chat.completions.create(
4537
model=model_name,
46-
messages=history_openai_format,
38+
messages=messages,
4739
temperature=temp,
4840
stream=True,
4941
extra_body={

0 commit comments

Comments
 (0)