import os from dotenv import load_dotenv from crewai import Agent, Task, Crew from crewai_tools import BaseTool import requests # Load environment variables from .env file load_dotenv() class VerboseSearchTool(BaseTool): name: str = "VerboseSearchTool" description: str = "A tool for searching web data with verbose output for debugging." def _run(self, query: str) -> dict: api_key = os.getenv("SERPER_API_KEY") base_url = os.getenv("SERPER_API_BASE") headers = {'Authorization': f'Bearer {api_key}'} params = {'q': query} # Log the URL and headers for debugging print(f"Attempting to hit URL: {base_url} with query: {params} using API Key: {api_key}") try: response = requests.get(base_url, headers=headers, params=params) response.raise_for_status() # This will raise an exception for HTTP errors return response.json() except requests.exceptions.HTTPError as e: print(f"HTTP error occurred: {e} - Status code: {response.status_code}") return {"error": str(e), "status_code": response.status_code} except Exception as e: print(f"An error occurred: {e}") return {"error": str(e)} # Initialize the search tool search_tool = VerboseSearchTool() # Define your agents with roles, goals, tools, and additional attributes researcher = Agent( role='Senior Research Analyst', goal='Uncover cutting-edge developments in AI and data science', backstory=( "You are a Senior Research Analyst at a leading tech think tank. " "Your expertise lies in identifying emerging trends and technologies in AI and data science. " "You have a knack for dissecting complex data and presenting actionable insights." ), verbose=True, allow_delegation=False, tools=[search_tool], max_rpm=1000 ) writer = Agent( role='Tech Content Strategist', goal='Craft compelling content on tech advancements', backstory=( "You are a renowned Tech Content Strategist, known for your insightful and engaging articles on technology and innovation. " "With a deep understanding of the tech industry, you transform complex concepts into compelling narratives." ), verbose=True, allow_delegation=True, tools=[search_tool], cache=False # Disable cache for this agent ) # Create tasks for your agents task1 = Task( description=( "Perform an in-depth review of the most recent progress in open-source large language models (LLMs) for business applications and custom solution integrations in 2024. " "Highlight pivotal trends, innovative technologies, and their prospective implications for various industries." "Compile your findings in a detailed report." "Make sure to check with a human if the draft is good before finalizing your answer." ), expected_output='A comprehensive full report on the latest AI advancements in 2024, leave nothing out', agent=researcher, human_input=True, ) task2 = Task( description=( "Using the insights from the researcher's report, develop an engaging blog post that highlights the most significant AI advancements. " "Your post should be informative yet accessible, catering to an investor audience. " "Aim for a narrative that captures the essence of these breakthroughs and their implications for the future." ), expected_output='A detailed and engaging three-paragraph blog post, formatted in Markdown, discussing the newest developments in open-source large language models (LLMs) for business applications and custom solution integrations in 2024', agent=writer ) # Instantiate your crew with a sequential process crew = Crew( agents=[researcher, writer], tasks=[task1, task2], verbose=2 ) # Get your crew to work! result = crew.kickoff() print("######################") print(result)