from crewai import Agent, Task, Crew, Process from langchain_google_genai import ChatGoogleGenerativeAI from crewai_tools import SerperDevTool import os from langchain_community.utilities import GoogleSerperAPIWrapper from langchain_community.tools import DuckDuckGoSearchRun from langchain_openai import OpenAI from langchain.agents import initialize_agent, Tool from crewai_tools import tool import gradio as gr @tool('DuckDuckGoSearch') def search(search_query: str): """Search the web for information on a given topic""" return DuckDuckGoSearchRun().run(search_query) llm = ChatGoogleGenerativeAI( model="gemini-pro", verbose=True, temperature=0.1, google_api_key=os.getenv('GOOGLE_API_KEY') ) search = DuckDuckGoSearchRun() @tool('DuckDuckGoSearch') def search(search_query: str): """Search the web for information on a given topic""" return DuckDuckGoSearchRun(max_results=5).run(search_query) SerpBot = Agent( role='Search Expert', goal=' You find all the information that the Writer requests you to find. Then you compile it and give it back to him.', backstory=""" You are an agent that uses SERP in the best way to search for the input query. Action input is the following format: {"search_query"="input text"} """, verbose=True, tools=[search], llm=llm ) Writer = Agent( role='Expert Writer', goal='Write engaging, well written articles with a pinch of humour', backstory=""" A seasoned funny writer who writes about the latest news on that topic and can convert boring information to amazing articles that everyone loves reading. Your articles have a satirical tone and the humour is based on real life events """, verbose=True, allow_delegation=True, tools=[search], llm=llm ) def get_human_input(input): """Get the human input for the search query""" human_input = input return human_input def create_task(human_input): """Create a task based on the human input""" task1 = Task( description=f"Write a newsletter based on the following topics: +{human_input}. Research on topic separately and write detailed articles about them", agent = Writer, human_feedback=True, expected_output="When the entire newsletter is generated", ) return task1 def create_crew(task): """Create a crew with the task and agents""" story_crew = Crew( agents=[SerpBot, Writer], tasks=[task], verbose=True, process=Process.sequential, ) return story_crew def kickoff_crew(story_crew): """Kick off the crew""" story_output = story_crew.kickoff() return story_output def response(input,history=[]): human_input = get_human_input(input) task = create_task(human_input) story_crew = create_crew(task) res = kickoff_crew(story_crew) return res #story_output = kickoff_crew(story_crew) gr.ChatInterface(response).launch(share=True, debug=True)