Skip to content

Commit

Permalink
Add agent descriptions to prompts (#44)
Browse files Browse the repository at this point in the history
* Add agent descriptions to prompts

* Update tests data
  • Loading branch information
whimo authored Jun 13, 2024
1 parent 2b00d35 commit 02023b6
Show file tree
Hide file tree
Showing 64 changed files with 90 additions and 60 deletions.
4 changes: 1 addition & 3 deletions examples/delegation_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def main():
# You can give agents as tools to other agents
writer = ReactMotleyAgent(
name="AI writer agent",
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""",
description="You are an experienced writer with a passion for technology.",
tools=[researcher],
verbose=True,
)
Expand Down
4 changes: 1 addition & 3 deletions examples/old/delegation_crewai.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def main():
# You can give agents as tools to other agents
writer = ReactMotleyAgent(
name="AI writer agent",
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""",
description="You are an experienced writer with a passion for technology.",
tools=[researcher],
verbose=True,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/old/single_llama_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main():

# TODO: add LlamaIndex native tools
researcher = ReActLlamaIndexMotleyAgent(
description="Uncover cutting-edge developments in AI and data science",
description="Your goal is to uncover cutting-edge developments in AI and data science",
tools=[search_tool],
verbose=True,
)
Expand Down
17 changes: 7 additions & 10 deletions motleycrew/agents/crewai/crewai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Module description """

from typing import Any, Optional, Sequence

from langchain_core.runnables import RunnableConfig
Expand All @@ -8,7 +9,6 @@
from motleycrew.agents.crewai import CrewAIAgentWithConfig
from motleycrew.common import MotleySupportedTool
from motleycrew.common import MotleyAgentFactory
from motleycrew.common.utils import to_str
from motleycrew.tracking import add_default_callbacks_to_langchain_config
from motleycrew.common.utils import ensure_module_is_installed

Expand All @@ -27,7 +27,7 @@ def __init__(
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
):
""" Description
"""Description
Args:
goal (str):
Expand All @@ -51,7 +51,7 @@ def invoke(
config: Optional[RunnableConfig] = None,
**kwargs: Any,
) -> Any:
""" Description
"""Description
Args:
task_dict (dict):
Expand All @@ -62,10 +62,7 @@ def invoke(
Any:
"""
self.materialize()

prompt = task_dict.get("prompt")
if not prompt:
raise ValueError("Task must have a prompt")
prompt = self.compose_prompt(task_dict, task_dict.get("prompt"))

langchain_tools = [tool.to_langchain_tool() for tool in self.tools.values()]
config = add_default_callbacks_to_langchain_config(config)
Expand All @@ -79,7 +76,7 @@ def invoke(

# TODO: what do these do?
def set_cache_handler(self, cache_handler: Any) -> None:
""" Description
"""Description
Args:
cache_handler (Any):
Expand All @@ -90,7 +87,7 @@ def set_cache_handler(self, cache_handler: Any) -> None:
return self.agent.set_cache_handler(cache_handler)

def set_rpm_controller(self, rpm_controller: Any) -> None:
""" Description
"""Description
Args:
rpm_controller (Any):
Expand All @@ -106,7 +103,7 @@ def from_agent(
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
) -> "CrewAIMotleyAgentParent":
""" Description
"""Description
Args:
agent (CrewAIAgentWithConfig):
Expand Down
16 changes: 5 additions & 11 deletions motleycrew/agents/langchain/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class LangchainMotleyAgent(MotleyAgentParent):
def __init__(
self,
description: str,
description: str | None = None,
name: str | None = None,
agent_factory: MotleyAgentFactory | None = None,
tools: Sequence[MotleySupportedTool] | None = None,
Expand All @@ -35,7 +35,7 @@ def __init__(
"""Description
Args:
description (str):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
agent_factory (:obj:`MotleyAgentFactory`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
Expand Down Expand Up @@ -85,10 +85,7 @@ def invoke(
"""
self.materialize()

prompt = task_dict.get("prompt")
if not prompt:
raise ValueError("Task must have a prompt")
prompt = self.compose_prompt(task_dict, task_dict.get("prompt"))

config = add_default_callbacks_to_langchain_config(config)
if self.with_history:
Expand All @@ -107,10 +104,9 @@ def invoke(
@staticmethod
def from_function(
function: Callable[..., Any],
description: str,
description: str | None = None,
name: str | None = None,
llm: BaseLanguageModel | None = None,
delegation: bool | Sequence[MotleyAgentAbstractParent] = False,
tools: Sequence[MotleySupportedTool] | None = None,
prompt: ChatPromptTemplate | Sequence[ChatPromptTemplate] | None = None,
require_tools: bool = False,
Expand All @@ -121,10 +117,9 @@ def from_function(
Args:
function (Callable):
description (str):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
llm (:obj:`BaseLanguageModel`, optional):
delegation: (:obj:`bool`, :obj:`Sequence[MotleyAgentAbstractParent]`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
prompt (:obj:`ChatPromptTemplate`, :obj:`Sequence[ChatPromptTemplate]`, optional):
require_tools (bool):
Expand All @@ -141,7 +136,6 @@ def from_function(

def agent_factory(tools: dict[str, MotleyTool]):
langchain_tools = [t.to_langchain_tool() for t in tools.values()]
# TODO: feed description into the agent's prompt
agent = function(llm=llm, tools=langchain_tools, prompt=prompt)
agent_executor = AgentExecutor(
agent=agent,
Expand Down
5 changes: 3 additions & 2 deletions motleycrew/agents/langchain/react.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Module description"""

from typing import Sequence

from langchain import hub
Expand All @@ -14,13 +15,13 @@ class ReactMotleyAgent(LangchainMotleyAgent):
def __new__(
cls,
tools: Sequence[MotleySupportedTool],
description: str = "", # gets ignored at the moment
description: str | None = None,
name: str | None = None,
prompt: str | None = None,
llm: BaseLanguageModel | None = None,
verbose: bool = False,
):
""" Description
"""Description
Args:
tools (Sequence[MotleySupportedTool]):
Expand Down
6 changes: 3 additions & 3 deletions motleycrew/agents/langchain/tool_calling_react.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class ReActToolCallingAgent(LangchainMotleyAgent):
def __new__(
cls,
tools: Sequence[MotleySupportedTool],
goal: str = "", # gets ignored at the moment
description: str | None = None,
name: str | None = None,
prompt: ChatPromptTemplate | Sequence[ChatPromptTemplate] | None = None,
with_history: bool = False,
Expand All @@ -231,14 +231,14 @@ def __new__(
Args:
tools (Sequence[MotleySupportedTool]):
goal (:obj:`str`, optional):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
prompt (:obj:ChatPromptTemplate`, :obj:`Sequence[ChatPromptTemplate]', optional):
llm (:obj:`BaseLanguageModel`, optional):
verbose (:obj:`bool`, optional):
"""
return cls.from_function(
description=goal,
description=description,
name=name,
llm=llm,
tools=tools,
Expand Down
24 changes: 10 additions & 14 deletions motleycrew/agents/llama_index/llama_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Module description """

from typing import Any, Optional, Sequence

try:
Expand All @@ -9,8 +10,6 @@
from langchain_core.runnables import RunnableConfig

from motleycrew.agents.parent import MotleyAgentParent
from motleycrew.agents.abstract_parent import MotleyAgentAbstractParent
from motleycrew.tasks import TaskUnit
from motleycrew.common import MotleySupportedTool
from motleycrew.common import MotleyAgentFactory
from motleycrew.common.utils import ensure_module_is_installed
Expand All @@ -19,16 +18,16 @@
class LlamaIndexMotleyAgent(MotleyAgentParent):
def __init__(
self,
description: str,
description: str | None = None,
name: str | None = None,
agent_factory: MotleyAgentFactory | None = None,
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
):
""" Description
"""Description
Args:
description (str):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
agent_factory (:obj:`MotleyAgentFactory`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
Expand All @@ -48,7 +47,7 @@ def invoke(
config: Optional[RunnableConfig] = None,
**kwargs: Any,
) -> Any:
""" Description
"""Description
Args:
task_dict (dict):
Expand All @@ -59,33 +58,30 @@ def invoke(
Any:
"""
self.materialize()

prompt = task_dict.get("prompt")
if not prompt:
raise ValueError("Task must have a prompt")
prompt = self.compose_prompt(task_dict, task_dict.get("prompt"))

output = self.agent.chat(prompt)
return output.response

@staticmethod
def from_agent(
agent: AgentRunner,
goal: str,
description: Optional[str] = None,
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
) -> "LlamaIndexMotleyAgent":
""" Description
"""Description
Args:
agent (AgentRunner):
goal (str):
description (:obj:`str`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
verbose (:obj:`bool`, optional):
Returns:
LlamaIndexMotleyAgent:
"""
ensure_module_is_installed("llama_index")
wrapped_agent = LlamaIndexMotleyAgent(description=goal, tools=tools, verbose=verbose)
wrapped_agent = LlamaIndexMotleyAgent(description=description, tools=tools, verbose=verbose)
wrapped_agent._agent = agent
return wrapped_agent
9 changes: 4 additions & 5 deletions motleycrew/agents/llama_index/llama_index_react.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Module description """

from typing import Sequence

try:
Expand All @@ -9,7 +10,6 @@
LLM = object

from motleycrew.agents.llama_index import LlamaIndexMotleyAgent
from motleycrew.agents.abstract_parent import MotleyAgentAbstractParent
from motleycrew.tools import MotleyTool
from motleycrew.common import MotleySupportedTool
from motleycrew.common import LLMFramework
Expand All @@ -21,16 +21,16 @@
class ReActLlamaIndexMotleyAgent(LlamaIndexMotleyAgent):
def __init__(
self,
description: str,
description: str | None = None,
name: str | None = None,
tools: Sequence[MotleySupportedTool] | None = None,
llm: LLM | None = None,
verbose: bool = False,
):
""" Description
"""Description
Args:
description (str):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
llm (:obj:`LLM`, optional):
Expand All @@ -42,7 +42,6 @@ def __init__(

def agent_factory(tools: dict[str, MotleyTool]):
llama_index_tools = [t.to_llama_index_tool() for t in tools.values()]
# TODO: feed description into the agent's prompt
callbacks = get_default_callbacks_list(LLMFramework.LLAMA_INDEX)
agent = ReActAgent.from_tools(
tools=llama_index_tools,
Expand Down
Loading

0 comments on commit 02023b6

Please sign in to comment.