diff --git a/README.md b/README.md index d0f7d43..101a026 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,17 @@ percentage, missing keywords, and profile summary. - Choose between vector search or summarization for responses. - Support for multiple Open Source LLMs models. +### 21. Tech News Agent using Crew ai + +- **Route:** `/news_agent` +- **Description:** This endpoint leverages AI agents to conduct research and generate articles on various tech topics. The agents are designed to uncover groundbreaking technologies and narrate compelling tech stories. +- **Features:** + - Accepts a `topic` parameter specifying the tech topic of interest. + - Utilizes caching mechanisms for improved performance by storing and retrieving responses from Redis cache. + - Integrates with MongoDB for storing endpoint usage data. + - Returns articles and research findings related to the specified tech topic. + - Handles exceptions gracefully and returns error messages in JSON format. + ## Usage Each endpoint accepts specific parameters as described in the respective endpoint documentation. Users can make POST requests to these endpoints with the required parameters to perform the desired tasks. diff --git a/api.py b/api.py index d160236..3942e62 100644 --- a/api.py +++ b/api.py @@ -29,6 +29,7 @@ from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail from uuid import uuid4 +from tech_news_agent.crew import run_crew os.environ["LANGCHAIN_TRACING_V2"]="true" @@ -626,6 +627,33 @@ async def get_data(endpoint_name: str, token: str = Depends(oauth2_scheme)): redis.set(cache_key, json.dumps(data), ex=60) return data + +@app.post("/news_agent",description=""" + This endpoint leverages AI agents to conduct research and generate articles on various tech topics. + The agents are designed to uncover groundbreaking technologies and narrate compelling tech stories + """) +async def run_news_agent(topic: str = Form("AI in healthcare")): + try: + cache_key = f"news_agent:{topic}" + cached_response = redis.get(cache_key) + if cached_response: + print("Retrieving response from Redis cache") + return ResponseText(response=cached_response.decode("utf-8")) + + output = run_crew(topic=topic) + redis.set(cache_key, output, ex=10) + db = MongoDB() + payload = { + "endpoint": "/news_agent", + "topic" : topic, + "output": output + } + mongo_data = {"Document": payload} + result = db.insert_data(mongo_data) + print(result) + return ResponseText(response=output) + except Exception as e: + return {"error": str(e)} if __name__ == '__main__': import uvicorn diff --git a/requirements.txt b/requirements.txt index db8aa9c..4121ea0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,7 @@ redis python-jose sendgrid llama-index-llms-groq -llama-index-embeddings-langchain \ No newline at end of file +llama-index-embeddings-langchain +crewai +crewai_tools +proto-plus \ No newline at end of file diff --git a/tech_news_agent/__init__.py b/tech_news_agent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tech_news_agent/agents.py b/tech_news_agent/agents.py new file mode 100644 index 0000000..44cfd68 --- /dev/null +++ b/tech_news_agent/agents.py @@ -0,0 +1,48 @@ +from crewai import Agent +from .tools import tool +from dotenv import load_dotenv +load_dotenv() +from langchain_google_genai import ChatGoogleGenerativeAI +import os + + +## call the gemini models +llm=ChatGoogleGenerativeAI(model="gemini-1.5-flash", + verbose=True, + temperature=0.5, + google_api_key=os.getenv("GOOGLE_API_KEY")) + +# Creating a senior researcher agent with memory and verbose mode + +news_researcher=Agent( + role="Senior Researcher", + goal='Unccover ground breaking technologies in {topic}', + verbose=True, + memory=True, + backstory=( + "Driven by curiosity, you're at the forefront of" + "innovation, eager to explore and share knowledge that could change" + "the world." + ), + # tools=[tool], + llm=llm, + allow_delegation=True + +) + +## creating a write agent with custom tools responsible in writing news blog + +news_writer = Agent( + role='Writer', + goal='Narrate compelling tech stories about {topic}', + verbose=True, + memory=True, + backstory=( + "With a flair for simplifying complex topics, you craft" + "engaging narratives that captivate and educate, bringing new" + "discoveries to light in an accessible manner." + ), + # tools=[tool], + llm=llm, + allow_delegation=False +) diff --git a/tech_news_agent/crew.py b/tech_news_agent/crew.py new file mode 100644 index 0000000..32edddf --- /dev/null +++ b/tech_news_agent/crew.py @@ -0,0 +1,17 @@ +from crewai import Crew,Process +from .tasks import research_task,write_task +from .agents import news_researcher,news_writer + +crew=Crew( + agents=[news_researcher,news_writer], + tasks=[research_task,write_task], + process=Process.sequential, + +) + +def run_crew(topic): + result = crew.kickoff(inputs={'topic': topic}) + return result + +if __name__=='__main__': + print(run_crew(topic="AI in Constructions")) \ No newline at end of file diff --git a/tech_news_agent/tasks.py b/tech_news_agent/tasks.py new file mode 100644 index 0000000..17bc509 --- /dev/null +++ b/tech_news_agent/tasks.py @@ -0,0 +1,30 @@ +from crewai import Task +from .tools import tool +from .agents import news_researcher,news_writer + +# Research task +research_task = Task( + description=( + "Identify the next big trend in {topic}." + "Focus on identifying pros and cons and the overall narrative." + "Your final report should clearly articulate the key points," + "its market opportunities, and potential risks." + ), + expected_output='A comprehensive 3 paragraphs long report on the latest AI trends.', + # tools=[tool], + agent=news_researcher, +) + +# Writing task with language model configuration +write_task = Task( + description=( + "Compose an insightful article on {topic}." + "Focus on the latest trends and how it's impacting the industry." + "This article should be easy to understand, engaging, and positive." + ), + expected_output='A 4 paragraph article on {topic} advancements formatted as markdown.', + # tools=[tool], + agent=news_writer, + async_execution=False, + # output_file='new-blog-post.md' +) \ No newline at end of file diff --git a/tech_news_agent/tools.py b/tech_news_agent/tools.py new file mode 100644 index 0000000..09a9aac --- /dev/null +++ b/tech_news_agent/tools.py @@ -0,0 +1,11 @@ +from dotenv import load_dotenv +load_dotenv() +import os + +os.environ['SERPER_API_KEY'] = os.getenv('SERPER_API_KEY') + + +from crewai_tools import SerperDevTool + +# Initialize the tool for internet searching capabilities +tool = SerperDevTool() \ No newline at end of file