File size: 4,304 Bytes
d445f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from typing import List, Dict, Any, Optional
from crewai import Agent
from crewai_tools import BraveSearchTool, ScrapeWebsiteTool
from tools import ContentAnalyzerTool, RateLimitedToolWrapper, TavilySearchTool, SearchRotationTool

def create_researcher_agent(llm=None, verbose=True) -> Agent:
    """
    Creates a researcher agent responsible for query refinement and web search.
    
    Args:
        llm: Language model to use for the agent
        verbose: Whether to log agent activity
        
    Returns:
        Configured researcher agent
    """
    # Initialize search tools
    brave_search_tool = BraveSearchTool(
        n_results=5,
        save_file=False
    )
    
    # Initialize Tavily search tool
    # Requires a TAVILY_API_KEY in environment variables
    tavily_search_tool = TavilySearchTool(
        max_results=5,
        search_depth="basic",
        timeout=15  # Increase timeout for more reliable results
    )
    
    # Add minimal rate limiting to avoid API throttling
    # Set delay to 0 to disable rate limiting completely
    rate_limited_brave_search = RateLimitedToolWrapper(tool=brave_search_tool, delay=0)
    rate_limited_tavily_search = RateLimitedToolWrapper(tool=tavily_search_tool, delay=0)
    
    # Create the search rotation tool
    search_rotation_tool = SearchRotationTool(
        search_tools=[rate_limited_brave_search, rate_limited_tavily_search],
        max_searches_per_query=5  # Limit to 5 searches per query as requested
    )
    
    return Agent(
        role="Research Specialist",
        goal="Discover accurate and relevant information from the web",
        backstory=(
            "You are an expert web researcher with a talent for crafting effective search queries "
            "and finding high-quality information on any topic. Your goal is to find the most "
            "relevant and factual information to answer user questions. You have access to multiple "
            "search engines and know how to efficiently use them within the search limits."
        ),
        # Use the search rotation tool
        tools=[search_rotation_tool],
        verbose=verbose,
        allow_delegation=True,
        memory=True,
        llm=llm
    )

def create_analyst_agent(llm=None, verbose=True) -> Agent:
    """
    Creates an analyst agent responsible for content analysis and evaluation.
    
    Args:
        llm: Language model to use for the agent
        verbose: Whether to log agent activity
        
    Returns:
        Configured analyst agent
    """
    # Initialize tools
    scrape_tool = ScrapeWebsiteTool()
    content_analyzer = ContentAnalyzerTool()
    
    return Agent(
        role="Content Analyst",
        goal="Analyze web content for relevance, factuality, and quality",
        backstory=(
            "You are a discerning content analyst with a keen eye for detail and a strong "
            "commitment to factual accuracy. You excel at evaluating information and filtering "
            "out irrelevant or potentially misleading content. Your expertise helps ensure that "
            "only the most reliable information is presented."
        ),
        tools=[scrape_tool, content_analyzer],
        verbose=verbose,
        allow_delegation=True,
        memory=True,
        llm=llm
    )

def create_writer_agent(llm=None, verbose=True) -> Agent:
    """
    Creates a writer agent responsible for synthesizing information into coherent responses.
    
    Args:
        llm: Language model to use for the agent
        verbose: Whether to log agent activity
        
    Returns:
        Configured writer agent
    """    
    return Agent(
        role="Research Writer",
        goal="Create informative, factual, and well-cited responses to research queries",
        backstory=(
            "You are a skilled writer specializing in creating clear, concise, and informative "
            "responses based on research findings. You have a talent for synthesizing information "
            "from multiple sources and presenting it in a coherent and readable format, always with "
            "proper citations. You prioritize factual accuracy and clarity in your writing."
        ),
        verbose=verbose,
        allow_delegation=True,
        memory=True,
        llm=llm
    )