-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat: agentspec adapter #4035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gojkoc54
wants to merge
4
commits into
crewAIInc:main
Choose a base branch
from
gojkoc54:feat/agentspec-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−9
Open
feat: agentspec adapter #4035
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
107 changes: 107 additions & 0 deletions
107
lib/crewai/src/crewai/agents/agent_adapters/agentspec/agentspec_adapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from pydantic import Field, PrivateAttr | ||
| from typing import Any, Optional, Dict, Union, Callable, List | ||
|
|
||
| from crewai.agents.agent_builder.base_agent import BaseAgent | ||
| from crewai.agents.agent_adapters.base_agent_adapter import BaseAgentAdapter | ||
| from crewai import Agent as CrewAIAgent | ||
| from crewai.tools.base_tool import BaseTool, Tool as CrewAITool | ||
| from crewai.utilities.import_utils import import_and_validate_definition | ||
| from crewai.utilities.types import LLMMessage | ||
|
|
||
|
|
||
| class AgentSpecAgentAdapter(BaseAgentAdapter): | ||
| """ | ||
| Adapter that lets CrewAI import agents defined using Oracle's AgentSpec specification language. | ||
| (https://github.com/oracle/agent-spec.git) | ||
|
|
||
| This adapter wraps around the crewaiagentspecadapter which provides all required | ||
| conversion methods for loading an AgentSpec representation into a CrewAI Agent. | ||
| (https://github.com/oracle/agent-spec/tree/main/adapters/crewaiagentspecadapter) | ||
|
|
||
| When the conversion is done, this adapter delegates required methods to corresponding | ||
| methods of the underlying converted agent. | ||
|
|
||
| Supported features: | ||
| - ReAct-style agents | ||
| - Tools | ||
|
|
||
| Not currently supported: | ||
| - Flows | ||
| - Multi-agent patterns | ||
|
|
||
| Installation: | ||
| 1) git clone https://github.com/oracle/agent-spec.git | ||
| 2) cd agent-spec | ||
| 3) pip install pyagentspec | ||
| 4) pip install adapters/crewaiagentspecadapter | ||
| """ | ||
|
|
||
| _crewai_agent: CrewAIAgent = PrivateAttr() | ||
| function_calling_llm: Any = Field(default=None) | ||
| step_callback: Any = Field(default=None) | ||
|
|
||
| def __init__( | ||
| self, | ||
| agentspec_agent_json: str, | ||
| tool_registry: Optional[Dict[str, Union[Callable, CrewAITool]]] = None, | ||
| **kwargs: Any, | ||
| ): | ||
| agent_spec_loader: type[Any] = import_and_validate_definition( | ||
| "crewai_agentspec_adapter.AgentSpecLoader" | ||
| ) | ||
| loader = agent_spec_loader(tool_registry=tool_registry) | ||
| crewai_agent = loader.load_json(agentspec_agent_json) | ||
|
|
||
| init_kwargs = { | ||
| "role": getattr(crewai_agent, "role", "AgentSpec Agent"), | ||
| "goal": getattr(crewai_agent, "goal", "Execute tasks defined by AgentSpec"), | ||
| "backstory": getattr( | ||
| crewai_agent, "backstory", "Adapter wrapper around AgentSpec-generated CrewAI agent" | ||
| ), | ||
| "llm": getattr(crewai_agent, "llm", None), | ||
| "function_calling_llm": getattr(crewai_agent, "llm", None), | ||
| "tools": getattr(crewai_agent, "tools", None), | ||
| "verbose": getattr(crewai_agent, "verbose", False), | ||
| "max_iter": getattr(crewai_agent, "max_iter", 25), | ||
| } | ||
| init_kwargs.update(kwargs or {}) | ||
| super().__init__(**{k: v for k, v in init_kwargs.items() if v is not None}) | ||
|
|
||
| self.function_calling_llm = getattr(crewai_agent, "llm", None) | ||
| self._crewai_agent = crewai_agent | ||
|
|
||
|
|
||
| # --- Abstract methods of BaseAgentAdapter --- | ||
|
|
||
| def configure_tools(self, tools: list[BaseTool] | None = None) -> None: | ||
| # Nothing to do, tools were already converted by AgentSpecLoader | ||
| pass | ||
|
|
||
| @property | ||
| def last_messages(self) -> list[LLMMessage]: | ||
| return self._crewai_agent.last_messages | ||
|
|
||
|
|
||
| # --- Abstract methods of BaseAgent --- | ||
| # We just delegate to the underlying agent's methods, since it's all already | ||
| # created by AgentSpecLoader (the output is crewai.Agent which is derived from BaseAgent) | ||
|
|
||
| def execute_task( | ||
| self, | ||
| task: Any, | ||
| context: Optional[str] = None, | ||
| tools: Optional[List[Any]] = None, | ||
| ) -> Any: | ||
| return self._crewai_agent.execute_task(task, context=context, tools=tools) | ||
|
|
||
| def create_agent_executor(self, tools: Optional[List[Any]] = None) -> None: | ||
| self._crewai_agent.create_agent_executor(tools=tools) | ||
|
|
||
| def get_delegation_tools(self, agents: List[BaseAgent]) -> List[Any]: | ||
| return self._crewai_agent.get_delegation_tools(agents) | ||
|
|
||
| def get_platform_tools(self, apps: List[Any]) -> List[Any]: | ||
| return self._crewai_agent.get_platform_tools(apps) | ||
|
|
||
| def get_mcp_tools(self, mcps: List[Any]) -> List[Any]: | ||
| return self._crewai_agent.get_mcp_tools(mcps) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Import shadowing causes wrong Agent class usage
The import
from pyagentspec.agent import Agenton line 338 shadows the earlier importfrom crewai import Agenton line 336. Whencode_helper_agent = Agent(...)is created on line 348 withrole,goal, andbackstoryparameters, it will incorrectly usepyagentspec.agent.Agentinstead ofcrewai.Agent. The pyagentspec Agent class likely doesn't accept these parameters, causing a runtime error. The pyagentspecAgentimport needs an alias likeAgentSpecAgent.Additional Locations (1)
docs/en/learn/bring-your-own-agent.mdx#L347-L354