modelscope / agentscope

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

[Bug]: #374

Open fahdmirza opened 1 month ago

fahdmirza commented 1 month ago

Hello, I am just trying to create a simple end to end example to call a python function from agentscope. When I run it in jupyter notebook, it runs fine and gives me text box. I am confused as how to call that function now, or in other words how to use this? I have tried documentation (which can be greatly improved) but couldn't find any help there. Following is my code:

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)

llama3_8b_ollama_model_configuration = {
    "config_name": "ollama_llama3_8b",
    "model_type": "ollama_chat",
    "model_name": "llama3.1",
    "options": {
        "temperature": 0., "seed": 123,
    },
    "keep_alive": "5m",
}

agentscope.init(
    model_configs=llama3_8b_ollama_model_configuration,
    project="Conversation with ReActAgent",
)

# Create agents
agent = ReActAgent(
    name="assistant",
    model_config_name="ollama_llama3_8b",
    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)

Please advise.

DavdGao commented 1 month ago

@fahdmirza Thanks for your attention.

The text box is used to give the user input in jupyter. In this example, the user (you) is talking to an agent equipped with some tools.

The following code snippet in your code describes the order of speech, which requires the user to speak first. So just input something in the text box (like hello or sth), and then the agent will give a feedback. When user input "exit", the conversation loop will end.

# ...

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

If you want to adjust the order (agent speaks first), just modify the above code as follows:

# ...

x = None
while True:
    x = agent(x)
    x = user(x)
    if x.content == "exit":
        break
fahdmirza commented 1 month ago

Sorry @DavdGao , my question and issue is different. How do I call the function from the main python code? Please check my original post again. Thanks.

DavdGao commented 1 month ago

Sorry, but I'm a little confused about "call the function from the main python code".

However, in ReAct Agent example, the tool function execute_python_code is given to the agent as a candidated tool, and it's the react agent will decide whether to use this tool function rather than user, and the choiced tool function will be executed within the agent automatically. Refer to the source code of react agent for more details.

The following is the combined system prompt of this react agent (print(agent.sys_prompt)), which provides the tool function for the agent to use.

You're a helpful assistant. Your name is assistant.

## Tool Functions:
The following tool functions are available in the format of

{index}. {function name}: {function description} {argument1 name} ({argument type}): {argument description} {argument2 name} ({argument type}): {argument description} ...


1. bing_search: Search question in Bing Search API and return the searching results
    question (string): The search query string.
2. execute_python_code: Execute Python code and capture the output. Note you must `print` the output to get the result.
    code (string): The Python code to be executed.
3. read_text_file: Read the content of the text file.
    file_path (string): The path to the text file to be read.
4. write_text_file: Write content to a text file.
    file_path (string): The path to the file where content will be written.
    overwrite (boolean): Whether to overwrite the file if it already exists.
    content (string): Content to write into the file.

## What You Should Do:
1. First, analyze the current situation, and determine your goal.
2. Then, check if your goal is already achieved. If so, try to generate a response. Otherwise, think about how to achieve it with the help of provided tool functions.
3. Respond in the required format.

## Note:
1. Fully understand the tool functions and their arguments before using them.
2. You should decide if you need to use the tool functions, if not then return an empty list in "function" field.
3. Make sure the types and values of the arguments you provided to the tool functions are correct.
4. Don't take things for granted. For example, where you are, what's the time now, etc. You can try to use the tool functions to get information.
5. If the function execution fails, you should analyze the error and try to solve it.

If you want the agent to call the function, just ask it to finish some tasks that may require this tool function. For example, I ask the react agent to solve a complex calculation problem, and it will call the execute_python_code function.

20240801114305

Hope this could answer your question.

fahdmirza commented 1 month ago

@DavdGao Thanks for your response. I tried but its not doing anything. Is it possible to get an end-to-end example please? I am trying to review agentscope for my channel https://www.youtube.com/@fahdmirza . Thanks.

DavdGao commented 1 month ago

Please provide more information, for example, the running log.

I have tried your code in the issue description, and asked the agent to calculate a number as shown in the following video.

In the beginning, the llama3.1 model responds in the correct format and the function executes successfully. However, llama3.1 fails to generate a final respond with the tag <response>xxx</response> and keeps on calculating the number. So a more powerful llm is suggested in this case.

https://github.com/user-attachments/assets/d661e968-b99d-4d32-8047-4784973b3004

fahdmirza commented 1 month ago

Hi, I am sorry, I am not sure what you are doing there as the code simply doesn't work with Ollama. There is no end-to-end example in your documentation and I am not sure what you are saying above. The link to source code which you posted doesnt help at all. I have again wasted 3 hours today trying to get this work. With all due respect, I would suggest improving your documentation and putting in some better end-to-end examples.