A web search agent that can search the internet and provide answers based on current information using PocketFlow-Node.
This search agent can:
- Search the web for current information
- Extract relevant content from search results
- Generate comprehensive answers based on search results
- Handle multiple search queries in a single session
- Provide source citations for information
The search agent uses a three-node flow:
- SearchNode: Performs web searches using a search API
- ExtractNode: Extracts and summarizes relevant content
- AnswerNode: Generates comprehensive answers with citations
flowchart LR
A[SearchNode] --> B[ExtractNode]
B --> C[AnswerNode]
- Web Search: Uses DuckDuckGo API for web searches
- Content Extraction: Extracts relevant information from search results
- Answer Generation: Creates comprehensive answers with citations
- Error Handling: Graceful fallbacks for search failures
- Rate Limiting: Built-in rate limiting to respect API limits
-
Install dependencies:
npm install
-
Set up environment:
cp env.example .env # Edit .env and add your OpenAI API key -
Run the search agent:
npm start
-
Start searching:
Search: What is the latest news about AI? Agent: Based on recent search results, here are the latest developments in AI... Search: How does quantum computing work? Agent: Quantum computing is a revolutionary technology that...
Search: What happened in the stock market today?
Agent: Based on today's market data, the major indices showed mixed results...
Search: What is the difference between React and Vue?
Agent: React and Vue are both popular JavaScript frameworks, but they differ in...
Search: Is it true that coffee helps with weight loss?
Agent: According to recent research, coffee may have some effects on metabolism...
OPENAI_API_KEY: Your OpenAI API key (required)OPENAI_MODEL: Model to use (default: "gpt-3.5-turbo")SEARCH_RESULTS_LIMIT: Number of search results to process (default: 5)
You can modify the search behavior by editing:
src/nodes.ts: Change how searches are performed and answers generatedsrc/flow.ts: Modify the search flowsrc/utils/search.ts: Switch to a different search provider
pocketflow-node-search/
├── README.md # This file
├── package.json # Dependencies and scripts
├── env.example # Environment variables template
├── src/
│ ├── main.ts # Entry point
│ ├── nodes.ts # Node definitions
│ ├── flow.ts # Flow orchestration
│ └── utils/
│ ├── llm.ts # LLM utility functions
│ └── search.ts # Search API utilities
└── data/
└── sample_queries.json # Sample search queries
- Purpose: Performs web searches
- Input: Search query from user
- Output: Raw search results
- Purpose: Extracts relevant content from search results
- Input: Raw search results
- Output: Summarized relevant content
- Purpose: Generates comprehensive answers
- Input: Extracted content and original query
- Output: Final answer with citations
-
"Search API rate limit exceeded"
- The agent includes rate limiting, but you may need to wait between searches
-
"No search results found"
- Try rephrasing your query or using different keywords
-
"API key not found"
- Make sure you've set
OPENAI_API_KEYin your.envfile
- Make sure you've set
Run with debug logging:
DEBUG=true npm start- Multiple search engines: Add support for Google, Bing, or other search APIs
- Image search: Extend to search for and analyze images
- News filtering: Add filters for recent news vs. general information
- Language support: Add support for searching in different languages
// In src/nodes.ts
class NewsSearchNode extends Node {
async exec(query: string): Promise<any> {
// Use a news-specific API
const newsResults = await searchNews(query);
return newsResults;
}
}This example is provided under the MIT license.