在本指南中,我们将通过几个示例来介绍如何使用gr.ChatInterface
与流行的LLM库和API提供商。
我们将涵盖以下库和API提供商:
对于许多LLM库和提供商,存在社区维护的集成库,使得启动Gradio应用程序变得更加容易。我们在下面的适当部分中引用了这些库。
让我们从在openai
之上使用llama-index
开始,构建一个RAG聊天机器人,可以在任何文本或PDF文件上进行演示和分享,代码不超过30行。你需要一个OpenAI密钥来完成这个示例(继续阅读以获取免费的、开源的替代方案!)
# This is a simple RAG chatbot built on top of Llama Index and Gradio. It allows you to upload any text or PDF files and ask questions about them!
# Before running this, make sure you have exported your OpenAI API key as an environment variable:
# export OPENAI_API_KEY="your-openai-api-key"
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
import gradio as gr
def answer(message, history):
files = []
for msg in history:
if msg['role'] == "user" and isinstance(msg['content'], tuple):
files.append(msg['content'][0])
for file in message["files"]:
files.append(file)
documents = SimpleDirectoryReader(input_files=files).load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
return str(query_engine.query(message["text"]))
demo = gr.ChatInterface(
answer,
type="messages",
title="Llama Index RAG Chatbot",
description="Upload any text or pdf files and ask questions about them!",
textbox=gr.MultimodalTextbox(file_types=[".pdf", ".txt"]),
multimodal=True
)
demo.launch()
这里有一个在openai
之上使用langchain
构建通用聊天机器人的示例。和之前一样,你需要有一个OpenAI密钥来运行这个示例。
# This is a simple general-purpose chatbot built on top of LangChain and Gradio.
# Before running this, make sure you have exported your OpenAI API key as an environment variable:
# export OPENAI_API_KEY="your-openai-api-key"
from langchain_openai import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
import gradio as gr
model = ChatOpenAI(model="gpt-4o-mini")
def predict(message, history):
history_langchain_format = []
for msg in history:
if msg['role'] == "user":
history_langchain_format.append(HumanMessage(content=msg['content']))
elif msg['role'] == "assistant":
history_langchain_format.append(AIMessage(content=msg['content']))
history_langchain_format.append(HumanMessage(content=message))
gpt_response = model.invoke(history_langchain_format)
return gpt_response.content
demo = gr.ChatInterface(
predict,
type="messages"
)
demo.launch()
提示: 为了快速原型设计,社区维护的 langchain-gradio repo 使得在 LangChain 上构建聊天机器人变得更加容易。
当然,我们也可以直接使用openai
库。这里有一个与LangChain类似的例子,但这次还包括了流式处理:
提示: 为了快速原型设计,openai-gradio 库 使得在 OpenAI 模型上构建聊天机器人变得更加容易。
transformers
当然,在许多情况下,您可能希望在本地运行一个聊天机器人。以下是使用Hugging Face transformers
库的SmolLM2-135M-Instruct模型的等效示例。
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
checkpoint = "HuggingFaceTB/SmolLM2-135M-Instruct"
device = "cpu" # "cuda" or "cpu"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
def predict(message, history):
history.append({"role": "user", "content": message})
input_text = tokenizer.apply_chat_template(history, tokenize=False)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_new_tokens=100, temperature=0.2, top_p=0.9, do_sample=True)
decoded = tokenizer.decode(outputs[0])
response = decoded.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0]
return response
demo = gr.ChatInterface(predict, type="messages")
demo.launch()
SambaNova云API提供了对全精度开源模型的访问,例如Llama系列。以下是如何围绕SambaNova API构建Gradio应用程序的示例
# This is a simple general-purpose chatbot built on top of SambaNova API.
# Before running this, make sure you have exported your SambaNova API key as an environment variable:
# export SAMBANOVA_API_KEY="your-sambanova-api-key"
import os
import gradio as gr
from openai import OpenAI
api_key = os.getenv("SAMBANOVA_API_KEY")
client = OpenAI(
base_url="https://api.sambanova.ai/v1/",
api_key=api_key,
)
def predict(message, history):
history.append({"role": "user", "content": message})
stream = client.chat.completions.create(messages=history, model="Meta-Llama-3.1-70B-Instruct-8k", stream=True)
chunks = []
for chunk in stream:
chunks.append(chunk.choices[0].delta.content or "")
yield "".join(chunks)
demo = gr.ChatInterface(predict, type="messages")
demo.launch()
提示: 为了快速原型设计,sambanova-gradio库使得在SambaNova模型上构建聊天机器人变得更加容易。
Hyperbolic AI API 提供了对许多开源模型的访问,例如 Llama 系列。以下是如何围绕 SambaNova API 构建 Gradio 应用程序的示例
# This is a simple general-purpose chatbot built on top of Hyperbolic API.
# Before running this, make sure you have exported your Hyperbolic API key as an environment variable:
# export HYPERBOLIC_API_KEY="your-hyperbolic-api-key"
import os
import gradio as gr
from openai import OpenAI
api_key = os.getenv("HYPERBOLIC_API_KEY")
client = OpenAI(
base_url="https://api.hyperbolic.xyz/v1/",
api_key=api_key,
)
def predict(message, history):
history.append({"role": "user", "content": message})
stream = client.chat.completions.create(messages=history, model="gpt-4o-mini", stream=True)
chunks = []
for chunk in stream:
chunks.append(chunk.choices[0].delta.content or "")
yield "".join(chunks)
demo = gr.ChatInterface(predict, type="messages")
demo.launch()
提示: 为了快速原型设计,hyperbolic-gradio 库使得在 Hyperbolic 模型上构建聊天机器人变得更加容易。
Anthropic的Claude模型也可以通过API使用。这里有一个基于Anthropic API构建的简单的20个问题风格的游戏:
# This is a simple 20 questions-style game built on top of the Anthropic API.
# Before running this, make sure you have exported your Anthropic API key as an environment variable:
# export ANTHROPIC_API_KEY="your-anthropic-api-key"
import anthropic
import gradio as gr
client = anthropic.Anthropic()
def predict(message, history):
keys_to_keep = ["role", "content"]
history = [{k: d[k] for k in keys_to_keep if k in d} for d in history]
history.append({"role": "user", "content": message})
if len(history) > 20:
history.append({"role": "user", "content": "DONE"})
output = client.messages.create(
messages=history,
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
system="You are guessing an object that the user is thinking of. You can ask 10 yes/no questions. Keep asking questions until the user says DONE"
)
return {
"role": "assistant",
"content": output.content[0].text,
"options": [{"value": "Yes"}, {"value": "No"}]
}
placeholder = """
<center><h1>10 Questions</h1><br>Think of a person, place, or thing. I'll ask you 10 yes/no questions to try and guess it.
</center>
"""
demo = gr.ChatInterface(
predict,
examples=["Start!"],
chatbot=gr.Chatbot(placeholder=placeholder),
type="messages"
)
demo.launch()