使用LangChain与ChatGLM实现本地知识库(一)

发布时间 2023-11-28 20:56:34作者: AiFly

  本篇主要内容为介绍ChatGLM3的安装使用,后续才会涉及到使用LangChain实现本地知识库的内容;
  ChatGLM为智谱与清华大学开源的一个大语言模型,支持多轮对话、内容创作等,ChatGLM3-6B为ChatGLM3系列中门槛相对较低的一个,本地部署提供兼容OpenAI的API;
  LangChain用于快速开发基于大语言模型应用程序的框架,提供了一整套工具、组件、接口等使得程序与大语言模型轻松交互组件快速组合、集成;如在模型上外挂本地知识库等;

图片1.png

ChatGLM3安装

  这里将安装使用int4量化版本的ChatGLM3-6B推理程序ChatGLM.cpp项目地址为:
  https://github.com/li-plus/chatglm.cpp 这里有详细的安装流程,安装完成并下载好预训练模型后即可在Python代码通过ChatGLM.cpp推理程序调用预训练模型;

示例如下:

import chatglm_cpp

pipeline = chatglm_cpp.Pipeline("/mnt/d/software/dev/gpt/chatglm.cpp/chatglm3-ggml.bin")
p = pipeline.chat([chatglm_cpp.ChatMessage(role="user", content="海南在哪里")])
print(p.content)


API Server模式:

安装组件:pip install 'chatglm-cpp[api]'

启动基于LangChain Api的接口服务程序:

MODEL=./chatglm2-ggml.bin uvicorn chatglm_cpp.langchain_api:app --host 127.0.0.1 --port 8000

curl http请求调用:

curl http://127.0.0.1:8000 -H 'Content-Type: application/json' -d '{"prompt": "你好"}'

LangChain调用:


from langchain.llms import ChatGLM
llm = ChatGLM(endpoint_url="http://127.0.0.1:8000")
llm.predict("你好")
'你好!我是人工智能助手,很高兴见到你,欢迎问我任何问题。'


结合gradio使用:

import gradio as gr
import chatglm_cpp

def chat(quetion,history):
    pipeline = chatglm_cpp.Pipeline("/mnt/d/software/dev/gpt/chatglm.cpp/chatglm3-ggml.bin")
    p = pipeline.chat([chatglm_cpp.ChatMessage(role="user", content=quetion)])
    return p.content

demo = gr.ChatInterface(chat)
demo.launch(inbrowser=True)


2.png

  虽然只是60亿参数规模的模型,还只是int4量化版的,但从输出内容看算得上一个玩具;在与LangChain结合后可玩性将会提高;