Using an output handler for validating agent’s output — motleycrew documentation (2024)

As no one can guarantee the validity or integrity of a raw LLM output, getting a sensible and/or structured output from an agent is tricky. In motleycrew, we addressed this issue with a novel approach: please welcome output handlers.

As of now, tool calling is the most reliable way to get a structured output from an LLM. So why not have an output handler - a regular tool with an input schema and a description which instructs the agent to return the output only via this tool. Moreover, we can force the agent into doing this: if it attempts to finish the regular way, we intercept the message and remind the agent that it must call the tool instead.

[1]:
from dotenv import load_dotenvfrom langchain_community.tools import DuckDuckGoSearchRunfrom motleycrew import MotleyCrewfrom motleycrew.agents.langchain.tool_calling_react import ReActToolCallingAgentfrom motleycrew.common import configure_loggingfrom motleycrew.tasks import SimpleTaskfrom motleycrew.common.exceptions import InvalidOutputfrom langchain_core.tools import StructuredTool
/Users/whimo/motleycrew/.venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm

We’ll be prompting our agent to create a report on the latest advancements in AI.

Say we want it to also include some info on AI applications in medicine if it did not already. So our output handler will look for the keyword “medicine” in the agent’s output.

Also, we can do any data manipulations we want inside the output handler. In this case, let’s just place the output into a certain field.

[2]:
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,) # Schema is inferred from the function signature, but you can also specify it explicitly

Now let’s create our agent and launch the crew!

[3]:
search_tool = DuckDuckGoSearchRun()researcher = ReActToolCallingAgent( tools=[search_tool], verbose=True, chat_history=True, output_handler=output_handler,)crew = MotleyCrew()task = SimpleTask( crew=crew, name="produce comprehensive analysis report on AI advancements in 2024", 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,)
2024-06-18 18:06:55,525 - motleycrew - WARNING - No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string2024-06-18 18:06:55,530 - motleycrew - WARNING - No known Cypher type matching annotation typing.List[str], will use JSON string
[4]:
crew.run()
2024-06-18 18:07:03,577 - motleycrew - WARNING - No known Cypher type matching annotation typing.List[str], will use JSON string2024-06-18 18:07:03,727 - motleycrew - WARNING - Lunary public key is not set, tracking will be disabledWARNING:langchain_core.tracers.base:Parent run 4ef46a4a-2e8e-4b9d-9c4b-e6a89e0d5598 not found for run 4ca4a208-ac27-4809-a7e8-db55b0ed8338. Treating as a root run.
> Entering new AgentExecutor chain...Invoking: `duckduckgo_search` with `{'query': 'latest advancements in AI 2024'}`In 2024, generative AI might actually become useful for the regular, non-tech person, and we are going to see more people tinkering with a million little AI models. State-of-the-art AI models ... If 2023 was the year the world discovered generative AI (gen AI), 2024 is the year organizations truly began using—and deriving business value from—this new technology.In the latest McKinsey Global Survey on AI, 65 percent of respondents report that their organizations are regularly using gen AI, nearly double the percentage from our previous survey just ten months ago. Now. When OpenAI launched a free web app called ChatGPT in November 2022, nobody knew what was coming. But that low-key release changed everything. By January, ChatGPT had become the fastest ... Here are some important current AI trends to look out for in the coming year. Reality check: more realistic expectations. Multimodal AI. Small (er) language models and open source advancements. GPU shortages and cloud costs. Model optimization is getting more accessible. Customized local models and data pipelines. 2. Open source AI models. In 2024, open source pretrained AI models gain significant traction, empowering businesses to accelerate growth by combining these models with private or real-time data. This synergy enhances productivity and cost-efficiency. IBM actively contributes to open source AI models, which are exemplified by its collaboration ...Invoking: `duckduckgo_search` with `{'query': 'key trends in AI 2024'}`The most important AI trends in 2024. Artificial intelligence Business transformation. February 9, 2024 By Dave Bergmann 12 min read. ... the need to reduce costs and automate key processes and the increasing amount of AI embedded into standard off-the-shelf business applications. Three new surveys of data executives have identified five trends they'll be paying attention to in 2024. Four hot trends to watch out for this year. ... Both have launched web-based tools that allow anyone to become a generative-AI app developer. In 2024, generative AI might actually become useful ... Discover the top 10 machine learning and AI trends for 2024 that are shaping technology and business, including multimodal, open source and customization. Enterprise AI. ... The importance and impact of AI is covered next, followed by information on AI's key benefits and risks, current and potential AI use cases, building a successful AI ... Discover the groundbreaking AI trends of 2024, from quantum advancements to the next generation of generative AI, poised to redefine human-AI collaborations.Invoking: `duckduckgo_search` with `{'query': 'breakthrough AI technologies 2024'}`Generative AI tools like ChatGPT reached mass adoption in record time, and reset the course of an entire industry. ... AI for everything: 10 Breakthrough Technologies 2024. 10 Breakthrough Technologies. Every year, the reporters and editors at MIT Technology Review survey the tech landscape and pick 10 technologies that we think have the greatest potential to change ... If 2023 was the year the world discovered generative AI (gen AI), 2024 is the year organizations truly began using—and deriving business value from—this new technology.In the latest McKinsey Global Survey on AI, 65 percent of respondents report that their organizations are regularly using gen AI, nearly double the percentage from our previous survey just ten months ago. In 2024, generative AI might actually become useful for the regular, non-tech person, and we are going to see more people tinkering with a million little AI models. State-of-the-art AI models ... The editors will also host a live conversation on LinkedIn, on Monday, January 8, 2024, at 2:30 pm ET to walk viewers through some of the technologies and why they were picked. The 2024 list of 10 ...Invoking: `duckduckgo_search` with `{'query': 'AI technology impact on industries 2024'}`If 2023 was the year the world discovered generative AI (gen AI), 2024 is the year organizations truly began using—and deriving business value from—this new technology.In the latest McKinsey Global Survey on AI, 65 percent of respondents report that their organizations are regularly using gen AI, nearly double the percentage from our previous survey just ten months ago. The Duet AI digest. Generative AI is making significant changes across industries, improving access to information, boosting productivity, and enhancing experiences. Don't focus on the next big thing, which will take time to figure out. Relieving pain points for customers and especially workers offers the best ROI now. Here are some important current AI trends to look out for in the coming year. Reality check: more realistic expectations. Multimodal AI. Small (er) language models and open source advancements. GPU shortages and cloud costs. Model optimization is getting more accessible. Customized local models and data pipelines. This unprecedented rise is just one indication of the massive disruption gen AI is poised to unleash on the enterprise software sector. Its impact could lead to a sizeable shift in user segments, value pools, and industry dynamics within and across software categories, presenting software leaders with vast opportunities and significant challenges. Generative AI is significantly accelerating innovation cycles in various industries. For instance, in the technology sector, companies use generative AI to analyze large volumes of data, surface ...Invoking: `output_handler` with `{'output': '**Comprehensive Analysis of AI Advancements in 2024**\n\n**1. Key Trends:**\n- **Generative AI Utilization:** 2024 has seen a significant increase in the practical use of generative AI across various sectors. Organizations are now deriving substantial business value from these technologies, with a notable rise in adoption rates from the previous year.\n- **Realistic Expectations and Multimodal AI:** There is a shift towards more realistic expectations of AI capabilities, coupled with advancements in multimodal AI, which integrates multiple forms of data (e.g., text, image, sound) to enhance learning and decision-making processes.\n- **Open Source AI Models:** The adoption of open source AI models has surged, allowing businesses to integrate these models with private or real-time data, thus enhancing productivity and cost-efficiency.\n\n**2. Breakthrough Technologies:**\n- **Small and Customizable AI Models:** There is a trend towards smaller, more efficient AI models that are easier to train and deploy. These models are increasingly open-source, allowing for greater customization and flexibility in their application.\n- **Advancements in Generative AI:** Generative AI continues to be a focal point of technological breakthroughs, with applications expanding into everyday use for non-technical individuals and becoming integral in business operations.\n\n**3. Industry Impacts:**\n- **Enhanced Productivity and Innovation:** Generative AI is accelerating innovation cycles, particularly in the technology sector, by enabling the analysis of large data volumes and generating insights at unprecedented speeds.\n- **Software Industry Transformation:** The rise of generative AI is reshaping the enterprise software sector, impacting user segments, value pools, and industry dynamics. This shift presents both significant opportunities and challenges for software leaders.\n- **Cost Efficiency and Business Transformation:** AI technologies are increasingly embedded into standard business applications, driving cost reductions and automating key processes across industries.\n\n**Conclusion:**\nThe landscape of AI in 2024 is marked by significant advancements in generative AI, the rise of open source and customizable models, and a pragmatic approach to AI expectations and applications. These developments are transforming industries by enhancing productivity, fostering innovation, and reshaping business operations. As AI continues to evolve, its integration into daily business practices and decision-making processes is expected to deepen, offering substantial benefits and posing new challenges.'}`InvalidOutput: Add more information about AI applications in medicine.Invoking: `duckduckgo_search` with `{'query': 'AI advancements in medicine 2024'}`February 01, 2024. 0. Artificial intelligence (AI) and machine learning (ML) technologies are making waves in healthcare by offering groundbreaking opportunities for medical practices. The US Food ... Artificial Intelligence in Medicine. A.L. Beam and OthersN Engl J Med 2023;388:1220-1221. The editors announce both a series of articles focusing on AI and machine learning in health care and the ... As artificial intelligence goes multimodal, medical applications multiply. Science 381 , adk6139 (2023). Article PubMed Google Scholar Of course, in 2024, we'll also continue to see gen AI experimentation for use cases that require more testing and development, like assimilating information from diverse sources such as medical images, textual clinical reports and voice. Ultimately, this technology will drive a new understanding about health and healthcare. As 2024 begins, healthcare technology leaders shared their projections for the coming year. Yes, many of the projections below center on AI. But healthcare leaders also offer thoughts on the digital health industry, using data more effectively, e-prescribing and other topics. Leaders also offered suggestions on using technology wisely.
2024-06-18 18:09:29,495 - motleycrew - WARNING - No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string
Invoking: `output_handler` with `{'output': '**Comprehensive Analysis of AI Advancements in 2024**\n\n**1. Key Trends:**\n- **Generative AI Utilization:** 2024 has seen a significant increase in the practical use of generative AI across various sectors. Organizations are now deriving substantial business value from these technologies, with a notable rise in adoption rates from the previous year.\n- **Realistic Expectations and Multimodal AI:** There is a shift towards more realistic expectations of AI capabilities, coupled with advancements in multimodal AI, which integrates multiple forms of data (e.g., text, image, sound) to enhance learning and decision-making processes.\n- **Open Source AI Models:** The adoption of open source AI models has surged, allowing businesses to integrate these models with private or real-time data, thus enhancing productivity and cost-efficiency.\n\n**2. Breakthrough Technologies:**\n- **Small and Customizable AI Models:** There is a trend towards smaller, more efficient AI models that are easier to train and deploy. These models are increasingly open-source, allowing for greater customization and flexibility in their application.\n- **Advancements in Generative AI:** Generative AI continues to be a focal point of technological breakthroughs, with applications expanding into everyday use for non-technical individuals and becoming integral in business operations.\n\n**3. Industry Impacts:**\n- **Enhanced Productivity and Innovation:** Generative AI is accelerating innovation cycles, particularly in the technology sector, by enabling the analysis of large data volumes and generating insights at unprecedented speeds.\n- **Software Industry Transformation:** The rise of generative AI is reshaping the enterprise software sector, impacting user segments, value pools, and industry dynamics. This shift presents both significant opportunities and challenges for software leaders.\n- **Cost Efficiency and Business Transformation:** AI technologies are increasingly embedded into standard business applications, driving cost reductions and automating key processes across industries.\n\n**4. AI in Medicine:**\n- **Multimodal Applications in Healthcare:** AI is increasingly going multimodal in medical applications, integrating data from medical images, textual clinical reports, and voice to enhance diagnostics and treatment planning. This integration is expected to drive new understandings in health and healthcare, particularly in areas requiring extensive data analysis.\n\n**Conclusion:**\nThe landscape of AI in 2024 is marked by significant advancements in generative AI, the rise of open source and customizable models, and a pragmatic approach to AI expectations and applications. These developments are transforming industries by enhancing productivity, fostering innovation, and reshaping business operations. As AI continues to evolve, its integration into daily business practices and decision-making processes is expected to deepen, offering substantial benefits and posing new challenges.'}`
[4]:
[TaskUnit(status=done)]

As you can see in the logs, our medicine requirement was successfully fed back to the agent!

[6]:
from IPython.display import display, Markdownfinal_result = task.output["checked_output"]display(Markdown(final_result))

Comprehensive Analysis of AI Advancements in 2024

1. Key Trends: - Generative AI Utilization: 2024 has seen a significant increase in the practical use of generative AI across various sectors. Organizations are now deriving substantial business value from these technologies, with a notable rise in adoption rates from the previous year. - Realistic Expectations and Multimodal AI: There is a shift towards more realistic expectations of AI capabilities, coupled with advancements in multimodal AI, which integrates multiple forms of data(e.g., text, image, sound) to enhance learning and decision-making processes. - Open Source AI Models: The adoption of open source AI models has surged, allowing businesses to integrate these models with private or real-time data, thus enhancing productivity and cost-efficiency.

2. Breakthrough Technologies: - Small and Customizable AI Models: There is a trend towards smaller, more efficient AI models that are easier to train and deploy. These models are increasingly open-source, allowing for greater customization and flexibility in their application. - Advancements in Generative AI: Generative AI continues to be a focal point of technological breakthroughs, with applications expanding into everyday use for non-technical individuals and becoming integral inbusiness operations.

3. Industry Impacts: - Enhanced Productivity and Innovation: Generative AI is accelerating innovation cycles, particularly in the technology sector, by enabling the analysis of large data volumes and generating insights at unprecedented speeds. - Software Industry Transformation: The rise of generative AI is reshaping the enterprise software sector, impacting user segments, value pools, and industry dynamics. This shift presents both significant opportunities and challenges forsoftware leaders. - Cost Efficiency and Business Transformation: AI technologies are increasingly embedded into standard business applications, driving cost reductions and automating key processes across industries.

4. AI in Medicine: - Multimodal Applications in Healthcare: AI is increasingly going multimodal in medical applications, integrating data from medical images, textual clinical reports, and voice to enhance diagnostics and treatment planning. This integration is expected to drive new understandings in health and healthcare, particularly in areas requiring extensive data analysis.

Conclusion: The landscape of AI in 2024 is marked by significant advancements in generative AI, the rise of open source and customizable models, and a pragmatic approach to AI expectations and applications. These developments are transforming industries by enhancing productivity, fostering innovation, and reshaping business operations. As AI continues to evolve, its integration into daily business practices and decision-making processes is expected to deepen, offering substantial benefitsand posing new challenges.

Using an output handler for validating agent’s output — motleycrew  documentation (2024)

References

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6589

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.