IMPROVED Chatting with Stateflow | Autogen Tutorial
IMPROVED Chatting with Stateflow | Autogen Tutorial
Mastering Improved Chatting with Stateflow: An Autogen Tutorial
Welcome to our comprehensive guide on harnessing the power of Stateflow with Autogen. This tutorial is designed to delve into the intricacies of managing different states in Stateflow and efficiently automate multiple agents, facilitating seamless task transitions and optimizing overall chat performance.
Introduction to Stateflow
Stateflow is a powerful concept where multiple processes or "states" are defined, and each state has one or more agents handling specific tasks. Each agent completes its designated actions before transitioning to the next state, creating a controlled, systematic flow that ensures task completion with precision.
Stateflow System Architecture
Understanding the basic architecture of Stateflow is crucial in implementing its functionalities effectively. Let’s break down the components involved:
Agents Defined
- Initializer and Executor: Typically user proxy agents, these are pivotal in starting and executing tasks.
- Coder and Scientist: These are assistant agents; the Coder develops the necessary code, while the Scientist analyzes and summarizes the results.
Group Chat Dynamics
In Stateflow, a group chat scenario involves three different states, each represented by a green circle in visual diagrams:
- Initializer State: This involves a single agent responsible for initiating the conversation and setting the stage for task execution.
- Coder and Executor State: Here, the Coder scripts the required actions, which the Executor then attempts to implement.
- Scientist State: This final state involves summarizing and analyzing the data retrieved and processed by the previous agents.
State Transition: Navigating through Flows
A critical aspect of Stateflow is managing state transitions, which is controlled by the state transition function
. This function dictates the next agent based on the response of the current one.
- If the Executor encounters errors, the function reassigns the task to the Coder to modify the existing code.
- Once the issue is resolved, the process progresses to the Scientist for final analysis and summarization.
Practical Implementation: Coding with Autogen
To illustrate Stateflow in action, we implement a sample project using Python and the Autogen library. Here is how we set up the agents and manage transitions:
Configuration and Setup
- Creating a Config List: Essential settings like model, API key, and execution parameters are defined here.
- Defining Agents: Agents are initialized with specific roles, such as not executing code or formulating specific queries.
State Transition Handling
- Function Definition: The
state transition function
is defined to manage transitions based on agent responses and task outcomes. - Group Chat Management: Incorporating the
state transition function
into the group chat setup allows for dynamic response handling and agent reassignment based on task execution results.
Running the Code
Upon executing the primary Python script, one can observe:
- Posting of the initial task by the
initializer
. - Task handling by the
coder
andexecutor
, with error checks and rectifications. - Final data summarization by the
scientist
, culminating in a detailed markdown table presentation.
Conclusion
Stateflow with Autogen offers a robust framework for managing complex workflows divided into distinct states, each handled by specialized agents. By properly setting up agents, defining state transitions, and integrating these elements into a coherent code structure, we can significantly enhance the efficiency and effectiveness of automated chat tasks.
Invite comments, discussions, or queries in the section below, to explore more about Stateflow and its applications. Catch our upcoming videos for more insightful tutorials and walkthroughs. Thank assistant
[h3]Watch this video for the full details:[/h3]
Hey and welcome back to another AutoGen Tutorial. We are going to see how a relatively new feature called Stateflow works. This is another way to have your agents chat with each other allowing for a better flow. We are introduced to something called state with agents allowing us to move from one ‘state’ to another. Each state represents an action such as research or summarization. We can have one or multiple agents within each state. The idea is when we are finished with one state, or really a task from a single or group of agents, we move on to the next. We can even think of this as teams completing something, and then sending the result to the next team until we are finished.
We will dive in to see how this is done!
Don’t forget to sign up for the FREE newsletter below to give updates in AI, what I’m working on and struggles I’ve dealt with (which you may have too!):
=========================================================
???? Newsletter Sign-up: https://bit.ly/tylerreed
=========================================================
Join me on Discord: https://discord.gg/tte8eNmW
Connect With Me:
???? X (twitter): @TylerReedAI
????♂️ GitHub: https://github.com/tylerprogramming/ai
???? Instagram: TylerReedAI
???? LinkedIn: https://www.linkedin.com/in/tylerreedai/
???? 31 Day Challenge Playlist: https://youtube.com/playlist?list=PLwPL8GA9A_umryTQCIjf3lU6Tq9ioNe36&si=4XCDtT8ep1U6KjkR
????♂️ GitHub 31 Day Challenge: https://github.com/tylerprogramming/31-day-challenge-ai
???? PyCharm Download: https://www.jetbrains.com/pycharm/download
???? Anaconda Download: https://www.anaconda.com/download
???? Ollama Download: https://ollama.com/
???? LM Studio Download: https://lmstudio.ai/
The paper: https://arxiv.org/abs/2403.04783
???? Chapters:
00:00 Intro
00:30 Explanation
05:08 Coding
08:41 Example Walkthrough
10:18 Outro
???? If you have any issues, let me know in the comments and I will help you out!
[h3]Transcript[/h3]
hey and welcome back to another video and today we’re going to be going over State flow agents now the key word there is State because we’re going to have different states and in each state you can have any number of Agents so once all the agents are done performing a task in one state they’ll move on to the next and then those agents or even just a single agent will try to perform something until we’re done with the last state now personally I like this controlled way of the flow of all the agents but let’s just get into the explanation so I can show you what I mean all right so before we begin these are the four agents that I’m going to be showing you and kind of going over how this works we have the initializer and the executor they’re going to be user agents or user proxy agents and then the coder and the scientists are going to be both assistant agents let’s first talk about what a group chat looks like we create a group chat so we’ll have a group chat of three different agents then we create a group chat manager and then the initializer or some user proxy agent initiates a chat with that group chat manager and then the group chat talks among each other and uh we get out we get an output okay so here’s another way that we’re going to look at this now with State flow here we have three different states each state is a green circle the first state is the initializer okay this is the agent only has one agent in the state and this agent is going to start the chat and go go to the next state and then give them the information that we want them to uh talk to the llm about in this case we want the initializer to tell the next date that we want to retrieve some papers online so then we move on from initializer to the next date and in this date we have two different agents we have a coder and we have an executor the coder is going to write up some python code that is going to go out to the web retrieve some information for us and then send that to the executor well then the executor is going to try to actually execute the code that the coder just wrote If it wasn’t successful we’re going to send that back to the coder and we’re going to keep this kind of loop between the coder and the executor until the code from the coder is successfully executed once it is successfully executed we are done with this state and then we’re going to move on to the last date which is comprised of the scientist and the scientist is going to summarize everything that the code retrieved online so the thing is here is that we have three different states Again by the three circles and each state can have more than one agent right and each agent is just going to do something within their States before it gets sent off to the next state okay so how’s this going to look in code well how is this actually going to work how do we make State flow happen well the first thing is we’re going to have a function called State transition that takes in a perimeter called last speaker which is just the last agent that spoke in the chat whenever we go to initiate the chat there will be another prer that can take this function in and what’s going to happen is once we initiate the chat when the first agent is done speaking which in this case will be initializer when they’re done speaking we’re going to call this function right so then we say if the last speaker is the initializer then return the coder okay so that means now the coder is going to perform whatever it needs to do to write the code based when it calls the llm it’s going to write that python code and then whenever it’s done we call this function again well the last speaker was the coder so we would check the first if statement while the last speaker was not the initializer so we move on to the next one if the last speaker is the coder then we’re going to return the executor the executor going to try and execute the code and then whenever it tries to execute it whenever they’re done then we call the state transition function again then we go back in this FL statement we eventually come down to if the last speaker is the executor then we have if the exec Cod is one meaning it failed then we would move back to the coder we would return the coder or if it was successful then we would return the scientist so let’s say it wasn’t successful so we got exit code of one we would now return the coder well that means now the coder is going to try and hopefully revamp the code so that it does work based on whatever failed when the executor tried to execute it so that means the coder tries to do something and then whenever they’re done we call the state transition function again well the last speaker in this case was the coder so again we would come down here and we would make it to this second if statement say if the last speaker is the coder then call the executor okay the executor is going to try and execute the code again once they’re done if the last speaker is the executor again right here then this time let’s just say that it was successful well it didn’t have an execut of one so it was successful meaning that we now call the scientist the scientist is then going to probably make a call T llm to have uh some some model summarize everything that was found or returned from the python code so once they’re done with that then we’re going to call the state transition function again and eventually we make it down this IFL statement so we say if the last speaker is the scientist then guess what we’re done we just you can just like return none and that means we are done with the state flow now I know that was a lot so redo that part if you didn’t quite understand it but we’re going to go into the code now now and actually execute it so then we can see how it actually runs okay so here for the code we’re going to have two different files we’re going to have a main python file and then our typical openi config list. Json file so the first thing is we only need autogen for this so you only import autogen we need a config list where I use autogen config list from Json and in this example I am going to be using gp4 just so I can run this in a reasonable amount of time however if we look at the config list whenever you go to use this I have it set up to where if you want to use LM Studio you’re more than welcome to if you want to use AMA here it is you just have to replace the model which with with whichever model you pull using oama or if you just want to use dpt4 or even 3.5 turbo just put in your API key here but the options will be here for you and again you know just use the model right so if you want to if I wanted to use LM Studio I’ll just put LM Studio here and that’s it then I create the llm config with some just default settings for temperature the seed and timeout and then we start Crea any agents well the initializer that we talked about right this is just the user proxy agent with the name and we don’t want this this uh agent to actually execute any code so I just set it to false decoder so this is the coder agent that it will write um the software that is going to uh find some papers online for us so this is just the prompt for it we have the executor which is another user proxy agent because we need a user proxy agent to actually execute the code from the coder all right you’re going to use user agent to do that and actually in the system message that’s it right here execute the code written by the coder and Report the result so I’m not going to be using in Docker so I set that’s a false and we will have a paper directory hopefully with the python code and then we have the scientist which is going to categorize the papers after seeing their abstracts and then create a markdown table one of the last two new things is the state transition that we talked about all right so I said in the state transition function we take the last speaker which was the last agent that spoke and then we just had the group chat and the reason we had the group chat is um because we for the last speaker for the executor we want to know if the last message had an exode ex code of one in it meaning when I tried to ex when I tried to execute the code it failed that’s all this is for right so we can get the group chat messages here you know this is like I said it’s it’s going to be IFL statements right so if the last speaker was the initializer then we return the coder and then it’s the coder’s turn uh to talk in the chat and the same thing if the the last speaker was the coder then it’s the executor’s turn and then if the last speaker was the executor we see at the last message had an xcode of one in it if it did meaning it failed then we return the coder otherwise return the scientist and then finally if the last speaker was the scientist then we return none okay we had the group chat and the group chat manager now the only difference here with the group chat is like I mentioned briefly was that there is another property here that allows us to take in this function called State transition so if you go into the group chat here the class right here you see there is a speaker selection method all right typically um this is just done this is you basically put this in Auto I think that that’s kind of been default for a while but you can have a custom speaker selection function right we just created the function and they give you an example they give you an example here right so basically you want to return you want to return um an agent and the function takes in the last speaker which is an agent and the group chat okay and that’s all we need so now all I’m going to do next is actually run the code and we’ll just go through and you can see how the state flow works all right so we executed it and it worked so again we start with the initializer or in it was the name of that agent uh we had the LM application papers from last week requirement is 5 to 10 papers from different domains so the next state after the initializer state is over with which is basically just sending this topic it goes to the coder now we’re in kind of the research state where we had the coder in the executor running so the coder creates python code right so it does all of this then it goes on to the executor well the executor tried to execute this code but as you can see here we got an exit code of one so that means that there was some failure and then because of that if it found an exit code of one the state function it’s going to return the coder so here we are back at the coder so it seems like there was an error so it tried to the coder tried fix the issue and then the executor tried to reexecute it and now we have an xcode of zero meaning that there was no error so we finally got an output right so I got um all these different papers here um don’t need to go through all of them right now and then finally because we were done with that state now the executor got executed code that was successful we now move on to the last state which is the scientist who is going to put all the summary of everything into a markdown table so after I cop after I copied it in we finally got um the markdown preview okay and we got the domain title author summary and the link which is what we wanted and they are each from different domains okay and that’s awesome this what we wanted this is just another way that we can have different states control the flow of how the agents going to talk to each other and perform the tasks okay that was an introduction to State flow and state flow agents this wasn’t a huge example but I hope like the PowerPoint kind of helped you understand how this works this is another way to control the flow of your agents if you have any comments or anything you want to talk about leave them down in the comment section below thank you for watching I’ll see you next video