├── README.md ├── __pycache__ ├── backstory.cpython-310.pyc ├── llms.cpython-310.pyc └── tasks.cpython-310.pyc ├── app.py ├── backstory.py ├── config.yaml ├── llms.py ├── requirements.txt └── tasks.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Setting Up and Running Crew AI 3 | 4 | ### Prerequisites 5 | 1. **Install Anaconda:** 6 | Download Anaconda from [https://www.anaconda.com/](https://www.anaconda.com/). 7 | 8 | 2. **Create a Virtual Environment:** 9 | ```bash 10 | conda create -n crew_env python=3.10 pip 11 | ``` 12 | 13 | 3. **Activate the Virtual Environment:** 14 | ```bash 15 | conda activate crew_env 16 | ``` 17 | 18 | 4. **Install Requirements:** 19 | ```bash 20 | pip install -r requirements.txt 21 | ``` 22 | 23 | ### Clone and Navigate to the Repository 24 | 1. **Clone the Repo:** 25 | ```bash 26 | git clone https://github.com/your-repo/crew-ai.git 27 | ``` 28 | 29 | 2. **Navigate to the Repo:** 30 | ```bash 31 | cd /path/to/your-repo/crew-ai 32 | ``` 33 | 34 | ### Configure API Keys 35 | 1. **Open the `config.yaml`:** 36 | ```bash 37 | nano config.yaml 38 | ``` 39 | 40 | 2. **Enter API Keys:** 41 | - **Serper API Key:** Get it from [https://serper.dev/](https://serper.dev/) 42 | - **OpenAI API Key:** Get it from [https://openai.com/](https://openai.com/) 43 | 44 | ### Run Your Query 45 | ```bash 46 | python app.py "YOUR QUERY" 47 | ``` 48 | -------------------------------------------------------------------------------- /__pycache__/backstory.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-adeojo/crew_ai_tutorial/8067c36bde79dadf77a4679bdfff246aad6dfb94/__pycache__/backstory.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/llms.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-adeojo/crew_ai_tutorial/8067c36bde79dadf77a4679bdfff246aad6dfb94/__pycache__/llms.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/tasks.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/john-adeojo/crew_ai_tutorial/8067c36bde79dadf77a4679bdfff246aad6dfb94/__pycache__/tasks.cpython-310.pyc -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | from crewai import Agent, Crew, Process, Task 3 | from crewai_tools import SerperDevTool, ScrapeWebsiteTool 4 | from langchain_openai import ChatOpenAI 5 | import argparse 6 | import yaml 7 | import os 8 | from tasks import task_planner, task_searcher, task_reporter, task_integration 9 | from backstory import backstory_planner, backstory_searcher, backstory_integration, backstory_reporter 10 | from langchain_openai import ChatOpenAI 11 | 12 | 13 | def load_config(file_path): 14 | with open(file_path, 'r') as file: 15 | config = yaml.safe_load(file) 16 | for key, value in config.items(): 17 | os.environ[key] = value 18 | 19 | # loads API keys from config.yaml 20 | load_config(file_path="./config.yaml") 21 | # INFERENCE_SERVER_URL = os.getenv("INFERENCE_SERVER_URL") 22 | 23 | 24 | # Define the tools 25 | serper_tool = SerperDevTool() 26 | website_search_tool = ScrapeWebsiteTool() 27 | 28 | manager_llm = ChatOpenAI(temperature=0, model="gpt-4-turbo") 29 | llm = ChatOpenAI(temperature=0, model="gpt-4-turbo") 30 | 31 | 32 | # Define the agents 33 | planning_agent = Agent( 34 | role='Planner', 35 | goal='Streamline complex inquiries into organized, manageable components.', 36 | backstory=backstory_planner, 37 | verbose=True, 38 | llm=llm, 39 | cache=True, 40 | allow_delegation=True, 41 | ) 42 | 43 | search_agent = Agent( 44 | role='Searcher', 45 | goal='Identify and retrieve essential data for sophisticated inquiries.', 46 | backstory=backstory_searcher, 47 | tools=[serper_tool, website_search_tool], 48 | verbose=True, 49 | llm=llm, 50 | cache=True, 51 | allow_delegation=True 52 | ) 53 | 54 | integration_agent = Agent( 55 | role='Integration', 56 | goal="Organise and sythesize information from multiple sources", 57 | backstory=backstory_integration, 58 | verbose=True, 59 | llm=llm, 60 | cache=True, 61 | allow_delegation=True 62 | ) 63 | 64 | reporting_agent = Agent( 65 | role='Reporter', 66 | goal="Communicate insights clearly, ensuring depth and accuracy for further exploration", 67 | backstory=backstory_reporter, 68 | verbose=True, 69 | llm=llm, 70 | cache=True, 71 | allow_delegation=True 72 | ) 73 | 74 | 75 | def main(query): 76 | 77 | planning_task = Task( 78 | description=task_planner.format(query=query), 79 | expected_output='A detailed view of the sub-questions and their relationships to the main question and how to proceed with the investigation to answer the main question', 80 | agent=planning_agent, 81 | 82 | ) 83 | 84 | search_task = Task( 85 | description=task_searcher.format(query=query), 86 | expected_output='Specific information and sources relevant to the sub-questions identified by the Planner Agent', 87 | agent=search_agent, 88 | tools=[serper_tool, website_search_tool], 89 | context=[planning_task] 90 | ) 91 | 92 | integration_task = Task( 93 | description=task_integration.format(query=query), 94 | expected_output='All the information gathered from the searcher agent organised and integrated with website links and references.', 95 | agent=integration_agent, 96 | context=[search_task, planning_task] 97 | ) 98 | 99 | reporting_task = Task( 100 | description=task_reporter.format(query=query), 101 | expected_output='A clear, accurate, and concise response to the user, with references and website links to sources of information.', 102 | agent=reporting_agent, 103 | context=[integration_task, search_task, planning_task] 104 | ) 105 | 106 | 107 | crew = Crew( 108 | agents=[planning_agent, search_agent, integration_agent, reporting_agent], 109 | process=Process.hierarchical, 110 | manager_llm=manager_llm, 111 | memory=True, 112 | tasks=[planning_task, search_task, integration_task, reporting_task] 113 | ) 114 | 115 | try: 116 | result = crew.kickoff() 117 | print(result) 118 | except Exception as e: 119 | print("failed to kickoff the crew: {e}") 120 | 121 | 122 | if __name__ == "__main__": 123 | parser = argparse.ArgumentParser(description='Run a query through CrewAI agents.') 124 | parser.add_argument('query', type=str, help='The query to be answered') 125 | 126 | args = parser.parse_args() 127 | main(args.query) -------------------------------------------------------------------------------- /backstory.py: -------------------------------------------------------------------------------- 1 | # Backstories for each agent in the CrewAI setup 2 | 3 | backstory_planner = """ 4 | Expert in deconstructing complex, multi-hop questions into a network of simpler, interconnected queries. 5 | """ 6 | 7 | backstory_searcher = """ 8 | Specialist in conducting targeted searches for information based on structured paths provided by the Planner Agent. 9 | """ 10 | 11 | backstory_integration = """ 12 | Skilled in synthesizing answers obtained for each sub-question into a coherent, comprehensive response that addresses the user's original, multi-hop question. 13 | """ 14 | 15 | backstory_reporter = """ 16 | Skilled in delivering final, integrated responses to users, ensuring accuracy, clarity, and completeness. 17 | """ 18 | -------------------------------------------------------------------------------- /config.yaml: -------------------------------------------------------------------------------- 1 | SERPER_API_KEY: "YOUR KEY HERE" 2 | OPENAI_API_KEY: "YOUR KEY HERE" -------------------------------------------------------------------------------- /llms.py: -------------------------------------------------------------------------------- 1 | from openai import OpenAI 2 | 3 | class OpenAIChatManager: 4 | def __init__(self, model="gpt-3.5-turbo-0125", api_key=None, **kwargs): 5 | self.model = model 6 | self.client = OpenAI(api_key=api_key) 7 | self.kwargs = kwargs # Store additional kwargs 8 | 9 | def chat_query(self, messages): 10 | try: 11 | response = self.client.chat.completions.create( 12 | model=self.model, 13 | messages=messages, 14 | **self.kwargs # Pass all stored kwargs to the API call 15 | ) 16 | return response.choices[0].message['content'] 17 | except Exception as e: 18 | print(f"Error querying OpenAI Chat: {e}") 19 | return None 20 | 21 | 22 | class OpenAICompletionManager: 23 | def __init__(self, model="gpt-3.5-turbo-instruct", api_key=None, **kwargs): 24 | self.model = model 25 | self.client = OpenAI(api_key=api_key) 26 | self.kwargs = kwargs # Store additional kwargs 27 | 28 | def query(self, prompt): 29 | try: 30 | response = self.client.completions.create( 31 | model=self.model, 32 | prompt=prompt, 33 | **self.kwargs # Pass all stored kwargs to the API call 34 | ) 35 | return response.choices[0].text.strip() 36 | except Exception as e: 37 | print(f"Error querying OpenAI Completions: {e}") 38 | return None 39 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | crewai[tools]==0.28.8 2 | openai==1.23.2 3 | PyYAML==6.0.1 -------------------------------------------------------------------------------- /tasks.py: -------------------------------------------------------------------------------- 1 | task_planner = ( 2 | "As the Planner, your primary task is to deconstruct the user's complex, multi-hop question into a network of simpler, interconnected questions.\n" 3 | "This involves identifying the key components and relationships within the question.\n" 4 | "You must determine if the question involves linear, branching, or converging paths and plan accordingly.\n" 5 | "For each identified sub-question, you should outline a logical sequence or network that progressively builds towards answering the overarching query.\n" 6 | "This structured approach facilitates a comprehensive investigation by guiding subsequent agents through a clear, methodical process.\n" 7 | "Be prepared to receive feedback from the Integration Agent on missing information or clarity needed and adjust the investigation accordingly.\n" 8 | "Here's an example of how you might break down a question: Who succeeded the first President of Namibia?\n" 9 | "1. Who was the first President of Namibia?\n" 10 | "2. Who succeeded Sam Nujoma?\n" 11 | " here's the query: {query}" 12 | ) 13 | 14 | task_searcher = ( 15 | "As the Searcher, your responsibility is to conduct targeted searches for information based on the structured path provided by the Planner Agent.\n" 16 | "You should tackle each sub-question individually, using available resources to gather relevant, specific information.\n" 17 | "Adapt your search strategy based on the type of sub-question be it factual, conceptual, or contextual and incorporate knowledge from previous searches to inform subsequent ones.\n" 18 | "Your goal is to systematically assemble the pieces of information required to construct the context needed for addressing the original, complex multi-hop question.\n" 19 | "If you encounter challenges in finding the required information, note down what is missing or unclear for feedback to the Planner Agent.\n" 20 | " here's the query: {query}" 21 | 22 | ) 23 | 24 | task_integration = ( 25 | "As the Integration Agent, synthesize the answers obtained for each sub-question into a coherent, comprehensive response that addresses the user's original, multi-hop question.\n" 26 | "If you identify information gaps or need further clarification, provide specific feedback to the Planner Agent.\n" 27 | "This feedback is crucial for refining the investigation and ensuring the final response is as comprehensive and accurate as possible.\n" 28 | "Ensure you include the sources of information in your integrated response to maintain transparency and credibility.\n" 29 | " here's the query: {query}" 30 | 31 | ) 32 | 33 | task_reporter = ( 34 | "As the Reporter Agent, your role is to deliver the final, integrated response to the user, ensuring it accurately and comprehensively addresses the multi-hop question.\n" 35 | "Review the synthesized answer for clarity, accuracy, and completeness, incorporating citations for all referenced information.\n" 36 | "Present the findings in a clear, concise, and informative manner, providing citations and links to sources.\n" 37 | "Your presentation should reflect the structured investigation and synthesis process, offering a complete answer and facilitating further exploration by the user if desired.\n" 38 | "If the Integration Agent has identified that the question cannot be fully answered with the available information, communicate this transparently to the user along with any potential next steps or recommendations for further inquiry.\n" 39 | " here's the query: {query}" 40 | 41 | ) 42 | --------------------------------------------------------------------------------