Skip to content

Commit

Permalink
Separate agent description and prompt_prefix (#57)
Browse files Browse the repository at this point in the history
* add prompt_prefix for MotleyAgentParent

* add prompt_prefix for crewai agents

* add prompt_prefix for langchain agents

* add prompt_prefix for llama_index agents

* change sequence of the prompt_prefix argument

* add prompt_prefix for ReactToolCallingAgent

* update tests agents

* update delegation_demo example

* update delegation_demo example db_path for integration tests

* update single_llama_index example

* update integration tests cache

* update single_llama_index integration test data

* update blog with images.ipynb and update integration test cache

* update math via python code with a single agent.ipynb and update integration test cache

* removing unnecessary changes

* update run_integration_tests

* update cache for Math via python code with a single agent.ipynb run_integration_tests

* update old examples

* update examples

* update CrewAIMotleyAgent set prompt_prefix default None

* replace backstory to prompt_prefix for examples

* update cache for delegation_demo integration_test

* update cache for blog_with_images_ipynb integration_test

* update cache for math_via_python_code_with_a_single_agent_ipynb integration_test

---------

Co-authored-by: User <[email protected]>
  • Loading branch information
BespalovSergey and User committed Jul 3, 2024
1 parent 7ed839b commit e4b9fd3
Show file tree
Hide file tree
Showing 137 changed files with 94 additions and 45 deletions.
4 changes: 2 additions & 2 deletions examples/Advanced output handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -558,7 +558,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions examples/Blog with Images.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"id": "16b8ccc4-b34b-47c3-94ee-cbd82ef4c8f2",
"metadata": {},
"source": [
"We use this example of creating a web search-based blog with illustrations to show how to \n\n",
"We use this example of creating a web search-based blog with illustrations to show how to \n",
"\n",
"* instantiate one agent each from Langchain, CrewAI, and LlamaIndex\n",
"* give agents as tools to other agents\n",
"* create tasks and link them using the >> operator"
Expand Down Expand Up @@ -115,7 +116,7 @@
"# You can give agents as tools to other agents\n",
"writer = ReActMotleyAgent(\n",
" name=\"AI writer agent\",\n",
" description=\"\"\"Conduct a comprehensive analysis of the latest advancements in AI in 2024.\n",
" prompt_prefix=\"\"\"Conduct a comprehensive analysis of the latest advancements in AI in 2024.\n",
" Identify key trends, breakthrough technologies, and potential industry impacts.\n",
" Your final answer MUST be a full analysis report\"\"\",\n",
" tools=[researcher],\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/Caching and observability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"\n",
"writer = ReActMotleyAgent(\n",
" name=\"writer\",\n",
" description=\"Using the results of a web search, write an article on the latest advancements in AI in 2024.\",\n",
" prompt_prefix=\"Using the results of a web search, write an article on the latest advancements in AI in 2024.\",\n",
" tools=[duckduckgo_search],\n",
")\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/Validating agent output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -259,7 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.8.10"
}
},
"nbformat": 4,
Expand Down
22 changes: 14 additions & 8 deletions examples/delegation_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
motleycrew_location = os.path.realpath(WORKING_DIR / "..")
sys.path.append(motleycrew_location)

if "Dropbox" in WORKING_DIR.parts and platform.system() == "Windows":
# On Windows, kuzu has file locking issues with Dropbox
DB_PATH = os.path.realpath(os.path.expanduser("~") + "/Documents/research_db")
if __name__ == '__main__':
if "Dropbox" in WORKING_DIR.parts and platform.system() == "Windows":
# On Windows, kuzu has file locking issues with Dropbox
DB_PATH = os.path.realpath(os.path.expanduser("~") + "/Documents/research_db")
else:
DB_PATH = os.path.realpath(WORKING_DIR / "research_db")

else:
DB_PATH = os.path.realpath(WORKING_DIR / "research_db")
DB_PATH = os.path.realpath(WORKING_DIR / "tests/research_db")


def main():
Expand All @@ -43,25 +47,27 @@ def main():
researcher = CrewAIMotleyAgent(
role="Senior Research Analyst",
goal="Uncover cutting-edge developments in AI and data science, doing web search if necessary",

backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting actionable insights.""",
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting actionable insights.""",
verbose=True,
tools=[search_tool],
)

# You can give agents as tools to other agents
writer = ReActMotleyAgent(
name="AI writer agent",
description="You are an experienced writer with a passion for technology.",
prompt_prefix="You are an experienced writer with a passion for technology.",
description="Experienced writer with a passion for technology.",
tools=[researcher],
verbose=True,
)

# Illustrator
illustrator = ReActLlamaIndexMotleyAgent(
name="Illustrator",
description="Create beautiful and insightful illustrations for a blog post",
prompt_prefix="Create beautiful and insightful illustrations for a blog post",
tools=[DallEImageGeneratorTool(os.path.realpath("./images"))],
)

Expand Down
2 changes: 1 addition & 1 deletion examples/llama_index_output_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def check_output(output: str):

# TODO: add LlamaIndex native tools
researcher = ReActLlamaIndexMotleyAgent(
description="Your goal is to uncover cutting-edge developments in AI and data science",
prompt_prefix="Your goal is to uncover cutting-edge developments in AI and data science",
tools=[search_tool],
output_handler=output_handler,
verbose=True,
Expand Down
4 changes: 2 additions & 2 deletions examples/old/delegation_crewai.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def main():
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting actionable insights.""",
description="Tool Research Analysis",
delegation=False,
verbose=True,
tools=[search_tool],
Expand Down Expand Up @@ -77,7 +78,7 @@ def main():
agent=writer,
)

[analysis_report_task, literature_summary_task] >> blog_post_task
# [analysis_report_task, literature_summary_task] >> blog_post_task

# Get your crew to work!
result = crew.run()
Expand All @@ -89,6 +90,5 @@ def main():

if __name__ == "__main__":
configure_logging(verbose=True)

load_dotenv()
main()
2 changes: 1 addition & 1 deletion examples/old/single_llama_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def main():

# TODO: add LlamaIndex native tools
researcher = ReActLlamaIndexMotleyAgent(
description="Your goal is to uncover cutting-edge developments in AI and data science",
prompt_prefix="Your goal is to uncover cutting-edge developments in AI and data science",
tools=[search_tool],
verbose=True,
)
Expand Down
9 changes: 8 additions & 1 deletion motleycrew/agents/crewai/crewai.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class CrewAIMotleyAgentParent(MotleyAgentParent):
def __init__(
self,
goal: str,
prompt_prefix: str | None = None,
description: str | None = None,
name: str | None = None,
agent_factory: MotleyAgentFactory[CrewAIAgentWithConfig] | None = None,
tools: Sequence[MotleySupportedTool] | None = None,
Expand All @@ -32,6 +34,8 @@ def __init__(
Args:
goal (str):
prompt_prefix (:obj:`str`, optional):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
agent_factory (:obj:`MotleyAgentFactory`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional:
Expand All @@ -46,7 +50,8 @@ def __init__(

ensure_module_is_installed("crewai")
super().__init__(
description=goal,
prompt_prefix=prompt_prefix,
description=description or goal,
name=name,
agent_factory=agent_factory,
tools=tools,
Expand Down Expand Up @@ -132,6 +137,8 @@ def from_agent(

wrapped_agent = CrewAIMotleyAgentParent(
goal=agent.goal,
prompt_prefix=agent.prompt_prefix,
description=agent.description,
name=agent.role,
tools=tools,
verbose=verbose,
Expand Down
25 changes: 15 additions & 10 deletions motleycrew/agents/crewai/crewai_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@
from motleycrew.common import MotleySupportedTool
from motleycrew.common import LLMFramework
from motleycrew.common.llms import init_llm
from motleycrew.agents.abstract_parent import MotleyAgentAbstractParent
from motleycrew.agents.crewai import CrewAIMotleyAgentParent
from motleycrew.agents.crewai import CrewAIAgentWithConfig


class CrewAIMotleyAgent(CrewAIMotleyAgentParent):
def __init__(
self,
role: str,
goal: str,
backstory: str,
delegation: bool = False,
tools: Sequence[MotleySupportedTool] | None = None,
llm: Optional[Any] = None,
output_handler: MotleySupportedTool | None = None,
verbose: bool = False,
self,
role: str,
goal: str,
backstory: str,
prompt_prefix: str | None = None,
description: str | None = None,
delegation: bool = False,
tools: Sequence[MotleySupportedTool] | None = None,
llm: Optional[Any] = None,
output_handler: MotleySupportedTool | None = None,
verbose: bool = False,
):
""" Description
Args:
role (str):
goal (str):
backstory (str):
prompt_prefix (str):
description (str, optional):
delegation (bool):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
llm (:obj:'Any', optional):
Expand Down Expand Up @@ -60,6 +63,8 @@ def agent_factory(tools: dict[str, MotleyTool]):

super().__init__(
goal=goal,
prompt_prefix=prompt_prefix,
description=description,
name=role,
agent_factory=agent_factory,
tools=tools,
Expand Down
9 changes: 8 additions & 1 deletion motleycrew/agents/langchain/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class LangchainMotleyAgent(MotleyAgentParent, LangchainOutputHandlingAgentMixin):
def __init__(
self,
prompt_prefix: str | None = None,
description: str | None = None,
name: str | None = None,
agent_factory: MotleyAgentFactory[AgentExecutor] | None = None,
Expand All @@ -28,6 +29,7 @@ def __init__(
"""Description
Args:
prompt_prefix (:obj:`str`, optional):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
agent_factory (:obj:`MotleyAgentFactory`, optional):
Expand All @@ -40,6 +42,7 @@ def __init__(
See Langchain `RunnableWithMessageHistory` get_session_history param for more details.
"""
super().__init__(
prompt_prefix=prompt_prefix,
description=description,
name=name,
agent_factory=agent_factory,
Expand Down Expand Up @@ -139,6 +142,7 @@ def invoke(
def from_agent(
agent: AgentExecutor,
goal: str,
description: str | None = None,
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
) -> "LangchainMotleyAgent":
Expand All @@ -147,6 +151,7 @@ def from_agent(
Args:
agent (AgentExecutor):
goal (str):
description (:obj:`str`, optional)
tools(:obj:`Sequence[MotleySupportedTool]`, optional):
verbose (bool):
Expand All @@ -160,6 +165,8 @@ def from_agent(
if tools or agent.tools:
tools = list(tools or []) + list(agent.tools or [])

wrapped_agent = LangchainMotleyAgent(description=goal, tools=tools, verbose=verbose)
wrapped_agent = LangchainMotleyAgent(
prompt_prefix=goal, description=description, tools=tools, verbose=verbose
)
wrapped_agent._agent = agent
return wrapped_agent
3 changes: 3 additions & 0 deletions motleycrew/agents/langchain/react.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ReActMotleyAgent(LangchainMotleyAgent):
def __init__(
self,
tools: Sequence[MotleySupportedTool],
prompt_prefix: str | None = None,
description: str | None = None,
name: str | None = None,
prompt: str | None = None,
Expand All @@ -41,6 +42,7 @@ def __init__(
Args:
tools (Sequence[MotleySupportedTool]):
prompt_prefix (:obj:`str`, optional):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
prompt (:obj:`str`, optional): Prompt to use. If not provided, uses hwchase17/react.
Expand Down Expand Up @@ -88,6 +90,7 @@ def agent_factory(
return agent_executor

super().__init__(
prompt_prefix=prompt_prefix,
description=description,
name=name,
agent_factory=agent_factory,
Expand Down
2 changes: 2 additions & 0 deletions motleycrew/agents/langchain/tool_calling_react.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class ReActToolCallingAgent(LangchainMotleyAgent):
def __init__(
self,
tools: Sequence[MotleySupportedTool],
prompt_prefix: str | None = None,
description: str | None = None,
name: str | None = None,
think_prompt: ChatPromptTemplate | None = None,
Expand Down Expand Up @@ -319,6 +320,7 @@ def agent_factory(
return agent_executor

super().__init__(
prompt_prefix=prompt_prefix,
description=description,
name=name,
agent_factory=agent_factory,
Expand Down
10 changes: 9 additions & 1 deletion motleycrew/agents/llama_index/llama_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LlamaIndexMotleyAgent(MotleyAgentParent):

def __init__(
self,
prompt_prefix: str | None = None,
description: str | None = None,
name: str | None = None,
agent_factory: MotleyAgentFactory[AgentRunner] | None = None,
Expand All @@ -37,6 +38,7 @@ def __init__(
"""Description
Args:
prompt_prefix (:obj:`str`, optional):
description (:obj:`str`, optional):
name (:obj:`str`, optional):
agent_factory (:obj:`MotleyAgentFactory`, optional):
Expand All @@ -45,6 +47,7 @@ def __init__(
"""
super().__init__(
description=description,
prompt_prefix=prompt_prefix,
name=name,
agent_factory=agent_factory,
tools=tools,
Expand All @@ -55,6 +58,7 @@ def __init__(
def run_step_decorator(self):
"""Decorator for inclusion in the call chain of the agent, the output handler tool"""
ensure_module_is_installed("llama_index")

def decorator(func):
def wrapper(
task_id: str,
Expand Down Expand Up @@ -133,6 +137,7 @@ def invoke(
def from_agent(
agent: AgentRunner,
description: Optional[str] = None,
prompt_prefix: Optional[str] = None,
tools: Sequence[MotleySupportedTool] | None = None,
verbose: bool = False,
) -> "LlamaIndexMotleyAgent":
Expand All @@ -141,13 +146,16 @@ def from_agent(
Args:
agent (AgentRunner):
description (:obj:`str`, optional):
prompt_prefix (:obj:`str`, optional):
tools (:obj:`Sequence[MotleySupportedTool]`, optional):
verbose (:obj:`bool`, optional):
Returns:
LlamaIndexMotleyAgent:
"""
ensure_module_is_installed("llama_index")
wrapped_agent = LlamaIndexMotleyAgent(description=description, tools=tools, verbose=verbose)
wrapped_agent = LlamaIndexMotleyAgent(
description=description, prompt_prefix=prompt_prefix, tools=tools, verbose=verbose
)
wrapped_agent._agent = agent
return wrapped_agent
Loading

0 comments on commit e4b9fd3

Please sign in to comment.