My use case is to extract names of people from a given text and then find details about each of those person by doing search on Internet and finally create a summary with all the details. I have implemented a Crew that includes two agents, one that extracts names of people and another agent that uses the output of first agent and uses a search tool to search for each of the people. However, while generating summary, the second agent is using only the output of last invocation of search tool.
Any idea how to resolve this?
Here is the code.
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool
from pydantic import BaseModel
from typing import List
import os
llm = LLM(model=“ollama/llama3.2:latest”, base_url=“http://localhost:11434”)
serper_tool = SerperDevTool(search_url=“https://google.serper.dev/scholar”, n_results=5)
Person name model
class PersonName(BaseModel):
name: List[str]
Define Name Extraction Agent
nameextractor_agent = Agent(
role=“Name extractor”,
goal=“”"
You have to extract names of people from given text {text}
“”",
backstory=“You are an expert in finding names of people from a given text”,
llm=llm
)
Define Name Extraction Task
nameextraction_task = Task(
description=“”"
Find out names of people from the given text {text}
“”“,
expected_output=”“”
List out all the names of people, one name in each line
“”",
agent=nameextractor_agent,
output_pydantic=PersonName
)
Define Person Researcher Agent
researcher_agent = Agent(
role=“Researcher”,
goal=“”"
Find details about a list of people from Internet using the provided tool and then provide a summary.
“”",
backstory=“You are an expert in finding details about a list of people by searching various resources in Internet”,
tools=[SerperDevTool()],
llm=llm
)
Define Research Task
research_task = Task(
description=“”"
Find out details about a list of people by searching on Internet
“”“,
expected_output=”“”
Find out details about people provided as input to this task and then create a summary report
“”",
agent=researcher_agent,
context=[nameextraction_task]
)
Launch Crew
def main():
crew=Crew(
agents=[nameextractor_agent, researcher_agent ],
tasks=[nameextraction_task, research_task ],
process=Process.sequential,
verbose=True
)
lines =
while True:
line = input()
if line == ‘END’:
break
lines.append(line)
print("Text entered -> ", lines)
result = crew.kickoff(inputs={'text':lines})
print("Result -> ", result)
if name == “main”:
main()
With following input text,
“At the highest level, figures like Fernando Fernandez, the Chief Executive Officer, drive the company’s overall strategy. Furthermore, the company’s structure includes a strong board of directors, and also leadership within regional branches of the company, such as Rohit Jawa, President, CEO & Managing Director.
END”
I see the output given below.
# Agent: Name extractor
Task:
Find out names of people from the given text [“At the highest level, figures like Fernando Fernandez, the Chief Executive Officer, drive the company’s overall strategy. Furthermore, the company’s structure includes a strong board of directors, and also leadership within regional branches of the company, such as Rohit Jawa, President, CEO & Managing Director.”]
# Agent: Name extractor
Final Answer:
{“name”: [“Fernando Fernandez”, “Rohit Jawa”]}
# Agent: Researcher
Task:
Find out details about a list of people by searching on Internet
# Agent: Researcher
Using tool: Search the internet with Serper
Tool Input:
“{"search_query": "Fernando Fernandez", "type": "search"}”
Tool Output:
{‘searchParameters’: {‘q’: ‘Fernando Fernandez’, ‘type’: 'search …
# Agent: Researcher
Using tool: Search the internet with Serper
Tool Input:
“{"search_query": "Rohit Jawa", "type": "search"}”
Tool Output:
{‘searchParameters’: {‘q’: ‘Rohit Jawa’, ‘type’: ‘search’ …
# Agent: Researcher
Final Answer:
The CEO of Hindustan Unilever (HUL) is Rohit Jawa. He was appointed …
Essentially, the Researcher Agent is generating response using output from last invocation of search tool and skipping the outputs from previous invocations.