Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: #374

Open
fahdmirza opened this issue Jul 30, 2024 · 6 comments
Open

[Bug]: #374

fahdmirza opened this issue Jul 30, 2024 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@fahdmirza
Copy link

fahdmirza commented Jul 30, 2024

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.

@fahdmirza fahdmirza added the bug Something isn't working label Jul 30, 2024
@DavdGao
Copy link
Collaborator

DavdGao commented Aug 1, 2024

@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
Copy link
Author

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 DavdGao self-assigned this Aug 1, 2024
@DavdGao
Copy link
Collaborator

DavdGao commented Aug 1, 2024

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
Copy link
Author

@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
Copy link
Collaborator

DavdGao commented Aug 1, 2024

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.

2.mp4

@fahdmirza
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants