Skip to content

Commit

Permalink
perf: 控制台输出请求响应过程
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Feb 20, 2024
1 parent 3258d5b commit 8085867
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pkg/pipeline/process/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ async def handle(
query: core_entities.Query,
) -> entities.StageProcessResult:
raise NotImplementedError

def cut_str(self, s: str) -> str:
"""
取字符串第一行,最多20个字符,若有多行,或超过20个字符,则加省略号
"""
s0 = s.split('\n')[0]
if len(s0) > 20 or '\n' in s:
s0 = s0[:20] + '...'
return s0
5 changes: 5 additions & 0 deletions pkg/pipeline/process/handlers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ async def handle(
async for result in query.use_model.requester.request(query):
query.resp_messages.append(result)

self.ap.logger.info(f'对话({query.query_id})响应: {self.cut_str(result.readable_str())}')

if result.content is not None:
text_length += len(result.content)

Expand All @@ -86,6 +88,9 @@ async def handle(
new_query=query
)
except Exception as e:

self.ap.logger.error(f'对话({query.query_id})请求失败: {str(e)}')

yield entities.StageProcessResult(
result_type=entities.ResultType.INTERRUPT,
new_query=query,
Expand Down
4 changes: 4 additions & 0 deletions pkg/pipeline/process/handlers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async def handle(
)
)

self.ap.logger.info(f'命令({query.query_id})报错: {self.cut_str(str(ret.error))}')

yield entities.StageProcessResult(
result_type=entities.ResultType.CONTINUE,
new_query=query
Expand All @@ -106,6 +108,8 @@ async def handle(
)
)

self.ap.logger.info(f'命令返回: {self.cut_str(ret.text)}')

yield entities.StageProcessResult(
result_type=entities.ResultType.CONTINUE,
new_query=query
Expand Down
13 changes: 9 additions & 4 deletions pkg/pipeline/process/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ async def process(

self.ap.logger.info(f"处理 {query.launcher_type.value}_{query.launcher_id} 的请求({query.query_id}): {message_text}")

if message_text.startswith('!') or message_text.startswith('!'):
return self.cmd_handler.handle(query)
else:
return self.chat_handler.handle(query)
async def generator():
if message_text.startswith('!') or message_text.startswith('!'):
async for result in self.cmd_handler.handle(query):
yield result
else:
async for result in self.chat_handler.handle(query):
yield result

return generator()
10 changes: 10 additions & 0 deletions pkg/provider/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ class Message(pydantic.BaseModel):
tool_calls: typing.Optional[list[ToolCall]] = None

tool_call_id: typing.Optional[str] = None

def readable_str(self) -> str:
if self.content is not None:
return self.content
elif self.function_call is not None:
return f'{self.function_call.name}({self.function_call.arguments})'
elif self.tool_calls is not None:
return f'调用工具: {self.tool_calls[0].id}'
else:
return '未知消息'

0 comments on commit 8085867

Please sign in to comment.