跳到主要内容
Open In ColabOpen on GitHub

Xorbits Inference (Xinference)

Xinference 是一个强大且通用的库,旨在提供 LLM、语音识别模型和多模态模型服务,即使在您的笔记本电脑上也能运行。它支持各种与 GGML 兼容的模型,例如 chatglm、baichuan、whisper、vicuna、orca 等等。 本笔记本演示了如何将 Xinference 与 LangChain 一起使用。

安装

通过 PyPI 安装 Xinference

%pip install --upgrade --quiet  "xinference[all]"

在本地或分布式集群中部署 Xinference。

对于本地部署,运行 xinference

要在集群中部署 Xinference,首先使用 xinference-supervisor 启动 Xinference supervisor。您还可以使用选项 -p 指定端口,使用 -H 指定主机。默认端口为 9997。

然后,在您要运行 Xinference worker 的每台服务器上使用 xinference-worker 启动 Xinference worker。

您可以查阅 Xinference 中的 README 文件以获取更多信息。

包装器

要将 Xinference 与 LangChain 一起使用,您需要首先启动一个模型。您可以使用命令行界面 (CLI) 来执行此操作

!xinference launch -n vicuna-v1.3 -f ggmlv3 -q q4_0
Model uid: 7167b2b0-2a04-11ee-83f0-d29396a3f064

将返回一个模型 UID 供您使用。现在您可以将 Xinference 与 LangChain 一起使用

from langchain_community.llms import Xinference

llm = Xinference(
server_url="http://0.0.0.0:9997", model_uid="7167b2b0-2a04-11ee-83f0-d29396a3f064"
)

llm(
prompt="Q: where can we visit in the capital of France? A:",
generate_config={"max_tokens": 1024, "stream": True},
)
API 参考:Xinference
' You can visit the Eiffel Tower, Notre-Dame Cathedral, the Louvre Museum, and many other historical sites in Paris, the capital of France.'

与 LLMChain 集成

from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate

template = "Where can we visit in the capital of {country}?"

prompt = PromptTemplate.from_template(template)

llm_chain = LLMChain(prompt=prompt, llm=llm)

generated = llm_chain.run(country="France")
print(generated)

A: You can visit many places in Paris, such as the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, the Champs-Elysées, Montmartre, Sacré-Cœur, and the Palace of Versailles.

最后,当您不需要使用模型时,终止模型

!xinference terminate --model-uid "7167b2b0-2a04-11ee-83f0-d29396a3f064"

此页面是否对您有帮助?