kyegomez / swarms

The Enterprise-Grade Production-Ready Multi-Agent Orchestration Framework Join our Community: https://discord.com/servers/agora-999382051935506503
https://docs.swarms.world
Other
1.12k stars 163 forks source link

[BUG] OpenAI schema generator #517

Closed lapetiteclef closed 2 months ago

lapetiteclef commented 2 months ago

Tried to resolve local issue using OpenAI with @tool annotation. Debugged then assumed it was some dependency diff so tried to run the colab link provided in project that contain several examples. Changed devin example to OpenAIChat to find out if I had python version issue that created the error via functools.wrap I use locally.

Got same issue the colab version. Looks like something where schema generator for OpenAI tries to introspect the decorator instead of the func.wrapped or similar. If your busy I can look further, just let me know if there is a known functools version requirement.

2024-06-26T19:34:11.548067+0000 Tools provided make sure the functions have documentation ++ type hints, otherwise tool execution won't be reliable. 2024-06-26T19:34:11.548848+0000 Tools granted, initializing tool protocol. 2024-06-26T19:34:11.549122+0000 Number of tools: 4 2024-06-26T19:34:11.549344+0000 Tool -> OpenAI Schema Process Starting Now. 2024-06-26T19:34:11.549766+0000 There was an error converting your tool into a OpenAI certified function calling schema. Add documentation and type hints: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func' 2024-06-26T19:34:11.549945+0000 Error detected: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func' make sure you have inputted a callable and that it has documentation as docstrings

TypeError Traceback (most recent call last) in <cell line: 81>() 79 80 # Agent ---> 81 agent = Agent( 82 agent_name="Devin", 83 system_prompt=(

4 frames /usr/local/lib/python3.10/dist-packages/swarms/tools/py_func_to_openai_func_str.py in get_openai_function_schema_from_func(function, name, description) 433 if missing != set(): 434 missing_s = [f"'{k}'" for k in sorted(missing)] --> 435 raise TypeError( 436 f"All parameters of the function '{function.name}' without default values must be annotated. " 437 + f"The annotations are missing for the following parameters: {', '.join(missing_s)}"

TypeError: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func'

2024-06-26T19:34:11.548067+0000 Tools provided make sure the functions have documentation ++ type hints, otherwise tool execution won't be reliable. 2024-06-26T19:34:11.548848+0000 Tools granted, initializing tool protocol. 2024-06-26T19:34:11.549122+0000 Number of tools: 4 2024-06-26T19:34:11.549344+0000 Tool -> OpenAI Schema Process Starting Now. 2024-06-26T19:34:11.549766+0000 There was an error converting your tool into a OpenAI certified function calling schema. Add documentation and type hints: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func' 2024-06-26T19:34:11.549945+0000 Error detected: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func' make sure you have inputted a callable and that it has documentation as docstrings

TypeError Traceback (most recent call last) in <cell line: 81>() 79 80 # Agent ---> 81 agent = Agent( 82 agent_name="Devin", 83 system_prompt=(

4 frames /usr/local/lib/python3.10/dist-packages/swarms/tools/py_func_to_openai_func_str.py in get_openai_function_schema_from_func(function, name, description) 433 if missing != set(): 434 missing_s = [f"'{k}'" for k in sorted(missing)] --> 435 raise TypeError( 436 f"All parameters of the function '{function.name}' without default values must be annotated. " 437 + f"The annotations are missing for the following parameters: {', '.join(missing_s)}"

TypeError: All parameters of the function 'decorator' without default values must be annotated. The annotations are missing for the following parameters: 'func'

Upvote & Fund

Fund with Polar

github-actions[bot] commented 2 months ago

Hello there, thank you for opening an Issue ! 🙏🏻 The team was notified and they will get back to you asap.

kyegomez commented 2 months ago

@lapetiteclef every parameter must have a type and documentation!

kyegomez commented 2 months ago

@lapetiteclef can you send me the code?

lapetiteclef commented 2 months ago

https://colab.research.google.com/github/kyegomez/swarms/blob/master/playground/swarms_example.ipynb

Used Devin example above as it contained a @tool. Changed Anthropic in above colab to OpenAIChat no other changes. I get same issue with @tool using OpenAI schema generator in above colab as I get locally. previous print was from running swarms_example.ipynb above. Example using OpenAIChat:

from swarms import Agent, OpenAIChat, tool import subprocess

api_key = "a_openai_key"

Initialize the language model

llm = OpenAIChat( temperature=0.5, openai_api_key=api_key, )

Tools

@tool def terminal( code: str, ): """ Run code in the terminal.

Args:
    code (str): The code to run in the terminal.

Returns:
    str: The output of the code.
"""
out = subprocess.run(
    code, shell=True, capture_output=True, text=True
).stdout
return str(out)

@tool def browser(query: str): """ Search the query in the browser with the browser tool.

Args:
    query (str): The query to search in the browser.

Returns:
    str: The search results.
"""
import webbrowser

url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
return f"Searching for {query} in the browser."

@tool def create_file(file_path: str, content: str): """ Create a file using the file editor tool.

Args:
    file_path (str): The path to the file.
    content (str): The content to write to the file.

Returns:
    str: The result of the file creation operation.
"""
with open(file_path, "w") as file:
    file.write(content)
return f"File {file_path} created successfully."

@tool def file_editor(file_path: str, mode: str, content: str): """ Edit a file using the file editor tool.

Args:
    file_path (str): The path to the file.
    mode (str): The mode to open the file in.
    content (str): The content to write to the file.

Returns:
    str: The result of the file editing operation.
"""
with open(file_path, mode) as file:
    file.write(content)
return f"File {file_path} edited successfully."

Agent

agent = Agent( agent_name="Devin", system_prompt=( "Autonomous agent that can interact with humans and other" " agents. Be Helpful and Kind. Use the tools provided to" " assist the user. Return all code in markdown format." ), llm=llm, max_loops="auto", autosave=True, dashboard=False, streaming_on=True, verbose=True, stopping_token="", interactive=True, tools=[terminal, browser, file_editor, create_file], code_interpreter=True,

streaming=True,

)

Run the agent

out = agent("Create a new file for a plan to take over the world.") print(out)

lapetiteclef commented 2 months ago

figured it out. swarms_example.ipynb example uses @tool decorator without parameters

If without any parameters need to be @tool() as this decorator takes parameters. if not using () a parameterized decorator is not called returning the real decorator containing the wrap. Maybe simple for you Python champs to see but for me a C++ guy I had to stare on code for a while as issue was not in schema handing where error triggered. At least now I know decorators and functools.wraps :)

Library looks superb BTW, thanks!