I have a crew with 3 agents. First agent define a query to be search using SerperAI, second agent take the query and do the search extracting the content, and last agent analyse the articles found.
The problem that I am facing is that agent 3 (article_analyst) is not receiving or considering the articles found by agent 2 (article_researcher), sometimes it invents the result (fake articles) and sometimes only take one article.
This is the code used:
- Crew.py:
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai_tools import SerperDevTool
from datetime import datetime, timedelta
import logging
import osfrom nexago_agents_project.src.nexago_agents_project.crews.mrk.search_articles.tools.article_analysis import ArticleAnalysis
from nexago_agents_project.src.nexago_agents_project.crews.mrk.search_articles.tools.article_validator_extractor import ArticleValidatorExtractorlogging.basicConfig(level=logging.DEBUG, format=“%(asctime)s - %(name)s - %(levelname)s - %(message)s”)
logger = logging.getLogger(name)from pydantic import BaseModel
from typing import Optional, List
from langchain_openai import ChatOpenAIclass ArticleOutput(BaseModel):
title: str
date: str
link: str
snippet: str
content: str
imageUrl: Optional[str]
relevance_score: int
sentiment: str@CrewBase
class ArticleResearchCrew:
“”“NexagoAgentsProject crew”“”agents_config = "config/agents.yaml" tasks_config = "config/tasks.yaml" def prepare_data(self, data): start_date = (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d') end_date = datetime.today().strftime('%Y-%m-%d') keywords = " and ".join(data.get("keywords", [])) return { "keywords": keywords, "startDate": start_date, "endDate": end_date, "numQueries": data.get("numQueries"), "userQuery": data.get("userQuery") } def kickoff(self, data): try: prepared_data = self.prepare_data(data) crew_instance = self.crew() result = crew_instance.kickoff(inputs=prepared_data) logger.debug("Kickoff result: %s", result) return result except Exception as e: logger.error(f"Error in kickoff: {e}") return {"error": str(e), "status": "kickoff_failed"} @agent def query_generator(self) -> Agent: return Agent( config=self.agents_config['query_generator'], verbose = True, allow_delegation=False ) @agent def article_researcher(self) -> Agent: serper_tool = SerperDevTool(n_results=15, tbs="qdr:w") return Agent( config=self.agents_config['article_researcher'], tools=[serper_tool, ArticleValidatorExtractor(result_as_answer=True)], verbose = True, memory=True, allow_delegation=False ) @agent def article_analyst(self) -> Agent: return Agent( config=self.agents_config['article_analyst'], verbose=True, max_iter=1, memory=True, allow_delegation=False ) @task def query_generator_task(self) -> Task: return Task( config=self.tasks_config['query_generator_task'], expected_output=self.tasks_config['query_generator_task'].get('expected_output'), agent=self.query_generator() ) @task def article_research_task(self) -> Task: return Task( config=self.tasks_config['article_research_task'], expected_output=self.tasks_config['article_research_task'].get('expected_output'), agent=self.article_researcher(), context=[self.query_generator_task()] ) @task def article_analyst_task(self) -> Task: return Task( config=self.tasks_config['article_analyst_task'], expected_output=self.tasks_config['article_analyst_task'].get('expected_output'), agent=self.article_analyst(), context=[self.article_research_task()], # Ensure it gets the output from the article_research_task output_type='json' ) @crew def crew(self) -> Crew: """Creates the NexagoAgentsProject crew""" logger.debug("Initializing agents and tasks in crew") return Crew( agents=self.agents, # Automatically created by the @agent decorator tasks=self.tasks, # Automatically created by the @task decorator process=Process.sequential, verbose=True, memory=True )
Following other posts, I have activated the memory, I have also done test creating a manager, but it doesn’t work.