alipay / agentUniverse

agentUniverse is a LLM multi-agent framework that allows developers to easily build multi-agent applications.
Apache License 2.0
848 stars 108 forks source link

[Question] Can I use AgentUniverse Manager in threading.Thread? #131

Closed powerfooI closed 3 months ago

powerfooI commented 3 months ago

I call AgentUniverse().start() in a separate working Thread besides main thread. It's expected for working thread to poll tasks, get instance object from AgentManager and call agent.run(). While error TypeError: cannot pickle '_thread.RLock' object occurs when an agent is called the second time and the AU never recovers until I kill the process and restart it.

powerfooI commented 3 months ago

The traceback stack is as following,

Traceback (most recent call last):
  File "/app/my_server/web/task_worker.py", line 206, in task_worker
    output_object: OutputObject = my_agent.run(
                                  ^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/agentuniverse/agent/agent.py", line 80, in run
    planner_result = self.execute(input_object, agent_input)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/agentuniverse/agent/agent.py", line 100, in execute
    planner_result = planner_base.invoke(self.agent_model, agent_input, input_object)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/agentuniverse/agent/plan/planner/planning_planner/planning_planner.py", line 42, in invoke
    llm: LLM = self.handle_llm(agent_model)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/agentuniverse/agent/plan/planner/planner.py", line 145, in handle_llm
    llm: LLM = LLMManager().get_instance_obj(component_instance_name=llm_name, new_instance=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/agentuniverse/base/component/component_manager_base.py", line 46, in get_instance_obj
    return copy.deepcopy(self._instance_obj_map.get(instance_code))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 143, in deepcopy
    y = copier(memo)
        ^^^^^^^^^^^^
  File "/root/.cache/pypoetry/virtualenvs/ob-dba-agent-9TtSrW0h-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 777, in __deepcopy__
    _object_setattr(m, '__dict__', deepcopy(self.__dict__, memo=memo))
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
    state = deepcopy(state, memo)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
    state = deepcopy(state, memo)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
    state = deepcopy(state, memo)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
    state = deepcopy(state, memo)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/copy.py", line 151, in deepcopy
    rv = reductor(4)
         ^^^^^^^^^^^
TypeError: cannot pickle '_thread.RLock' object
LandJerry commented 3 months ago

I call AgentUniverse().start() in a separate working Thread besides main thread. It's expected for working thread to poll tasks, get instance object from AgentManager and call agent.run(). While error TypeError: cannot pickle '_thread.RLock' object occurs when an agent is called the second time and the AU never recovers until I kill the process and restart it.

What is your version number? Upgrading to version 0.0.11 can resolve the issue.

powerfooI commented 3 months ago

Thanks a lot! The error disappears after I upgrade agentUniverse to 0.0.11.

aadd1989 commented 2 months ago

I have updated version 0.0.11, but I still have this issue

LandJerry commented 2 months ago

I have updated version 0.0.11, but I still have this issue

Can you display the error stack to see if it matches the previous error? And if it does, can you confirm if the running version is indeed 0.0.11?

aadd1989 commented 2 months ago

I have updated version 0.0.11, but I still have this issue

Can you display the error stack to see if it matches the previous error? And if it does, can you confirm if the running version is indeed 0.0.11?

| ERROR | [default] | app.bootstarp.flask_server:handle_exception:153 | Traceback (most recent call last): File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(*view_args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent_serve/web/web_util.py", line 50, in wrapper return func(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/nlpProjects/llm-multi-agent/app/bootstarp/flask_server.py", line 51, in service_run result = request_task.run() ^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent_serve/web/request_task.py", line 136, in run raise e File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent_serve/web/request_task.py", line 129, in run result = self.func(self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent_serve/service_instance.py", line 34, in run return self.__service.run(kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent_serve/service.py", line 49, in run return self.agent.run(kwargs).to_json_str() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/nlpProjects/llm-multi-agent/app/core/agent/judge_rag_agent/case_rag_agent.py", line 44, in run planner_result = self.execute(input_object, agent_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/agent/agent.py", line 106, in execute planner_result = planner_base.invoke(self.agent_model, agent_input, input_object) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/nlpProjects/llm-multi-agent/app/core/planner/judge_rag_planner.py", line 47, in invoke llm: LLM = self.handle_llm(agent_model, planner_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/nlpProjects/llm-multi-agent/app/core/planner/judge_rag_planner.py", line 109, in handle_llm llm: LLM = LLMManager().get_instance_obj(component_instance_name=llm_name, new_instance=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/agentuniverse/base/component/component_manager_base.py", line 46, in get_instance_obj return copy.deepcopy(self._instance_obj_map.get(instance_code)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 153, in deepcopy y = copier(memo) ^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/site-packages/pydantic/main.py", line 760, in deepcopy _object_setattr(m, 'dict', deepcopy(self.dict, memo=memo)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy y = copier(x, memo) ^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy y = _reconstruct(x, memo, rv) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 271, in _reconstruct state = deepcopy(state, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy y = copier(x, memo) ^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy y = _reconstruct(x, memo, rv) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 271, in _reconstruct state = deepcopy(state, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy y = copier(x, memo) ^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy y = _reconstruct(x, memo, rv) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 271, in _reconstruct state = deepcopy(state, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy y = copier(x, memo) ^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy y = _reconstruct(x, memo, rv) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 271, in _reconstruct state = deepcopy(state, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy y = copier(x, memo) ^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) ^^^^^^^^^^^^^^^^^^^^^ File "/mnt/raid5/anaconda3/lib/python3.11/copy.py", line 161, in deepcopy rv = reductor(4) ^^^^^^^^^^^ TypeError: cannot pickle '_thread.RLock' object

aadd1989 commented 2 months ago

image

EdwinInAu commented 2 months ago

@aadd1989 the issue lies in your handle_llm method of the judge_rag_planner planner. Please adjust to LLMManager().get_instance_obj(component_instance_name=llm_name, new_instance=False) and try again.