2023-08-01 14:15:42 +08:00
|
|
|
|
# 该文件包含webui通用工具,可以被不同的webui使用
|
|
|
|
|
|
from typing import *
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from configs.model_config import (
|
2023-08-09 22:00:33 +08:00
|
|
|
|
EMBEDDING_MODEL,
|
2023-08-11 13:53:20 +08:00
|
|
|
|
DEFAULT_VS_TYPE,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
KB_ROOT_PATH,
|
|
|
|
|
|
LLM_MODEL,
|
2023-08-16 13:18:58 +08:00
|
|
|
|
SCORE_THRESHOLD,
|
2023-08-04 12:49:39 +08:00
|
|
|
|
VECTOR_SEARCH_TOP_K,
|
|
|
|
|
|
SEARCH_ENGINE_TOP_K,
|
2023-08-15 14:24:54 +08:00
|
|
|
|
logger,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
)
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
from server.chat.openai_chat import OpenAiChatMsgIn
|
|
|
|
|
|
from fastapi.responses import StreamingResponse
|
2023-08-03 12:52:49 +08:00
|
|
|
|
import contextlib
|
2023-08-04 12:49:39 +08:00
|
|
|
|
import json
|
2023-08-04 15:53:44 +08:00
|
|
|
|
from io import BytesIO
|
2023-08-10 11:37:14 +08:00
|
|
|
|
from server.db.repository.knowledge_base_repository import get_kb_detail
|
|
|
|
|
|
from server.db.repository.knowledge_file_repository import get_file_detail
|
2023-08-11 08:37:07 +08:00
|
|
|
|
from server.utils import run_async, iter_over_async
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-13 10:25:02 +08:00
|
|
|
|
from configs.model_config import NLTK_DATA_PATH
|
|
|
|
|
|
import nltk
|
|
|
|
|
|
nltk.data.path = [NLTK_DATA_PATH] + nltk.data.path
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-15 14:24:54 +08:00
|
|
|
|
|
2023-08-01 14:15:42 +08:00
|
|
|
|
def set_httpx_timeout(timeout=60.0):
|
|
|
|
|
|
'''
|
|
|
|
|
|
设置httpx默认timeout到60秒。
|
|
|
|
|
|
httpx默认timeout是5秒,在请求LLM回答时不够用。
|
|
|
|
|
|
'''
|
|
|
|
|
|
httpx._config.DEFAULT_TIMEOUT_CONFIG.connect = timeout
|
|
|
|
|
|
httpx._config.DEFAULT_TIMEOUT_CONFIG.read = timeout
|
|
|
|
|
|
httpx._config.DEFAULT_TIMEOUT_CONFIG.write = timeout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KB_ROOT_PATH = Path(KB_ROOT_PATH)
|
|
|
|
|
|
set_httpx_timeout()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiRequest:
|
|
|
|
|
|
'''
|
|
|
|
|
|
api.py调用的封装,主要实现:
|
|
|
|
|
|
1. 简化api调用方式
|
|
|
|
|
|
2. 实现无api调用(直接运行server.chat.*中的视图函数获取结果),无需启动api.py
|
|
|
|
|
|
'''
|
|
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
base_url: str = "http://127.0.0.1:7861",
|
|
|
|
|
|
timeout: float = 60.0,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = False, # call api view function directly
|
2023-08-01 14:15:42 +08:00
|
|
|
|
):
|
|
|
|
|
|
self.base_url = base_url
|
|
|
|
|
|
self.timeout = timeout
|
2023-08-03 10:49:57 +08:00
|
|
|
|
self.no_remote_api = no_remote_api
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
|
|
|
|
|
def _parse_url(self, url: str) -> str:
|
|
|
|
|
|
if (not url.startswith("http")
|
|
|
|
|
|
and self.base_url
|
|
|
|
|
|
):
|
|
|
|
|
|
part1 = self.base_url.strip(" /")
|
|
|
|
|
|
part2 = url.strip(" /")
|
|
|
|
|
|
return f"{part1}/{part2}"
|
|
|
|
|
|
else:
|
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def get(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
params: Union[Dict, List[Tuple], bytes] = None,
|
|
|
|
|
|
retry: int = 3,
|
2023-08-11 08:37:07 +08:00
|
|
|
|
stream: bool = False,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
**kwargs: Any,
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
|
|
|
|
|
url = self._parse_url(url)
|
|
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
if stream:
|
|
|
|
|
|
return httpx.stream("GET", url, params=params, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return httpx.get(url, params=params, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
|
|
|
|
|
async def aget(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
params: Union[Dict, List[Tuple], bytes] = None,
|
|
|
|
|
|
retry: int = 3,
|
2023-08-11 08:37:07 +08:00
|
|
|
|
stream: bool = False,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
**kwargs: Any,
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
2023-08-11 13:53:20 +08:00
|
|
|
|
url = self._parse_url(url)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
if stream:
|
|
|
|
|
|
return await client.stream("GET", url, params=params, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return await client.get(url, params=params, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
|
|
|
|
|
def post(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
data: Dict = None,
|
|
|
|
|
|
json: Dict = None,
|
|
|
|
|
|
retry: int = 3,
|
|
|
|
|
|
stream: bool = False,
|
|
|
|
|
|
**kwargs: Any
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
|
|
|
|
|
url = self._parse_url(url)
|
|
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
|
|
|
|
|
# return requests.post(url, data=data, json=json, stream=stream, **kwargs)
|
|
|
|
|
|
if stream:
|
|
|
|
|
|
return httpx.stream("POST", url, data=data, json=json, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return httpx.post(url, data=data, json=json, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
|
|
|
|
|
async def apost(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
data: Dict = None,
|
|
|
|
|
|
json: Dict = None,
|
|
|
|
|
|
retry: int = 3,
|
2023-08-11 08:37:07 +08:00
|
|
|
|
stream: bool = False,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
**kwargs: Any
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
2023-08-11 13:53:20 +08:00
|
|
|
|
url = self._parse_url(url)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
if stream:
|
|
|
|
|
|
return await client.stream("POST", url, data=data, json=json, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return await client.post(url, data=data, json=json, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-11 08:37:07 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
|
|
|
|
|
def delete(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
data: Dict = None,
|
|
|
|
|
|
json: Dict = None,
|
|
|
|
|
|
retry: int = 3,
|
|
|
|
|
|
stream: bool = False,
|
|
|
|
|
|
**kwargs: Any
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
|
|
|
|
|
url = self._parse_url(url)
|
|
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
|
|
|
|
|
if stream:
|
|
|
|
|
|
return httpx.stream("DELETE", url, data=data, json=json, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return httpx.delete(url, data=data, json=json, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-11 08:37:07 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
|
|
|
|
|
async def adelete(
|
|
|
|
|
|
self,
|
|
|
|
|
|
url: str,
|
|
|
|
|
|
data: Dict = None,
|
|
|
|
|
|
json: Dict = None,
|
|
|
|
|
|
retry: int = 3,
|
|
|
|
|
|
stream: bool = False,
|
|
|
|
|
|
**kwargs: Any
|
|
|
|
|
|
) -> Union[httpx.Response, None]:
|
2023-08-11 13:53:20 +08:00
|
|
|
|
url = self._parse_url(url)
|
2023-08-11 08:37:07 +08:00
|
|
|
|
kwargs.setdefault("timeout", self.timeout)
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
|
while retry > 0:
|
|
|
|
|
|
try:
|
|
|
|
|
|
if stream:
|
|
|
|
|
|
return await client.stream("DELETE", url, data=data, json=json, **kwargs)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return await client.delete(url, data=data, json=json, **kwargs)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
retry -= 1
|
|
|
|
|
|
|
2023-08-04 12:49:39 +08:00
|
|
|
|
def _fastapi_stream2generator(self, response: StreamingResponse, as_json: bool =False):
|
2023-08-01 14:15:42 +08:00
|
|
|
|
'''
|
|
|
|
|
|
将api.py中视图函数返回的StreamingResponse转化为同步生成器
|
|
|
|
|
|
'''
|
|
|
|
|
|
try:
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
except:
|
|
|
|
|
|
loop = asyncio.new_event_loop()
|
2023-08-04 12:49:39 +08:00
|
|
|
|
|
2023-08-15 14:24:54 +08:00
|
|
|
|
try:
|
|
|
|
|
|
for chunk in iter_over_async(response.body_iterator, loop):
|
|
|
|
|
|
if as_json and chunk:
|
|
|
|
|
|
yield json.loads(chunk)
|
|
|
|
|
|
elif chunk.strip():
|
|
|
|
|
|
yield chunk
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-04 12:49:39 +08:00
|
|
|
|
def _httpx_stream2generator(
|
|
|
|
|
|
self,
|
|
|
|
|
|
response: contextlib._GeneratorContextManager,
|
|
|
|
|
|
as_json: bool = False,
|
|
|
|
|
|
):
|
2023-08-03 12:52:49 +08:00
|
|
|
|
'''
|
|
|
|
|
|
将httpx.stream返回的GeneratorContextManager转化为普通生成器
|
|
|
|
|
|
'''
|
2023-08-15 14:24:54 +08:00
|
|
|
|
try:
|
|
|
|
|
|
with response as r:
|
|
|
|
|
|
for chunk in r.iter_text(None):
|
|
|
|
|
|
if as_json and chunk:
|
|
|
|
|
|
yield json.loads(chunk)
|
|
|
|
|
|
elif chunk.strip():
|
|
|
|
|
|
yield chunk
|
|
|
|
|
|
except httpx.ConnectError as e:
|
|
|
|
|
|
msg = f"无法连接API服务器,请确认已执行python server\\api.py"
|
|
|
|
|
|
logger.error(msg)
|
|
|
|
|
|
logger.error(e)
|
|
|
|
|
|
yield {"code": 500, "errorMsg": msg}
|
|
|
|
|
|
except httpx.ReadTimeout as e:
|
|
|
|
|
|
msg = f"API通信超时,请确认已启动FastChat与API服务(详见RADME '5. 启动 API 服务或 Web UI')"
|
|
|
|
|
|
logger.error(msg)
|
|
|
|
|
|
logger.error(e)
|
|
|
|
|
|
yield {"code": 500, "errorMsg": msg}
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
|
|
|
|
|
yield {"code": 500, "errorMsg": str(e)}
|
2023-08-03 12:52:49 +08:00
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
# 对话相关操作
|
|
|
|
|
|
|
2023-08-01 14:15:42 +08:00
|
|
|
|
def chat_fastchat(
|
|
|
|
|
|
self,
|
|
|
|
|
|
messages: List[Dict],
|
|
|
|
|
|
stream: bool = True,
|
|
|
|
|
|
model: str = LLM_MODEL,
|
|
|
|
|
|
temperature: float = 0.7,
|
|
|
|
|
|
max_tokens: int = 1024, # todo:根据message内容自动计算max_tokens
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
**kwargs: Any,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/chat/fastchat接口
|
|
|
|
|
|
'''
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
2023-08-01 14:15:42 +08:00
|
|
|
|
msg = OpenAiChatMsgIn(**{
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"stream": stream,
|
|
|
|
|
|
"model": model,
|
|
|
|
|
|
"temperature": temperature,
|
|
|
|
|
|
"max_tokens": max_tokens,
|
|
|
|
|
|
**kwargs,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.chat.openai_chat import openai_chat
|
|
|
|
|
|
response = openai_chat(msg)
|
2023-08-03 12:52:49 +08:00
|
|
|
|
return self._fastapi_stream2generator(response)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
else:
|
|
|
|
|
|
data = msg.dict(exclude_unset=True, exclude_none=True)
|
|
|
|
|
|
response = self.post(
|
|
|
|
|
|
"/chat/fastchat",
|
|
|
|
|
|
json=data,
|
|
|
|
|
|
stream=stream,
|
|
|
|
|
|
)
|
2023-08-03 12:52:49 +08:00
|
|
|
|
return self._httpx_stream2generator(response)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
|
|
|
|
|
def chat_chat(
|
|
|
|
|
|
self,
|
|
|
|
|
|
query: str,
|
2023-08-09 11:00:22 +08:00
|
|
|
|
history: List[Dict] = [],
|
2023-08-14 11:46:36 +08:00
|
|
|
|
stream: bool = True,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
2023-08-01 14:15:42 +08:00
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/chat/chat接口
|
|
|
|
|
|
'''
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-14 11:46:36 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"query": query,
|
|
|
|
|
|
"history": history,
|
|
|
|
|
|
"stream": stream,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-01 14:15:42 +08:00
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.chat.chat import chat
|
2023-08-14 11:46:36 +08:00
|
|
|
|
response = chat(**data)
|
2023-08-03 12:52:49 +08:00
|
|
|
|
return self._fastapi_stream2generator(response)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
else:
|
2023-08-14 11:46:36 +08:00
|
|
|
|
response = self.post("/chat/chat", json=data, stream=True)
|
2023-08-03 12:52:49 +08:00
|
|
|
|
return self._httpx_stream2generator(response)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-03 09:33:24 +08:00
|
|
|
|
def knowledge_base_chat(
|
|
|
|
|
|
self,
|
|
|
|
|
|
query: str,
|
|
|
|
|
|
knowledge_base_name: str,
|
2023-08-04 12:49:39 +08:00
|
|
|
|
top_k: int = VECTOR_SEARCH_TOP_K,
|
2023-08-16 13:18:58 +08:00
|
|
|
|
score_threshold: float = SCORE_THRESHOLD,
|
2023-08-09 10:46:01 +08:00
|
|
|
|
history: List[Dict] = [],
|
2023-08-09 23:35:36 +08:00
|
|
|
|
stream: bool = True,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
2023-08-03 09:33:24 +08:00
|
|
|
|
):
|
|
|
|
|
|
'''
|
2023-08-03 10:49:57 +08:00
|
|
|
|
对应api.py/chat/knowledge_base_chat接口
|
2023-08-03 09:33:24 +08:00
|
|
|
|
'''
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-09 23:35:36 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"query": query,
|
|
|
|
|
|
"knowledge_base_name": knowledge_base_name,
|
|
|
|
|
|
"top_k": top_k,
|
2023-08-16 13:18:58 +08:00
|
|
|
|
"score_threshold": score_threshold,
|
2023-08-09 23:35:36 +08:00
|
|
|
|
"history": history,
|
|
|
|
|
|
"stream": stream,
|
2023-08-14 11:46:36 +08:00
|
|
|
|
"local_doc_url": no_remote_api,
|
2023-08-09 23:35:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-03 09:33:24 +08:00
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.chat.knowledge_base_chat import knowledge_base_chat
|
2023-08-09 23:35:36 +08:00
|
|
|
|
response = knowledge_base_chat(**data)
|
2023-08-04 12:49:39 +08:00
|
|
|
|
return self._fastapi_stream2generator(response, as_json=True)
|
2023-08-03 09:33:24 +08:00
|
|
|
|
else:
|
|
|
|
|
|
response = self.post(
|
|
|
|
|
|
"/chat/knowledge_base_chat",
|
2023-08-09 23:35:36 +08:00
|
|
|
|
json=data,
|
2023-08-03 09:33:24 +08:00
|
|
|
|
stream=True,
|
|
|
|
|
|
)
|
2023-08-04 12:49:39 +08:00
|
|
|
|
return self._httpx_stream2generator(response, as_json=True)
|
2023-08-03 09:33:24 +08:00
|
|
|
|
|
2023-08-03 18:22:36 +08:00
|
|
|
|
def search_engine_chat(
|
2023-08-03 09:33:24 +08:00
|
|
|
|
self,
|
|
|
|
|
|
query: str,
|
2023-08-03 18:22:36 +08:00
|
|
|
|
search_engine_name: str,
|
2023-08-04 12:49:39 +08:00
|
|
|
|
top_k: int = SEARCH_ENGINE_TOP_K,
|
2023-08-09 23:35:36 +08:00
|
|
|
|
stream: bool = True,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
2023-08-03 09:33:24 +08:00
|
|
|
|
):
|
|
|
|
|
|
'''
|
2023-08-03 18:22:36 +08:00
|
|
|
|
对应api.py/chat/search_engine_chat接口
|
2023-08-03 09:33:24 +08:00
|
|
|
|
'''
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-09 23:35:36 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"query": query,
|
|
|
|
|
|
"search_engine_name": search_engine_name,
|
|
|
|
|
|
"top_k": top_k,
|
|
|
|
|
|
"stream": stream,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-03 09:33:24 +08:00
|
|
|
|
if no_remote_api:
|
2023-08-03 18:22:36 +08:00
|
|
|
|
from server.chat.search_engine_chat import search_engine_chat
|
2023-08-09 23:35:36 +08:00
|
|
|
|
response = search_engine_chat(**data)
|
2023-08-04 12:49:39 +08:00
|
|
|
|
return self._fastapi_stream2generator(response, as_json=True)
|
2023-08-03 09:33:24 +08:00
|
|
|
|
else:
|
|
|
|
|
|
response = self.post(
|
2023-08-03 18:22:36 +08:00
|
|
|
|
"/chat/search_engine_chat",
|
2023-08-09 23:35:36 +08:00
|
|
|
|
json=data,
|
2023-08-03 09:33:24 +08:00
|
|
|
|
stream=True,
|
|
|
|
|
|
)
|
2023-08-04 12:49:39 +08:00
|
|
|
|
return self._httpx_stream2generator(response, as_json=True)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
# 知识库相关操作
|
|
|
|
|
|
|
2023-08-15 14:24:54 +08:00
|
|
|
|
def _check_httpx_json_response(
|
|
|
|
|
|
self,
|
|
|
|
|
|
response: httpx.Response,
|
|
|
|
|
|
errorMsg: str = f"无法连接API服务器,请确认已执行python server\\api.py",
|
|
|
|
|
|
) -> Dict:
|
|
|
|
|
|
'''
|
|
|
|
|
|
check whether httpx returns correct data with normal Response.
|
|
|
|
|
|
error in api with streaming support was checked in _httpx_stream2enerator
|
|
|
|
|
|
'''
|
|
|
|
|
|
try:
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(e)
|
|
|
|
|
|
return {"code": 500, "errorMsg": errorMsg or str(e)}
|
|
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
def list_knowledge_bases(
|
|
|
|
|
|
self,
|
|
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/list_knowledge_bases接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_api import list_kbs
|
|
|
|
|
|
response = run_async(list_kbs())
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = self.get("/knowledge_base/list_knowledge_bases")
|
2023-08-15 14:24:54 +08:00
|
|
|
|
data = self._check_httpx_json_response(response)
|
|
|
|
|
|
return data.get("data", [])
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
def create_knowledge_base(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
2023-08-09 22:00:33 +08:00
|
|
|
|
vector_store_type: str = "faiss",
|
|
|
|
|
|
embed_model: str = EMBEDDING_MODEL,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/create_knowledge_base接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-09 23:35:36 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"knowledge_base_name": knowledge_base_name,
|
|
|
|
|
|
"vector_store_type": vector_store_type,
|
|
|
|
|
|
"embed_model": embed_model,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_api import create_kb
|
2023-08-09 23:35:36 +08:00
|
|
|
|
response = run_async(create_kb(**data))
|
2023-08-03 10:49:57 +08:00
|
|
|
|
return response.dict()
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = self.post(
|
|
|
|
|
|
"/knowledge_base/create_knowledge_base",
|
2023-08-09 23:35:36 +08:00
|
|
|
|
json=data,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
return self._check_httpx_json_response(response)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
def delete_knowledge_base(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
|
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/delete_knowledge_base接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_api import delete_kb
|
|
|
|
|
|
response = run_async(delete_kb(knowledge_base_name))
|
|
|
|
|
|
return response.dict()
|
|
|
|
|
|
else:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
response = self.post(
|
2023-08-03 10:49:57 +08:00
|
|
|
|
"/knowledge_base/delete_knowledge_base",
|
2023-08-11 23:35:27 +08:00
|
|
|
|
json=f"{knowledge_base_name}",
|
2023-08-03 10:49:57 +08:00
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
return self._check_httpx_json_response(response)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
def list_kb_docs(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
|
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/list_docs接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_doc_api import list_docs
|
|
|
|
|
|
response = run_async(list_docs(knowledge_base_name))
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = self.get(
|
|
|
|
|
|
"/knowledge_base/list_docs",
|
|
|
|
|
|
params={"knowledge_base_name": knowledge_base_name}
|
|
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
data = self._check_httpx_json_response(response)
|
|
|
|
|
|
return data.get("data", [])
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
def upload_kb_doc(
|
|
|
|
|
|
self,
|
2023-08-04 15:53:44 +08:00
|
|
|
|
file: Union[str, Path, bytes],
|
2023-08-03 10:49:57 +08:00
|
|
|
|
knowledge_base_name: str,
|
2023-08-04 15:53:44 +08:00
|
|
|
|
filename: str = None,
|
|
|
|
|
|
override: bool = False,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/upload_docs接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-09 16:52:04 +08:00
|
|
|
|
if isinstance(file, bytes): # raw bytes
|
2023-08-04 15:53:44 +08:00
|
|
|
|
file = BytesIO(file)
|
2023-08-09 16:52:04 +08:00
|
|
|
|
elif hasattr(file, "read"): # a file io like object
|
|
|
|
|
|
filename = filename or file.name
|
|
|
|
|
|
else: # a local path
|
2023-08-04 15:53:44 +08:00
|
|
|
|
file = Path(file).absolute().open("rb")
|
|
|
|
|
|
filename = filename or file.name
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_doc_api import upload_doc
|
|
|
|
|
|
from fastapi import UploadFile
|
|
|
|
|
|
from tempfile import SpooledTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
temp_file = SpooledTemporaryFile(max_size=10 * 1024 * 1024)
|
2023-08-04 15:53:44 +08:00
|
|
|
|
temp_file.write(file.read())
|
2023-08-09 16:52:04 +08:00
|
|
|
|
temp_file.seek(0)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
response = run_async(upload_doc(
|
2023-08-10 15:51:22 +08:00
|
|
|
|
UploadFile(file=temp_file, filename=filename),
|
2023-08-03 10:49:57 +08:00
|
|
|
|
knowledge_base_name,
|
2023-08-04 15:53:44 +08:00
|
|
|
|
override,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
))
|
|
|
|
|
|
return response.dict()
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = self.post(
|
|
|
|
|
|
"/knowledge_base/upload_doc",
|
2023-08-04 15:53:44 +08:00
|
|
|
|
data={"knowledge_base_name": knowledge_base_name, "override": override},
|
|
|
|
|
|
files={"file": (filename, file)},
|
2023-08-03 10:49:57 +08:00
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
return self._check_httpx_json_response(response)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
|
|
|
|
|
def delete_kb_doc(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
|
|
|
|
|
doc_name: str,
|
2023-08-09 16:52:04 +08:00
|
|
|
|
delete_content: bool = False,
|
2023-08-03 10:49:57 +08:00
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/delete_doc接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-09 23:35:36 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"knowledge_base_name": knowledge_base_name,
|
|
|
|
|
|
"doc_name": doc_name,
|
|
|
|
|
|
"delete_content": delete_content,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_doc_api import delete_doc
|
2023-08-09 23:35:36 +08:00
|
|
|
|
response = run_async(delete_doc(**data))
|
2023-08-03 10:49:57 +08:00
|
|
|
|
return response.dict()
|
|
|
|
|
|
else:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
response = self.post(
|
2023-08-03 10:49:57 +08:00
|
|
|
|
"/knowledge_base/delete_doc",
|
2023-08-09 23:35:36 +08:00
|
|
|
|
json=data,
|
2023-08-09 16:52:04 +08:00
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
return self._check_httpx_json_response(response)
|
2023-08-09 16:52:04 +08:00
|
|
|
|
|
|
|
|
|
|
def update_kb_doc(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
2023-08-11 08:37:07 +08:00
|
|
|
|
file_name: str,
|
2023-08-09 16:52:04 +08:00
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/update_doc接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
|
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_doc_api import update_doc
|
2023-08-11 08:37:07 +08:00
|
|
|
|
response = run_async(update_doc(knowledge_base_name, file_name))
|
2023-08-09 16:52:04 +08:00
|
|
|
|
return response.dict()
|
|
|
|
|
|
else:
|
2023-08-11 08:37:07 +08:00
|
|
|
|
response = self.post(
|
2023-08-09 16:52:04 +08:00
|
|
|
|
"/knowledge_base/update_doc",
|
2023-08-11 08:37:07 +08:00
|
|
|
|
json={"knowledge_base_name": knowledge_base_name, "file_name": file_name},
|
2023-08-03 10:49:57 +08:00
|
|
|
|
)
|
2023-08-15 14:24:54 +08:00
|
|
|
|
return self._check_httpx_json_response(response)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
2023-08-04 20:26:14 +08:00
|
|
|
|
def recreate_vector_store(
|
|
|
|
|
|
self,
|
|
|
|
|
|
knowledge_base_name: str,
|
2023-08-11 13:53:20 +08:00
|
|
|
|
allow_empty_kb: bool = True,
|
|
|
|
|
|
vs_type: str = DEFAULT_VS_TYPE,
|
|
|
|
|
|
embed_model: str = EMBEDDING_MODEL,
|
2023-08-04 20:26:14 +08:00
|
|
|
|
no_remote_api: bool = None,
|
|
|
|
|
|
):
|
|
|
|
|
|
'''
|
|
|
|
|
|
对应api.py/knowledge_base/recreate_vector_store接口
|
|
|
|
|
|
'''
|
|
|
|
|
|
if no_remote_api is None:
|
|
|
|
|
|
no_remote_api = self.no_remote_api
|
|
|
|
|
|
|
2023-08-11 13:53:20 +08:00
|
|
|
|
data = {
|
|
|
|
|
|
"knowledge_base_name": knowledge_base_name,
|
|
|
|
|
|
"allow_empty_kb": allow_empty_kb,
|
|
|
|
|
|
"vs_type": vs_type,
|
|
|
|
|
|
"embed_model": embed_model,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-04 20:26:14 +08:00
|
|
|
|
if no_remote_api:
|
|
|
|
|
|
from server.knowledge_base.kb_doc_api import recreate_vector_store
|
2023-08-11 13:53:20 +08:00
|
|
|
|
response = run_async(recreate_vector_store(**data))
|
2023-08-04 20:26:14 +08:00
|
|
|
|
return self._fastapi_stream2generator(response, as_json=True)
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = self.post(
|
|
|
|
|
|
"/knowledge_base/recreate_vector_store",
|
2023-08-11 13:53:20 +08:00
|
|
|
|
json=data,
|
|
|
|
|
|
stream=True,
|
2023-08-21 18:01:59 +08:00
|
|
|
|
timeout=None,
|
2023-08-04 20:26:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
return self._httpx_stream2generator(response, as_json=True)
|
|
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
2023-08-15 14:24:54 +08:00
|
|
|
|
def check_error_msg(data: Union[str, dict, list], key: str = "errorMsg") -> str:
|
|
|
|
|
|
'''
|
|
|
|
|
|
return error message if error occured when requests API
|
|
|
|
|
|
'''
|
|
|
|
|
|
if isinstance(data, dict) and key in data:
|
|
|
|
|
|
return data[key]
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-10 14:12:02 +08:00
|
|
|
|
if __name__ == "__main__":
|
2023-08-04 20:26:14 +08:00
|
|
|
|
api = ApiRequest(no_remote_api=True)
|
2023-08-03 10:49:57 +08:00
|
|
|
|
|
2023-08-01 14:15:42 +08:00
|
|
|
|
# print(api.chat_fastchat(
|
|
|
|
|
|
# messages=[{"role": "user", "content": "hello"}]
|
|
|
|
|
|
# ))
|
|
|
|
|
|
|
2023-08-03 10:49:57 +08:00
|
|
|
|
# with api.chat_chat("你好") as r:
|
|
|
|
|
|
# for t in r.iter_text(None):
|
|
|
|
|
|
# print(t)
|
|
|
|
|
|
|
|
|
|
|
|
# r = api.chat_chat("你好", no_remote_api=True)
|
|
|
|
|
|
# for t in r:
|
|
|
|
|
|
# print(t)
|
|
|
|
|
|
|
|
|
|
|
|
# r = api.duckduckgo_search_chat("室温超导最新研究进展", no_remote_api=True)
|
|
|
|
|
|
# for t in r:
|
|
|
|
|
|
# print(t)
|
2023-08-01 14:15:42 +08:00
|
|
|
|
|
2023-08-04 20:26:14 +08:00
|
|
|
|
# print(api.list_knowledge_bases())
|