Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Feature] 支持 stream agent 流式返回step和message #345

Merged
merged 8 commits into from
May 7, 2024

Conversation

xiabo0816
Copy link
Contributor

刚需呀!!

使得FunctionAgent支持流式调用,返回stepmessage,注意:

  • EndStep会返回一次;
  • EndStep会返回多次,每次返回新的短句,和ERNIEBotstream=True效果相同
from erniebot_agent.agents import FunctionAgent
from erniebot_agent.chat_models import ERNIEBot
from erniebot_agent.memory import WholeMemory
from erniebot_agent.tools.calculator_tool import CalculatorTool


async def agent_stream():
    llm = ERNIEBot(model="ernie-3.5", access_token="<token>")
    memory = WholeMemory()
    agent = FunctionAgent(llm=llm, tools=[CalculatorTool()], memory=memory)
    prompt = "1+4等于几?"
    async for step, msgs in agent.run_stream(prompt):
        print(step, msgs)

import asyncio
asyncio.run(agent_stream())

output:

ToolStep(info={'tool_name': 'CalculatorTool', 'tool_args': '{"math_formula":"1+4"}'}, result='{"formula_result": 5}', input_files=[], output_files=[]) [<AIMessageChunk role: 'assistant', function_call: {'name': 'CalculatorTool', 'thoughts': '用户询问了1+4的结果,这是一个简单的数学问题,可以使用CalculatorTool工具来计算。', 'arguments': '{"math_formula":"1+4"}'}, token_count: 29>, <FunctionMessage role: 'function', name: 'CalculatorTool', content: '{"formula_result": 5}'>]
EndStep(info={'end_reason': 'FINISHED'}, result=None) [<AIMessageChunk role: 'assistant', content: '根据你的数学', token_count: 0>]
EndStep(info={'end_reason': 'FINISHED'}, result=None) [<AIMessageChunk role: 'assistant', content: '公式1+4,计算结果为5。', token_count: 0>]
EndStep(info={'end_reason': 'FINISHED'}, result=None) [<AIMessageChunk role: 'assistant', content: '如果你还有其他问题或需要执行其他数学公式,请随时告诉我。', token_count: 0>]
EndStep(info={'end_reason': 'FINISHED'}, result=None) [<AIMessageChunk role: 'assistant', token_count: 25>]

也有类似的issue需求#335

Copy link

paddle-bot bot commented Apr 26, 2024

Thanks for your contribution!

@CLAassistant
Copy link

CLAassistant commented Apr 26, 2024

CLA assistant check
All committers have signed the CLA.

@xiabo0816
Copy link
Contributor Author

求求大佬有空review一下,真的太需要了😭

@w5688414 w5688414 requested a review from sijunhe April 26, 2024 10:45
@w5688414
Copy link
Collaborator

检查一下代码的格式:

make format-check

@xiabo0816
Copy link
Contributor Author

检查一下代码的格式:

make format-check

好嘞,已经使用python -m black <.py>优化了修改的代码文件并提交,辛苦您🌹

Copy link
Contributor Author

@xiabo0816 xiabo0816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了常规修改,还抽象出_first_tool_step方法用于处理最开始的tool step

@w5688414
Copy link
Collaborator

w5688414 commented Apr 27, 2024

代码格式没过:

image

.PHONY: format-check

你可以执行命令:make format-check

Copy link
Contributor Author

@xiabo0816 xiabo0816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make format-check All done! ✨ 🍰 ✨

@w5688414
Copy link
Collaborator

w5688414 commented Apr 28, 2024

检查一下代码规范:

python -m mypy src

.PHONY: type-check

Copy link
Contributor Author

@xiabo0816 xiabo0816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python -m mypy src Success: no issues found in 73 source files

Copy link
Member

@Bobholamovic Bobholamovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢贡献!我觉得这是一个不错的实现agent流式输出的想法~

对于代码实现我没有大的疑问,一些小问题留在评论里了。另外想和大家讨论一下,从实用性的角度出发,我们是否有必要为流式输出增加新的callback时点?

This method should yield a sequence of (AgentStep, List[Message]) tuples based on the given
prompt and optionally accompanying files.
"""
if False:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是为什么呀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特别不好意思,这个是对mypy的妥协:

这里直接pass或者raise都会mypy编译器报错,原因应该是返回AsyncIterator时,如果内部没有yield就认为返回值是Coroutine,从而导致和子类重载函数返回值类型(FunctionAgent._run_stream)不同而报错;由于我一直没能找到通过mypy检测的抽象方法@abc.abstractmethod async def _run_stream的返回值数据类型,就很丑陋的if False:了,哭哭

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

噢噢,好吧,那我觉得可以先保留这个(不过或许if False可以用if typing.TYPE_CHECKING代替,顺带可以在注释里加个HACK标记)~

erniebot-agent/src/erniebot_agent/agents/agent.py Outdated Show resolved Hide resolved
erniebot-agent/src/erniebot_agent/agents/agent.py Outdated Show resolved Hide resolved
erniebot-agent/src/erniebot_agent/agents/function_agent.py Outdated Show resolved Hide resolved
erniebot-agent/src/erniebot_agent/agents/function_agent.py Outdated Show resolved Hide resolved
@w5688414
Copy link
Collaborator

请在提交前检查一下格式:

python -m black --check --diff src applications cookbook tests

Copy link
Contributor Author

@xiabo0816 xiabo0816 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经使用python -m black --check检查格式啦

@sijunhe sijunhe merged commit 3f2ecc0 into PaddlePaddle:develop May 7, 2024
4 checks passed
@sijunhe
Copy link
Collaborator

sijunhe commented May 7, 2024

感谢贡献~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants