Skip to content

Commit

Permalink
fix: 修复agent的action日志展示格式
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit committed Apr 16, 2024
1 parent 9dceb5a commit d72d41b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
50 changes: 16 additions & 34 deletions src/backend/bisheng/api/v1/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,15 @@ async def on_text(self, text: str, **kwargs: Any) -> Any:
async def on_agent_action(self, action: AgentAction, **kwargs: Any):
logger.debug(f'on_agent_action action={action} kwargs={kwargs}')

log = f'Thought: {action.log}'
log = f'\nThought: {action.log}'
# if there are line breaks, split them and send them
# as separate messages
if '\n' in log:
logs = log.split('\n')
for log in logs:
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp.dict())
else:
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp.dict())
log = log.replace('\n', '\n\n')
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
await self.websocket.send_json(resp.dict())

async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any:
"""Run on agent end."""
Expand Down Expand Up @@ -232,27 +224,17 @@ def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
asyncio.run_coroutine_threadsafe(coroutine, loop)

def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
log = f'Thought: {action.log}'
log = f'\nThought: {action.log}'
# if there are line breaks, split them and send them
# as separate messages
if '\n' in log:
logs = log.split('\n')
for log in logs:
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
loop = asyncio.get_event_loop()
coroutine = self.websocket.send_json(resp.dict())
asyncio.run_coroutine_threadsafe(coroutine, loop)
else:
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
loop = asyncio.get_event_loop()
coroutine = self.websocket.send_json(resp.dict())
asyncio.run_coroutine_threadsafe(coroutine, loop)
log = log.replace("\n", "\n\n")
resp = ChatResponse(type='stream',
intermediate_steps=log,
flow_id=self.flow_id,
chat_id=self.chat_id)
loop = asyncio.get_event_loop()
coroutine = self.websocket.send_json(resp.dict())
asyncio.run_coroutine_threadsafe(coroutine, loop)

def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any:
"""Run on agent end."""
Expand Down
10 changes: 7 additions & 3 deletions src/backend/bisheng/chat/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,17 @@ async def intermediate_logs(self, session: ChatManager, client_id, chat_id, user
# agent model will produce the steps log
from langchain.schema import Document # noqa
if chat_id and intermediate_steps.strip():
finally_log = ''
for s in intermediate_steps.split('\n'):
# 清理召回日志中的一些冗余日志
if 'source_documents' in s:
answer = eval(s.split(':', 1)[1])
if 'result' in answer:
s = 'Answer: ' + answer.get('result')
msg = ChatResponse(intermediate_steps=s, type='end', user_id=user_id)
steps.append(msg)
finally_log += 'Answer: ' + answer.get('result') + "\n\n"
else:
finally_log += s + "\n\n"
msg = ChatResponse(intermediate_steps=finally_log, type='end', user_id=user_id)
steps.append(msg)
else:
# 只有L3用户给出详细的log
end_resp.intermediate_steps = intermediate_steps
Expand Down

0 comments on commit d72d41b

Please sign in to comment.