Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mirrored_repos/machinelearning/metauto-ai/GPTSwarm
1 result
Show changes
Commits on Source (2)
OPENAI_API_KEY="" OPENAI_API_KEY=""
SEARCHAPI_API_KEY="" SEARCHAPI_API_KEY=""
GOOGLE_CSE_ID="" GOOGLE_CSE_ID=""
GOOGLE_API_KEY="" GOOGLE_API_KEY=""
\ No newline at end of file BING_API_KEY=""
\ No newline at end of file
...@@ -96,9 +96,19 @@ poetry install ...@@ -96,9 +96,19 @@ poetry install
```python ```python
OPENAI_API_KEY="" # for OpenAI LLM backend OPENAI_API_KEY="" # for OpenAI LLM backend
SEARCHAPI_API_KEY="" # for Web Search BING_API_KEY="" # for Bing Web Search
GOOGLE_API_KEY="" # for Google Web Search
SEARCHAPI_API_KEY="" # for SearchAPI Web Search
``` ```
**Selecting the Search Engine**
The system will automatically select the appropriate search engine based on the following priority:
- [Bing API](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api): If `BING_API_KEY` is provided
- [Search API](https://www.searchapi.io/): If `SEARCHAPI_API_KEY` is provided
- [Google API](https://developers.google.com/custom-search/?csw=1): If `GOOGLE_API_KEY` is provided
**Getting started with GPTSwarm is easy. Quickly run a predefined swarm** **Getting started with GPTSwarm is easy. Quickly run a predefined swarm**
```python ```python
......
...@@ -62,8 +62,9 @@ tqdm = "^4.66.1" ...@@ -62,8 +62,9 @@ tqdm = "^4.66.1"
transformers = "^4.36.2" transformers = "^4.36.2"
wikipedia = "^1.4.0" wikipedia = "^1.4.0"
astunparse = "^1.6.3" astunparse = "^1.6.3"
torch = "^2.1.0" torch = ">=2.1.0, <=2.2.2"
async-timeout = "^4.0.3" async-timeout = "^4.0.3"
numpy = "^1.25.2"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
pytest = "^8.0.2" pytest = "^8.0.2"
......
from swarm.environment.tools.reader.readers import GeneralReader from swarm.environment.tools.reader.readers import GeneralReader
from swarm.environment.tools.search.search import GoogleSearchEngine, SearchAPIEngine from swarm.environment.tools.search.search import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine
__all__ = [ __all__ = [
"GeneralReader", "GeneralReader",
"GoogleSearchEngine", "GoogleSearchEngine",
"SearchAPIEngine" "SearchAPIEngine",
"BingSearchEngine"
] ]
\ No newline at end of file
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
from copy import deepcopy from copy import deepcopy
from collections import defaultdict from collections import defaultdict
from dotenv import load_dotenv
from typing import List, Any, Optional from typing import List, Any, Optional
from swarm.llm.format import Message from swarm.llm.format import Message
from swarm.graph import Node from swarm.graph import Node
from swarm.environment import GoogleSearchEngine, SearchAPIEngine from swarm.environment import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine
from swarm.utils.log import logger, swarmlog from swarm.utils.log import logger, swarmlog
from swarm.utils.globals import Cost from swarm.utils.globals import Cost
from swarm.environment.prompt.prompt_set_registry import PromptSetRegistry from swarm.environment.prompt.prompt_set_registry import PromptSetRegistry
from swarm.llm import LLMRegistry from swarm.llm import LLMRegistry
#searcher = GoogleSearchEngine()
searcher = SearchAPIEngine()
class WebSearch(Node): class WebSearch(Node):
def __init__(self, def __init__(self,
domain: str, domain: str,
...@@ -29,10 +27,20 @@ class WebSearch(Node): ...@@ -29,10 +27,20 @@ class WebSearch(Node):
self.prompt_set = PromptSetRegistry.get(domain) self.prompt_set = PromptSetRegistry.get(domain)
self.role = self.prompt_set.get_role() self.role = self.prompt_set.get_role()
self.constraint = self.prompt_set.get_constraint() self.constraint = self.prompt_set.get_constraint()
self.searcher =self._get_searcher()
@property @property
def node_name(self): def node_name(self):
return self.__class__.__name__ return self.__class__.__name__
def _get_searcher(self):
load_dotenv()
if os.getenv("BING_API_KEY"):
return BingSearchEngine()
if os.getenv("SEARCHAPI_API_KEY"):
return SearchAPIEngine()
if os.getenv("GOOGLE_API_KEY"):
return GoogleSearchEngine()
async def _execute(self, inputs: List[Any] = [], max_keywords: int = 5, **kwargs): async def _execute(self, inputs: List[Any] = [], max_keywords: int = 5, **kwargs):
...@@ -78,6 +86,6 @@ class WebSearch(Node): ...@@ -78,6 +86,6 @@ class WebSearch(Node):
return outputs return outputs
def web_search(self, query): def web_search(self, query):
return searcher.search(query) return self.searcher.search(query)
...@@ -2,16 +2,27 @@ ...@@ -2,16 +2,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
from dotenv import load_dotenv
from googleapiclient.discovery import build from googleapiclient.discovery import build
import requests import requests
import ast import ast
load_dotenv()
class BingSearchEngine():
def __init__(self) -> None:
self.api_key = os.getenv("BING_API_KEY")
self.endpoint = "https://api.bing.microsoft.com/v7.0/search"
self.headers = {"Ocp-Apim-Subscription-Key": self.api_key}
def search(self, query: str, num: int = 3):
try:
params = {"q": query, "count": num}
res = requests.get(self.endpoint, headers=self.headers, params=params)
res = res.json()
return '\n'.join([item['snippet'] for item in res['webPages']['value']])
except:
return 'Cannot get search results from Bing API'
class GoogleSearchEngine(): class GoogleSearchEngine():
def __init__(self) -> None: def __init__(self) -> None:
load_dotenv()
self.api_key = os.getenv("GOOGLE_API_KEY") self.api_key = os.getenv("GOOGLE_API_KEY")
self.cse_id = os.getenv("GOOGLE_CSE_ID") self.cse_id = os.getenv("GOOGLE_CSE_ID")
self.service = build("customsearch", "v1", developerKey=self.api_key) self.service = build("customsearch", "v1", developerKey=self.api_key)
...@@ -21,8 +32,7 @@ class GoogleSearchEngine(): ...@@ -21,8 +32,7 @@ class GoogleSearchEngine():
res = self.service.cse().list(q=query, cx=self.cse_id, num=num).execute() res = self.service.cse().list(q=query, cx=self.cse_id, num=num).execute()
return '\n'.join([item['snippet'] for item in res['items']]) return '\n'.join([item['snippet'] for item in res['items']])
except: except:
return '' return 'Cannot get search results from Google API'
class SearchAPIEngine(): class SearchAPIEngine():
...@@ -45,7 +55,7 @@ class SearchAPIEngine(): ...@@ -45,7 +55,7 @@ class SearchAPIEngine():
if 'organic_results' in response.keys() and len(response['organic_results']) > 0: if 'organic_results' in response.keys() and len(response['organic_results']) > 0:
return '\n'.join([res['snippet'] for res in response['organic_results'][:item_num]]) return '\n'.join([res['snippet'] for res in response['organic_results'][:item_num]])
return '' return 'Cannot get search results from SearchAPI'
......