diff --git a/.well-known/skills.yaml b/.well-known/skills.yaml index 06b9ffd0c..009368dbe 100644 --- a/.well-known/skills.yaml +++ b/.well-known/skills.yaml @@ -45,3 +45,22 @@ entities: returns: type: string format: base64 + + - name: web_search + description: Perform Google searches to provide real-time information. + id: web_search.web_search + x-prerequisite: + - name: SEARCH_ENGINE + description: "Supported values: serpapi/google/serper/ddg" + - name: SERPER_API_KEY + description: "SERPER API KEY, For more details, checkout: `https://serper.dev/api-key`" + arguments: + query: 'The search query. Required.' + max_results: 'The number of search results to retrieve. Default value: 6.' + examples: + - ask: 'Search for information about artificial intelligence' + answer: 'web_search(query="Search for information about artificial intelligence", max_results=6)' + - ask: 'Find news articles about climate change' + answer: 'web_search(query="Find news articles about climate change", max_results=6)' + returns: + type: string \ No newline at end of file diff --git a/metagpt/learn/__init__.py b/metagpt/learn/__init__.py index c8270dbfb..bab9f3e37 100644 --- a/metagpt/learn/__init__.py +++ b/metagpt/learn/__init__.py @@ -8,8 +8,6 @@ from metagpt.learn.text_to_image import text_to_image from metagpt.learn.text_to_speech import text_to_speech +from metagpt.learn.google_search import google_search -__all__ = [ - "text_to_image", - "text_to_speech", -] \ No newline at end of file +__all__ = ["text_to_image", "text_to_speech", "google_search"] diff --git a/metagpt/learn/google_search.py b/metagpt/learn/google_search.py new file mode 100644 index 000000000..ef099fe94 --- /dev/null +++ b/metagpt/learn/google_search.py @@ -0,0 +1,12 @@ +from metagpt.tools.search_engine import SearchEngine + + +async def google_search(query: str, max_results: int = 6, **kwargs): + """Perform a web search and retrieve search results. + + :param query: The search query. + :param max_results: The number of search results to retrieve + :return: The web search results in markdown format. + """ + resluts = await SearchEngine().run(query, max_results=max_results, as_string=False) + return "\n".join(f"{i}. [{j['title']}]({j['link']}): {j['snippet']}" for i, j in enumerate(resluts, 1)) diff --git a/tests/metagpt/learn/test_google_search.py b/tests/metagpt/learn/test_google_search.py new file mode 100644 index 000000000..da32e8923 --- /dev/null +++ b/tests/metagpt/learn/test_google_search.py @@ -0,0 +1,27 @@ +import asyncio + +from pydantic import BaseModel + +from metagpt.learn.google_search import google_search + + +async def mock_google_search(): + class Input(BaseModel): + input: str + + inputs = [{"input": "ai agent"}] + + for i in inputs: + seed = Input(**i) + result = await google_search(seed.input) + assert result != "" + + +def test_suite(): + loop = asyncio.get_event_loop() + task = loop.create_task(mock_google_search()) + loop.run_until_complete(task) + + +if __name__ == "__main__": + test_suite()