Lab 2 Intro to the Code Interpreter
Introduction¶
The Azure AI Agent Service Code Interpreter enables the LLM to generate Python code for tasks such as creating charts or performing complex data analyses based on user queries. It leverages natural language processing (NLP), sales data from an SQLite database, and user prompts to automate code generation. The LLM-generated Python code executes within a secure sandbox environment, utilizing a restricted subset of Python to ensure safe and controlled execution.
Lab Exercise¶
In this lab, you'll enable the Code Interpreter to execute Python code generated by the LLM.
-
Open the
main.py
. -
Uncomment the following lines by removing the "# " characters
# INSTRUCTIONS_FILE = "instructions/instructions_code_interpreter.txt # code_interpreter = CodeInterpreterTool() # toolset.add(code_interpreter)
Warning
The lines to be uncommented are not adjacent. When removing the # character, ensure you also delete the space that follows it.
-
Review the code in the
main.py
file.After uncommenting, your code should look like this:
INSTRUCTIONS_FILE = "instructions/instructions_function_calling.txt" INSTRUCTIONS_FILE = "instructions/instructions_code_interpreter.txt" # INSTRUCTIONS_FILE = "instructions/instructions_file_search.txt" async def add_agent_tools(): """Add tools for the agent.""" # Add the functions tool toolset.add(functions) # Add the code interpreter tool code_interpreter = CodeInterpreterTool() toolset.add(code_interpreter) # Add the tents data sheet to a new vector data store # vector_store = await utilities.create_vector_store( # project_client, # files=[TENTS_DATA_SHEET_FILE], # vector_name_name="Contoso Product Information Vector Store", # ) # file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id]) # toolset.add(file_search_tool)
Review the Instructions¶
Review the instructions/instructions_code_interpreter.txt file. This file provides the LLM with specific instructions on how to utilize the Code Interpreter in the Tools section.
Run the Agent App¶
- Press F5 to run the app.
- In the terminal, the app will start, and the agent app will prompt you to Enter your query.
Start a Conversation with the Agent¶
Try these questions:
-
Show sales by region as a pie chart
Once the task is complete, the pie chart image will be saved in the files folder. Open the folder in VS Code and click on the image file to view it.
Info
This might feel like magic, so what’s happening behind the scenes to make it all work?
The LLM orchestrates the following steps:
-
The LLM generates a SQL query to answer the user's question. In this example, the query is:
SELECT region, SUM(revenue) AS total_revenue FROM sales_data GROUP BY region;
-
The LLM asks the agent app to call the async_fetch_sales_data_using_sqlite_query function. The SQL command is executed, and the resulting data is returned to the LLM.
- Using the returned data, the LLM writes Python code to create a Pie Chart.
- Finally, the Code Interpreter executes the Python code to generate the chart.
-
-
Download the sales by region data
Once the task is complete, check the files folder to see the downloaded file.
Info
By default, the instructions specify that data downloads in CSV format. You can request other formats, such as JSON or Excel, by including the desired format in your query (e.g., ‘Download as JSON’).
-
Download as JSON
Once the task is complete, check the files folder to see the downloaded file.
-
Continue asking questions about Contoso sales data to see the Code Interpreter in action.
Stop the Agent App¶
When you're done, type exit, or press Shift+F5 to stop the agent app.