srszzw 9b62b1c72b
dev分支解决pydantic版本冲突问题,增加ollama配置,支持ollama会话和向量接口 (#3508)
* dev分支解决pydantic版本冲突问题,增加ollama配置,支持ollama会话和向量接口
1、因dev版本的pydantic升级到了v2版本,由于在class History(BaseModel)中使用了from server.pydantic_v1,而fastapi的引用已变为pydantic的v2版本,所以fastapi用v2版本去校验用v1版本定义的对象,当会话历史histtory不为空的时候,会报错:TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given。经测试,解方法为在class History(BaseModel)中也使用v2版本即可;
2、配置文件参照其它平台配置,增加了ollama平台相关配置,会话模型用户可根据实际情况自行添加,向量模型目前支持nomic-embed-text(必须升级ollama到0.1.29以上)。
3、因ollama官方只在会话部分对openai api做了兼容,向量api暂未适配,好在langchain官方库支持OllamaEmbeddings,因而在get_Embeddings方法中添加了相关支持代码。

* 修复 pydantic 升级到 v2 后 DocumentWithVsID 和 /v1/embeddings 兼容性问题

---------

Co-authored-by: srszzw <srszzw@163.com>
Co-authored-by: liunux4odoo <liunux@qq.com>
2024-03-25 16:35:45 +08:00

89 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import List
from fastapi import APIRouter, Request
from server.chat.file_chat import upload_temp_docs
from server.knowledge_base.kb_api import list_kbs, create_kb, delete_kb
from server.knowledge_base.kb_doc_api import (list_files, upload_docs, delete_docs,
update_docs, download_doc, recreate_vector_store,
search_docs, update_info)
from server.knowledge_base.kb_summary_api import (summary_file_to_vector_store, recreate_summary_vector_store,
summary_doc_ids_to_vector_store)
from server.utils import BaseResponse, ListResponse
kb_router = APIRouter(prefix="/knowledge_base", tags=["Knowledge Base Management"])
kb_router.get("/list_knowledge_bases",
response_model=ListResponse,
summary="获取知识库列表")(list_kbs)
kb_router.post("/create_knowledge_base",
response_model=BaseResponse,
summary="创建知识库"
)(create_kb)
kb_router.post("/delete_knowledge_base",
response_model=BaseResponse,
summary="删除知识库"
)(delete_kb)
kb_router.get("/list_files",
response_model=ListResponse,
summary="获取知识库内的文件列表"
)(list_files)
kb_router.post("/search_docs",
response_model=List[dict],
summary="搜索知识库"
)(search_docs)
kb_router.post("/upload_docs",
response_model=BaseResponse,
summary="上传文件到知识库,并/或进行向量化"
)(upload_docs)
kb_router.post("/delete_docs",
response_model=BaseResponse,
summary="删除知识库内指定文件"
)(delete_docs)
kb_router.post("/update_info",
response_model=BaseResponse,
summary="更新知识库介绍"
)(update_info)
kb_router.post("/update_docs",
response_model=BaseResponse,
summary="更新现有文件到知识库"
)(update_docs)
kb_router.get("/download_doc",
summary="下载对应的知识文件")(download_doc)
kb_router.post("/recreate_vector_store",
summary="根据content中文档重建向量库流式输出处理进度。"
)(recreate_vector_store)
kb_router.post("/upload_temp_docs",
summary="上传文件到临时目录,用于文件对话。"
)(upload_temp_docs)
summary_router = APIRouter(prefix="/kb_summary_api")
summary_router.post("/summary_file_to_vector_store",
summary="单个知识库根据文件名称摘要"
)(summary_file_to_vector_store)
summary_router.post("/summary_doc_ids_to_vector_store",
summary="单个知识库根据doc_ids摘要",
response_model=BaseResponse,
)(summary_doc_ids_to_vector_store)
summary_router.post("/recreate_summary_vector_store",
summary="重建单个知识库文件摘要"
)(recreate_summary_vector_store)
kb_router.include_router(summary_router)