Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ OPENAI_API_KEY=""
SEARCHAPI_API_KEY=""
GOOGLE_CSE_ID=""
GOOGLE_API_KEY=""
BING_API_KEY=""
BING_API_KEY=""
BOCHA_API_KEY=""
5 changes: 3 additions & 2 deletions swarm/environment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from swarm.environment.tools.reader.readers import GeneralReader
from swarm.environment.tools.search.search import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine
from swarm.environment.tools.search.search import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine,BoChaSearchEngine


__all__ = [
"GeneralReader",
"GoogleSearchEngine",
"SearchAPIEngine",
"BingSearchEngine"
"BingSearchEngine",
"BoChaSearchEngine"
]
4 changes: 3 additions & 1 deletion swarm/environment/operations/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from swarm.llm.format import Message
from swarm.graph import Node
from swarm.environment import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine
from swarm.environment import GoogleSearchEngine, SearchAPIEngine, BingSearchEngine, BoChaSearchEngine
from swarm.utils.log import logger, swarmlog
from swarm.utils.globals import Cost
from swarm.environment.prompt.prompt_set_registry import PromptSetRegistry
Expand Down Expand Up @@ -41,6 +41,8 @@ def _get_searcher(self):
return SearchAPIEngine()
if os.getenv("GOOGLE_API_KEY"):
return GoogleSearchEngine()
if os.getenv("BOCHA_API_KEY"):
return BoChaSearchEngine()

async def _execute(self, inputs: List[Any] = [], max_keywords: int = 5, **kwargs):

Expand Down
25 changes: 25 additions & 0 deletions swarm/environment/tools/search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
import requests
import ast


class BoChaSearchEngine():
def __init__(self) -> None:
self.api_key = os.getenv("BOCHA_API_KEY")

def search(self, query: str, num: int = 3):
try:
url = 'https://api.bochaai.com/v1/web-search'
headers = {
'Authorization': f'Bearer {self.api_key}', # 请替换为你的API密钥
'Content-Type': 'application/json'
}
data = {
"query": query,
}

response = requests.post(url, headers=headers, json=data)

json_response = response.json()
res = json_response["data"]["webPages"]["value"]
print(res)
return '\n'.join([item['snippet'] for item in res])
except:
return 'Cannot get search results from BoCha API'

class BingSearchEngine():
def __init__(self) -> None:
self.api_key = os.getenv("BING_API_KEY")
Expand Down
Loading