1. 聊天机器人
  2. ChatInterface 示例

使用流行的 LLM 库和 API

在本指南中,我们将通过几个示例来介绍如何将 gr.ChatInterface 与流行的 LLM 库和 API 提供商结合使用。

我们将涵盖以下库和 API 提供商

对于许多 LLM 库和提供商,都存在社区维护的集成库,可以更轻松地启动 Gradio 应用。我们将在下面的相应部分中引用这些库。

Llama Index

让我们开始使用基于 openaillama-index 来构建一个 RAG 聊天机器人,它可以在不到 30 行代码内演示和共享任何文本或 PDF 文件。你需要一个 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,
    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,
    api_name="chat",
)

demo.launch()

LangChain

这是一个使用基于 openailangchain 来构建通用聊天机器人的示例。与之前一样,你需要一个 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"

import gradio as gr
from langchain.messages import AIMessage, HumanMessage  
from langchain_openai import ChatOpenAI  

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,
    api_name="chat",
)

demo.launch()

为了快速原型设计,社区维护的 langchain-gradio 仓库 使在 LangChain 之上构建聊天机器人变得更加容易。

OpenAI

当然,我们也可以直接使用 openai 库。这是一个与 LangChain 类似的示例,但这次还添加了流式传输。

为了快速原型设计,openai-gradio 库 使在 OpenAI 模型之上构建聊天机器人变得更加容易。

Hugging Face transformers

当然,在很多情况下,你希望在本地运行聊天机器人。这是使用 Hugging Face transformers 库中的 SmolLM2-135M-Instruct 模型的等效示例。


import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

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):
    messages = history + [{"role": "user", "content": message}]
    input_text = tokenizer.apply_chat_template(messages, 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, api_name="chat")

demo.launch()

SambaNova

SambaNova Cloud 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, api_name="chat")

demo.launch()

为了快速原型设计,sambanova-gradio 库 使在 SambaNova 模型之上构建聊天机器人变得更加容易。

Hyperbolic

Hyperbolic AI API 提供对许多开源模型的访问,例如 Llama 系列。这是一个围绕 Hyperbolic 构建 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, api_name="chat")

demo.launch()

为了快速原型设计,hyperbolic-gradio 库 使在 Hyperbolic 模型之上构建聊天机器人变得更加容易。

Anthropic 的 Claude

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),
    api_name="chat",
)

demo.launch()
gradio