Skip to content

Add WikipediaSearchTool to default tools #514

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

Merged
merged 15 commits into from
Mar 25, 2025

Conversation

touseefahmed96
Copy link
Contributor

@touseefahmed96 touseefahmed96 commented Feb 6, 2025

Example Code:

from smolagents import CodeAgent, HfApiModel, WikipediaSearchTool

agent = CodeAgent(
    tools=[
        WikipediaSearchTool(
            user_agent="MyResearchBot ([email protected])",
            language="en",
            summary_only=True,
            extract_format="WIKI",
        )
    ],
    model=HfApiModel(),
)

agent.run("Elon Musk")

output :

Out - Final answer: **Wikipedia Page:** Elon Musk

**Summary:** Elon Reeve Musk (; born June 28, 1971) is a businessman and United States special Government employee known for his key roles in the automotive company Tesla, Inc. and the space company SpaceX. He is also known   
for his ownership of the social media service X and his role in the founding of the Boring Company, xAI, Neuralink, and OpenAI. Musk is the wealthiest individual in the world; as of January 2025, Forbes estimates his net worth
to be US$426 billion.
A member of the wealthy South African Musk family, Musk was born in Pretoria and immigrated to Canada in 1989, acquiring citizenship through his Canadian-born mother. He studied at Queen's University and later at the 
University of Pennsylvania in the U.S., where he received bachelor's degrees in economics and physics. In 1995, he moved to California and with his brother Kimbal founded the software company Zip2, which was acquired by Compaq
in 1999. That same year, Musk co-founded X.com, a direct bank which later merged to form PayPal. In 2002, Musk acquired U.S. citizenship, and eBay acquired PayPal. Using the money he made from the sale, Musk founded SpaceX, a 
spaceflight services company, in 2002.
In 2004, Musk was an early investor in electric vehicle manufacturer Tesla Motors, Inc. He assumed the position of the company's chairman, and later became the CEO. In 2006, Musk helped create SolarCity, a solar energy company
that was acquired by Tesla in 2016 and became Tesla Energy. In 2013, he proposed a hyperloop high-speed vactrain transportation system. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The  
following year Musk co-founded Neuralink, a neurotechnology company developing brain–computer interfaces, and the Boring Company, a tunnel construction company. In 2018, the U.S. Securities and Exchange Commission (SEC) sued  
Musk, alleging he falsely announced that he had secured funding for a private takeover of Tesla, stepped down as chairman, and paid a fine. In 2022, he acquired Twitter, and rebranded the service as X the following year. In   
2023, Musk founded xAI, an artificial intelligence company. In January 2025, Musk was appointed director of the Department of Government Efficiency as a special Government employee.
Musk's actions and expressed views have made him a polarizing figure. He has been criticized for making unscientific and misleading statements, including COVID-19 misinformation, affirming antisemitic and transphobic comments,
and promoting conspiracy theories. His acquisition of Twitter was controversial due to large employee layoffs, an increase in hate speech, the spread of misinformation and disinformation on the service, and changes to various 
service features including verification. Musk has engaged in political activities in several countries, including as a vocal and financial supporter of U.S. president Donald Trump. He became the largest donor in the 2024      
United States presidential election, and is a supporter of far-right political parties, activists, and causes....

**Read more:** https://en.wikipedia.org/wiki/Elon_Musk

@touseefahmed96
Copy link
Contributor Author

Anything else you want me to add @aymeric-roucher ?

@aymeric-roucher
Copy link
Collaborator

One last thing: we don't allow init arguments that don't have a default in tools (to be able to serialize them easily). So could you set a default value like "smolagents" to the user_agent arg?

@touseefahmed96
Copy link
Contributor Author

touseefahmed96 commented Feb 12, 2025

One last thing: we don't allow init arguments that don't have a default in tools (to be able to serialize them easily). So could you set a default value like "smolagents" to the user_agent arg?

Ok got it

Something like this:

def __init__(
        self,
        user_agent: str = "smolagents ([email protected])",
        language: str = "en",
        summary_only: bool = False,
        full_text: bool = True,
        extract_format: str = "WIKI",
):

Set default value of user_agent to "Smolagents ([email protected])"
@aymeric-roucher
Copy link
Collaborator

Nice ! And one last last thing: please add a test with a pytest.parameterize to test un summary mode vs full mode, two different languages and requests!

@touseefahmed96
Copy link
Contributor Author

touseefahmed96 commented Feb 13, 2025

@aymeric-roucher test cases for Default Tools are written using unittest.TestCase, and pytest.mark.parametrize does not work with unittest.TestCase, and I don't want to separate the WikipediaSearchTool from the DefaultToolTests class so, I used self.subTest() instead to parameterize the test cases while ensuring each iteration runs as a separate sub-test.

@touseefahmed96
Copy link
Contributor Author

@aymeric-roucher let know if this is ok or you want me to change anything

@@ -33,6 +39,40 @@ def test_ddgs_with_kwargs(self):
result = DuckDuckGoSearchTool(timeout=20)("DeepSeek parent company")
assert isinstance(result, str)

def test_wikipedia_search_tool(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertvillanova should we prefer pytest for these kind of grid tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aymeric-roucher current test test_wikipedia_search_tool is not ok?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @touseefahmed96 it's better to use pytest.parametrize: you can find examples in other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aymeric-roucher i did use the pytest.parametrize but for that to work we have to write seperate class for this tool because we cannot integrate it in DefaultToolTests as its is unittest and pytest.parametrize does not work with unittest.TestCase

touseefahmed96 and others added 3 commits February 17, 2025 11:50
- Replaced unittest subTest with pytest.mark.parametrize for WikipediaSearchTool as suggested.
- Replaced unittest subTest with pytest.mark.parametrize for WikipediaSearchTool as suggested.
@touseefahmed96
Copy link
Contributor Author

touseefahmed96 commented Feb 17, 2025

@aymeric-roucher I created a new test function for Wikipedia search tool and used pytest.mark.parametrize as you asked

@touseefahmed96
Copy link
Contributor Author

@aymeric-roucher

@aymeric-roucher
Copy link
Collaborator

@touseefahmed96 please check failing tests (https://github.com/huggingface/smolagents/actions/runs/13364705034/job/38867123683?pr=514): you need to add your dependancy to test requirements for tests to pass.

@touseefahmed96
Copy link
Contributor Author

@aymeric-roucher I added the wikipedia in test requirements but i dont know why the second one is failing the Models test one

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @touseefahmed96!

Just a comment below.

To fix the CI, please merge the main branch:

git fetch upstream main
git merge upstream/main

@touseefahmed96
Copy link
Contributor Author

Done @albertvillanova

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to update your tests after the last modification.

@touseefahmed96
Copy link
Contributor Author

Done @albertvillanova

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🤗

@albertvillanova albertvillanova merged commit 71270ec into huggingface:main Mar 25, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants