Maplemx / Agently

[AI Agent Application Development Framework] - 🚀 Build AI agent native application in very few code 💬 Easy to interact with AI agent in code using structure data and chained-calls syntax 🧩 Enhance AI Agent using plugins instead of rebuild a whole new agent
http://agently.tech
Apache License 2.0
935 stars 105 forks source link

Event loop is closed #65

Closed ZohnSun closed 1 month ago

ZohnSun commented 3 months ago

pycharm中执行样例总是报线程问题,这个异步是否需要优化一下

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B09421C4C0> Traceback (most recent call last): File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del self.close() File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon self._check_closed() File "D:\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

ZohnSun commented 3 months ago

调用百度样例会出现,智谱没有出现

agent_factory\

.set_settings("current_model", "ERNIE")\

.set_settings("model.ERNIE.auth", {

"aistudio": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",

})

agent = agent_factory.create_agent() agent.alias_manager.print_alias_info()

result = ( agent_GLM_3.general("输出规定", "必须使用中文进行输出")

role: agent自身的角色设定信息

    .role({
        "姓名": "Agently小助手",
        "任务": "使用自己的知识为用户解答常见问题",
    })
    # user_info: agent需要了解的用户相关的信息
    .user_info("和你对话的用户是一个只具有Python编程基础知识的入门初学者")
    # abstract: 对于之前对话(尤其是较长对话)的总结信息
    .abstract(None)
    # chat_history: 按照OpenAI消息列格式的对话记录list
    ## 支持:
    ## [{ "role": "system", "content": "" },
    ##  { "role": "assistant", "content": "" },
    ##  { "role": "user", "content": "" }]
    ## 三种角色
    .chat_history([])
    # input: 和本次请求相关的输入信息
    .input({
        "question": "请问Python中协程和线程的关系和区别是什么?",
        "reply_style_expect": "请用对编程概念零基础的人能理解的方式进行回复"
    })
    # info: 为本次请求提供的额外补充信息
    .info("协程相关关键词", ["asyncio", "coroutine", "event loop"])
    .info("线程相关关键词", ["threads", "queue"])
    # instruct: 为本次请求提供的行动指导信息
    .instruct([
        "请使用{reply_style_expect}的回复风格,回复{question}提出的问题",
    ])
    # output: 对本次请求的输出提出格式和内容的要求
    .output({
        "reply": ("str", "对{question}的直接回复"),
        "next_questions": ([
            ("str",
             "根据{reply}内容,结合{user_info}提供的用户信息," +
             "给用户推荐的可以进一步提问的问题"
            )], "不少于3个"),
    })
    # start: 用于开始本次主要交互请求
    .start()

)

Maplemx commented 3 months ago

已经关注到这个问题,因为不是必定出现,正在排查出现的时机

Xls1994 commented 3 months ago

我这边也出现了这个问题,使用的是本地部署的 OpenAI 接口的模型,另外,使用 Yi34B 的 API 也会出现此问题。 智谱的确实,目前没有发现问题

Maplemx commented 3 months ago

我这边也出现了这个问题,使用的是本地部署的 OpenAI 接口的模型,另外,使用 Yi34B 的 API 也会出现此问题。 智谱的确实,目前没有发现问题

请问是每次请求都会出现吗

Xls1994 commented 3 months ago

调用全局工具的时候,几乎都是稳定会触发的

Maplemx commented 3 months ago

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

Maplemx commented 3 months ago

pycharm中执行样例总是报线程问题,这个异步是否需要优化一下

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B09421C4C0> Traceback (most recent call last): File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del self.close() File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon self._check_closed() File "D:\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

我在Agently 3.2.2.3版本修正了一些异步的问题,目前本地使用PyCharm 3.9环境下调用相同的代码,或是使用浏览器或者搜索工具,目前都没有遇到Event loop is closed的情况。

可以更新至3.2.2.3再试试

Xls1994 commented 3 months ago

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

不是的,我这边没有做异步处理,是否和这个有关系呢

Maplemx commented 3 months ago

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

不是的,我这边没有做异步处理,是否和这个有关系呢

我处理了一些涉及异步调用未等待的问题,可以更新到3.2.2.3版本再试试

standing88 commented 3 months ago

Agently.AgentFactory().set_settings("current_model", "OpenAI").set_settings("model.OpenAI.auth", { "api_key": "" }).set_settings("model.OpenAI.url", "https://api.moonshot.cn/v1").set_settings("model.OpenAI.options", { "model": "moonshot-v1-8k" })

我用kimi 也很容易触发这个问题 Task exception was never retrieved future: <Task finished name='Task-116' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')> Traceback (most recent call last): File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose await proxy.aclose() File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose await self._pool.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose await connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose await self._connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose await self._network_stream.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose await self._stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose await self.transport_stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose self._transport.close() File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon self._check_closed() File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed Task exception was never retrieved future: <Task finished name='Task-117' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')> Traceback (most recent call last): File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose await proxy.aclose() File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose await self._pool.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose await connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose await self._connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose await self._network_stream.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose await self._stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose await self.transport_stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose self._transport.close() File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon self._check_closed() File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

Task exception was never retrieved future: <Task finished name='Task-122' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')> Traceback (most recent call last): File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose await proxy.aclose() File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose await self._pool.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose await connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose await self._connection.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose await self._network_stream.aclose() File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose await self._stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose await self.transport_stream.aclose() File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose self._transport.close() File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon self._check_closed() File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

Maplemx commented 3 months ago

进展:根据目前的反馈,怀疑是OpenAI客户端对其他模型的适配存在问题,正在考虑用httpx请求替换掉对OAI客户端的依赖

Maplemx commented 3 months ago

@Xls1994 @dudu8246 @ZohnSun 可以尝试在代码头部引入nest_asyncio看看能解决吗?

import nest_asyncio
nest_asyncio.apply()
Maplemx commented 3 months ago

部分测试显示nest_asyncio引入有效果,会在后续版本引入进包

Maplemx commented 3 months ago

已经将nest_asyncio引入框架内,无需额外设置,自动启用

应该会极大降低event loop is closed的概率

Maplemx commented 2 months ago

因为nest_asyncio和Gradio有冲突,只能从框架中移出

目前定位到event loop is closed错误很可能是OpenAI客户端的问题,使用pip install -U OpenAI升级到最新版就好

也可以自行引入nest_asyncio降低这个错误出现的概率

Maplemx commented 2 months ago

This bug still happen time to time so I remove the solved label and will keep on locate it.

But this bug seems have no harm to your process and will not break or stop the process except the annoying console error report...

zhaopengme commented 1 month ago

还是不行,依旧有这样的错误

Maplemx commented 1 month ago

还是不行,依旧有这样的错误

是的,我还在查看问题具体的位置,同时也在改写请求核心,期望后续重构后的大版本更新能解决这个问题

Maplemx commented 1 month ago

update in https://github.com/Maplemx/Agently/commit/efd937a8da6b196c4061cbf0c97a049b3e1caf60 publish in version 3.2.2.9

wait to see if this issue can be solved or not.

Maplemx commented 1 month ago

updated in https://github.com/Maplemx/Agently/commit/81242f4009b6c44d1bd0c159cb45ae311ebb6cd5 published in version 3.2.2.10

local test shows no 'event loop is close' error any more.

please use pip install Agently>=3.2.2.10 to update your Agently package to solve this issue.

Maplemx commented 1 month ago

related issue from httpx: https://github.com/encode/httpx/discussions/2959