modelscope / agentscope

Start building LLM-empowered multi-agent applications in an easier way.
https://doc.agentscope.io/
Apache License 2.0
5.19k stars 316 forks source link

[Bug]:AttributeError: no attribute '_colored_name' #361

Closed ruifengma closed 3 months ago

ruifengma commented 3 months ago

Describe the bug

Traceback (most recent call last):
  File "/repos/agentscope/src/agentscope/message.py", line 68, in __getattr__
    return self[key]
           ~~~~^^^^^
KeyError: '_colored_name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "ascope.py", line 113, in <module>
    x = agent(x)
        ^^^^^^^^
  File "/repos/agentscope/src/agentscope/agents/agent.py", line 297, in __call__
    res = self.reply(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/repos/agentscope/src/agentscope/agents/react_agent.py", line 156, in reply
    hint_msg = Msg(
               ^^^^
  File "/repos/agentscope/src/agentscope/message.py", line 164, in __init__
    logger.chat(self)
  File "/repos/agentscope/src/agentscope/logging.py", line 109, in log_msg
    print(msg.formatted_str(colored=True))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/repos/agentscope/src/agentscope/message.py", line 178, in formatted_str
    name = self._colored_name
           ^^^^^^^^^^^^^^^^^^
  File "/repos/agentscope/src/agentscope/message.py", line 70, in __getattr__
    raise AttributeError(f"no attribute '{key}'") from e
AttributeError: no attribute '_colored_name'

To Reproduce

import sys
import io

from agentscope.agents import UserAgent
from agentscope.agents.react_agent import ReActAgent
from agentscope.service import (
    bing_search,  # or google_search,
    read_text_file,
    write_text_file,
    ServiceToolkit,
    ServiceResponse,
    ServiceExecStatus,
)
import agentscope

# Prepare a new tool function
def execute_python_code(code: str) -> ServiceResponse:  # pylint: disable=C0301
    """
    Execute Python code and capture the output. Note you must `print` the output to get the result.
    Args:
        code (`str`):
            The Python code to be executed.
    """  # noqa

    # Create a StringIO object to capture the output
    old_stdout = sys.stdout
    new_stdout = io.StringIO()
    sys.stdout = new_stdout

    try:
        # Using `exec` to execute code
        exec(code)
    except Exception as e:
        # If an exception occurs, capture the exception information
        output = str(e)
        status = ServiceExecStatus.ERROR
    else:
        # If the execution is successful, capture the output
        output = new_stdout.getvalue()
        status = ServiceExecStatus.SUCCESS
    finally:
        # Recover the standard output
        sys.stdout = old_stdout

    # Wrap the output and status into a ServiceResponse object
    return ServiceResponse(status, output)

# Prepare the tools for the agent
service_toolkit = ServiceToolkit()

service_toolkit.add(execute_python_code)
service_toolkit.add(read_text_file)
service_toolkit.add(write_text_file)

agentscope.init(
    model_configs="/repos/agentscope/scripts/fastchat/model_config.json",
    project="Conversation with ReActAgent",
)

# Create agents
agent = ReActAgent(
    name="assistant",
    model_config_name="fastchat-qwen1.5-72b-chat",
    verbose=True,
    service_toolkit=service_toolkit,
)
user = UserAgent(name="User")

# Build
x = None
while True:
    x = user(x)
    if x.content == "exit":
        break
    x = agent(x)

This is the example from the repo

Expected behavior inference without error

Error messages AttributeError: no attribute '_colored_name'

Environment (please complete the following information):

Additional context Add any other context about the problem here.

DavdGao commented 3 months ago

According to line numbers and code in the logging, you're using the commit before supporting streaming mode, but the formatted_str function and attribute _colored_name is added to support streaming mode.

Could you provide the commit id of your current agentscope repo? (The top commit id when executing git log)

ruifengma commented 3 months ago

Hi @DavdGao ,here you go 0f1571497e863d3e186eb486a55bfe90a2844b4a

DavdGao commented 3 months ago

It's very strange since we can find the attribute _colored_name in your commit https://github.com/modelscope/agentscope/blob/0f1571497e863d3e186eb486a55bfe90a2844b4a/src/agentscope/message.py#L167

Please try to reinstall AgentScope from the source as follows

git clone https://github.com/modelscope/agentscope.git
cd agentscope
pip install -e .
RCliang commented 3 months ago

It's very strange since we can find the attribute _colored_name in your commit

https://github.com/modelscope/agentscope/blob/0f1571497e863d3e186eb486a55bfe90a2844b4a/src/agentscope/message.py#L167

Please try to reinstall AgentScope from the source as follows

git clone https://github.com/modelscope/agentscope.git
cd agentscope
pip install -e .

got the same problem, and I have reinstalled the agentscope, but I still meet the same bug.

DavdGao commented 3 months ago

I have reproduced the error in the commit 0f1571 successfully. However, this bug has been already solved in PR: https://github.com/modelscope/agentscope/commit/83537a190c56c4d8a07034f5426b90e7518a6fc4

Please check if you're using the latest commit (17aec270d041a25c74d0db899508302174eb4af3)

DavdGao commented 3 months ago

Feel free to create a new issue if needed, closing this for now.