{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29 langchain-groq" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import warnings \n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from crewai import Agent, Task , Crew" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import os\n", "from utils import get_openai_api_key\n", "\n", "openai_api_key = get_openai_api_key() \n", "# Please restart kernel to load new env variables\n", "MODEL = ['gpt-4o', 'gpt-4' , 'gpt-3.5-turbo'] # to change models\n", "os.environ[\"OPENAI_MODEL_NAME\"] = MODEL[2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# IF WANT TO USE GROQ \n", "# from langchain_openai import ChatOpenAI\n", "\n", "# llm = ChatOpenAI(\n", "# openai_api_base=\"https://api.groq.com/openai/v1\",\n", "# openai_api_key=os.environ['GROQ_API_KEY'],\n", "# model_name=\"llama3-8b-8192\",\n", "# temperature=0,\n", "# max_tokens=1000,\n", "# )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Agents" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "Planner = Agent(\n", " role = \"Plan the Route to be taken\",\n", " goal = \"Understand the user {topic}and decide whether to converse more with user or perform a vector database search\",\n", " backstory = \"You are an expert at routing user query: {topic}.\"\n", " \"If the user asks for educational information, you do a vector database search and retrieve the relevant answer\"\n", " \"However if the user wants to help thier family member OR a friend, you engage in further converation and provide emphathetic resposne\",\n", " allow_delegation = False,\n", " verbose = True\n", " # llm = llm\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# from crewai_tools import tool\n", "# @tool\n", "# def router_tool(query):\n", "# if 'help' in query:\n", "# return 'plan'\n", "# else:\n", "# return 'retrieve'" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "Retriever = Agent(\n", " role=\"Retriever\",\n", " goal=\"Use the information retrieved from the vectorstore to answer the {topic}\",\n", " backstory=(\n", " \"You are an assistant for question-answering tasks.\"\n", " \"Use the information present in the retrieved context to answer the question.\"\n", " \"You have to provide a clear concise answer.\"\n", " ),\n", " verbose=True,\n", " allow_delegation=False # attach llamindex rag here\n", " # llm=llm,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Tasks" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "plan = Task(\n", " description=(\n", " \"1. Analyze the user's {topic} to determine the nature of the request.\\n\"\n", " \"2. If the query is educational, perform a vector database search to retrieve the most relevant and accurate information.\\n\"\n", " \"3. If the query is related to helping a family member or friend, engage in a conversation to provide a supportive and empathetic response.\\n\"\n", " \"4. Ensure the response is detailed and accurately addresses the user's needs.\"\n", " ),\n", " expected_output=\"A decision on whether to perform a vector database search or engage in a conversation based on the user's query, \"\n", " \"with a detailed and appropriate response provided accordingly.\",\n", " agent=Planner,\n", ")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "retrieve = Task(\n", " description=(\n", " \"1. Analyze the question to understand the user's {topic}.\\n\"\n", " \"2. Perform a search in the vectorstore to retrieve the most relevant context.\\n\"\n", " \"3. Use the retrieved context to formulate a clear and concise answer.\\n\"\n", " \"4. Ensure the answer is accurate and directly addresses the question.\"\n", " ),\n", " expected_output=\"A clear and concise answer to the user's question, based on the information retrieved from the vectorstore.\",\n", " agent=Retriever,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### starting the crew" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-06-26 18:42:51,855 - 4794308096 - __init__.py-__init__:518 - WARNING: Overriding of current TracerProvider is not allowed\n" ] } ], "source": [ "crew = Crew(\n", " agents=[Planner, Retriever],\n", " tasks=[plan, retrieve], # order matter\n", " verbose=2\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m\u001b[95m [DEBUG]: == Working Agent: Plan the Route to be taken\u001b[00m\n", "\u001b[1m\u001b[95m [INFO]: == Starting Task: 1. Analyze the user's I want to talk about my son who's having stress and low confidence for some time to determine the nature of the request.\n", "2. If the query is educational, perform a vector database search to retrieve the most relevant and accurate information.\n", "3. If the query is related to helping a family member or friend, engage in a conversation to provide a supportive and empathetic response.\n", "4. Ensure the response is detailed and accurately addresses the user's needs.\u001b[00m\n", "\n", "\n", "\u001b[1m> Entering new CrewAgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mI now can give a great answer\n", "\n", "Final Answer: \n", "Based on your statement, \"I want to talk about my son who's having stress and low confidence for some time,\" it seems like you are seeking advice or guidance on how to help your son navigate through his current challenges. As this query is related to helping a family member, I will engage in a conversation to provide a supportive and empathetic response.\n", "\n", "I understand that seeing your son struggle with stress and low confidence can be concerning as a parent. It's important to create a safe and open space for him to express his feelings and concerns without judgment. Encouraging him to talk about what's been bothering him and actively listening can make a significant difference in how he copes with his emotions.\n", "\n", "Additionally, you may want to consider seeking professional help from a counselor or therapist who specializes in working with adolescents dealing with stress and confidence issues. They can provide valuable strategies and support tailored to your son's specific needs.\n", "\n", "Remember to reassure your son that you are there for him and that it's okay to not be okay sometimes. Building his self-esteem and resilience will take time, but with patience, understanding, and support, he can overcome these challenges and thrive.\n", "\n", "If you need further assistance or resources, feel free to reach out. Your son's well-being is important, and it's commendable that you are seeking help for him.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\u001b[1m\u001b[92m [DEBUG]: == [Plan the Route to be taken] Task output: Based on your statement, \"I want to talk about my son who's having stress and low confidence for some time,\" it seems like you are seeking advice or guidance on how to help your son navigate through his current challenges. As this query is related to helping a family member, I will engage in a conversation to provide a supportive and empathetic response.\n", "\n", "I understand that seeing your son struggle with stress and low confidence can be concerning as a parent. It's important to create a safe and open space for him to express his feelings and concerns without judgment. Encouraging him to talk about what's been bothering him and actively listening can make a significant difference in how he copes with his emotions.\n", "\n", "Additionally, you may want to consider seeking professional help from a counselor or therapist who specializes in working with adolescents dealing with stress and confidence issues. They can provide valuable strategies and support tailored to your son's specific needs.\n", "\n", "Remember to reassure your son that you are there for him and that it's okay to not be okay sometimes. Building his self-esteem and resilience will take time, but with patience, understanding, and support, he can overcome these challenges and thrive.\n", "\n", "If you need further assistance or resources, feel free to reach out. Your son's well-being is important, and it's commendable that you are seeking help for him.\n", "\n", "\u001b[00m\n", "\u001b[1m\u001b[95m [DEBUG]: == Working Agent: Retriever\u001b[00m\n", "\u001b[1m\u001b[95m [INFO]: == Starting Task: 1. Analyze the question to understand the user's I want to talk about my son who's having stress and low confidence for some time.\n", "2. Perform a search in the vectorstore to retrieve the most relevant context.\n", "3. Use the retrieved context to formulate a clear and concise answer.\n", "4. Ensure the answer is accurate and directly addresses the question.\u001b[00m\n", "\n", "\n", "\u001b[1m> Entering new CrewAgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mI now can give a great answer\n", "\n", "Final Answer: As a parent wanting to help your son cope with stress and low confidence, it's crucial to create a safe and open environment for him to share his feelings. Encouraging open communication and seeking professional help from a counselor or therapist specializing in adolescent issues can provide valuable support. Reassuring your son of your presence, patience, and understanding will aid in building his self-esteem and resilience, ultimately helping him overcome these challenges and thrive.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\u001b[1m\u001b[92m [DEBUG]: == [Retriever] Task output: As a parent wanting to help your son cope with stress and low confidence, it's crucial to create a safe and open environment for him to share his feelings. Encouraging open communication and seeking professional help from a counselor or therapist specializing in adolescent issues can provide valuable support. Reassuring your son of your presence, patience, and understanding will aid in building his self-esteem and resilience, ultimately helping him overcome these challenges and thrive.\n", "\n", "\u001b[00m\n" ] } ], "source": [ "TOPIC = \"I want to talk about my son who's having stress and low confidence for some time\"\n", "result = crew.kickoff(inputs={\"topic\": TOPIC})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The PDFSearchTool is a RAG tool designed for semantic searches within PDF content. It allows for inputting a search query and a PDF document, leveraging advanced search techniques to find relevant content efficiently. This capability makes it especially useful for extracting specific information from large PDF files quickly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# from crewai_tools import PDFSearchTool\n", "\n", "# rag_tool = PDFSearchTool(pdf='/content/17.pdf',\n", "# config=dict(\n", "# llm=dict(\n", "# provider=\"groq\", # or google, openai, anthropic, llama2, ...\n", "# config=dict(\n", "# model=\"llama3-8b-8192\",\n", "# # temperature=0.5,\n", "# # top_p=1,\n", "# # stream=true,\n", "# ),\n", "# ),\n", "# embedder=dict(\n", "# provider=\"huggingface\", # or openai, ollama, ...\n", "# config=dict(\n", "# model=\"BAAI/bge-small-en-v1.5\",\n", "# #task_type=\"retrieval_document\",\n", "# # title=\"Embeddings\",\n", "# ),\n", "# ),\n", "# )\n", "# )" ] } ], "metadata": { "kernelspec": { "display_name": "crew", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }