diff --git a/external_agents/README.md b/external_agents/README.md new file mode 100644 index 00000000..9ee52242 --- /dev/null +++ b/external_agents/README.md @@ -0,0 +1,36 @@ +# Adding external agents to the crew + +This example shows how you can add external agents to the crew, on the example of a langchain agent. + +You do this by inheriting from the `AgentWrapperParent` class and implementing its abstract methods. + +These agents can then be mixed and matched with native CrewAI agents. + +An implementation for langchain agent is already provided and can be used as in the following example: + + ```python + from langchain import hub + from langchain.agents import create_openai_tools_agent + from langchain_openai import ChatOpenAI + from langchain_community.tools import DuckDuckGoSearchRun + + from crewai.agents.langchain_agent import LangchainCrewAgent + + llm = ChatOpenAI(model="gpt-4-0125-preview", temperature=0) + tools = [DuckDuckGoSearchRun()] + researcher_prompt = hub.pull("hwchase17/openai-tools-agent") + + researcher_agent = AgentExecutor( + agent=create_openai_tools_agent(llm, tools, researcher_prompt), + tools=tools, + verbose=True, + ) + + researcher = LangchainCrewAgent( + agent=researcher_agent, + tools=[search_tool], + role="Senior Research Analyst", + allow_delegation=False, + ) + +You need to pass a function that creates the desired agent from the list of its tools, to enable adding other agents as tools at runtime (for delegation). diff --git a/external_agents/external_agent_demo.py b/external_agents/external_agent_demo.py new file mode 100644 index 00000000..ac068e2f --- /dev/null +++ b/external_agents/external_agent_demo.py @@ -0,0 +1,93 @@ +import os +from typing import Any, List +import copy +from uuid import uuid4 + +from crewai import Agent, Task, Crew +from crewai.agents.langchain_agent import LangchainCrewAgent + +from langchain import hub +from langchain_community.tools import DuckDuckGoSearchRun +from langchain_openai import ChatOpenAI +from langchain.agents import AgentExecutor, create_openai_tools_agent + +# Please set the OPENAI_API_KEY environment variable to your OpenAI API key +# os.environ["OPENAI_API_KEY"] = ... + +# You can delete this block if you don't want to use Langsmith +from langsmith import Client + +unique_id = uuid4().hex[0:8] +os.environ["LANGCHAIN_TRACING_V2"] = "true" +os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}" +os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" + +# Replace the LANGCHAIN_API_KEY with your Langsmith API key +# os.environ["LANGCHAIN_API_KEY"] = ... + +client = Client() +# End of Langsmith block + + +search_tool = DuckDuckGoSearchRun() +tools = [DuckDuckGoSearchRun()] + +researcher_prompt = hub.pull("hwchase17/openai-tools-agent") +llm = ChatOpenAI(model="gpt-4-0125-preview", temperature=0) + +researcher_agent = AgentExecutor( + agent=create_openai_tools_agent(llm, tools, researcher_prompt), + tools=tools, + verbose=True, +) +writer_agent = AgentExecutor( + agent=create_openai_tools_agent(llm, [], researcher_prompt), tools=[], verbose=True +) + +researcher = LangchainCrewAgent( + agent=researcher_agent, + tools=[search_tool], + role="Senior Research Analyst", + allow_delegation=False, +) + +writer = Agent( + role="Tech Content Strategist", + goal="Craft compelling content on tech advancements", + backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles. + You transform complex concepts into compelling narratives.""", + verbose=True, + allow_delegation=True, +) + +# From here onwards it's exactly like the original example + +# Create tasks for your agents +task1 = Task( + description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024. + Identify key trends, breakthrough technologies, and potential industry impacts. + Your final answer MUST be a full analysis report""", + agent=researcher, +) + +task2 = Task( + description="""Using the insights provided, develop an engaging blog + post that highlights the most significant AI advancements. + Your post should be informative yet accessible, catering to a tech-savvy audience. + Make it sound cool, avoid complex words so it doesn't sound like AI. + Your final answer MUST be the full blog post of at least 4 paragraphs.""", + agent=writer, +) + +# Instantiate your crew with a sequential process +crew = Crew( + agents=[researcher, writer], + tasks=[task2], + verbose=2, # You can set it to 1 or 2 to different logging levels +) + +# Get your crew to work! +result = crew.kickoff() + +print("######################") +print(result)