Langchain-Chatchat是干嘛的?
Langchain-Chatchat是一个基于 LangChain 框架开发的、功能强大的本地知识库问答应用,简单来说,它就像一个可以部署在你自己电脑或服务器上的“智能聊天机器人”,但这个机器人的独特之处在于,它不仅能进行通用对话,更能**“读懂”并“记住”你给它的任何文档资料(如TXT、PDF、Word、PPT等),然后基于这些资料来回答你的问题**。
它解决了通用大模型“不了解特定领域知识”和“数据隐私安全”两大核心痛点。
安装Langchain-Chatchat和对应的模型
pipinstalllangchain-chatchat-U安装Ollama
它可以运行各种大语言模型和embedding模型
到官网下载安装包并安装:https://ollama.com/download/windows
安装完成后,重新打开一个 PowerShell,先验证:ollama --version
拉取模型
# 注意,实测过程中拉取的本地大语言模型比较拉胯,所以后续没有采用,直接使用的线上的大语言模型,效果好太多了(此处可以不拉取,后面直接配置线上模型DeepSeek)ollama pull qwen:7b# embedding模型效果可以,继续保持使用ollama pull quentinz/bge-large-zh-v1.5# 也可以搜索其他embedding相关模型,看实际Rag情况ollama search bge# 查看本地模型,是否拉取成功ollama list 可以看到: qwen:7b quentinz/bge-large-zh-v1.5初始化知识库
回到项目目录:
cdC:\Users\test\Desktop\my_langchain_chatchat chatchat kb-r注意:如果在初始化知识库时,因内容过长报错,(可以修改 kb_settings.yaml文件中的两个配置, 修改小一些)
# 知识库中单段文本长度(不适用MarkdownHeaderTextSplitter)CHUNK_SIZE:250# 知识库中相邻文本重合长度(不适用MarkdownHeaderTextSplitter)OVERLAP_SIZE:50还有一个版本兼容问题:
# 安装兼容版本 0.27.2pipinstallhttpx==0.27.2启动Ollama
ollama serve
查看Ollama是否启动,访问地址:http://127.0.0.1:11434/api/tags
启动Chatchat
chatchat start -a
# webui访问http://127.0.0.1:8501/# Chatchat对话文档http://127.0.0.1:7861/docs#/名词解释
Ollama: 它提供LLM模型服务和embedding模型服务(实际案例中由它提供的embedding服务,LLM服务使用的线上的)
Faiss: 它负责向量存储和相似检索
实际使用时,导入知识库文档时,先调用向量化模型生成向量,在写入faiss
用户提问时,先把问题向量化,再到faiss里查最相近的文档片段
模型配置文件改写后
# 模型配置项# 默认选用的 LLM 名称DEFAULT_LLM_MODEL:deepseek-chat# 默认选用的 Embedding 名称DEFAULT_EMBEDDING_MODEL:quentinz/bge-large-zh-v1.5# AgentLM模型的名称 (可以不指定,指定之后就锁定进入Agent之后的Chain的模型,不指定就是 DEFAULT_LLM_MODEL)Agent_MODEL:''# 默认历史对话轮数HISTORY_LEN:3# 大模型最长支持的长度,如果不填写,则使用模型默认的最大长度,如果填写,则为用户设定的最大长度MAX_TOKENS:# LLM通用对话参数TEMPERATURE:0.7# 支持的Agent模型SUPPORT_AGENT_MODELS:-chatglm3-6b-glm-4-openai-api-Qwen-2-qwen2-instruct-gpt-3.5-turbo-gpt-4o-deepseek-chat-deepseek-reasoner# LLM模型配置,包括了不同模态初始化参数。# `model` 如果留空则自动使用 DEFAULT_LLM_MODELLLM_MODEL_CONFIG:preprocess_model:model:''temperature:0.05max_tokens:4096history_len:10prompt_name:defaultcallbacks:falsellm_model:model:''temperature:0.9max_tokens:4096history_len:10prompt_name:defaultcallbacks:trueaction_model:model:''temperature:0.01max_tokens:4096history_len:10prompt_name:ChatGLM3callbacks:truepostprocess_model:model:''temperature:0.01max_tokens:4096history_len:10prompt_name:defaultcallbacks:trueimage_model:model:sd-turbosize:256*256# # 模型加载平台配置# # 平台名称# platform_name: xinference# # 平台类型# # 可选值:['xinference', 'ollama', 'oneapi', 'fastchat', 'openai', 'custom openai']# platform_type: xinference# # openai api url# api_base_url: http://127.0.0.1:9997/v1# # api key if available# api_key: EMPTY# # API 代理# api_proxy: ''# # 该平台单模型最大并发数# api_concurrencies: 5# # 是否自动获取平台可用模型列表。设为 True 时下方不同模型类型可自动检测# auto_detect_model: false# # 该平台支持的大语言模型列表,auto_detect_model 设为 True 时自动检测# llm_models: []# # 该平台支持的嵌入模型列表,auto_detect_model 设为 True 时自动检测# embed_models: []# # 该平台支持的图像生成模型列表,auto_detect_model 设为 True 时自动检测# text2image_models: []# # 该平台支持的多模态模型列表,auto_detect_model 设为 True 时自动检测# image2text_models: []# # 该平台支持的重排模型列表,auto_detect_model 设为 True 时自动检测# rerank_models: []# # 该平台支持的 STT 模型列表,auto_detect_model 设为 True 时自动检测# speech2text_models: []# # 该平台支持的 TTS 模型列表,auto_detect_model 设为 True 时自动检测# text2speech_models: []MODEL_PLATFORMS:-platform_name:xinferenceplatform_type:xinferenceapi_base_url:http://127.0.0.1:9997/v1api_key:EMPTYapi_proxy:''api_concurrencies:5auto_detect_model:falsellm_models:[]embed_models:[]text2image_models:[]image2text_models:[]rerank_models:[]speech2text_models:[]text2speech_models:[]-platform_name:ollamaplatform_type:ollamaapi_base_url:http://127.0.0.1:11434/v1api_key:EMPTYapi_proxy:''api_concurrencies:5auto_detect_model:falsellm_models:-qwen:7b-qwen2:7bembed_models:-quentinz/bge-large-zh-v1.5text2image_models:[]image2text_models:[]rerank_models:[]speech2text_models:[]text2speech_models:[]-platform_name:oneapiplatform_type:oneapiapi_base_url:http://127.0.0.1:3000/v1api_key:sk-api_proxy:''api_concurrencies:5auto_detect_model:falsellm_models:-chatglm_pro-chatglm_turbo-chatglm_std-chatglm_lite-qwen-turbo-qwen-plus-qwen-max-qwen-max-longcontext-ERNIE-Bot-ERNIE-Bot-turbo-ERNIE-Bot-4-SparkDeskembed_models:-text-embedding-v1-Embedding-V1text2image_models:[]image2text_models:[]rerank_models:[]speech2text_models:[]text2speech_models:[]-platform_name:deepseekplatform_type:openaiapi_base_url:https://api.deepseek.comapi_key:sk-xxxxx# 此处替换成实际的api_keyapi_proxy:''api_concurrencies:5auto_detect_model:falsellm_models:-deepseek-chat-deepseek-reasonerembed_models:[]text2image_models:[]image2text_models:[]rerank_models:[]speech2text_models:[]text2speech_models:[]api文档
地址:http://127.0.0.1:7861/docs#/
使用方法1,浏览器访问webui
地址:http://127.0.0.1:8501/
功能:多功能对话、RAG对话、知识库管理。
生成的答案还是蛮准确的。
使用方法2,调用Langchain-Chatchat的API接口,接入到我们自己的应用中来
Langchain-Chatchat 自己的原生知识库聊天接口
importrequests url="http://127.0.0.1:7861/chat/kb_chat"payload={"query":"Prompt工程是啥?","mode":"local_kb","kb_name":"samples","top_k":3,"score_threshold":0.5,"history":[],"stream":False,"model":"deepseek-chat","temperature":0.7,"prompt_name":"default"}resp=requests.post(url,json=payload,timeout=60)print(resp.json())兼容OpenAI SDK/调用习惯的Chat Completions接口
importjsonimportrequests url="http://127.0.0.1:7861/knowledge_base/local_kb/samples/chat/completions"payload={"model":"deepseek-chat","messages":[{"role":"user","content":"Prompt公式三要素"}],"stream":False,"temperature":0.7,"extra_body":{"top_k":3,"score_threshold":0.5,"prompt_name":"default"}}# 非流式输出resp=requests.post(url,json=payload,timeout=60)print(resp.json())# 流式输出, payload中的stream参数必须为True# with requests.post(url, json=payload, stream=True, timeout=60) as resp:# resp.raise_for_status()# for line in resp.iter_lines(decode_unicode=True):# if not line:# continue# if not line.startswith("data: "):# continue# data = line[6:]# if data == "[DONE]":# break# chunk = json.loads(data)# delta = chunk["choices"][0].get("delta", {})# content = delta.get("content")# if content:# print(content, end="", flush=True)# print()
Langchain-Chatchat启动方式
命令 说明 启动的服务 chatchat start -a或--all-webui启动所有服务(含WebUI) Controller、模型工作器、API服务、WebUI chatchat start --all-api启动所有API服务(不含WebUI) Controller、模型工作器、API服务 chatchat start --api仅启动API服务 仅API Server chatchat start --llm-api仅启动LLM相关服务 Controller、OpenAI API、模型工作器 chatchat start -w或--webui仅启动WebUI WebUI Server chatchat start --lite精简模式(仅使用在线API) API服务、WebUI(不加载本地模型)