-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathagents.py
More file actions
136 lines (111 loc) · 4.79 KB
/
agents.py
File metadata and controls
136 lines (111 loc) · 4.79 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
from typing import Type
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from linkup import LinkupClient
from crewai import Agent, Task, Crew, Process, LLM
from crewai.tools import BaseTool
# Load environment variables (for non-LinkUp settings)
load_dotenv()
def get_llm_client():
"""Initialize and return the LLM client"""
return LLM(
model="ollama/deepseek-r1:7b",
base_url="http://localhost:11434"
)
# Define LinkUp Search Tool
class LinkUpSearchInput(BaseModel):
"""Input schema for LinkUp Search Tool."""
query: str = Field(description="The search query to perform")
depth: str = Field(default="standard",
description="Depth of search: 'standard' or 'deep'")
output_type: str = Field(
default="searchResults", description="Output type: 'searchResults', 'sourcedAnswer', or 'structured'")
class LinkUpSearchTool(BaseTool):
name: str = "LinkUp Search"
description: str = "Search the web for information using LinkUp and return comprehensive results"
args_schema: Type[BaseModel] = LinkUpSearchInput
def __init__(self):
super().__init__()
def _run(self, query: str, depth: str = "standard", output_type: str = "searchResults") -> str:
"""Execute LinkUp search and return results."""
try:
# Initialize LinkUp client with API key from environment variables
linkup_client = LinkupClient(api_key=os.getenv("LINKUP_API_KEY"))
# Perform search
search_response = linkup_client.search(
query=query,
depth=depth,
output_type=output_type
)
return str(search_response)
except Exception as e:
return f"Error occurred while searching: {str(e)}"
def create_research_crew(query: str):
"""Create and configure the research crew with all agents and tasks"""
# Initialize tools
linkup_search_tool = LinkUpSearchTool()
# Get LLM client
client = get_llm_client()
web_searcher = Agent(
role="Web Searcher",
goal="Find the most relevant information on the web, along with source links (urls).",
backstory="An expert at formulating search queries and retrieving relevant information. Passes the results to the 'Research Analyst' only.",
verbose=True,
allow_delegation=True,
tools=[linkup_search_tool],
llm=client,
)
# Define the research analyst
research_analyst = Agent(
role="Research Analyst",
goal="Analyze and synthesize raw information into structured insights, along with source links (urls) as citations.",
backstory="An expert at analyzing information, identifying patterns, and extracting key insights. If required, can delagate the task of fact checking/verification to 'Web Searcher' only. Passes the final results to the 'Technical Writer' only.",
verbose=True,
allow_delegation=True,
llm=client,
)
# Define the technical writer
technical_writer = Agent(
role="Technical Writer",
goal="Create well-structured, clear, and comprehensive responses in markdown format, with citations/source links (urls).",
backstory="An expert at communicating complex information in an accessible way.",
verbose=True,
allow_delegation=False,
llm=client,
)
# Define tasks
search_task = Task(
description=f"Search for comprehensive information about: {query}.",
agent=web_searcher,
expected_output="Detailed raw search results including sources (urls).",
tools=[linkup_search_tool]
)
analysis_task = Task(
description="Analyze the raw search results, identify key information, verify facts and prepare a structured analysis.",
agent=research_analyst,
expected_output="A structured analysis of the information with verified facts and key insights, along with source links",
context=[search_task]
)
writing_task = Task(
description="Create a comprehensive, well-organized response based on the research analysis.",
agent=technical_writer,
expected_output="A clear, comprehensive response that directly answers the query with proper citations/source links (urls).",
context=[analysis_task]
)
# Create the crew
crew = Crew(
agents=[web_searcher, research_analyst, technical_writer],
tasks=[search_task, analysis_task, writing_task],
verbose=True,
process=Process.sequential
)
return crew
def run_research(query: str):
"""Run the research process and return results"""
try:
crew = create_research_crew(query)
result = crew.kickoff()
return result.raw
except Exception as e:
return f"Error: {str(e)}"