logo
Menu
AI running it’s own code! : Agentic Code Interpreter

AI running it’s own code! : Agentic Code Interpreter

Public Preview: Code Interpreter for Agents for Amazon Bedrock - With sample code.

Mike Chambers
Amazon Employee
Published Jul 18, 2024
In the world of AI and large language models (LLMs), one limitation has been their struggle with complex mathematical operations and data analysis. While LLMs excel at natural language understanding and generation, they often fall short when it comes to precise calculations or handling large datasets. Today, we're exploring an exciting new feature for Agents for Amazon Bedrock that addresses this limitation: the Code Interpreter.

The Power of Code Execution in AI Agents

Large language models are incredibly versatile, but they're not designed for complex mathematical computations or data analysis. However, they excel at writing code. By combining an LLM's code-writing abilities with a secure execution environment, we can create AI agents capable of performing sophisticated analytical tasks.
Let's dive into how we can implement this powerful feature using Agents for Amazon Bedrock.

Setting Up an Agent with Code Interpreter

To get started, we'll create an agent with Code Interpreter capabilities. Here's a quick step-by-step guide in the AWS console, before we dive in to some code:
Advanced settings > Code Interpreter
Select Code Interpreter in Advanced settings
  1. Choose Your Model: We'll use the Claude 3 Sonnet model for this example.
  2. Set Agent Instructions: Provide detailed instructions for your agent, focusing on problem-solving, data analysis, and utilizing the Code Interpreter tool. (See example in the code below)
  3. Configure the Agent: In the Amazon Bedrock console, create a new agent and select the Claude 3 Sonnet model.
  4. Enable Code Interpreter: In the agent's settings, enable the Code Interpreter feature. This connects a Python execution sandbox to your agent.

Implementing Code Interpreter Programmatically

While you can set up the agent through the console, let's look at how to do this programmatically using Python and the AWS SDK (boto3). This approach allows for easier integration into your applications.
First, import the necessary libraries:
Next, set up your AWS region and create the Bedrock Agent:
After creating the agent, we need to add the Code Interpreter action group:
Finally, prepare the agent and create an alias:

Interacting with the Code Interpreter Agent

Now that our agent is set up, let's create a function to interact with it:
This function handles sending requests to the agent, processing its responses, and handling any files (like graphs) that the agent generates.

Putting It All Together: Complex Data Analysis

Let's test our Code Interpreter agent with a complex data analysis task. We'll ask it to analyze some billing data and create a visualization:
The agent will process this request by:
  1. Parsing the provided data
  2. Writing Python code to analyze the data and create a bar graph
  3. Executing the code in its sandbox environment
  4. Generating the graph as an image file
  5. Providing an interpretation of the results
The output will includes the generated graph and a textual analysis of the spending patterns across different product categories.
Graph created by Amazon Bedrock
Generated plot
And the text output from the agent:
The bar chart shows the total spend amount for each product category based on the provided billing data. The x-axis displays the category names (Electronics, Home, and Clothing), and the y-axis represents the total spend amount in dollars.From the chart, we can observe that the Electronics category has the highest total spend at around $4,360. This is significantly higher than the other two categories, Home and Clothing, which have total spends of around $700 and $340, respectively.One notable observation is the large difference in spend between the Electronics category and the other two categories. This could indicate that the customer or business has a higher demand or preference for electronic products compared to home and clothing items during the given time period.Overall, the chart provides a clear visual representation of the spending patterns across different product categories, highlighting the dominance of the Electronics category in terms of total spend.

Extending the Capabilities

The Code Interpreter isn't limited creating graphs, during my experiments I have used it to perform a wide variety of data processing tasks, including training ML models and performing ML inference. We can also use it for other data manipulation tasks. For instance, we can ask it to format our data into JSON:
This command will create a JSON file with our structured data, which can be easily used in subsequent processing steps or other applications.

Conclusion

The addition of Code Interpreter to Agents for Amazon Bedrock represents a significant step forward in the capabilities of the service. By combining the natural language understanding of large language models with the ability to execute code, we can create powerful tools for data analysis, visualization, and complex problem-solving.
This feature opens up a world of possibilities for businesses and developers. From financial analysis to scientific computing, the ability to seamlessly integrate code execution with natural language interactions can streamline workflows and unlock new insights from data.
As this feature is currently in public preview, now is the perfect time to experiment with it and provide feedback. The potential applications are vast, and your input can help shape the future of this technology.
Remember, when implementing Code Interpreter in your own projects, consider the following:
  1. Security: While the execution environment is sandboxed, always be mindful of the data you're processing.
  2. Performance: Complex computations may take some time, so plan your application architecture accordingly.
  3. User Experience: Think about how to present the results of code execution in a way that's meaningful to your end-users.
The Code Interpreter feature in Amazon Bedrock Agents is a powerful tool that bridges the gap between natural language AI and computational capabilities. As we continue to explore and develop these technologies, we're moving closer to creating truly intelligent and versatile AI assistants.
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

3 Comments