-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly working output handlers for Langchain & LlamaIndex agents (#54)
* add output handler to llama_index agent * add llama_index_output_handler example * add check llama_index module installed in run_step_decorator * transfer run_step_decorator to the LlamaIndexMotleyAgent class * implementation in a langchain output handler using decorators * refactor add LangchainOutputHandlerMixin * add output_handler for CrewAIMotleyAgentParent * add crewai_output_handler example * changing the way attributes are saved for decorated methods * rename agent_plan_decorator * prepare for merge * Fix integration tests * Minor improvements * add use of output_handler via the agent_finish_blocker tool for the langchain library * update a user's message change * add processing of the final output_handler call * add test langchain output_handler * fix llama_index output_handler * add test llama_index output_handler * update test llama_index output_handler * refactor langchain output handler runs methods decorated * fix llama_index run_step_decorator * elimination of comments * remove _run_and_catch_output * Raise NotImplementedError when using output handlers with CrewAI --------- Co-authored-by: User <[email protected]> Co-authored-by: whimo <[email protected]>
- Loading branch information
1 parent
8a7996c
commit 15f9cfb
Showing
16 changed files
with
695 additions
and
154 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
# Has to be here to make the examples package importable for integration tests |
This file contains 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,75 @@ | ||
from pathlib import Path | ||
import os | ||
import sys | ||
|
||
from dotenv import load_dotenv | ||
|
||
from motleycrew.agents.crewai import CrewAIMotleyAgent | ||
from motleycrew.common import configure_logging | ||
from motleycrew.tasks import SimpleTask | ||
from motleycrew.common.exceptions import InvalidOutput | ||
|
||
from langchain_community.tools import DuckDuckGoSearchRun | ||
from langchain_core.tools import StructuredTool | ||
|
||
WORKING_DIR = Path(os.path.realpath("..")) | ||
|
||
try: | ||
from motleycrew import MotleyCrew | ||
except ImportError: | ||
# if we are running this from source | ||
motleycrew_location = os.path.realpath(WORKING_DIR / "..") | ||
sys.path.append(motleycrew_location) | ||
|
||
|
||
def main(): | ||
crew = MotleyCrew() | ||
|
||
search_tool = DuckDuckGoSearchRun() | ||
|
||
def check_output(output: str): | ||
if "medicine" not in output.lower(): | ||
raise InvalidOutput("Add more information about AI applications in medicine.") | ||
return {"checked_output": output.lower()} | ||
|
||
output_handler = StructuredTool.from_function( | ||
name="output_handler", | ||
description="Output handler", | ||
func=check_output, | ||
) | ||
|
||
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.""", | ||
delegation=False, | ||
output_handler=output_handler, | ||
verbose=True, | ||
tools=[search_tool], | ||
) | ||
|
||
# Create tasks for agent | ||
|
||
analysis_report_task = SimpleTask( | ||
crew=crew, | ||
name="produce comprehensive analysis report on AI advancements", | ||
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, | ||
) | ||
|
||
# Get your crew to work! | ||
result = crew.run() | ||
|
||
# Get the outputs of the task | ||
print(analysis_report_task.output) | ||
return analysis_report_task.output | ||
|
||
|
||
if __name__ == "__main__": | ||
configure_logging(verbose=True) | ||
load_dotenv() | ||
main() |
This file contains 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,65 @@ | ||
from dotenv import load_dotenv | ||
from langchain_community.tools import DuckDuckGoSearchRun | ||
|
||
|
||
from motleycrew import MotleyCrew | ||
from motleycrew.agents.llama_index import ReActLlamaIndexMotleyAgent | ||
from motleycrew.common import configure_logging | ||
from motleycrew.tasks import SimpleTask | ||
from motleycrew.common.exceptions import InvalidOutput | ||
from motleycrew.common import AsyncBackend | ||
|
||
from langchain_core.tools import StructuredTool | ||
|
||
|
||
def main(): | ||
"""Main function of running the example.""" | ||
search_tool = DuckDuckGoSearchRun() | ||
|
||
def check_output(output: str): | ||
if "medicine" not in output.lower(): | ||
raise InvalidOutput( | ||
"Add more information about AI applications in medicine." | ||
) | ||
|
||
return {"checked_output": output} | ||
|
||
output_handler = StructuredTool.from_function( | ||
name="output_handler", | ||
description="Output handler", | ||
func=check_output, | ||
) | ||
|
||
# TODO: add LlamaIndex native tools | ||
researcher = ReActLlamaIndexMotleyAgent( | ||
description="Your goal is to uncover cutting-edge developments in AI and data science", | ||
tools=[search_tool], | ||
output_handler=output_handler, | ||
verbose=True, | ||
max_iterations=16, # default is 10, we add more because the output handler may reject the output | ||
) | ||
|
||
crew = MotleyCrew(async_backend=AsyncBackend.NONE) | ||
|
||
# Create tasks for your agents | ||
task = SimpleTask( | ||
crew=crew, | ||
name="produce comprehensive analysis report on AI advancements", | ||
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, | ||
) | ||
|
||
# Get your crew to work! | ||
crew.run() | ||
|
||
print(task.output) | ||
return task.output | ||
|
||
|
||
if __name__ == "__main__": | ||
configure_logging(verbose=True) | ||
|
||
load_dotenv() | ||
main() |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.