Building agentic LLM application Workflows with LlamaIndex

LlamaIndex · Beginner ·🛠️ AI Tools & Apps ·1y ago

Key Takeaways

The video tutorial covers building agentic LLM application workflows with LlamaIndex, introducing the Workflows feature, and demonstrating how to define workflow classes, use custom event classes, and visualize workflow execution.

Full Transcript

hi everybody I'm lri VP of developer relations at llama index and Welcome to our YouTube channel today I'm going to be walking through workflows our brand new abstraction for building complex agentic applications I'm going to be walking you through our notebook which shows you all of the steps in building a workflow and all of the things that you can do with workflows first we have to install our dependencies we just have three llama index core which is the important one we're also installing llama index open AI because we're doing one thing with llms and we're installing llama index uil's workflow which is what contains our visualization tools that I'll be demonstrating later once we've installed our depths we bring them in as Imports having done that we bring in our open AI key from The notebook's Secret store because we need it to be able to instantiate the llm now let's look at the most basic possible workflow it just starts it does one thing and then it stops there's no reason to have a real workflow that's this simple uh this is just demonstrating the most basic possible workflow so you can see how they're defined so most workflows are going to be defined as a class the class can have any name so in this case it's called open AI generator and it inherits from the workflow class within your workflow you can have as many steps as you want want each is defined by a decorator called Step once you've uh thrown in your decorator you can define an arbitrary function name and you use type declarations to Define what types of events it accepts and what types of events it emits in this case it's accepting a start event which is a special kind of event and a stop event which is another special kind of event the start event is how the uh workflow orchestrator knows where your uh workflows should start the start you can have as many as you want but it all starting events will go to all the functions that have uh that accept start events so in our generate function we're just setting up our llm we're calling GPT 40 uh and we're using it to complete our query uh our query we're pulling out of our event as you're going to see uh you can attach arbitrary data uh to your events and uh in this case we've decided to pass a query once we've got a response from the llm we call stop event and we pass back our response as a string now that we've defined our workflow class we can execute it so first we instantiate our open AI generator and you have two parameters here one is your timeout which is how long it will run before halting it's important to give your give yourself enough time uh to actually execute whatever your workflow is going to do uh and verbos which we're going to have false at the moment but we'll see what that looks like later once you're once it's instantiated you call run on your workflow uh we're awaiting it here inside of a notebook that works if you were running this in vanilla python you would need to wrap this function in an async function in order for it to work we pass the query whats L index and we print the result and it gives us this response from G describing what llama index is one of the neat things about workflows is that you can visualize them we use p is to do this so we call draw all possible flows uh and we call it with the class name and what file name the output should go to and it will generate an interactive draggable diagram uh that shows all the possible flows within your workflow I've taken a screenshot of it here you can see that there's the start event which goes to the generate function which just goes to the stop event and then it's done there's not a lot to see here yet because we haven't made a complicated workflow so let's go to a more interesting example this one demonstrating our ability to to loop we're introducing a few new things here one of them is custom classes you can create Uh custom event classes to pass around your workflow you can create as many of them as you like you can give them arbitrary names and you can attach arbitrary metadata to each one in this case we've got a failed event with an error and a query event with a query in the loop example we've defined two steps one is the answer query step it can accept more than one kind of event this is the other new thing it can accept a start event or a query event and it can omit a failed event or a stop event because llms are so good these days and needed this thing to fail sometimes I'm using a random number generator to simulate failures in this case if my random number between uh is zero then I say emit a failed event and if my random number is one then I emit a stop event simulating a success our second step is improve query so if answer query emits a failed event then improve query will pick up that failed event it can either emit a query event or a stop event if the random number is zero then it will emit a query event simulating a an llm improving your query for instance if your query failed because it was too vague you could use an llm to improve your query and resubmit it uh or if it couldn't figure out how to improve the query the stop event gets emitted with that result let's draw all the possible workflows there you can see our start event goes to answer query which can either go to a stop event if it succeeds the first time or it can emit a failed event which goes to improve query improve query can also fail it can go to stop event or it can emit a query event which goes back to answer query this is that Loop that I was talking about and it can branch and you can see how you could in a complex workflow put together a whole bunch of these to manage your work manage your data moving uh around your flow let's execute this now this time when I instantiate it I'm going to set verbose equals true which means it's going to Output what steps are getting executed it's running an it's runs the step answer query which produces a failed event because it produces a failed event improved query gets run in this case improv query produces a query event which goes back to answer query answer query again proves a fail uh produces a failed event which goes back to improved query which produces another query event which goes back to answer query which this time produces a success and gives you the answer to your query so now we've done looping and branching let's talk about State uh as a convenience we have a global state within your workflow this is super useful for for instance loading data and then making it available to all of your functions in this case I'm simulating loading data by creating a a database variable with some values in it I'm doing something new here which is I'm calling the step decorator with pass context equals true this means that the context a variable of type context will get passed to the function you can attach arbitrary data to your uh context using the context. DAT object uh in this case I'm setting up this fake database and then returning a query event the query event will get picked up by our query function because it says that it accepts query events and in this case it's pulling right back out of the context data the value of some database which it can then output we run this with verbos equals true and you can see that it works exactly as we described it runs setup produces a query event it runs query produces a stop event which gives us our result of course this flow is still essentially a a linear flow a more realistic example would be if your start event could either be a query or a data population event and you needed to wait for your data to populate before you could start answering queries so let's set that up and see what it looks like here's our weight example flow class again we're passing our context and in this case if the incoming start event has a data attribute then we attach it to our uh fake database variable uh now our query accepts a start event if it sees a query attribute attached to the incoming start event and it goes and looks and sees if there's any data in the context data and if it does it returns a stop event with the data if it doesn't it simply returns none and does nothing let's run this the first time we run we're going to run with a query the result that we get is none so it says no you can't because we haven't loaded any data yet the second time we run we attach some data saying yes you can and you can see that the query step gets run but there was no event produced by it because there was no query attached to it what we attached to it was data but the setup step did uh run and set up our uh pulled our data into the context so now the third time when we run it and we ask it it we ask if we can kick it uh we get the data yes we can because query gets run and produces a stop event let's visualize how this flow Works our start event can go to either setup or query and both of them produce stop events under different circumstances waiting for incoming data like this is a very common pattern so our context object has a convenience function called collect events this will capture events and store them and it will return none until all of the events that it has been specified to to collect have been collected those events will then be attached to the output of collect events in the order that they were specified let's see what that looks like in practice in our collect example flow we have three types of of custom events an input event a setup event and a query event and we have three functions set up collect input and parse query Each of which accepts a start event the setup event uh runs only the first time and it uh sets up some data in the context and then it emits a setup event the collect input function will run uh if it sees an input attribute on the input on the incoming event if it does it emits an input event the parse query event simulates a query if it sees a query attribute then it outputs uh a query event which will then get uh then all three of these will get collected uh by run query runquery we've specified accepts an input event or a setup event or a query event and the first line calls Collective events and specifies that we want either that we want a query event an input event and a setup event if if uh all three events haven't happened yet then what ready will return is none so this function prints not enough events yet until it has seen all three events happen if it's seen all three events happen however it says now I have all the events and shows us the output of what ready is so let's see this running first we call run and we can see that the setup event runs but uh run query says not enough events yet then we run with some input and you can see the input function runs but still not enough events yet and then the third time we run with a query and it says I've got a query and now it runs it says I have all the events and it shows us what's in our the output already the important thing to notice here is that the output of ready has these events in the same in order that they were specified so in collect events I said give me a query event an input event and then a setup event and you can see here a query event an input event and then a setup event we can also visualize this so we can see a start event going to parse query collect input and setup in all three cases you can see them emitting their specific type of event and those events all going to run query which is what calls the stop event another thing you might want to do with collect events is to monitor failures in this case we're calling a failure example flow which tries to do a thing in this case generate a random number less than five uh sorry greater than five um if it's greater than five there was a success and it says and it calls a stop event saying that there was success if not it emits a failure event the monitor failure uh function accepts failure events and then it has a collect events call which is waiting for three separate failure events if there haven't been three failure events uh sorry if there have been three failure events then failed will be not none uh and it will print that the same simulation has failed and admit a failure uh result to the stop event so now let's call this six times in a row see what happens first there was a failure so it returns none then there was a success so it returns success then there was a success so it returns a success then there was a success so it returns a success then there was a second failure so it returns none then there's a third failure and now it says that the simulation has failed and you can see that our failure events are different and it has collected all three sep separately the visualization here is again pretty simple you can see it's calling try to do thing it can go to stop event if it succeeds or it can emit a failure event which goes to monitor failures which also goes to stop event the final thing I'm going to demonstrate is stepping through a workflow step by step this is really useful for debugging I've instantiated a new copy of failure example flow here with verbos equals true and instead of calling run I'm going to call run step you can see that it runs the step trying to do thing and that there was a failure and that it produced a failure event then it waits until I call run step again then it calls monitor failures because that failure event was waiting for me to run steps and then monitor failures produces a stop event we can visualize this using draw most recent execution in this case it calls the instance of the class rather than the class name and we give it a file name again it puts this file which says there was a start event which went to try to do thing which went to fail which generated a failure event which went to monitor failures you can see how this would be useful for debugging stepbystep a complicated workflow that concludes our tour of creating running and visualizing workflows feel free to check out the show notes with dots examples and the link to this notebook thanks for listening and I'll see you next time bye

Original Description

This video is a tutorial on LlamaIndex's new Workflows feature, presented by Laurie, head of Developer Relations. This video covers: - Introduction to Workflows: A new abstraction for building complex, agentic applications. - Basic Workflow Structure: Workflows are defined as classes inheriting from the Workflow class. Steps in a workflow are defined using the @step decorator. Each step can accept and emit different types of events. - Looping and Branching: You can create loops and branches in workflows. You can use custom event classes for passing data between steps. - State Management: how to use global state within workflows using the context object, and how to pass and access data between different steps. - Event Collection: the collect_events function can be used for gathering multiple events before proceeding. You can use this for synchronizing different parts of a workflow. - Error Handling: how to monitor and respond to failure events in a workflow. - Visualization: how to visualize workflows using PyViz. You can create diagrams of all possible flows and the most recent execution. - Debugging: the run_step method can be used for stepping through workflows one step at a time. This can be useful for debugging complex workflows. Links: The notebook: https://colab.research.google.com/drive/1GhF8uBC2LrnYf195CcTe_e5K8Ai6Z4ta Workflow docs: https://docs.llamaindex.ai/en/stable/module_guides/workflow/ Workflow examples: https://docs.llamaindex.ai/en/stable/examples/workflow/function_calling_agent/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LlamaIndex · LlamaIndex · 0 of 60

← Previous Next →
1 LlamaIndex Virtual Meetup (May 4th, 2023)
LlamaIndex Virtual Meetup (May 4th, 2023)
LlamaIndex
2 LlamaIndex + MongoDB Workshop/Fireside Chat
LlamaIndex + MongoDB Workshop/Fireside Chat
LlamaIndex
3 Discover LlamaIndex: Ask Complex Queries over Multiple Documents
Discover LlamaIndex: Ask Complex Queries over Multiple Documents
LlamaIndex
4 Discover LlamaIndex: Document Management
Discover LlamaIndex: Document Management
LlamaIndex
5 Discover LlamaIndex: Joint Text to SQL and Semantic Search
Discover LlamaIndex: Joint Text to SQL and Semantic Search
LlamaIndex
6 Discover LlamaIndex: JSON Query Engine
Discover LlamaIndex: JSON Query Engine
LlamaIndex
7 LlamaIndex Webinar: Active Retrieval Augmented Generation
LlamaIndex Webinar: Active Retrieval Augmented Generation
LlamaIndex
8 LlamaIndex Webinar: Demonstrate-Search-Predict (DSP) with Omar Khattab
LlamaIndex Webinar: Demonstrate-Search-Predict (DSP) with Omar Khattab
LlamaIndex
9 LlamaIndex Sessions: Practical challenges of building a Legal Chatbot over your PDFs
LlamaIndex Sessions: Practical challenges of building a Legal Chatbot over your PDFs
LlamaIndex
10 LlamaIndex Webinar: Graph Databases, Knowledge Graphs, and RAG with Wey (NebulaGraph)
LlamaIndex Webinar: Graph Databases, Knowledge Graphs, and RAG with Wey (NebulaGraph)
LlamaIndex
11 LlamaIndex Webinar: Community Project Showcase (07/07/2023)
LlamaIndex Webinar: Community Project Showcase (07/07/2023)
LlamaIndex
12 LlamaIndex Webinar: LLMs for Investment Research (with Didier Lopes, co-founder/CEO at OpenBB)
LlamaIndex Webinar: LLMs for Investment Research (with Didier Lopes, co-founder/CEO at OpenBB)
LlamaIndex
13 Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 1, LLMs and Prompts)
Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 1, LLMs and Prompts)
LlamaIndex
14 Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 2, Documents and Metadata)
Discover LlamaIndex: Bottoms-Up Development With LLMs (Part 2, Documents and Metadata)
LlamaIndex
15 Discover LlamaIndex: Key Components to build QA Systems
Discover LlamaIndex: Key Components to build QA Systems
LlamaIndex
16 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 3, Evaluation)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 3, Evaluation)
LlamaIndex
17 LlamaIndex Webinar: From Prompt to Schema Engineering with Pydantic  (with @jxnlco)
LlamaIndex Webinar: From Prompt to Schema Engineering with Pydantic (with @jxnlco)
LlamaIndex
18 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 4, Embeddings)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 4, Embeddings)
LlamaIndex
19 Discover LlamaIndex: Custom Retrievers + Hybrid Search
Discover LlamaIndex: Custom Retrievers + Hybrid Search
LlamaIndex
20 LlamaIndex Webinar: Document Metadata and Local Models for Better, Faster Retrieval
LlamaIndex Webinar: Document Metadata and Local Models for Better, Faster Retrieval
LlamaIndex
21 LlamaIndex Webinar: Build Personalized AI Characters with RealChar
LlamaIndex Webinar: Build Personalized AI Characters with RealChar
LlamaIndex
22 LlamaIndex Webinar: Make RAG Production-Ready
LlamaIndex Webinar: Make RAG Production-Ready
LlamaIndex
23 LlamaIndex Workshop: Building RAG with Knowledge Graphs
LlamaIndex Workshop: Building RAG with Knowledge Graphs
LlamaIndex
24 Discover LlamaIndex: Introduction to Data Agents for Developers
Discover LlamaIndex: Introduction to Data Agents for Developers
LlamaIndex
25 LlamaIndex Webinar: Finetuning + RAG
LlamaIndex Webinar: Finetuning + RAG
LlamaIndex
26 Discover LlamaIndex: SEC Insights, End-to-End Guide
Discover LlamaIndex: SEC Insights, End-to-End Guide
LlamaIndex
27 Discover LlamaIndex: Custom Tools for Data Agents
Discover LlamaIndex: Custom Tools for Data Agents
LlamaIndex
28 LlamaIndex Sessions: Building a Lending Criteria Chatbot in Production
LlamaIndex Sessions: Building a Lending Criteria Chatbot in Production
LlamaIndex
29 Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 5, Retrievers + Node Postprocessors)
Discover LlamaIndex: Bottoms-Up Development with LLMs (Part 5, Retrievers + Node Postprocessors)
LlamaIndex
30 LlamaIndex Webinar: How to Win a LLM Hackathon
LlamaIndex Webinar: How to Win a LLM Hackathon
LlamaIndex
31 LlamaIndex Webinar: LLM Challenges in Production (w/ Mayo Oshin, AI Jason, Dylan from Starmorph)
LlamaIndex Webinar: LLM Challenges in Production (w/ Mayo Oshin, AI Jason, Dylan from Starmorph)
LlamaIndex
32 LlamaIndex Webinar: Agents Showcase!
LlamaIndex Webinar: Agents Showcase!
LlamaIndex
33 LlamaIndex Webinar: Learn about DSPy
LlamaIndex Webinar: Learn about DSPy
LlamaIndex
34 LlamaIndex Webinar: Time-based retrieval for RAG (with Timescale)
LlamaIndex Webinar: Time-based retrieval for RAG (with Timescale)
LlamaIndex
35 LlamaIndex Webinar: Build/Break/Test LLM Apps Showcase (co-hosted with TrueEra, Pinecone)
LlamaIndex Webinar: Build/Break/Test LLM Apps Showcase (co-hosted with TrueEra, Pinecone)
LlamaIndex
36 LlamaIndex Workshop: Evaluation-Driven Development (EDD)
LlamaIndex Workshop: Evaluation-Driven Development (EDD)
LlamaIndex
37 LlamaIndex Webinar: Building LLM Apps for Production, Part 1 (co-hosted with Anyscale)
LlamaIndex Webinar: Building LLM Apps for Production, Part 1 (co-hosted with Anyscale)
LlamaIndex
38 LlamaIndex Webinar: Learn about Fine-tuning + RAG (w/ Victoria Lin, author of RA-DIT)
LlamaIndex Webinar: Learn about Fine-tuning + RAG (w/ Victoria Lin, author of RA-DIT)
LlamaIndex
39 LlamaIndex Webinar: What's next for AI after OpenAI Dev Day?
LlamaIndex Webinar: What's next for AI after OpenAI Dev Day?
LlamaIndex
40 Introducing create-llama
Introducing create-llama
LlamaIndex
41 LlamaIndex Webinar: PrivateGPT - Production RAG with Local Models
LlamaIndex Webinar: PrivateGPT - Production RAG with Local Models
LlamaIndex
42 Multi-modal Retrieval Augmented Generation with LlamaIndex
Multi-modal Retrieval Augmented Generation with LlamaIndex
LlamaIndex
43 LlamaIndex Webinar: LLaVa Deep Dive
LlamaIndex Webinar: LLaVa Deep Dive
LlamaIndex
44 A deep dive into Retrieval-Augmented Generation with Llamaindex
A deep dive into Retrieval-Augmented Generation with Llamaindex
LlamaIndex
45 LlamaIndex Workshop: Multimodal + Advanced RAG Workhop with Gemini
LlamaIndex Workshop: Multimodal + Advanced RAG Workhop with Gemini
LlamaIndex
46 LlamaIndex Webinar: Efficient Parallel Function Calling Agents with LLMCompiler
LlamaIndex Webinar: Efficient Parallel Function Calling Agents with LLMCompiler
LlamaIndex
47 Introduction to Query Pipelines (Building Advanced RAG, Part 1)
Introduction to Query Pipelines (Building Advanced RAG, Part 1)
LlamaIndex
48 LLMs for Advanced Question-Answering over Tabular/CSV/SQL Data (Building Advanced RAG, Part 2)
LLMs for Advanced Question-Answering over Tabular/CSV/SQL Data (Building Advanced RAG, Part 2)
LlamaIndex
49 LlamaIndex Webinar: Advanced Tabular Data Understanding with LLMs
LlamaIndex Webinar: Advanced Tabular Data Understanding with LLMs
LlamaIndex
50 Ollama X LlamaIndex Multi-Modal
Ollama X LlamaIndex Multi-Modal
LlamaIndex
51 Build Agents from Scratch (Building Advanced RAG, Part 3)
Build Agents from Scratch (Building Advanced RAG, Part 3)
LlamaIndex
52 LlamaIndex Webinar: Build No-Code RAG with Flowise
LlamaIndex Webinar: Build No-Code RAG with Flowise
LlamaIndex
53 LlamaIndex Sessions: Practical Tips and Tricks for Productionizing RAG (feat. Sisil @ Jasper)
LlamaIndex Sessions: Practical Tips and Tricks for Productionizing RAG (feat. Sisil @ Jasper)
LlamaIndex
54 Introduction to LlamaIndex v0.10
Introduction to LlamaIndex v0.10
LlamaIndex
55 Build SELF-DISCOVER from Scratch with LlamaIndex
Build SELF-DISCOVER from Scratch with LlamaIndex
LlamaIndex
56 Introducing LlamaCloud (and LlamaParse)
Introducing LlamaCloud (and LlamaParse)
LlamaIndex
57 LlamaIndex Sessions: 12 RAG Pain Points and Solutions
LlamaIndex Sessions: 12 RAG Pain Points and Solutions
LlamaIndex
58 LlamaIndex Webinar: RAG Beyond Basic Chatbots
LlamaIndex Webinar: RAG Beyond Basic Chatbots
LlamaIndex
59 A Comprehensive Cookbook for Claude 3
A Comprehensive Cookbook for Claude 3
LlamaIndex
60 LlamaIndex Webinar: RAPTOR - Tree-Structured Indexing and Retrieval
LlamaIndex Webinar: RAPTOR - Tree-Structured Indexing and Retrieval
LlamaIndex

This video tutorial teaches how to build agentic LLM application workflows using LlamaIndex, covering workflow definition, custom event classes, and visualization. By following the steps, viewers can create complex workflows and debug their execution.

Key Takeaways
  1. Install Llama Index Core, Llama Index Open AI, and Llama Index UI's Workflow
  2. Define a workflow class with steps decorated with the @step decorator
  3. Use type declarations to define the types of events a step accepts and emits
  4. Create custom event classes to pass around the workflow
  5. Visualize the workflow using Llama Index UI's Workflow visualization tools
  6. Run a workflow and step through it step by step
  7. Monitor failures in a workflow using Collect events
💡 LlamaIndex provides a powerful tool for building agentic LLM application workflows, allowing developers to define complex workflows, use custom event classes, and visualize workflow execution.

Related AI Lessons

I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →