Gradio Chatbot + LiteLLM 튜토리얼
Gradio Chatbot + LiteLLM 튜토리얼
LiteLLM completion 호출을 스트리밍 Gradio chatbot 데모와 통합하는 간단한 튜토리얼이에요.
출처: 문서
본문
의존성 설치 및 임포트
uv add gradio litellm
import gradioimport litellm
추론 함수 정의
LLM을 호스팅하는 서버가 기대하는 model과 api_base를 설정하는 것을 잊지 마세요.
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"
채팅 인터페이스 정의
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 앱 실행
- 명령줄에서:
python app.py또는gradio app.py(후자는 라이브 배포 업데이트를 지원) - 브라우저에서 제공된 하이퍼링크를 방문하세요.
- 원격 LLM 서버와 프롬프트 무관하게 상호작용을 즐기세요.
권장 확장:
- 대상 모델과 추론 엔드포인트를 정의하는 명령줄 인수 추가
이 튜토리얼은 ZQ에게 크레딧을 드립니다.