0%

基于Langchain使用开源大模型结合本地知识库实现问答

准备工作

  1. 首先在HuggingFace中下载ChatGLM-6B权重,并在GitHub中下载ChatGLM-6B源码
  2. 环境安装,控制台中进入ChatGLM源码并输入:
    1
    pip install -r requirements.txt
  3. 修改源码中api.py文件中的模型路径。
    image-20240311185002000

  4. 然后在ChatGLM-6B工程中执行:python api.py 开启ChatGLM api。
    image-20240311185336349

加载模型

1
from langchain.llms import ChatGLM
1
2
model_path = 'E:\hf_model\chatglm-6b'
api = 'http://127.0.0.1:8000'
1
2
3
llm = ChatGLM(endpoint_url=api,
max_token=80000,
top_p=0.7)

读取知识库

1
from langchain.document_loaders import DirectoryLoader
1
2
loader = DirectoryLoader('knowledge_base')
documents = loader.load()

文档切分

1
from langchain.text_splitter import CharacterTextSplitter
1
2
text_splitter = CharacterTextSplitter(chunk_size=256,   # 每个切片的token大小
chunk_overlap=0) # 相邻切片的重复token大小
1
split_documents = text_splitter.split_documents(documents)
Created a chunk of size 279, which is longer than the specified 256
Created a chunk of size 426, which is longer than the specified 256
Created a chunk of size 302, which is longer than the specified 256
Created a chunk of size 282, which is longer than the specified 256
Created a chunk of size 273, which is longer than the specified 256
Created a chunk of size 289, which is longer than the specified 256

文本向量化

1
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
1
2
3
4
5
6
7
# 加载Embedding模型
encode_kwargs = {'normalize_embeddings': False}
model_kwargs = {'device': 'cuda:0'}
embeddings = HuggingFaceEmbeddings(model_name="shibing624/text2vec-base-chinese",
cache_folder="E:\hf_model",
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs)
1
2
from langchain.vectorstores import Chroma
import os
1
db = Chroma.from_documents(split_documents, embedding=embeddings, persist_directory='VectorStore')

本地知识库问答

1
from langchain.chains import RetrievalQA
1
retriever = db.as_retriever()
1
2
3
qa = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=retriever)
1
response = qa.run('小王子为什么离开玫瑰?')
1
print(response)
小王子离开了他的玫瑰 because he realized that she was more important to him than any other person or thing in the world.
-------------本文结束感谢您的阅读-------------