Merge pull request #60 from Ming-jiayou/pocketflow-agent-add-search_web_brave

pocketflow-agent add search_web_brave
This commit is contained in:
Zachary Huang 2025-05-13 11:15:05 -04:00 committed by GitHub
commit 815f1dfbe5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 4 deletions

View File

@ -1,5 +1,5 @@
from pocketflow import Node from pocketflow import Node
from utils import call_llm, search_web from utils import call_llm, search_web_duckduckgo
import yaml import yaml
class DecideAction(Node): class DecideAction(Node):
@ -85,7 +85,7 @@ class SearchWeb(Node):
"""Search the web for the given query.""" """Search the web for the given query."""
# Call the search utility function # Call the search utility function
print(f"🌐 Searching the web for: {search_query}") print(f"🌐 Searching the web for: {search_query}")
results = search_web(search_query) results = search_web_duckduckgo(search_query)
return results return results
def post(self, shared, prep_res, exec_res): def post(self, shared, prep_res, exec_res):

View File

@ -2,3 +2,4 @@ pocketflow>=0.0.1
aiohttp>=3.8.0 # For HTTP requests aiohttp>=3.8.0 # For HTTP requests
openai>=1.0.0 # For LLM calls openai>=1.0.0 # For LLM calls
duckduckgo-search>=7.5.2 # For web search duckduckgo-search>=7.5.2 # For web search
requests>=2.25.1 # For HTTP requests

View File

@ -1,6 +1,7 @@
from openai import OpenAI from openai import OpenAI
import os import os
from duckduckgo_search import DDGS from duckduckgo_search import DDGS
import requests
def call_llm(prompt): def call_llm(prompt):
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key")) client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
@ -10,12 +11,33 @@ def call_llm(prompt):
) )
return r.choices[0].message.content return r.choices[0].message.content
def search_web(query): def search_web_duckduckgo(query):
results = DDGS().text(query, max_results=5) results = DDGS().text(query, max_results=5)
# Convert results to a string # Convert results to a string
results_str = "\n\n".join([f"Title: {r['title']}\nURL: {r['href']}\nSnippet: {r['body']}" for r in results]) results_str = "\n\n".join([f"Title: {r['title']}\nURL: {r['href']}\nSnippet: {r['body']}" for r in results])
return results_str 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__": if __name__ == "__main__":
print("## Testing call_llm") print("## Testing call_llm")
prompt = "In a few words, what is the meaning of life?" prompt = "In a few words, what is the meaning of life?"