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.11k stars 163 forks source link

[BUG] Error while convering custom function to openai function #548

Open Occupying-Mars opened 1 month ago

Occupying-Mars commented 1 month ago

Describe the bug When creating a new custom functions its trying to parse the function in a wrong way

To Reproduce Create a new custom function examples here https://medium.com/@kyeg/the-swarms-tool-system-functions-pydantic-basemodels-as-tools-and-radical-customization-c2a2e227b8ca and try to run it

Expected behavior should just perform the function call

possible fix read the agent.py file in the sdk and edit this one line

 if all(callable(tool) for tool in self.tools):

line 1611 it worked for me

Additional context my code

from swarms import Agent, OpenAIChat, tool
from pydantic import BaseModel
import requests
from bs4 import BeautifulSoup

class SearchResult(BaseModel):
    title: str
    url: str
    snippet: str

@tool(name="websearch", description="websearch tool")
def web_search(query: str) -> list[SearchResult]:
    """
    Perform a web search for the given query.

    Args:
        query (str): The search query.

    Returns:
        list[SearchResult]: A list of search results.
    """
    url = f"https://www.google.com/search?q={query}"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    print("yo yo honey singha")
    results = []
    for g in soup.find_all('div', class_='g'):
        anchors = g.find_all('a')
        if anchors:
            link = anchors[0]['href']
            title = g.find('h3', class_='r')
            snippet = g.find('div', class_='s')
            if title and snippet:
                results.append(SearchResult(
                    title=title.text,
                    url=link,
                    snippet=snippet.text
                ))
    print(results[:5], "results")
    return results[:5]  # Return top 5 results

# Initializing the agent with the OpenAI instance and other parameters
agent = Agent(
    agent_name="web search agent",
    system_prompt="This agent performs web searches based on user requests.",
    sop_list=["Perform web search", "Return results"],
    llm=OpenAIChat(),
    max_loops="auto",
    interactive=True,
    verbose=True,
    output_type=str,
    metadata_output_type="json",
    function_calling_format_type="OpenAI",
    tools=[web_search],
)

# Defining the task
task = "Search for recent advancements in artificial intelligence."

# Running the agent with the specified task
out = agent.run(task)
print(out)

Upvote & Fund

Fund with Polar

github-actions[bot] commented 1 month ago

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

kyegomez commented 1 month ago

@Occupying-Mars excuse me for the issue, I broke that in the last update, fixing now.