From the course: Building AI Agents with AutoGen
Execute user-defined code in AutoGen - Autogen Tutorial
From the course: Building AI Agents with AutoGen
Execute user-defined code in AutoGen
In this lesson, we are going to enhance our coding copilot by adding custom functions for stock analysis and exploring how to execute user-defined code. So I've imported the libraries, configured the LLM. Let's define our custom functions for stock analysis. Here, I have two functions. The first one is get stock prices. This fetches stock data using the yfinance library. The second function is the plot stock prices. This creates and saves a plot of stock prices. Now these functions will be made available to our coding copilot, allowing it to perform more specialized tasks. If you want to add something to these functions, you can do so as well. So we have defined these. The next step is to create a new executor with these custom functions. So here, I have a local command line code executor with a timeout of 60 seconds, working directory named coding, and our custom functions, which will be available to the executor. So I have passed that as a list. So in this list, I have passed the names of these custom functions. So executor is also ready. I have defined the code writer system message beforehand. So this is another way. So it's basically the same system message that we had in the assistant agent in the previous lesson. Here, I have defined it myself. To this, we'll further add format functions for prompt. So what is this doing? We are updating the system message for our code writer agent. So the format functions for prompt method adds information about our custom functions to the system message, allowing the agent to use them in its code generation process. So the code writer would have access to the custom functions that we have defined. So you can see, I'm simply appending this executor information, the custom function that I passed in my executor to the system message of my code writer agent. So this has become the updated system message for my code writer agent now. It's time to define the agents with the updated system message. So code writer agent, this is the updated code writer system message. Everything else remains the same as the previous lessons. Finally, the code executor agent with our executor provided here. Run this. The next step is to provide the initial instruction. Here, we initiate the chat between our agents, providing a task to download and plot the stock prices for Nvidia and Tesla. So if you run this, we have the conversation going on here, all the steps, et cetera. please save the code in this particular file and execute it. I'll hit enter here. Executing code block. Great. It looks like everything executed successfully and the stock prices has been saved to this file. Everything is getting stored in the coding directory. I will end the conversation here, exit. And finally, I will try to plot the image that these agents created using our custom functions. So plot these and you have the image plotted by the agents using the custom functions of the users. So this is how you can integrate custom functions and have the agents work, or use your code blocks instead of LLMs having to generate the code. So incorporate these functions into your code execution environment. Update the agents to use these new capabilities. So that's code execution with AutoGen.