π The First Self-Improving agents with RL / Prompting Optimization
Python agentaigptmulti-agentpythonreinforcement-learningself-improvementsociety-of-mindswarm-intelligence
Added Bing APIs and docs (#29)
Sandro Hanea3005e07301ea
diff --git a/.env.template b/.env.templateindex a77286e..156aaea 100644--- a/.env.template+++ b/.env.template@@ -1,4 +1,5 @@OPENAI_API_KEY=""SEARCHAPI_API_KEY=""GOOGLE_CSE_ID=""-GOOGLE_API_KEY=""\ No newline at end of file+GOOGLE_API_KEY=""+BING_API_KEY=""\ No newline at end of filediff --git a/README.md b/README.mdindex dd83674..e9f278d 100644--- a/README.md+++ b/README.md@@ -96,9 +96,19 @@ poetry install```pythonOPENAI_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**```pythondiff --git a/swarm/environment/__init__.py b/swarm/environment/__init__.pyindex 445f2ba..35e9d53 100644--- a/swarm/environment/__init__.py+++ b/swarm/environment/__init__.py@@ -1,9 +1,10 @@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__ = ["GeneralReader","GoogleSearchEngine",- "SearchAPIEngine"+ "SearchAPIEngine",+ "BingSearchEngine"]\ No newline at end of filediff --git a/swarm/environment/operations/web_search.py b/swarm/environment/operations/web_search.pyindex 126cf70..504e9f5 100644--- a/swarm/environment/operations/web_search.py+++ b/swarm/environment/operations/web_search.py@@ -1,22 +1,20 @@#!/usr/bin/env python# -*- coding: utf-8 -*-+import osfrom copy import deepcopyfrom collections import defaultdict+from dotenv import load_dotenvfrom typing import List, Any, Optionalfrom swarm.llm.format import Messagefrom swarm.graph import Node-from swarm.environment import GoogleSearchEngine, SearchAPIEngine+from swarm.environment import GoogleSearchEngine, SearchAPIEngine, BingSearchEnginefrom swarm.utils.log import logger, swarmlogfrom swarm.utils.globals import Costfrom swarm.environment.prompt.prompt_set_registry import PromptSetRegistryfrom swarm.llm import LLMRegistry-#searcher = GoogleSearchEngine()-searcher = SearchAPIEngine()--class WebSearch(Node):def __init__(self,domain: str,@@ -29,10 +27,20 @@ class WebSearch(Node):self.prompt_set = PromptSetRegistry.get(domain)self.role = self.prompt_set.get_role()self.constraint = self.prompt_set.get_constraint()+ self.searcher =self._get_searcher()@propertydef node_name(self):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):@@ -78,6 +86,6 @@ class WebSearch(Node):return outputsdef web_search(self, query):- return searcher.search(query)+ return self.searcher.search(query)diff --git a/swarm/environment/tools/search/search.py b/swarm/environment/tools/search/search.pyindex 2a8258b..9aaa31f 100644--- a/swarm/environment/tools/search/search.py+++ b/swarm/environment/tools/search/search.py@@ -2,16 +2,27 @@# -*- coding: utf-8 -*-import os-from dotenv import load_dotenvfrom googleapiclient.discovery import buildimport requestsimport 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():def __init__(self) -> None:- load_dotenv()self.api_key = os.getenv("GOOGLE_API_KEY")self.cse_id = os.getenv("GOOGLE_CSE_ID")self.service = build("customsearch", "v1", developerKey=self.api_key)@@ -21,8 +32,7 @@ class GoogleSearchEngine():res = self.service.cse().list(q=query, cx=self.cse_id, num=num).execute()return '\n'.join([item['snippet'] for item in res['items']])except:- return ''-+ return 'Cannot get search results from Google API'class SearchAPIEngine():@@ -45,7 +55,7 @@ class SearchAPIEngine():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 ''+ return 'Cannot get search results from SearchAPI'