Skip to main content
Open In ColabOpen on GitHub

Xorbits 推理 (Xinference)

Xinference 是一个功能强大且用途广泛的库,旨在为大型语言模型(LLMs)、语音识别模型和多模态模型提供服务,即使在您的笔记本电脑上也能运行。它支持多种兼容 GGML 的模型,例如 chatglm、baichuan、whisper、vicuna、orca 以及许多其他模型。本笔记本演示了如何将 Xinference 与 LangChain 结合使用。

安装

通过 PyPI 安装 Xinference

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

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

对于本地部署,请运行xinference

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

然后,在每个您希望运行它们的服务器上使用xinference-worker启动Xinference工作者。

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

包装器

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

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

您将获得一个模型UID以供使用。现在您可以使用LangChain与Xinference配合使用:

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"