LlamaIndex整合ChatGLM

LlamaIndex整合chatglm

LlamaIndex官方网站上给出的示例都是采用的Open AI,那么对于没有open_api_key的用户怎么执行查看LlamaIndex的效果嘞。下面是LlamaIndex整合ChatGLM的一个简单示例:

1. 安装相关依赖

pip install zhipuai
pip install langchain
pip install langchain-openai
pip install llama-index
pip install llama-index-embeddings-huggingface

2. 定义获取ChatGLM

具体怎么获取,请参考这篇文章:python实现在线 ChatGLM调用
本次使用的是langchain方式调用在线ChatGLM。

新建一个zhipu_llm.py文件

from langchain_openai import ChatOpenAI
import jwt
import time
from langchain_core.messages import HumanMessage

zhipuai_api_key = "智普清言的API-KEY"


def generate_token(apikey: str, exp_seconds: int):
    try:
        id, secret = apikey.split(".")
    except Exception as e:
        raise Exception("invalid apikey", e)

    payload = {
        "api_key": id,
        "exp": int(round(time.time() * 1000)) + exp_seconds * 1000,
        "timestamp": int(round(time.time() * 1000)),
    }

    return jwt.encode(
        payload,
        secret,
        algorithm="HS256",
        headers={"alg": "HS256", "sign_type": "SIGN"},
    )


class ChatZhiPuAI(ChatOpenAI):
    def __init__(self, model_name):
        super().__init__(model_name=model_name, openai_api_key=generate_token(zhipuai_api_key, 10),
                         openai_api_base="https://open.bigmodel.cn/api/paas/v4")

    def invoke(self, question):
        messages = [
            HumanMessage(content=question),
        ]
        return super().invoke(messages)

3. LlamaIndex整合ChatGLM实现自定义文档的简单问答

完整代码如下:

from llama_index.core import GPTVectorStoreIndex, SimpleDirectoryReader
from llm.zhipu_llm import ChatZhiPuAI
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

# 加载数据,需确认数据目录的正确性
documents = SimpleDirectoryReader('data').load_data()

# 实例化BAAI/bge-small-en-v1.5模型
baai_embedding = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")

# 使用 BAAI/bge-small-en-v1.5 模型初始化GPTVectorStoreIndex
index = GPTVectorStoreIndex.from_documents(documents, embed_model=baai_embedding)

chatglm = ChatZhiPuAI(model_name="glm-4")
query_engine = index.as_query_engine(llm=chatglm)
response = query_engine.query("LlamaIndex为何而生?")
print(response)

response = query_engine.query("LlamaIndex如何破局?")
print(response)

其中:
需要在上述代码同目录下新建一个data文件夹,里面放入需要检索的知识文档

from llm.zhipu_llm import ChatZhiPuAI这段代码导入的就是刚才新建的zhipu_llm.py文件中自定义的ChatZhiPuAI

如果不指定嵌入向量模型的话,会默认使用OpenAI的Embedding,需要设置OPEN-API-KEY,因此这里使用llama_index.embeddings.huggingface提供的BAAI/bge-small-en-v1.5作为嵌入模型。
首先将其下载并实例化成一个BaseEmbedding对象,然后将该对象传递给GPTVectorStoreIndex的embed_model参数。然后初始化GPTVectorStoreIndex:

from llama_index.embeddings.huggingface import HuggingFaceEmbedding

documents = SimpleDirectoryReader('data').load_data()
baai_embedding = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
index = GPTVectorStoreIndex.from_documents(documents, embed_model=baai_embedding)

4.运行效果如下:

LlamaIndex was created to enhance LLM (Large Language Model) applications, such as GPT, by integrating and structuring private or domain-specific data. These models typically interact with data through natural language interfaces and are pre-trained on a vast amount of publicly available data. However, applications built on top of LLMs often require the use of private or specialized data sources that are scattered across different platforms and storage systems, which can include being behind APIs, within SQL databases, or even in PDFs and presentations. LlamaIndex addresses this need by providing a framework to inject and access this diverse data.
LlamaIndex破局的方式是通过提供五大核心工具:Data connectors、Data indexes、Engines、Data agents以及Application integrations。这些工具协同工作,帮助用户注入、结构化并访问私有或特定领域的数据,从而增强基于自然语言交互的LLM模型。这些工具支持从不同来源整合数据,包括API、SQL数据库、PDF文件等,使得构建在LLM之上的应用程序能够更有效地利用这些数据。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Llamaindex是一个开源的搜索引擎,可以用于快速搜索和索引大型数据集。为了在本地部署Llamaindex,您需要按照以下步骤进行操作。 首先,您需要从Llamaindex的官方GitHub页面上下载源代码。确保您的计算机已安装了Git系统,然后使用命令行工具输入以下命令来克隆代码库: ``` git clone https://github.com/llama-lab/llamaindex.git ``` 下载完成后,进入项目文件夹并创建一个Python虚拟环境。使用以下命令可以创建一个虚拟环境: ``` python3 -m venv llama-env ``` 然后需要激活虚拟环境。在Mac和Linux系统下,使用以下命令: ``` source llama-env/bin/activate ``` 在Windows系统下,使用以下命令: ``` llama-env\Scripts\activate ``` 接下来,安装Llamaindex的依赖项。在虚拟环境中运行以下命令: ``` pip install -r requirements.txt ``` 等待依赖项安装完成后,可以开始配置Llamaindex。编辑`config.yaml`文件,根据您的需求进行相应的修改。您可以设置数据集的路径、索引文件的位置和其他相关参数。 完成配置后,运行以下命令来创建索引: ``` python3 llama.py -f path/to/dataset ``` 上述命令中的`path/to/dataset`应替换为实际的数据集路径。运行该命令后,Llamaindex会开始索引数据集。 当索引完成后,您可以使用以下命令来搜索索引中的数据: ``` python3 llama.py -s "your search query" ``` 您可以将`"your search query"`替换为实际的搜索关键字。Llamaindex将返回与关键字匹配的结果。 以上就是在本地部署Llamaindex的步骤。祝您在使用Llamaindex时取得成功!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值