Building an Advanced Web Intelligence Agent Using Tavily and Google Gemini AI
Discover how to build a powerful web intelligence agent combining Tavily and Google Gemini AI to extract and analyze web content interactively.
Overview of the Web Intelligence Agent
This tutorial presents an advanced, interactive web intelligence agent powered by Tavily and Google’s Gemini AI. The agent is designed to extract structured content from web pages seamlessly, perform sophisticated AI-driven analyses, and deliver insightful results through a user-friendly terminal interface.
Core Libraries and Setup
The implementation imports essential libraries for asynchronous programming, data handling, and rich terminal output visualization:
import os
import json
import asyncio
from typing import List, Dict, Any
from dataclasses import dataclass
from rich.console import Console
from rich.progress import track
from rich.panel import Panel
from rich.markdown import MarkdownThese modules enable efficient, structured, and visually appealing execution of web intelligence tasks.
Initializing AI Tools
LangChain components are initialized to facilitate web content extraction and AI analysis:
from langchain_tavily import TavilyExtract
from langchain.chat_models import init_chat_model
from langgraph.prebuilt import create_react_agentTavilyExtracthandles advanced web content retrieval.init_chat_modelsets up the Gemini AI-powered chat model.create_react_agentbuilds a dynamic reasoning agent for intelligent decision-making.
Configuration Management
The WebIntelligence dataclass stores API keys and extraction parameters:
@dataclass
class WebIntelligence:
"""Web Intelligence Configuration"""
tavily_key: str = os.getenv("TAVILY_API_KEY", "")
google_key: str = os.getenv("GOOGLE_API_KEY", "")
extract_depth: str = "advanced"
max_urls: int = 10This structure simplifies managing keys and settings for content extraction.
SmartWebAgent Class
The SmartWebAgent class encapsulates the extraction and analysis logic:
- It prompts users for missing API keys interactively.
- Initializes Tavily and Gemini AI tools.
- Extracts structured content from URLs.
- Performs AI-driven content analysis.
- Displays results with rich visual formatting.
Key methods include:
_setup_environment()for API key input._initialize_tools()for setting up AI components.extract_content(urls)to retrieve and structure web data.analyze_with_ai(query, urls)for intelligent query-based analysis.display_results(results)to present data clearly.
Handling Asynchronous Execution
The run_async_safely function ensures asynchronous functions run reliably across different Python environments, applying nest_asyncio if needed.
Interactive Command-line Interface
The main() function offers an intuitive menu:
- Extract content from custom URLs.
- Perform AI-powered analysis.
- Run demos with sample URLs on AI, Machine Learning, and Quantum Computing.
- Exit the program.
It enhances user experience with rich terminal panels and progress tracking.
Summary
This implementation demonstrates building a sophisticated web intelligence agent combining Tavily's extraction capabilities with Google Gemini AI's analytical power. The agent facilitates advanced research and data analysis workflows by providing structured content extraction and insightful AI-driven evaluations in an interactive and visually appealing environment.
Сменить язык
Читать эту статью на русском