Skip to content
LlamaIndex 文档版本 跟随上游 源快照 a34e7c16 更新于 2026-10-18

在这个演示 notebook 中,我们展示如何通过三步使用 Xorbits Inference(简称 Xinference)部署本地 LLM。

在示例中,我们将使用 GGML 格式的 Llama 2 聊天模型,但代码应该可以轻松迁移到 Xinference 支持的所有 LLM 聊天模型。以下是一些示例:

名称类型语言格式规模(十亿)量化
llama-2-chatRLHF 模型enggmlv37、13、70’q2_K’、‘q3_K_L’、… 、‘q6_K’、‘q8_0’
chatglmSFT 模型en、zhggmlv36’q4_0’、‘q4_1’、‘q5_0’、‘q5_1’、‘q8_0’
chatglm2SFT 模型en、zhggmlv36’q4_0’、‘q4_1’、‘q5_0’、‘q5_1’、‘q8_0’
wizardlm-v1.0SFT 模型enggmlv37、13、33’q2_K’、‘q3_K_L’、… 、‘q6_K’、‘q8_0’
wizardlm-v1.1SFT 模型enggmlv313’q2_K’、‘q3_K_L’、… 、‘q6_K’、‘q8_0’
vicuna-v1.3SFT 模型enggmlv37、13’q2_K’、‘q3_K_L’、… 、‘q6_K’、‘q8_0’

最新的完整支持模型列表可在 Xorbits Inference 的官方 GitHub 页面中找到。

🤖 安装 Xinference

i. 在终端窗口中运行 pip install "xinference[all]"。

ii. 安装完成后,重启此 jupyter notebook。

iii. 在新的终端窗口中运行 xinference。

iv. 你应该会看到类似以下内容的输出:

INFO:xinference:Xinference successfully started. Endpoint: http://127.0.0.1:9997
INFO:xinference.core.service:Worker 127.0.0.1:21561 has been added successfully
INFO:xinference.deploy.worker:Xinference worker successfully started.

v. 在端点描述中,找到冒号后的端点端口号。在上述情况中为 9997。

vi. 使用以下单元格设置端口号:

%pip install llama-index-llms-xinference
port = 9997  # replace with your endpoint port number

🚀 启动本地模型

在这一步中,我们首先从 llama_index 导入相关库

如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。

!pip install llama-index
# If Xinference can not be imported, you may need to restart jupyter notebook
from llama_index.core import SummaryIndex
from llama_index.core import (
    TreeIndex,
    VectorStoreIndex,
    KeywordTableIndex,
    KnowledgeGraphIndex,
    SimpleDirectoryReader,
)
from llama_index.llms.xinference import Xinference
from xinference.client import RESTfulClient
from IPython.display import Markdown, display

然后,我们启动一个模型并使用它。这使我们能够在后续步骤中将模型连接到文档和查询。

你可以随意调整参数以获得更好的性能!为获得最佳结果,建议使用规模大于 13B 的模型。话虽如此,对于这个简短的演示,7B 模型已经绰绰有余。

以下是 GGML 格式 Llama 2 聊天模型的更多参数选项,按从占用空间最少到资源消耗最大但性能最高的顺序排列。

model_size_in_billions:

7、13、70

7B 和 13B 模型的量化:

q2_K、q3_K_L、q3_K_M、q3_K_S、q4_0、q4_1、q4_K_M、q4_K_S、q5_0、q5_1、q5_K_M、q5_K_S、q6_K、q8_0

70B 模型的量化:

q4_0

# Define a client to send commands to xinference
client = RESTfulClient(f"http://localhost:{port}")

# Download and Launch a model, this may take a while the first time
model_uid = client.launch_model(
    model_name="llama-2-chat",
    model_size_in_billions=7,
    model_format="ggmlv3",
    quantization="q2_K",
)

# Initiate Xinference object to use the LLM
llm = Xinference(
    endpoint=f"http://localhost:{port}",
    model_uid=model_uid,
    temperature=0.0,
    max_tokens=512,
)

🕺 索引数据……然后聊天!

在这一步中,我们将模型和数据结合起来创建一个查询引擎。然后,该查询引擎可以作为聊天机器人使用,根据给定数据回答我们的查询。

我们将使用 VetorStoreIndex,因为它相对较快。话虽如此,你可以随意更换索引以获得不同体验。以下是一些上一步已经导入的可用索引:

ListIndex、TreeIndex、VetorStoreIndex、KeywordTableIndex、KnowledgeGraphIndex

要更换索引,只需在以下代码中将 VetorStoreIndex 替换为另一个索引即可。

所有可用索引的最新完整列表可在 Llama Index 的官方文档中找到

# create index from the data
documents = SimpleDirectoryReader("../data/paul_graham").load_data()

# change index name in the following line
index = VectorStoreIndex.from_documents(documents=documents)

# create the query engine
query_engine = index.as_query_engine(llm=llm)

在提问之前,我们可以通过 Xinference 对象直接设置温度和最大答案长度(以 token 计)。这使我们能够针对不同问题更改参数,而无需每次重新构建查询引擎。

temperature 是 0 到 1 之间的数字,用于控制响应的随机性。较高的值会增加创造性,但可能导致回复偏题。设为 0 可保证每次得到相同的响应。

max_tokens 是一个整数,用于设置响应长度的上限。如果答案似乎被截断,可以增大该值,但要注意响应过长可能会超出上下文窗口并引发错误。

# optionally, update the temperature and max answer length (in tokens)
llm.__dict__.update({"temperature": 0.0})
llm.__dict__.update({"max_tokens": 2048})

# ask a question and display the answer
question = "What did the author do after his time at Y Combinator?"

response = query_engine.query(question)
display(Markdown(f"<b>{response}</b>"))

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。