AG2 agents can execute code from a message passed to them (e.g., those containing code blocks) and output a message with the results of the execution for the following agent to interpret.There are two types of built-in code executors, one is the command line code executor, which runs code in a command line environment such as a MacOS or Linux shell, and the other is a Jupyter executor, which runs code in an interactive Jupyter kernel.For each type of executor, AG2 provides two ways to execute code: locally and in a Docker container. For development and testing, not recommended for production, you can run it on the same host platform where AG2 is running, i.e., the local operating system. For better The other way is to execute code in a Docker container. The table below shows the combinations of code executors and execution environments.
Executing LLM-generated code poses a security risk to your host environment.
Upon receiving a message with a code block, the local command line code executor first writes the code block to a code file, then starts a new subprocess to execute the code file. The executor reads the console output of the code execution and sends it back as a reply message.Here is an example of using the code executor to run a Python code block that prints a random number.Before running this example, we need to make sure the matplotlib and numpy are installed.
pip install -qqq matplotlib numpy
First we create an agent with the code executor that uses a temporary directory to store the code files.We specify human_input_mode="ALWAYS" to manually validate the safety of the code being executed.
import tempfilefrom autogen import ConversableAgent, LLMConfigfrom autogen.coding import LocalCommandLineCodeExecutor# Create a temporary directory to store the code files.temp_dir = tempfile.TemporaryDirectory()# Create a local command line code executor.executor = LocalCommandLineCodeExecutor( timeout=10, # Timeout for each code execution in seconds. work_dir=temp_dir.name, # Use the temporary directory to store the code files.)# Create an agent with code executor configuration.code_executor_agent = ConversableAgent( "code_executor_agent", llm_config=False, # Turn off LLM for this agent. code_execution_config={"executor": executor}, # Use the local command line code executor. human_input_mode="ALWAYS", # Always take human input for this agent for safety.)
Now we have the agent generate a reply given a message with a Python code block.
message_with_code_block = """This is a message with code block.The code block is below:``""" + """`pythonimport numpy as npimport matplotlib.pyplot as pltx = np.random.randint(0, 100, 100)y = np.random.randint(0, 100, 100)plt.scatter(x, y)plt.savefig('scatter.png')print('Scatter plot saved to scatter.png')``""" + """`This is the end of the message."""# Generate a reply for the given code.reply = code_executor_agent.generate_reply(messages=[{"role": "user", "content": message_with_code_block}])print(reply)
>>>>>>>> NO HUMAN INPUT RECEIVED.>>>>>>>> USING AUTO REPLY...>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...exitcode: 0 (execution succeeded)Code output:Scatter plot saved to scatter.png
During the generation of response, a human input is requested to give an opportunity to intercept the code execution. In this case, we choose to continue the execution, and the agent’s reply contains the output of the code execution.We can take a look at the generated plot in the temporary directory.
import os# We can see the output scatter.png and the code file generated by the agent.print(os.listdir(temp_dir.name))# Clean up the working directory to avoid affecting future conversations.temp_dir.cleanup()
To mitigate the security risk of running LLM-generated code locally, we can use the docker command line code executor (autogen.coding.DockerCommandLineCodeExecutor) to execute code in a docker container.This way, the generated code can only access resources that are explicitly given to it.The figure below illustrates how docker execution works.Similar to the local command line code executor, the docker executor extracts code blocks from input messages, writes them to code files. For each code file, it starts a docker container to execute the code file,
and reads the console output of the code execution.To use docker execution, you need to install Docker on your machine.
Once you have Docker installed and running, you can set up your code executor agent as follow:
from autogen.coding import DockerCommandLineCodeExecutor# Create a temporary directory to store the code files.temp_dir = tempfile.TemporaryDirectory()# Create a Docker command line code executor.executor = DockerCommandLineCodeExecutor( image="python:3.12-slim", # Execute code using the given docker image name. timeout=10, # Timeout for each code execution in seconds. work_dir=temp_dir.name, # Use the temporary directory to store the code files.)# Create an agent with code executor configuration that uses docker.code_executor_agent_using_docker = ConversableAgent( "code_executor_agent_docker", llm_config=False, # Turn off LLM for this agent. code_execution_config={"executor": executor}, # Use the docker command line code executor. human_input_mode="ALWAYS", # Always take human input for this agent for safety.)# When the code executor is no longer used, stop it to release the resources.# executor.stop()
The work_dir in the constructor points to a local file system directory just like in the local execution case.
The docker container will mount this directory and the executor write code files and output to it.
Use Code Execution in Conversation
Writing and executing code is necessary for many tasks such as data analysis, machine learning, and mathematical modeling. In AG2, coding can be a conversation between a code writer agent and a code executor agent, mirroring the interaction between a programmer and a code interpreter.The code writer agent can be powered by any LLM with code-writing capability.And the code executor agent is powered by a code executor.The following is an agent with a code writer role specified using system_message. The system message contains important instruction on how to use the code executor in the code executor agent.
# The code writer agent's system message is to instruct the LLM on how to use# the code executor in the code executor agent.code_writer_system_message = """You are a helpful AI assistant.Solve tasks using your coding and language skills.In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.Reply 'TERMINATE' in the end when everything is done."""code_writer_agent = ConversableAgent( "code_writer_agent", system_message=code_writer_system_message, llm_config=LLMConfig( api_type="openai", model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"] ), code_execution_config=False, # Turn off code execution for this agent.)chat_result = code_executor_agent.initiate_chat( code_writer_agent, message="Write Python code to calculate the 14th Fibonacci number.",)
>>>>>>>> NO HUMAN INPUT RECEIVED.>>>>>>>> USING AUTO REPLY...>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...exitcode: 0 (execution succeeded)Code output:Scatter plot saved to scatter.png['scatter.png', '6507ea07b63b45aabb027ade4e213de6.py']code_executor_agent (to code_writer_agent):Write Python code to calculate the 14th Fibonacci number.-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...code_writer_agent (to code_executor_agent):Sure, here is a Python code snippet to calculate the 14th Fibonacci number. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.'''pythondef fibonacci(n): if(n <= 0): return "Input should be a positive integer." elif(n == 1): return 0 elif(n == 2): return 1 else: fib = [0, 1] for i in range(2, n): fib.append(fib[i-1] + fib[i-2]) return fib[n-1]print(fibonacci(14))'''This Python code defines a function `fibonacci(n)` which computes the n-th Fibonacci number. The function uses a list `fib` to store the Fibonacci numbers as they are computed, and then returns the (n-1)-th element as the n-th Fibonacci number due to zero-indexing in Python lists.-------------------------------------------------------------------------------->>>>>>>> NO HUMAN INPUT RECEIVED.>>>>>>>> USING AUTO REPLY...>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...code_executor_agent (to code_writer_agent):exitcode: 0 (execution succeeded)Code output:233-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...code_writer_agent (to code_executor_agent):Great, the execution was successful and the 14th Fibonacci number is 233. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233... and so on, where each number is the sum of the previous two. Therefore, the 14th number in the Fibonacci series is 233.I hope this meets your expectations. If you have any other concerns or need further computations, feel free to ask.TERMINATE--------------------------------------------------------------------------------
Now we can try a more complex example that involves using external packages that can get data.Let’s say we want to get the stock price gains year-to-date for Tesla and Meta (formerly Facebook). We can also use the two agents with several iterations of conversation.
import datetimetoday = datetime.datetime.now().strftime("%Y-%m-%d")chat_result = code_executor_agent.initiate_chat( code_writer_agent, message=f"Today is {today}. Write Python code to plot TSLA's and META's " "stock price gains YTD, and save the plot to a file named 'stock_gains.png'.",)
code_executor_agent (to code_writer_agent):Today is 2024-02-28. Write Python code to plot TSLA's and META's stock price gains YTD, and save the plot to a file named 'stock_gains.png'.-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...code_writer_agent (to code_executor_agent):This task requires retrieving the historical data of the stocks from a reliable data source and calculating the Year-To-Date (YTD) gain values, and then plotting them. pandas_datareader library will be used for data retrieval, pandas will be used for data manipulation, and matplotlib for plotting.Below is the Python code to achieve this. To start, please install the required libraries by running to the following command:'''shpip install yfinance pandas matplotlib'''Then run the python code:'''python# filename: stock_gains.pyimport yfinance as yfimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetime# define the tickerstickers = ['TSLA', 'META']# define the start and end datesstart_date = datetime(2024, 1, 1)end_date = datetime(2024, 2, 28)# dictionary to hold dataframesdfs = {}for ticker in tickers: # get the data for the stocks df = yf.download(ticker, start_date, end_date) # get the close price and calculate the cumulative percentage gain df['Gain'] = df['Close'].pct_change().cumsum() # add to dictionary dfs[ticker] = df# plotplt.figure(figsize=(10, 5))for ticker, df in dfs.items(): plt.plot(df.index, df['Gain'], label=ticker)plt.title('YTD Stock Price Gain')plt.xlabel('Date')plt.ylabel('Percentage Gain')plt.legend()plt.grid(True)plt.savefig('stock_gains.png')plt.close()print("The 'stock_gains.png' file has been successfully saved")'''This script will download the historical data for TSLA and META from the start of the year to the specified date and calculates the YTD gains. It then generates the plot showing these gains and saves it to 'stock_gains.png'.Please save the script to a file named 'stock_gains.py' and run it using Python. Remember to have the correct start and end dates for the YTD value when running the script. If your Python version is below 3.9, you should update it to execute this code perfectly.-------------------------------------------------------------------------------->>>>>>>> NO HUMAN INPUT RECEIVED.>>>>>>>> USING AUTO REPLY...>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is sh)...>>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...code_executor_agent (to code_writer_agent):exitcode: 0 (execution succeeded)Code output:Requirement already satisfied: yfinance in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (0.2.36)Requirement already satisfied: pandas in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (2.1.4)Requirement already satisfied: matplotlib in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (3.9.2)...The 'stock_gains.png' file has been successfully saved-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...code_writer_agent (to code_executor_agent):Great! The code executed successfully and the 'stock_gains.png' file has been saved successfully. This file contains the plot of TSLA's and META's stock price gains from the start of the year until February 28, 2024. You should now be able to view this image file in the same directory that you ran the script from.Please make sure to verify this image file. It should contain two plotted lines, each representing the percentage gain over the time for each stock (TSLA and META). The x-axis represents the date, and the y-axis represents the percentage gain. If everything looks correct, this would be the end of the task.TERMINATE--------------------------------------------------------------------------------
In the previous conversation, the code writer agent generated a code block to install necessary packages and another code block for a script to fetch the stock price and calculate gains year-to-date for Tesla and Meta. The code executor agent installed the packages, executed the script, and returned the results.Let’s take a look at the chart that was generated.
from IPython.display import ImageImage(os.path.join(temp_dir, "stock_gains.png"))
# Clean up traces like code files and output in the file system between executionstemp_dir.cleanup()# Stop the docker command line code executorexecutor.stop()
Command Line or Jupyter Code Executor?
The command line code executor does not keep any state in memory between executions of different code blocks it receives, as it writes each code block to a separate file and executes the code block in a new process.Contrast to the command line code executor, the Jupyter code executor runs all code blocks in the same Jupyter kernel, which keeps the state in memory between executions.The choice between command line and Jupyter code executor depends on the nature of the code blocks in agents’ conversation. If each code block is a “script” that does not use variables from previous code blocks, the command line code executor is a good choice. If some code blocks contain expensive computations (e.g., training a machine learning model and loading a large amount of data), and you want to keep the state in memory to avoid repeated computations, the Jupyter code executor is a better choice.
{"docsConfig":{"$schema":"https://mintlify.com/docs.json","theme":"mint","name":"AG2","colors":{"primary":"#4b9cd6","light":"#63a9db","dark":"#338fd1"},"favicon":"/logo/favicon.svg","logo":{"light":"https://mintcdn.com/private-04b27de1/pvm3Z7mIWzwbwn7q/logo/ag2.svg?fit=max\u0026auto=format\u0026n=pvm3Z7mIWzwbwn7q\u0026q=85\u0026s=7e064e1c72768237baa4e260a7828dfe","dark":"https://mintcdn.com/private-04b27de1/pvm3Z7mIWzwbwn7q/logo/ag2-white.svg?fit=max\u0026auto=format\u0026n=pvm3Z7mIWzwbwn7q\u0026q=85\u0026s=5f3c0db50d8f566bd4448ae232799a59"},"navbar":{"primary":{"type":"github","href":"https://github.com/ag2ai/ag2"}},"search":{"prompt":"Search or ask..."},"seo":{"metatags":{"og:image":"https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/website/static/img/cover.png"},"indexing":"navigable"},"footer":{"socials":{"x":"https://x.com/ag2oss","github":"https://github.com/ag2ai/ag2","linkedin":"https://www.linkedin.com/company/ag2ai","discord":"https://discord.gg/pAbnFJrkgZ","youtube":"https://www.youtube.com/@ag2ai"}},"integrations":{"ga4":{"measurementId":"G-2GN2KN2CE4"}},"fonts":{"heading":{"family":"Jersey 10"},"body":{"family":"Inter"}},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"private-04b27de1","hasAudioNarration":false,"actualSubdomain":"private-04b27de1","customDomains":[],"gitSource":{"type":"github","owner":"harishmohanraj","repo":"ag2","deployBranch":"mintlify-pages","contentDirectory":"","isPrivate":true,"mountPath":"","label":""},"entitlements":{},"buildId":"6ae9239d3e9b7a540d51da54:success","clientVersion":"0.0.4344","searchFilterCounts":[{"language":"default","tag":"API References","count":879},{"language":"default","tag":"API Reference","count":879},{"language":"default","tag":"autogen","count":879},{"language":"default","tag":"agentchat","count":358},{"language":"default","tag":"Use Cases","count":264},{"language":"default","tag":"contrib","count":228},{"language":"default","tag":"User Guide","count":171},{"language":"default","tag":"tools","count":137},{"language":"default","tag":"Notebooks","count":128},{"language":"default","tag":"oai","count":125}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"site-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"treatment","enabled":true,"payload":{"type":"string","value":"treatment"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"wildcard-email-auth","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"starterQuestions":[],"googleFontsInlined":true},"customCss":[{"filePath":"mint-style.css","href":"/_mintlify/custom/private-04b27de1/8f3837c83da6c188/mint-style.css"}]}
{"page":{"pagePath":"docs/user-guide/advanced-concepts/code-execution","slug":"/docs/user-guide/advanced-concepts/code-execution","pageMetadata":{"title":"Code Execution","description":null,"href":"/docs/user-guide/advanced-concepts/code-execution"},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,p:`p`,pre:`pre`,span:`span`,tbody:`tbody`,td:`td`,th:`th`,thead:`thead`,tr:`tr`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Danger,Heading,Icon,OptimizedImage,Table}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Danger)_missingMdxReference(`Danger`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Icon)_missingMdxReference(`Icon`,true);if(!OptimizedImage)_missingMdxReference(`OptimizedImage`,true);if(!Table)_missingMdxReference(`Table`,true);return _jsxs(_Fragment,{children:[_jsx(_components.p,{children:`AG2 agents can execute code from a message passed to them (e.g., those containing code blocks) and output a message with the results of the execution for the following agent to interpret.`}),`\n`,_jsx(_components.p,{children:`There are two types of built-in code executors, one is the command line code executor, which runs code in a command line environment such as a MacOS or Linux shell, and the other is a Jupyter executor, which runs code in an interactive Jupyter kernel.`}),`\n`,_jsx(_components.p,{children:`For each type of executor, AG2 provides two ways to execute code: locally and in a Docker container. For development and testing, not recommended for production, you can run it on the same host platform where AG2 is running, i.e., the local operating system. For better The other way is to execute code in a Docker container. The table below shows the combinations of code executors and execution environments.`}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsxs(_components.th,{children:[`Code Executor (`,_jsx(_components.code,{children:`autogen.coding`}),`)`]}),_jsx(_components.th,{children:`Environment`}),_jsx(_components.th,{children:`Platform`})]})}),_jsxs(_components.tbody,{children:[_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/LocalCommandLineCodeExecutor`,children:_jsx(_components.code,{children:`LocalCommandLineCodeExecutor`})})}),_jsx(_components.td,{children:`Shell`}),_jsx(_components.td,{children:`Local`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/DockerCommandLineCodeExecutor`,children:_jsx(_components.code,{children:`DockerCommandLineCodeExecutor`})})}),_jsx(_components.td,{children:`Shell`}),_jsx(_components.td,{children:`Docker`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/jupyter/JupyterCodeExecutor`,children:_jsx(_components.code,{children:`jupyter.JupyterCodeExecutor`})})}),_jsx(_components.td,{children:`Jupyter Kernel (e.g., python3)`}),_jsx(_components.td,{children:`Local/Docker`})]})]})]}),`\n`,_jsx(Heading,{level:`2`,id:`local-execution`,children:`Local Execution`}),`\n`,_jsxs(_components.p,{children:[`The figure below shows the architecture of the local command line code executor\n(`,_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/LocalCommandLineCodeExecutor`,children:_jsx(_components.code,{children:`autogen.coding.LocalCommandLineCodeExecutor`})}),`).`]}),`\n`,_jsx(Danger,{children:_jsx(_components.p,{children:`Executing LLM-generated code poses a security risk to your host environment.`})}),`\n`,_jsx(OptimizedImage,{src:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=e65808efaa53c02531f274ea351b1808`,alt:`Code Executor No Docker`,\"data-og-width\":`1200`,width:`1200`,\"data-og-height\":`577`,height:`577`,\"data-path\":`docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=280\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=3aeb82b11b84318ae4381e515efb9979 280w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=560\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=af85208e8a6ef21ecef1790c6d19d2e7 560w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=840\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=e1db74cd255c0c38fa99697f1fba7d3a 840w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=1100\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=cbd23d92257df1caf158acb73d4beab4 1100w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=1650\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=6bb06dba30d6b2cde91bc142d7a5bc87 1650w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-no-docker.png?w=2500\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=43c408e613a451e8ed660febb0a56c31 2500w`}),`\n`,_jsx(_components.p,{children:`Upon receiving a message with a code block, the local command line code executor first writes the code block to a code file, then starts a new subprocess to execute the code file. The executor reads the console output of the code execution and sends it back as a reply message.`}),`\n`,_jsx(_components.p,{children:`Here is an example of using the code executor to run a Python code block that prints a random number.`}),`\n`,_jsxs(_components.p,{children:[`Before running this example, we need to make sure the `,_jsx(_components.code,{children:`matplotlib`}),` and `,_jsx(_components.code,{children:`numpy`}),` are installed.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`pip`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` install`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` -qqq`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` matplotlib`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` numpy`})]}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`First we create an agent with the code executor that uses a temporary directory to store the code files.`}),`\n`,_jsxs(_components.p,{children:[`We specify `,_jsx(_components.code,{children:`human_input_mode=\"ALWAYS\"`}),` to manually validate the safety of the code being executed.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`21`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`21`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tempfile`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` autogen `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ConversableAgent, LLMConfig`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` autogen.coding `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` LocalCommandLineCodeExecutor`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create a temporary directory to store the code files.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tempfile.TemporaryDirectory()`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create a local command line code executor.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`executor `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` LocalCommandLineCodeExecutor(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` timeout`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`10`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Timeout for each code execution in seconds.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` work_dir`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir.name, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Use the temporary directory to store the code files.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create an agent with code executor configuration.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ConversableAgent(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"code_executor_agent\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` llm_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:`False`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Turn off LLM for this agent.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` code_execution_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`{`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"executor\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: executor}, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Use the local command line code executor.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` human_input_mode`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"ALWAYS\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Always take human input for this agent for safety.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`Now we have the agent generate a reply given a message with a Python code block.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`17`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`17`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`message_with_code_block `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"\"\"This is a message with code block.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`The code block is below:`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:'``\"\"\"'}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` +`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:' \"\"\"`python'})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`import numpy as np`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`import matplotlib.pyplot as plt`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`x = np.random.randint(0, 100, 100)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`y = np.random.randint(0, 100, 100)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`plt.scatter(x, y)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`plt.savefig('scatter.png')`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`print('Scatter plot saved to scatter.png')`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:'``\"\"\"'}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` +`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:' \"\"\"`'})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`This is the end of the message.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"\"\"`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Generate a reply for the given code.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`reply `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` code_executor_agent.generate_reply(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`messages`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`[{`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"role\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"user\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"content\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: message_with_code_block}])`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(reply)`})]}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`8`,language:`shellsession`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellsession`,children:_jsxs(_components.code,{language:`shellsession`,numberOfLines:`8`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> NO HUMAN INPUT RECEIVED.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`exitcode: 0 (execution succeeded)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Code output:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Scatter plot saved to scatter.png`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`During the generation of response, a human input is requested to give an opportunity to intercept the code execution. In this case, we choose to continue the execution, and the agent’s reply contains the output of the code execution.`}),`\n`,_jsx(_components.p,{children:`We can take a look at the generated plot in the temporary directory.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`7`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` os`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# We can see the output scatter.png and the code file generated by the agent.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(os.listdir(temp_dir.name))`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Clean up the working directory to avoid affecting future conversations.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir.cleanup()`})}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`shellsession`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellsession`,children:_jsxs(_components.code,{language:`shellsession`,numberOfLines:`1`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`['scatter.png', '6507ea07b63b45aabb027ade4e213de6.py']`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`docker-execution`,children:`Docker Execution`}),`\n`,_jsxs(_components.p,{children:[`To mitigate the security risk of running LLM-generated code locally, we can use the docker command line code executor (`,_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/DockerCommandLineCodeExecutor`,children:_jsx(_components.code,{children:`autogen.coding.DockerCommandLineCodeExecutor`})}),`) to execute code in a docker container.`]}),`\n`,_jsx(_components.p,{children:`This way, the generated code can only access resources that are explicitly given to it.`}),`\n`,_jsx(_components.p,{children:`The figure below illustrates how docker execution works.`}),`\n`,_jsx(OptimizedImage,{src:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=1e5a40f7439a70751ee8ff000b2f2d12`,alt:`Code Executor Docker`,\"data-og-width\":`1200`,width:`1200`,\"data-og-height\":`577`,height:`577`,\"data-path\":`docs/user-guide/advanced-concepts/assets/code-executor-docker.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=280\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=a9049a0eae64a8fabec94cc35d4dadbf 280w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=560\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=0e4e8cef31d559012e7412afe250263d 560w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=840\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=f538d56f267fdc127f968e46dcb8f583 840w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=1100\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=194cb379e669ddc3f377420d0e5c20a1 1100w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=1650\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=8a138bf3d6ac7a5106fb13d780b0cfb1 1650w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-docker.png?w=2500\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=6eb0abb1b8c25e8b44752878e3268d60 2500w`}),`\n`,_jsx(_components.p,{children:`Similar to the local command line code executor, the docker executor extracts code blocks from input messages, writes them to code files. For each code file, it starts a docker container to execute the code file,\nand reads the console output of the code execution.`}),`\n`,_jsxs(_components.p,{children:[`To use docker execution, you need to `,_jsx(_components.a,{href:`https://docs.docker.com/engine/install/`,children:`install Docker`}),` on your machine.\nOnce you have Docker installed and running, you can set up your code executor agent as follow:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`22`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`22`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` autogen.coding `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` DockerCommandLineCodeExecutor`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create a temporary directory to store the code files.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tempfile.TemporaryDirectory()`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create a Docker command line code executor.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`executor `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` DockerCommandLineCodeExecutor(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` image`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"python:3.12-slim\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Execute code using the given docker image name.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` timeout`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`10`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Timeout for each code execution in seconds.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` work_dir`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir.name, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Use the temporary directory to store the code files.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Create an agent with code executor configuration that uses docker.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent_using_docker `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ConversableAgent(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"code_executor_agent_docker\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` llm_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:`False`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Turn off LLM for this agent.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` code_execution_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`{`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"executor\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: executor}, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Use the docker command line code executor.`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` human_input_mode`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"ALWAYS\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Always take human input for this agent for safety.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# When the code executor is no longer used, stop it to release the resources.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# executor.stop()`})}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.code,{children:`work_dir`}),` in the constructor points to a local file system directory just like in the local execution case.\nThe docker container will mount this directory and the executor write code files and output to it.`]}),`\n`,_jsx(Heading,{level:`3`,id:`use-code-execution-in-conversation`,children:`Use Code Execution in Conversation`}),`\n`,_jsx(_components.p,{children:`Writing and executing code is necessary for many tasks such as data analysis, machine learning, and mathematical modeling. In AG2, coding can be a conversation between a code writer agent and a code executor agent, mirroring the interaction between a programmer and a code interpreter.`}),`\n`,_jsx(OptimizedImage,{src:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=6b9d4ce327fdacb16d5f9ef116545e60`,alt:`Code Writer and Code Executor`,\"data-og-width\":`1200`,width:`1200`,\"data-og-height\":`577`,height:`577`,\"data-path\":`docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=280\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=0a1cd1a4fe1a21bfe2ef7c66a9f86868 280w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=560\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=854fea25253e939b4b2a84d097ab6c23 560w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=840\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=0c696d74ed1f7a5c06817de08ce5645f 840w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=1100\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=9f24b57ab95553c88530481708a111db 1100w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=1650\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=14072e019be84625e8f3be9a3cbd1e8a 1650w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-execution-in-conversation.png?w=2500\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=86350ee66de94e494296dfd4c02db499 2500w`}),`\n`,_jsx(_components.p,{children:`The code writer agent can be powered by any LLM with code-writing capability.`}),`\n`,_jsx(_components.p,{children:`And the code executor agent is powered by a code executor.`}),`\n`,_jsxs(_components.p,{children:[`The following is an agent with a code writer role specified using `,_jsx(_components.code,{children:`system_message`}),`. The system message contains important instruction on how to use the code executor in the code executor agent.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`28`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`28`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# The code writer agent's system message is to instruct the LLM on how to use`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# the code executor in the code executor agent.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_system_message `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"\"\"You are a helpful AI assistant.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`Solve tasks using your coding and language skills.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`If you want the user to save the code in a file before executing it, put # filename: \u003cfilename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`Reply 'TERMINATE' in the end when everything is done.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"\"\"`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_agent `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ConversableAgent(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"code_writer_agent\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` system_message`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_system_message,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` llm_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`LLMConfig(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` api_type`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"openai\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`model`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"gpt-4o-mini\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`api_key`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`os.environ[`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"OPENAI_API_KEY\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`]`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ),`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` code_execution_config`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:`False`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Turn off code execution for this agent.`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`chat_result `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` code_executor_agent.initiate_chat(`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` code_writer_agent,`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` message`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Write Python code to calculate the 14th Fibonacci number.\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`66`,language:`shellsession`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellsession`,children:_jsxs(_components.code,{language:`shellsession`,numberOfLines:`66`,children:[_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> NO HUMAN INPUT RECEIVED.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`exitcode: 0 (execution succeeded)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Code output:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Scatter plot saved to scatter.png`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`['scatter.png', '6507ea07b63b45aabb027ade4e213de6.py']`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent (to code_writer_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Write Python code to calculate the 14th Fibonacci number.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_agent (to code_executor_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Sure, here is a Python code snippet to calculate the 14th Fibonacci number. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''python`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`def fibonacci(n):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` if(n \u003c= 0):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` return \"Input should be a positive integer.\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` elif(n == 1):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` return 0`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` elif(n == 2):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` return 1`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` else:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` fib = [0, 1]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` for i in range(2, n):`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` fib.append(fib[i-1] + fib[i-2])`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` return fib[n-1]`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`print(fibonacci(14))`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:\"This Python code defines a function `fibonacci(n)` which computes the n-th Fibonacci number. The function uses a list `fib` to store the Fibonacci numbers as they are computed, and then returns the (n-1)-th element as the n-th Fibonacci number due to zero-indexing in Python lists.\"})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> NO HUMAN INPUT RECEIVED.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent (to code_writer_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`exitcode: 0 (execution succeeded)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Code output:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`233`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_agent (to code_executor_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Great, the execution was successful and the 14th Fibonacci number is 233. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233... and so on, where each number is the sum of the previous two. Therefore, the 14th number in the Fibonacci series is 233.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`I hope this meets your expectations. If you have any other concerns or need further computations, feel free to ask.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`TERMINATE`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`Now we can try a more complex example that involves using external packages that can get data.`}),`\n`,_jsx(_components.p,{children:`Let’s say we want to get the stock price gains year-to-date for Tesla and Meta (formerly Facebook). We can also use the two agents with several iterations of conversation.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`8`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`8`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` datetime`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`today `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` datetime.datetime.now().strftime(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"%Y-%m-`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`%d`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`chat_result `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` code_executor_agent.initiate_chat(`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` code_writer_agent,`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` message`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`f`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Today is `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`{`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`today`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`. Write Python code to plot TSLA's and META's \"`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"stock price gains YTD, and save the plot to a file named 'stock_gains.png'.\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`97`,language:`shellsession`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellsession`,children:_jsxs(_components.code,{language:`shellsession`,numberOfLines:`97`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent (to code_writer_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Today is 2024-02-28. Write Python code to plot TSLA's and META's stock price gains YTD, and save the plot to a file named 'stock_gains.png'.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_agent (to code_executor_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`This task requires retrieving the historical data of the stocks from a reliable data source and calculating the Year-To-Date (YTD) gain values, and then plotting them. pandas_datareader library will be used for data retrieval, pandas will be used for data manipulation, and matplotlib for plotting.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Below is the Python code to achieve this. To start, please install the required libraries by running to the following command:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''sh`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`pip install yfinance pandas matplotlib`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Then run the python code:`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''python`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`# filename: stock_gains.py`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`import yfinance as yf`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`import pandas as pd`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`import matplotlib.pyplot as plt`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`from datetime import datetime`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`# define the tickers`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`tickers = ['TSLA', 'META']`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`# define the start and `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`end`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` dates`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`start_date = datetime(2024, 1, 1)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`end_date = datetime(2024, 2, 28)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`# dictionary to hold dataframes`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`dfs = {}`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`for ticker in tickers:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` # get the data for the stocks`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` df = yf.download(ticker, start_date, end_date)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` # get the close price and calculate the cumulative percentage gain`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` df['Gain'] = df['Close'].pct_change().cumsum()`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` # add to dictionary`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` dfs[ticker] = df`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`# plot`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.figure(figsize=(10, 5))`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`for ticker, df in dfs.items():`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:` plt.plot(df.index, df['Gain'], label=ticker)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.title('YTD Stock Price Gain')`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.xlabel('Date')`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.ylabel('Percentage Gain')`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.legend()`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.grid(True)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.savefig('stock_gains.png')`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`plt.close()`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`print(\"The 'stock_gains.png' file has been successfully saved\")`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`'''`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`This script will download the historical data for TSLA and META from the start of the year to the specified date and calculates the YTD gains. It then generates the plot showing these gains and saves it to 'stock_gains.png'.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Please save the script to a file named 'stock_gains.py' and run it using Python. Remember to have the correct start and end dates for the YTD value when running the script. If your Python version is below 3.9, you should update it to execute this code perfectly.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> NO HUMAN INPUT RECEIVED.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is sh)...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_executor_agent (to code_writer_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`exitcode: 0 (execution succeeded)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Code output:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Requirement already satisfied: yfinance in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (0.2.36)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Requirement already satisfied: pandas in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (2.1.4)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Requirement already satisfied: matplotlib in /Users/ekzhu/miniconda3/envs/autogen/lib/python3.11/site-packages (3.9.2)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`...`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`The 'stock_gains.png' file has been successfully saved`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`>>>>>>>> USING AUTO REPLY...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`code_writer_agent (to code_executor_agent):`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Great! The code executed successfully and the 'stock_gains.png' file has been saved successfully. This file contains the plot of TSLA's and META's stock price gains from the start of the year until February 28, 2024. You should now be able to view this image file in the same directory that you ran the script from.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`Please make sure to verify this image file. It should contain two plotted lines, each representing the percentage gain over the time for each stock (TSLA and META). The x-axis represents the date, and the y-axis represents the percentage gain. If everything looks correct, this would be the end of the task.`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`TERMINATE`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`--------------------------------------------------------------------------------`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`In the previous conversation, the code writer agent generated a code block to install necessary packages and another code block for a script to fetch the stock price and calculate gains year-to-date for Tesla and Meta. The code executor agent installed the packages, executed the script, and returned the results.`}),`\n`,_jsx(_components.p,{children:`Let’s take a look at the chart that was generated.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`3`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`3`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` IPython.display `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Image`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`Image(os.path.join(temp_dir, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"stock_gains.png\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`))`})]}),`\n`]})})}),`\n`,_jsx(OptimizedImage,{src:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=a3fbdb5121249d7aea99fd6b140942ba`,alt:`Code Executor Stock Chart`,\"data-og-width\":`1842`,width:`1842`,\"data-og-height\":`921`,height:`921`,\"data-path\":`docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=280\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=7455e6c0713a7f1093ad027024e33e88 280w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=560\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=5043501b77654e35a2c60080dd5cff1d 560w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=840\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=37ccd6326aec983f9c1985feecfe2822 840w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=1100\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=857ad95ba4aa2f0a5b5442ce59c9221e 1100w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=1650\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=2516d5d8bd1f514a51f0e038d3c83991 1650w, https://mintcdn.com/private-04b27de1/bqMUtTNHI7tnk6hN/docs/user-guide/advanced-concepts/assets/code-executor-stock-chart.png?w=2500\u0026fit=max\u0026auto=format\u0026n=bqMUtTNHI7tnk6hN\u0026q=85\u0026s=096b0a52710e16b1db4f9f91e0d4178a 2500w`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`5`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`5`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Clean up traces like code files and output in the file system between executions`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`temp_dir.cleanup()`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Stop the docker command line code executor`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`executor.stop()`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`4`,id:`command-line-or-jupyter-code-executor`,children:`Command Line or Jupyter Code Executor?`}),`\n`,_jsx(_components.p,{children:`The command line code executor does not keep any state in memory between executions of different code blocks it receives, as it writes each code block to a separate file and executes the code block in a new process.`}),`\n`,_jsx(_components.p,{children:`Contrast to the command line code executor, the Jupyter code executor runs all code blocks in the same Jupyter kernel, which keeps the state in memory between executions.`}),`\n`,_jsx(_components.p,{children:`The choice between command line and Jupyter code executor depends on the nature of the code blocks in agents’ conversation. If each code block is a “script” that does not use variables from previous code blocks, the command line code executor is a good choice. If some code blocks contain expensive computations (e.g., training a machine learning model and loading a large amount of data), and you want to keep the state in memory to avoid repeated computations, the Jupyter code executor is a better choice.`}),`\n`,_jsx(Heading,{level:`3`,id:`more-code-execution-examples`,children:`More Code Execution examples`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/use-cases/notebooks/notebooks/agentchat_auto_feedback_from_code_execution`,children:`Task Solving with Code Generation, Execution, and Debugging`})}),`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/use-cases/notebooks/notebooks/agentchat_human_feedback`,children:`Auto-Generated Agent Chat: Task Solving with Code Gen, Execution, Debugging \u0026 Human Feedback`})}),`\n`]}),`\n`,_jsx(Heading,{level:`3`,id:`api`,children:`API`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/ConversableAgent#conversableagent`,children:`ConversableAgent.code_execution_config`})}),`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/LocalCommandLineCodeExecutor`,children:`LocalCommandLineCodeExecutor`})}),`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/DockerCommandLineCodeExecutor`,children:`DockerCommandLineCodeExecutor`})}),`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/docs/api-reference/autogen/coding/jupyter/JupyterCodeExecutor`,children:`jupyter.JupyterCodeExecutor`})}),`\n`]}),`\n`,_jsx(`div`,{className:`edit-url-container`,children:_jsxs(`a`,{className:`edit-url`,href:`https://github.com/ag2ai/ag2/edit/main/website/docs/user-guide/advanced-concepts/code-execution.mdx`,target:`_blank`,children:[_jsx(Icon,{icon:`pen`,iconType:`solid`,size:`13px`}),` Edit this page`]})})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Code Execution","description":null,"href":"/docs/user-guide/advanced-concepts/code-execution"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Local Execution","slug":"local-execution","depth":2,"children":[]},{"title":"Docker Execution","slug":"docker-execution","depth":2,"children":[{"title":"Use Code Execution in Conversation","slug":"use-code-execution-in-conversation","depth":3,"children":[]},{"title":"Command Line or Jupyter Code Executor?","slug":"command-line-or-jupyter-code-executor","depth":4,"children":[]},{"title":"More Code Execution examples","slug":"more-code-execution-examples","depth":3,"children":[]},{"title":"API","slug":"api","depth":3,"children":[]}]}],"codeExamples":{}},"jsLinks":["/_mintlify/custom/private-04b27de1/eb75e01a19010b3e/docusaurus.config.js","/_mintlify/custom/private-04b27de1/2750b385f6fad3d7/mint-app-gallery-deps.js","/_mintlify/custom/private-04b27de1/00d416e040b2eaa6/mint-script.js"],"subdomain":"private-04b27de1","siteName":"AG2","title":"Code Execution - AG2","contextualStarterQuestions":[],"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-01-07T04:58:48.457Z","originalFileLocation":"docs/user-guide/advanced-concepts/code-execution.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"application-name\" content=\"AG2\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Code Execution - AG2\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"AG2\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/website/static/img/cover.png\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Code Execution - AG2\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/website/static/img/cover.png\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/private-04b27de1/5PK9RLmJtXeBrawe/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"AG2\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#4b9cd6\">\u003clink data-mint-head rel=\"canonical\" href=\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/docs/user-guide/advanced-concepts/code-execution.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://private-04b27de1.mintlify.app/#organization\",\"name\":\"AG2\",\"url\":\"https://private-04b27de1.mintlify.app\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/private-04b27de1/pvm3Z7mIWzwbwn7q/logo/ag2.svg?fit=max\u0026auto=format\u0026n=pvm3Z7mIWzwbwn7q\u0026q=85\u0026s=7e064e1c72768237baa4e260a7828dfe\"}},{\"@type\":\"WebSite\",\"@id\":\"https://private-04b27de1.mintlify.app#website\",\"name\":\"AG2\",\"url\":\"https://private-04b27de1.mintlify.app\",\"publisher\":{\"@id\":\"https://private-04b27de1.mintlify.app/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution#webpage\",\"url\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution\",\"name\":\"Code Execution\",\"dateModified\":\"2026-01-07T04:58:48.457Z\",\"isPartOf\":{\"@id\":\"https://private-04b27de1.mintlify.app#website\"},\"breadcrumb\":{\"@id\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"User Guide\",\"item\":\"https://private-04b27de1.mintlify.app/docs/user-guide/basic-concepts/installing-ag2\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced Concepts\",\"item\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/llm-configuration-deep-dive\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Code Execution\",\"item\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution#article\",\"headline\":\"Code Execution\",\"name\":\"Code Execution\",\"url\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution\",\"mainEntityOfPage\":{\"@id\":\"https://private-04b27de1.mintlify.app/docs/user-guide/advanced-concepts/code-execution#webpage\"},\"image\":\"https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/website/static/img/cover.png\",\"dateModified\":\"2026-01-07T04:58:48.457Z\",\"publisher\":{\"@id\":\"https://private-04b27de1.mintlify.app/#organization\"},\"isPartOf\":{\"@id\":\"https://private-04b27de1.mintlify.app#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"AG2\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"AG2\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/docs/user-guide/advanced-concepts/code-execution.md","siteName":"AG2","nav":{"global":null,"tabs":[{"tab":"Documentation","pages":[{"title":"Documentation","href":"/docs/quick-start"}]},{"tab":"User Guide","groups":[{"group":"User Guide","pages":[{"title":"Installing AG2","href":"/docs/user-guide/basic-concepts/installing-ag2"},{"group":"Basic Concepts","pages":[{"title":"LLM Configuration: Powering Agent Intelligence","sidebarTitle":"LLM Configuration: Powering Agent Intelligence","href":"/docs/user-guide/basic-concepts/llm-configuration"},{"title":"ConversableAgent: Building Intelligent Agents","sidebarTitle":"ConversableAgent: Building Intelligent Agents","href":"/docs/user-guide/basic-concepts/conversable-agent"},{"title":"Human in the Loop: Adding Human Oversight","sidebarTitle":"Human in the Loop: Adding Human Oversight","href":"/docs/user-guide/basic-concepts/human-in-the-loop"},{"title":"Agent Orchestration: Coordinating Multiple Agents","sidebarTitle":"Agent Orchestration: Coordinating Multiple Agents","href":"/docs/user-guide/basic-concepts/introducing-group-chat"},{"title":"Tools: Extending Agent Capabilities","sidebarTitle":"Tools: Extending Agent Capabilities","href":"/docs/user-guide/basic-concepts/introducing-tools"},{"title":"Structured Outputs: Ensuring Consistent Responses","sidebarTitle":"Structured Outputs: Ensuring Consistent Responses","href":"/docs/user-guide/basic-concepts/structured-outputs"}]},{"group":"Advanced Concepts","pages":[{"title":"LLM Config Deep-dive","href":"/docs/user-guide/advanced-concepts/llm-configuration-deep-dive"},{"group":"Agent Orchestration Deep Dive","pages":[{"title":"Orchestrating agents","sidebarTitle":"Overview","href":"/docs/user-guide/advanced-concepts/orchestration/orchestrations"},{"title":"Two-Agent Chat","href":"/docs/user-guide/advanced-concepts/orchestration/two-agent-chat"},{"title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/sequential-chat"},{"title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/nested-chat"},{"group":"Group Chats","pages":[{"sidebarTitle":"Overview","title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/group-chat/introduction"},{"sidebarTitle":"Tools and Functions","title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/group-chat/agent-tools-functions"},{"sidebarTitle":"Context Variables","title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/group-chat/context-variables"},{"title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/group-chat/handoffs"},{"sidebarTitle":"Orchestration Patterns","title":null,"href":"/docs/user-guide/advanced-concepts/orchestration/group-chat/patterns"}]},{"group":"Swarm","pages":[{"title":"Swarm deprecation","href":"/docs/user-guide/advanced-concepts/orchestration/swarm/deprecation"}]},{"title":"Ending a chat","href":"/docs/user-guide/advanced-concepts/orchestration/ending-a-chat"},{"group":"Pattern Cookbook","pages":[{"title":"Pattern Cookbook","sidebarTitle":"Overview","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/overview"},{"title":"Context-Aware Routing","sidebarTitle":"Context-Aware Routing","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/context_aware_routing"},{"title":"Escalation","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/escalation"},{"title":"Feedback Loop","sidebarTitle":"Feedback Loop","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/feedback_loop"},{"title":"Hierarchical / Tree","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/hierarchical"},{"title":"Organic","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/organic"},{"title":"Pipeline / Sequential Processing","sidebarTitle":"Pipeline / Sequential","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/pipeline"},{"title":"Redundant","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/redundant"},{"title":"Star / Hub-and-Spoke","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/star"},{"title":"Triage with Tasks","sidebarTitle":"Triage with Tasks","href":"/docs/user-guide/advanced-concepts/pattern-cookbook/triage_with_tasks"}]}]},{"group":"Tools","pages":[{"sidebarTitle":"Tools","title":null,"href":"/docs/user-guide/advanced-concepts/tools/index"},{"title":"Overview","sidebarTitle":"Overview","href":"/docs/user-guide/advanced-concepts/tools/basics"},{"title":"Controlling Tool Use","sidebarTitle":"Controlling Use","href":"/docs/user-guide/advanced-concepts/tools/controlling-use"},{"title":"Tools with Secrets","href":"/docs/user-guide/advanced-concepts/tools/tools-with-secrets"},{"group":"Model Context Protocol","pages":[{"title":"MCP Clients","href":"/docs/user-guide/advanced-concepts/tools/mcp/client"}]},{"group":"Interoperability","pages":[{"title":"LangChain Tools Integration","sidebarTitle":"LangChain Tools Integration","href":"/docs/user-guide/advanced-concepts/tools/interop/langchain"},{"title":"CrewAI Tools Integration","sidebarTitle":"CrewAI Tools Integration","href":"/docs/user-guide/advanced-concepts/tools/interop/crewai"},{"title":"PydanticAI Tools Integration","sidebarTitle":"PydanticAI Tools Integration","href":"/docs/user-guide/advanced-concepts/tools/interop/pydanticai"}]}]},{"group":"RealtimeAgent","pages":[{"title":"Overview","href":"/docs/user-guide/advanced-concepts/realtime-agent/index"},{"title":"RealtimeAgent over WebSocket Audio Adapter","sidebarTitle":"WebSocket Audio Adapter","href":"/docs/user-guide/advanced-concepts/realtime-agent/websocket"},{"title":"RealtimeAgent over WebRTC","sidebarTitle":"WebRTC Integration","href":"/docs/user-guide/advanced-concepts/realtime-agent/webrtc"},{"title":"RealtimeAgent over Twilio Audio Adapter","sidebarTitle":"Twilio Audio Adapter","href":"/docs/user-guide/advanced-concepts/realtime-agent/twilio"}]},{"title":"RAG","description":null,"href":"/docs/user-guide/advanced-concepts/rag"},{"title":"Code Execution","href":"/docs/user-guide/advanced-concepts/code-execution"}]},{"group":"Model Providers","pages":[{"title":"Amazon Bedrock","sidebarTitle":"Amazon Bedrock","description":null,"href":"/docs/user-guide/models/amazon-bedrock"},{"title":"Anthropic","sidebarTitle":"Anthropic","href":"/docs/user-guide/models/anthropic"},{"title":"Cerebras","sidebarTitle":"Cerebras","href":"/docs/user-guide/models/cerebras"},{"title":"Cohere","sidebarTitle":"Cohere","href":"/docs/user-guide/models/cohere"},{"title":"DeepSeek","sidebarTitle":"DeepSeek","href":"/docs/user-guide/models/deepseek-v3"},{"title":"Google Gemini","sidebarTitle":"Google Gemini","href":"/docs/user-guide/models/google-gemini"},{"title":"Google Vertex AI","sidebarTitle":"Google Vertex AI","href":"/docs/user-guide/models/google-vertexai"},{"title":"Groq","sidebarTitle":"Groq","href":"/docs/user-guide/models/groq"},{"group":"LiteLLM Proxy Server","pages":[{"title":"Installation","sidebarTitle":"Installation","href":"/docs/user-guide/models/litellm-proxy-server/installation"},{"title":"LiteLLM with OpenAI","sidebarTitle":"LiteLLM with OpenAI","href":"/docs/user-guide/models/litellm-proxy-server/openai"},{"title":"LiteLLM with Azure","sidebarTitle":"LiteLLM with Azure","href":"/docs/user-guide/models/litellm-proxy-server/azure"},{"title":"LiteLLM with WatsonX","sidebarTitle":"LiteLLM with WatsonX","href":"/docs/user-guide/models/litellm-proxy-server/watsonx"}]},{"title":"LM Studio","sidebarTitle":"LM Studio","href":"/docs/user-guide/models/lm-studio"},{"title":"Mistral AI","sidebarTitle":"Mistral AI","href":"/docs/user-guide/models/mistralai"},{"title":"Ollama","sidebarTitle":"Ollama","href":"/docs/user-guide/models/ollama"},{"title":"OpenAI","sidebarTitle":"OpenAI","href":"/docs/user-guide/models/openai"},{"title":"Together AI","sidebarTitle":"Together AI","href":"/docs/user-guide/models/togetherai"},{"title":"vLLM","sidebarTitle":"vLLM","href":"/docs/user-guide/models/vLLM"}]},{"group":"Reference Agents","pages":[{"title":"Overview","href":"/docs/user-guide/reference-agents/index"},{"title":"CaptainAgent","sidebarTitle":"CaptainAgent","href":"/docs/user-guide/reference-agents/captainagent"},{"group":"Communication Platforms","pages":[{"title":"Communication Agents","sidebarTitle":"Overview","href":"/docs/user-guide/reference-agents/communication-platforms/overview"},{"title":"DiscordAgent","sidebarTitle":"DiscordAgent","href":"/docs/user-guide/reference-agents/communication-platforms/discordagent"},{"title":"SlackAgent","sidebarTitle":"SlackAgent","href":"/docs/user-guide/reference-agents/communication-platforms/slackagent"},{"title":"TelegramAgent","sidebarTitle":"TelegramAgent","href":"/docs/user-guide/reference-agents/communication-platforms/telegramagent"}]},{"title":"DocAgent","sidebarTitle":"DocAgent","href":"/docs/user-guide/reference-agents/docagent"},{"title":"DocAgent Performance","sidebarTitle":"DocAgent Performance","href":"/docs/user-guide/reference-agents/docagent-performance"},{"title":"ReasoningAgent","sidebarTitle":"ReasoningAgent","href":"/docs/user-guide/reference-agents/reasoningagent"},{"title":"DeepResearchAgent","sidebarTitle":"DeepResearchAgent","href":"/docs/user-guide/reference-agents/deepresearchagent"},{"title":"WebSurferAgent","sidebarTitle":"WebSurferAgent","href":"/docs/user-guide/reference-agents/websurferagent"},{"title":"WikipediaAgent","sidebarTitle":"WikipediaAgent","href":"/docs/user-guide/reference-agents/wikipediaagent"}]},{"group":"Reference Tools","pages":[{"title":"Overview","href":"/docs/user-guide/reference-tools/index"},{"group":"Google APIs","pages":[{"title":"YouTube Search Tool","href":"/docs/user-guide/reference-tools/google-api/youtube-search"},{"title":"Google Search Tool","href":"/docs/user-guide/reference-tools/google-api/google-search"},{"title":"Google Drive Tools","href":"/docs/user-guide/reference-tools/google-api/google-drive"}]},{"title":"Perplexity Search Tool","href":"/docs/user-guide/reference-tools/perplexity-search"},{"title":"Wikipedia Search Tools","href":"/docs/user-guide/reference-tools/wikipedia-search"},{"title":"Browser Use","href":"/docs/user-guide/reference-tools/browser-use"},{"title":"Deep Research","href":"/docs/user-guide/reference-tools/deep-research"},{"group":"Communication Platforms","pages":[{"title":"Discord Tools","href":"/docs/user-guide/reference-tools/communication-platforms/discord"},{"title":"Slack Tools","href":"/docs/user-guide/reference-tools/communication-platforms/slack"},{"title":"Telegram Tools","href":"/docs/user-guide/reference-tools/communication-platforms/telegram"}]},{"title":"Crawl4AI","sidebarTitle":"Crawl4AI","href":"/docs/user-guide/reference-tools/crawl4ai"}]}]}]},{"tab":"API References","pages":[{"title":"API References","href":"/docs/api-reference/autogen/overview"}]},{"tab":"Use Cases","pages":[{"title":"Use Cases","href":"/docs/use-cases/use-cases/customer-service"}]},{"tab":"User Stories","pages":[{"title":"User Stories","href":"/docs/user-stories/2025-04-30-Cegid-Pulse-OS"}]},{"tab":"Contribute","pages":[{"title":"Contribute","href":"/docs/contributor-guide/contributing"}]},{"tab":"FAQs","pages":[{"title":"FAQs","href":"/docs/faq/FAQ"}]},{"tab":"Ecosystem","pages":[{"title":"Ecosystem","href":"/docs/ecosystem/agentops"}]},{"tab":"Blog","pages":[{"title":"Blog","href":"/docs/blog/2025-05-07-AG2-Copilot-Integration"}]}]},"basePath":"","title":"Code Execution - AG2","deploymentId":"dpl_EwfxjeKENdVkfzdag8rM6mKzcTUJ","buildId":"dpl_EwfxjeKENdVkfzdag8rM6mKzcTUJ","dynamics":["a74be98ba7"],"chunks":["zoom-image-lightbox-uS0aI388.js","lightbox-handle-r17bVUz3.js"]}