Gradio Chatbot + LiteLLM Tutorial
Einfaches Tutorial zur Integration von LiteLLM-Completion-Aufrufen mit Streaming-Gradio-Chatbot-Demos
Abhängigkeiten installieren & importieren​
!pip install gradio litellm
import gradio
import litellm
Inferenzfunktion definieren​
Denken Sie daran, model und api_base so einzustellen, wie sie vom Server, der Ihr LLM hostet, erwartet werden.
def inference(message, history):
try:
flattened_history = [item for sublist in history for item in sublist]
full_message = " ".join(flattened_history + [message])
messages_litellm = [{"role": "user", "content": full_message}] # litellm message format
partial_message = ""
for chunk in litellm.completion(model="huggingface/meta-llama/Llama-2-7b-chat-hf",
api_base="x.x.x.x:xxxx",
messages=messages_litellm,
max_new_tokens=512,
temperature=.7,
top_k=100,
top_p=.9,
repetition_penalty=1.18,
stream=True):
partial_message += chunk['choices'][0]['delta']['content'] # extract text from streamed litellm chunks
yield partial_message
except Exception as e:
print("Exception encountered:", str(e))
yield f"An Error occurred please 'Clear' the error and try your question again"
Chat-Interface definieren​
gr.ChatInterface(
inference,
chatbot=gr.Chatbot(height=400),
textbox=gr.Textbox(placeholder="Enter text here...", container=False, scale=5),
description=f"""
CURRENT PROMPT TEMPLATE: {model_name}.
An incorrect prompt template will cause performance to suffer.
Check the API specifications to ensure this format matches the target LLM.""",
title="Simple Chatbot Test Application",
examples=["Define 'deep learning' in once sentence."],
retry_btn="Retry",
undo_btn="Undo",
clear_btn="Clear",
theme=theme,
).queue().launch()
Gradio-App starten​
- Über die Befehlszeile:
python app.pyodergradio app.py(letzteres ermöglicht Live-Deployment-Updates) - Besuchen Sie den bereitgestellten Hyperlink in Ihrem Browser.
- Genießen Sie prompt-agnostische Interaktion mit dem entfernten LLM-Server.
Empfohlene Erweiterungen:​
- Befehlszeilenargumente hinzufügen, um das Zielmodell und die Inferenzendpunkte zu definieren
Dank an ZQ für dieses Tutorial.