使用Xorbits Inference进行本地大模型部署及查询

介绍

在本文中,我们将展示如何使用Xorbits Inference (简称Xinference) 在本地部署大型语言模型(LLM)。我们将使用Llama 2聊天模型作为示例,但代码适用于所有支持的LLM聊天模型。

步骤概述

  1. 安装Xinference
  2. 启动本地模型
  3. 索引数据并进行查询

安装Xinference

首先,我们需要安装Xinference。请在终端窗口中运行以下命令:

pip install "xinference[all]"

安装完成后,重启您的Jupyter Notebook。然后在新的终端窗口中运行以下命令:

xinference

您应该会看到如下输出:

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.

在端点描述中查找端口号,在上面的例子中是9997

接下来设置端口号:

%pip install llama-index-llms-xinference

port = 9997  # 替换为您的端口号

启动本地模型

在这一步中,我们将导入相关库并启动模型。

!pip install llama-index  # 如果缺少LlamaIndex,需安装

from llama_index.core import (
    SummaryIndex,
    TreeIndex,
    VectorStoreIndex,
    KeywordTableIndex,
    KnowledgeGraphIndex,
    SimpleDirectoryReader
)
from llama_index.llms.xinference import Xinference
from xinference.client import RESTfulClient
from IPython.display import Markdown, display

定义客户端并启动模型:

# 定义客户端以发送命令到Xinference
client = RESTfulClient(f"http://localhost:{port}")

# 下载并启动模型,初次可能需要等待
model_uid = client.launch_model(
    model_name="llama-2-chat",
    model_size_in_billions=7,
    model_format="ggmlv3",
    quantization="q2_K",
)

# 初始化Xinference对象以使用LLM
llm = Xinference(
    endpoint=f"http://localhost:{port}",
    model_uid=model_uid,
    temperature=0.0,
    max_tokens=512,
)

索引数据并进行查询

在这一步中,我们将模型与数据结合以创建查询引擎。

# 从数据创建索引
documents = SimpleDirectoryReader("../data/paul_graham").load_data()

# 使用VectorStoreIndex创建索引
index = VectorStoreIndex.from_documents(documents=documents)

# 创建查询引擎
query_engine = index.as_query_engine(llm=llm)

我们可以在询问问题前设置温度和最大回答长度。

# 可选,更新温度和最大回答长度
llm.__dict__.update({"temperature": 0.0})
llm.__dict__.update({"max_tokens": 2048})

# 问一个问题并显示答案
question = "What did the author do after his time at Y Combinator?"

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

可能遇到的错误

  1. 无法安装Xinference: 确保您的网络连接正常,或者尝试使用国内镜像源安装包。
  2. 模型启动失败: 检查是否有足够的系统资源,尤其是内存和存储空间。
  3. 查询引擎创建失败: 确认数据路径正确且数据格式符合要求。

参考资料:

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值