PaddlePaddle / ERNIE-SDK

ERNIE Bot Agent is a Large Language Model (LLM) Agent Framework, powered by the advanced capabilities of ERNIE Bot and the platform resources of Baidu AI Studio.
http://ernie-bot-agent.readthedocs.io/
Apache License 2.0
324 stars 48 forks source link

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

Closed xiabo0816 closed 1 month ago

xiabo0816 commented 2 months ago

刚需呀!!

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

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

paddle-bot[bot] commented 2 months ago

Thanks for your contribution!

CLAassistant commented 2 months ago

CLA assistant check
All committers have signed the CLA.

xiabo0816 commented 2 months ago

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

w5688414 commented 2 months ago

检查一下代码的格式:

make format-check
xiabo0816 commented 2 months ago

检查一下代码的格式:

make format-check

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

w5688414 commented 2 months ago

代码格式没过:

image

https://github.com/PaddlePaddle/ERNIE-SDK/blob/8de8fdbde86119db9ade57a65011e6de18fdea8a/erniebot-agent/Makefile#L12

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

w5688414 commented 2 months ago

检查一下代码规范:

python -m mypy src

https://github.com/PaddlePaddle/ERNIE-SDK/blob/8de8fdbde86119db9ade57a65011e6de18fdea8a/erniebot-agent/Makefile#L21

w5688414 commented 2 months ago

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

python -m black --check --diff src applications cookbook tests
sijunhe commented 1 month ago

感谢贡献~