From 458d2bdc77114774b725778e0e0d0d8e347e22fe Mon Sep 17 00:00:00 2001 From: mingupup <2539823901@qq.com> Date: Tue, 13 May 2025 11:31:44 +0800 Subject: [PATCH] pocketflow-agent add search_web_brave --- cookbook/pocketflow-agent/nodes.py | 4 ++-- cookbook/pocketflow-agent/requirements.txt | 3 ++- cookbook/pocketflow-agent/utils.py | 24 +++++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cookbook/pocketflow-agent/nodes.py b/cookbook/pocketflow-agent/nodes.py index d777bde..d2fd202 100644 --- a/cookbook/pocketflow-agent/nodes.py +++ b/cookbook/pocketflow-agent/nodes.py @@ -1,5 +1,5 @@ from pocketflow import Node -from utils import call_llm, search_web +from utils import call_llm, search_web_duckduckgo import yaml class DecideAction(Node): @@ -85,7 +85,7 @@ class SearchWeb(Node): """Search the web for the given query.""" # Call the search utility function print(f"🌐 Searching the web for: {search_query}") - results = search_web(search_query) + results = search_web_duckduckgo(search_query) return results def post(self, shared, prep_res, exec_res): diff --git a/cookbook/pocketflow-agent/requirements.txt b/cookbook/pocketflow-agent/requirements.txt index e15206b..2825054 100644 --- a/cookbook/pocketflow-agent/requirements.txt +++ b/cookbook/pocketflow-agent/requirements.txt @@ -1,4 +1,5 @@ pocketflow>=0.0.1 aiohttp>=3.8.0 # For HTTP requests openai>=1.0.0 # For LLM calls -duckduckgo-search>=7.5.2 # For web search \ No newline at end of file +duckduckgo-search>=7.5.2 # For web search +requests>=2.25.1 # For HTTP requests \ No newline at end of file diff --git a/cookbook/pocketflow-agent/utils.py b/cookbook/pocketflow-agent/utils.py index c56a175..2c61289 100644 --- a/cookbook/pocketflow-agent/utils.py +++ b/cookbook/pocketflow-agent/utils.py @@ -1,6 +1,7 @@ from openai import OpenAI import os from duckduckgo_search import DDGS +import requests def call_llm(prompt): client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key")) @@ -10,11 +11,32 @@ def call_llm(prompt): ) return r.choices[0].message.content -def search_web(query): +def search_web_duckduckgo(query): results = DDGS().text(query, max_results=5) # Convert results to a string results_str = "\n\n".join([f"Title: {r['title']}\nURL: {r['href']}\nSnippet: {r['body']}" for r in results]) return results_str + +def search_web_brave(query): + + url = f"https://api.search.brave.com/res/v1/web/search?q={query}" + api_key = "your brave search api key" + + headers = { + "accept": "application/json", + "Accept-Encoding": "gzip", + "x-subscription-token": api_key + } + + response = requests.get(url, headers=headers) + + if response.status_code == 200: + data = response.json() + results = data['web']['results'] + results_str = "\n\n".join([f"Title: {r['title']}\nURL: {r['url']}\nDescription: {r['description']}" for r in results]) + else: + print(f"Request failed with status code: {response.status_code}") + return results_str if __name__ == "__main__": print("## Testing call_llm")