Skip to content

Commit

Permalink
Feat/0.3.7.1 (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit authored Nov 29, 2024
2 parents 19d5953 + ac7b813 commit 988ff95
Show file tree
Hide file tree
Showing 26 changed files with 100,768 additions and 42 deletions.
4 changes: 2 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ services:

backend:
container_name: bisheng-backend
image: dataelement/bisheng-backend:v0.3.7.dev1
image: dataelement/bisheng-backend:v0.3.7.1
ports:
- "7860:7860"
environment:
Expand Down Expand Up @@ -92,7 +92,7 @@ services:

frontend:
container_name: bisheng-frontend
image: dataelement/bisheng-frontend:v0.3.7.dev1
image: dataelement/bisheng-frontend:v0.3.7.1
ports:
- "3001:3001"
environment:
Expand Down
2 changes: 1 addition & 1 deletion src/backend/bisheng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

try:
# 通过ci去自动修改
__version__ = '0.3.7.dev1'
__version__ = '0.3.7.1'
except metadata.PackageNotFoundError:
# Case where package metadata is not available.
__version__ = ''
Expand Down
1 change: 1 addition & 0 deletions src/backend/bisheng/api/services/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ async def update_assistant(cls, request: Request, login_user: UserPayload, req:
assistant.model_name = req.model_name
assistant.temperature = req.temperature
assistant.update_time = datetime.now()
assistant.max_token = req.max_token
AssistantDao.update_assistant(assistant)

# 更新助手关联信息
Expand Down
37 changes: 32 additions & 5 deletions src/backend/bisheng/api/services/assistant_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from langchain_core.callbacks import Callbacks
from langchain_core.language_models import BaseLanguageModel
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.messages import AIMessage, HumanMessage, BaseMessage
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import BaseTool, Tool
from langchain_core.utils.function_calling import format_tool_to_openai_tool
Expand Down Expand Up @@ -363,6 +363,32 @@ async def record_chat_history(self, message: List[Any]):
except Exception as e:
logger.error(f"record assistant history error: {str(e)}")

async def trim_messages(self, messages: List[Any]) -> List[Any]:
# 获取encoding
enc = self.cl100k_base()

def get_finally_message(new_messages: List[Any]) -> List[Any]:
# 修剪到只有一条记录则不再处理
if len(new_messages) == 1:
return new_messages
total_count = 0
for one in new_messages:
if isinstance(one, HumanMessage):
total_count += len(enc.encode(one.content))
elif isinstance(one, AIMessage):
total_count += len(enc.encode(one.content))
if 'tool_calls' in one.additional_kwargs:
total_count += len(
enc.encode(json.dumps(one.additional_kwargs['tool_calls'], ensure_ascii=False))
)
else:
total_count += len(enc.encode(str(one.content)))
if total_count > self.assistant.max_token:
return get_finally_message(new_messages[1:])
return new_messages

return get_finally_message(messages)

async def arun(self, query: str, chat_history: List = None, callback: Callbacks = None):
await self.fake_callback(callback)

Expand All @@ -375,7 +401,7 @@ async def arun(self, query: str, chat_history: List = None, callback: Callbacks
async for one in self.agent.astream(inputs, config=RunnableConfig(callbacks=callback)):
yield one

async def run(self, query: str, chat_history: List = None, callback: Callbacks = None):
async def run(self, query: str, chat_history: List = None, callback: Callbacks = None) -> List[BaseMessage]:
"""
运行智能体对话
"""
Expand All @@ -387,17 +413,18 @@ async def run(self, query: str, chat_history: List = None, callback: Callbacks =
else:
inputs = [HumanMessage(content=query)]

# trim message
inputs = await self.trim_messages(inputs)

if self.current_agent_executor == 'ReAct':
result = await self.react_run(inputs, callback)
else:
result = await self.agent.ainvoke(inputs, config=RunnableConfig(callbacks=callback))
# 包含了history,将history排除, 默认取最后一个为最终结果
res = [result[-1]]

# 记录聊天历史
await self.record_chat_history([one.to_json() for one in result])

return res
return result

async def react_run(self, inputs: List, callback: Callbacks = None):
""" react 模式的输入和执行 """
Expand Down
34 changes: 33 additions & 1 deletion src/backend/bisheng/api/services/assistant_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import os

from tiktoken.load import load_tiktoken_bpe
from tiktoken.core import Encoding as TikTokenEncoding


class AssistantUtils:
# 忽略助手配置已从系统配置中移除,暂不需要此类的方法

pass
@staticmethod
def cl100k_base() -> TikTokenEncoding:
ENDOFTEXT = "<|endoftext|>"
FIM_PREFIX = "<|fim_prefix|>"
FIM_MIDDLE = "<|fim_middle|>"
FIM_SUFFIX = "<|fim_suffix|>"
ENDOFPROMPT = "<|endofprompt|>"

tiktoken_file = os.path.join(os.path.dirname(__file__), "tiktoken_file/cl100k_base.tiktoken")

mergeable_ranks = load_tiktoken_bpe(
# "https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken",
tiktoken_file,
expected_hash="223921b76ee99bde995b7ff738513eef100fb51d18c93597a113bcffe865b2a7",
)
special_tokens = {
ENDOFTEXT: 100257,
FIM_PREFIX: 100258,
FIM_MIDDLE: 100259,
FIM_SUFFIX: 100260,
ENDOFPROMPT: 100276,
}
return TikTokenEncoding(**{
"name": "cl100k_base",
"pat_str": r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}++|\p{N}{1,3}+| ?[^\s\p{L}\p{N}]++[\r\n]*+|\s++$|\s*[\r\n]|\s+(?!\S)|\s""",
"mergeable_ranks": mergeable_ranks,
"special_tokens": special_tokens,
})
5 changes: 2 additions & 3 deletions src/backend/bisheng/api/services/knowledge_imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,9 @@ def add_file_embedding(vector_client,
metadatas.append(val['metadata'])
for index, one in enumerate(texts):
if len(one) > 10000:
raise ValueError('分段结果超长,请尝试使用自定义策略进行切分')
raise ValueError('分段结果超长,请尝试在自定义策略中使用更多切分符(例如 \n)进行切分')
# 入库时 拼接文件名和文档摘要
texts[
index] = f"{metadatas[index]['source']}\n{metadatas[index]['title']}{KnowledgeUtils.chunk_split}{one}"
texts[index] = f"{metadatas[index]['source']}\n{metadatas[index]['title']}{KnowledgeUtils.chunk_split}{one}"

db_file.parse_type = parse_type
# 存储ocr识别后的partitions结果
Expand Down
Loading

0 comments on commit 988ff95

Please sign in to comment.