mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-19 21:37:20 +08:00
* 优化configs (#1474) * remove llm_model_dict * optimize configs * fix get_model_path * 更改一些默认参数,添加千帆的默认配置 * Update server_config.py.example * fix merge conflict for #1474 (#1494) * 修复ChatGPT api_base_url错误;用户可以在model_config在线模型配置中覆盖默认的api_base_url (#1496) * 优化LLM模型列表获取、切换的逻辑: (#1497) 1、更准确的获取未运行的可用模型 2、优化WEBUI模型列表显示与切换的控制逻辑 * 更新migrate.py和init_database.py,加强知识库迁移工具: (#1498) 1. 添加--update-in-db参数,按照数据库信息,从本地文件更新向量库 2. 添加--increament参数,根据本地文件增量更新向量库 3. 添加--prune-db参数,删除本地文件后,自动清理相关的向量库 4. 添加--prune-folder参数,根据数据库信息,清理无用的本地文件 5. 取消--update-info-only参数。数据库中存储了向量库信息,该操作意义不大 6. 添加--kb-name参数,所有操作支持指定操作的知识库,不指定则为所有本地知识库 7. 添加知识库迁移的测试用例 8. 删除milvus_kb_service的save_vector_store方法 * feat: support volc fangzhou * 使火山方舟正常工作,添加错误处理和测试用例 * feat: support volc fangzhou (#1501) * feat: support volc fangzhou --------- Co-authored-by: liunux4odoo <41217877+liunux4odoo@users.noreply.github.com> Co-authored-by: liqiankun.1111 <liqiankun.1111@bytedance.com> * 第一版初步agent实现 (#1503) * 第一版初步agent实现 * 增加steaming参数 * 修改了weather.py --------- Co-authored-by: zR <zRzRzRzRzRzRzR> * 添加configs/prompt_config.py,允许用户自定义prompt模板: (#1504) 1、 默认包含2个模板,分别用于LLM对话,知识库和搜索引擎对话 2、 server/utils.py提供函数get_prompt_template,获取指定的prompt模板内容(支持热加载) 3、 api.py中chat/knowledge_base_chat/search_engine_chat接口支持prompt_name参数 * 增加其它模型的参数适配 * 增加传入矢量名称加载 * 1. 搜索引擎问答支持历史记录; 2. 修复知识库问答历史记录传参错误:用户输入被传入history,问题出在webui中重复获取历史消息,api知识库对话接口并无问题。 * langchain日志开关 * move wrap_done & get_ChatOpenAI from server.chat.utils to server.utils (#1506) * 修复faiss_pool知识库缓存key错误 (#1507) * fix ReadMe anchor link (#1500) * fix : Duplicate variable and function name (#1509) Co-authored-by: Jim <zhangpengyi@taijihuabao.com> * Update README.md * fix #1519: streamlit-chatbox旧版BUG,但新版有兼容问题,先在webui中作处理,并限定chatbox版本 (#1525) close #1519 * 【功能新增】在线 LLM 模型支持阿里云通义千问 (#1534) * feat: add qwen-api * 使Qwen API支持temperature参数;添加测试用例 * 将online-api的sdk列为可选依赖 --------- Co-authored-by: liunux4odoo <liunux@qq.com> * 处理序列化至磁盘的逻辑 * remove depends on volcengine * update kb_doc_api: use Form instead of Body when upload file * 将所有httpx请求改为使用Client,提高效率,方便以后设置代理等。 (#1554) 将所有httpx请求改为使用Client,提高效率,方便以后设置代理等。 将本项目相关服务加入无代理列表,避免fastchat的服务器请求错误。(windows下无效) * update QR code * update readme_en,readme,requirements_api,requirements,model_config.py.example:测试baichuan2-7b;更新相关文档 * 新增特性:1.支持vllm推理加速框架;2. 更新支持模型列表 * 更新文件:1. startup,model_config.py.example,serve_config.py.example,FAQ * 1. debug vllm加速框架完毕;2. 修改requirements,requirements_api对vllm的依赖;3.注释掉serve_config中baichuan-7b的device为cpu的配置 * 1. 更新congif中关于vllm后端相关说明;2. 更新requirements,requirements_api; * 增加了仅限GPT4的agent功能,陆续补充,中文版readme已写 (#1611) * Dev (#1613) * 增加了仅限GPT4的agent功能,陆续补充,中文版readme已写 * issue提到的一个bug * 温度最小改成0,但是不应该支持负数 * 修改了最小的温度 * fix: set vllm based on platform to avoid error on windows * fix: langchain warnings for import from root * 修复webui中重建知识库以及对话界面UI错误 (#1615) * 修复bug:webui点重建知识库时,如果存在不支持的文件会导致整个接口错误;migrate中没有导入CHUNK_SIZE * 修复:webui对话界面的expander一直为running状态;简化历史消息获取方法 * 根据官方文档,添加对英文版的bge embedding的指示模板 (#1585) Co-authored-by: zR <2448370773@qq.com> * Dev (#1618) * 增加了仅限GPT4的agent功能,陆续补充,中文版readme已写 * issue提到的一个bug * 温度最小改成0,但是不应该支持负数 * 修改了最小的温度 * 增加了部分Agent支持和修改了启动文件的部分bug * 修改了GPU数量配置文件 * 1 1 * 修复配置文件错误 * 更新readme,稳定测试 * 更改readme 0928 (#1619) * 增加了仅限GPT4的agent功能,陆续补充,中文版readme已写 * issue提到的一个bug * 温度最小改成0,但是不应该支持负数 * 修改了最小的温度 * 增加了部分Agent支持和修改了启动文件的部分bug * 修改了GPU数量配置文件 * 1 1 * 修复配置文件错误 * 更新readme,稳定测试 * 更新readme * fix readme * 处理序列化至磁盘的逻辑 * update version number to v0.2.5 --------- Co-authored-by: qiankunli <qiankun.li@qq.com> Co-authored-by: liqiankun.1111 <liqiankun.1111@bytedance.com> Co-authored-by: zR <2448370773@qq.com> Co-authored-by: glide-the <2533736852@qq.com> Co-authored-by: Water Zheng <1499383852@qq.com> Co-authored-by: Jim Zhang <dividi_z@163.com> Co-authored-by: Jim <zhangpengyi@taijihuabao.com> Co-authored-by: imClumsyPanda <littlepanda0716@gmail.com> Co-authored-by: Leego <leegodev@hotmail.com> Co-authored-by: hzg0601 <hzg0601@163.com> Co-authored-by: WilliamChen-luckbob <58684828+WilliamChen-luckbob@users.noreply.github.com>
136 lines
5.9 KiB
Python
136 lines
5.9 KiB
Python
from configs import (EMBEDDING_MODEL, DEFAULT_VS_TYPE, ZH_TITLE_ENHANCE,
|
|
CHUNK_SIZE, OVERLAP_SIZE,
|
|
logger, log_verbose)
|
|
from server.knowledge_base.utils import (get_file_path, list_kbs_from_folder,
|
|
list_files_from_folder,files2docs_in_thread,
|
|
KnowledgeFile,)
|
|
from server.knowledge_base.kb_service.base import KBServiceFactory
|
|
from server.db.repository.knowledge_file_repository import add_file_to_db
|
|
from server.db.base import Base, engine
|
|
import os
|
|
from typing import Literal, Any, List
|
|
|
|
|
|
def create_tables():
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def reset_tables():
|
|
Base.metadata.drop_all(bind=engine)
|
|
create_tables()
|
|
|
|
|
|
def file_to_kbfile(kb_name: str, files: List[str]) -> List[KnowledgeFile]:
|
|
kb_files = []
|
|
for file in files:
|
|
try:
|
|
kb_file = KnowledgeFile(filename=file, knowledge_base_name=kb_name)
|
|
kb_files.append(kb_file)
|
|
except Exception as e:
|
|
msg = f"{e},已跳过"
|
|
logger.error(f'{e.__class__.__name__}: {msg}',
|
|
exc_info=e if log_verbose else None)
|
|
return kb_files
|
|
|
|
|
|
def folder2db(
|
|
kb_names: List[str],
|
|
mode: Literal["recreate_vs", "update_in_db", "increament"],
|
|
vs_type: Literal["faiss", "milvus", "pg", "chromadb"] = DEFAULT_VS_TYPE,
|
|
embed_model: str = EMBEDDING_MODEL,
|
|
chunk_size: int = CHUNK_SIZE,
|
|
chunk_overlap: int = CHUNK_SIZE,
|
|
zh_title_enhance: bool = ZH_TITLE_ENHANCE,
|
|
):
|
|
'''
|
|
use existed files in local folder to populate database and/or vector store.
|
|
set parameter `mode` to:
|
|
recreate_vs: recreate all vector store and fill info to database using existed files in local folder
|
|
fill_info_only(disabled): do not create vector store, fill info to db using existed files only
|
|
update_in_db: update vector store and database info using local files that existed in database only
|
|
increament: create vector store and database info for local files that not existed in database only
|
|
'''
|
|
def files2vs(kb_name: str, kb_files: List[KnowledgeFile]):
|
|
for success, result in files2docs_in_thread(kb_files,
|
|
chunk_size=chunk_size,
|
|
chunk_overlap=chunk_overlap,
|
|
zh_title_enhance=zh_title_enhance):
|
|
if success:
|
|
_, filename, docs = result
|
|
print(f"正在将 {kb_name}/{filename} 添加到向量库,共包含{len(docs)}条文档")
|
|
kb_file = KnowledgeFile(filename=filename, knowledge_base_name=kb_name)
|
|
kb_file.splited_docs = docs
|
|
kb.add_doc(kb_file=kb_file, not_refresh_vs_cache=True)
|
|
else:
|
|
print(result)
|
|
|
|
kb_names = kb_names or list_kbs_from_folder()
|
|
for kb_name in kb_names:
|
|
kb = KBServiceFactory.get_service(kb_name, vs_type, embed_model)
|
|
kb.create_kb()
|
|
|
|
# 清除向量库,从本地文件重建
|
|
if mode == "recreate_vs":
|
|
kb.clear_vs()
|
|
kb_files = file_to_kbfile(kb_name, list_files_from_folder(kb_name))
|
|
files2vs(kb_name, kb_files)
|
|
kb.save_vector_store()
|
|
# # 不做文件内容的向量化,仅将文件元信息存到数据库
|
|
# # 由于现在数据库存了很多与文本切分相关的信息,单纯存储文件信息意义不大,该功能取消。
|
|
# elif mode == "fill_info_only":
|
|
# files = list_files_from_folder(kb_name)
|
|
# kb_files = file_to_kbfile(kb_name, files)
|
|
# for kb_file in kb_files:
|
|
# add_file_to_db(kb_file)
|
|
# print(f"已将 {kb_name}/{kb_file.filename} 添加到数据库")
|
|
# 以数据库中文件列表为基准,利用本地文件更新向量库
|
|
elif mode == "update_in_db":
|
|
files = kb.list_files()
|
|
kb_files = file_to_kbfile(kb_name, files)
|
|
files2vs(kb_name, kb_files)
|
|
kb.save_vector_store()
|
|
# 对比本地目录与数据库中的文件列表,进行增量向量化
|
|
elif mode == "increament":
|
|
db_files = kb.list_files()
|
|
folder_files = list_files_from_folder(kb_name)
|
|
files = list(set(folder_files) - set(db_files))
|
|
kb_files = file_to_kbfile(kb_name, files)
|
|
files2vs(kb_name, kb_files)
|
|
kb.save_vector_store()
|
|
else:
|
|
print(f"unspported migrate mode: {mode}")
|
|
|
|
|
|
def prune_db_docs(kb_names: List[str]):
|
|
'''
|
|
delete docs in database that not existed in local folder.
|
|
it is used to delete database docs after user deleted some doc files in file browser
|
|
'''
|
|
for kb_name in kb_names:
|
|
kb = KBServiceFactory.get_service_by_name(kb_name)
|
|
if kb and kb.exists():
|
|
files_in_db = kb.list_files()
|
|
files_in_folder = list_files_from_folder(kb_name)
|
|
files = list(set(files_in_db) - set(files_in_folder))
|
|
kb_files = file_to_kbfile(kb_name, files)
|
|
for kb_file in kb_files:
|
|
kb.delete_doc(kb_file, not_refresh_vs_cache=True)
|
|
print(f"success to delete docs for file: {kb_name}/{kb_file.filename}")
|
|
kb.save_vector_store()
|
|
|
|
|
|
def prune_folder_files(kb_names: List[str]):
|
|
'''
|
|
delete doc files in local folder that not existed in database.
|
|
is is used to free local disk space by delete unused doc files.
|
|
'''
|
|
for kb_name in kb_names:
|
|
kb = KBServiceFactory.get_service_by_name(kb_name)
|
|
if kb and kb.exists():
|
|
files_in_db = kb.list_files()
|
|
files_in_folder = list_files_from_folder(kb_name)
|
|
files = list(set(files_in_folder) - set(files_in_db))
|
|
for file in files:
|
|
os.remove(get_file_path(kb_name, file))
|
|
print(f"success to delete file: {kb_name}/{file}")
|