-
Notifications
You must be signed in to change notification settings - Fork 311
Migrating code-interpreter module to the samples folder. #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kdestin
merged 11 commits into
Azure-Samples:main
from
Vasanthik07:code_interpreter_forked
Feb 5, 2025
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f591317
Migrating code-interpreter module to the samples folder.
Vasanthik07 bac8fcf
Added data dir, quickstart to the Agents dir. Removed file path expos…
Vasanthik07 a510c84
ran pre-commit before commit new changes
Vasanthik07 af76731
relative path added
Vasanthik07 ade2db4
tested
Vasanthik07 492fa85
openai file added
Vasanthik07 5f7c3fb
Renamed Quickstart.md to README.md and added YAML front-matter to REA…
Vasanthik07 6fce5cd
Merge branch 'main' into code_interpreter_forked
Vasanthik07 f80bbdb
added YAML front-matter to readme.md
Vasanthik07 ecbce6e
Merge branch 'code_interpreter_forked' of https://github.com/Vasanthi…
Vasanthik07 6c2d3d8
fix: Move frontmatter to top
kdestin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
scenarios/Agents/samples/code-interpreter/python-code-interpreter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
|
|
||
|
|
||
| """ | ||
| FILE: python-sample.py | ||
|
|
||
| DESCRIPTION: | ||
| This sample demonstrates how to use agent operations with code interpreter from | ||
| the Azure Agents service using a synchronous client. | ||
|
|
||
| USAGE: | ||
| python python-sample.py | ||
|
|
||
| Before running the sample: | ||
|
|
||
| pip install azure-ai-projects azure-identity | ||
|
|
||
| Set this environment variables with your own values: | ||
| PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project. | ||
| """ | ||
|
|
||
| import os | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.ai.projects.models import CodeInterpreterTool | ||
| from azure.ai.projects.models import FilePurpose | ||
| from azure.identity import DefaultAzureCredential | ||
| from pathlib import Path | ||
|
|
||
| # Create an Azure AI Client from a connection string, copied from your AI Studio project. | ||
| # At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>" | ||
| # Customer needs to login to Azure subscription via Azure CLI and set the environment variables | ||
| # | ||
| project_client = AIProjectClient.from_connection_string( | ||
| credential=DefaultAzureCredential(), conn_str=os.environ["PROJECT_CONNECTION_STRING"] | ||
| ) | ||
|
|
||
| with project_client: | ||
| # upload a file and wait for it to be processed | ||
| file = project_client.agents.upload_file_and_poll( | ||
| file_path=os.environ["FILE_PATH"], | ||
| purpose=FilePurpose.AGENTS | ||
| # File path = "../../data/nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS | ||
| ) | ||
| print(f"Uploaded file, file ID: {file.id}") | ||
|
|
||
| # instantiate the CodeInterpreterTool with the necessary files | ||
| code_interpreter = CodeInterpreterTool(file_ids=[file.id]) | ||
|
|
||
| # notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment | ||
| agent = project_client.agents.create_agent( | ||
| model="gpt-4o-mini", | ||
| name="my-agent", | ||
| instructions="You are helpful agent", | ||
| tools=code_interpreter.definitions, | ||
| tool_resources=code_interpreter.resources, | ||
| ) | ||
| print(f"Created agent, agent ID: {agent.id}") | ||
|
|
||
| # create a thread | ||
| thread = project_client.agents.create_thread() | ||
| print(f"Created thread, thread ID: {thread.id}") | ||
|
|
||
| # create a message | ||
| message = project_client.agents.create_message( | ||
| thread_id=thread.id, | ||
| role="user", | ||
| content="Could you please create bar chart in the TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?", | ||
| ) | ||
| print(f"Created message, message ID: {message.id}") | ||
|
|
||
| # create and execute a run | ||
| run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id) | ||
| print(f"Run finished with status: {run.status}") | ||
|
|
||
| if run.status == "failed": | ||
| # Check if you got "Rate limit is exceeded.", then you want to get more quota | ||
| print(f"Run failed: {run.last_error}") | ||
|
|
||
| # delete the original file from the agent to free up space (note: this does not delete your version of the file) | ||
| project_client.agents.delete_file(file.id) | ||
| print("Deleted file") | ||
|
|
||
| # print the messages from the agent | ||
| messages = project_client.agents.get_messages(thread_id=thread.id) | ||
| print(f"Messages: {messages}") | ||
|
|
||
| # get the most recent message from the assistant | ||
| last_msg = messages.get_last_text_message_by_sender("assistant") | ||
| if last_msg: | ||
| print(f"Last Message: {last_msg.text.value}") | ||
|
|
||
| # save the newly created file | ||
| for image_content in messages.image_contents: | ||
| print(f"Image File ID: {image_content.image_file.file_id}") | ||
| file_name = f"{image_content.image_file.file_id}_image_file.png" | ||
| project_client.agents.save_file(file_id=image_content.image_file.file_id, file_name=file_name) | ||
| print(f"Saved image file to: {Path.cwd() / file_name}") | ||
|
|
||
| # iterates through file_path_annotations in messages and prints details for each annotation | ||
| for file_path_annotation in messages.file_path_annotations: | ||
| print("File Paths:") | ||
| print(f"Type: {file_path_annotation.type}") | ||
| print(f"Text: {file_path_annotation.text}") | ||
| print(f"File ID: {file_path_annotation.file_path.file_id}") | ||
| print(f"Start Index: {file_path_annotation.start_index}") | ||
| print(f"End Index: {file_path_annotation.end_index}") | ||
|
|
||
| # delete the agent once done | ||
| project_client.agents.delete_agent(agent.id) | ||
| print("Deleted agent") |
Vasanthik07 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Code interpreter | ||
|
|
||
| Code Interpreter allows the Agent to write and run Python code in a sandboxed execution environment. With Code Interpreter enabled, your Agent can run code iteratively to solve more challenging code, math, and data analysis problems. | ||
|
|
||
|
|
||
| ## Examples | ||
|
|
||
| Run the code samples below and view the output. | ||
|
|
||
| >[!NOTE] | ||
| > Be sure that you've [installed the SDK](../README.md#install-the-sdk-package) for your language. | ||
|
|
||
| * [Python](./python-code-interpreter.py) | ||
|
|
||
| # Additional samples | ||
|
|
||
| * [Math tutor](https://github.com/openai/openai-cookbook/blob/main/examples/data/oai_docs/tool-code-interpreter.txt) | ||
|
|
||
|
|
||
| ## Supported file types | ||
|
|
||
| |File format|MIME Type| | ||
| |---|---| | ||
| |.c| text/x-c | | ||
| |.cpp|text/x-c++ | | ||
| |.csv|application/csv| | ||
| |.docx|application/vnd.openxmlformats-officedocument.wordprocessingml.document| | ||
| |.html|text/html| | ||
| |.java|text/x-java| | ||
| |.json|application/json| | ||
| |.md|text/markdown| | ||
| |.pdf|application/pdf| | ||
| |.php|text/x-php| | ||
| |.pptx|application/vnd.openxmlformats-officedocument.presentationml.presentation| | ||
| |.py|text/x-python| | ||
| |.py|text/x-script.python| | ||
| |.rb|text/x-ruby| | ||
| |.tex|text/x-tex| | ||
| |.txt|text/plain| | ||
| |.css|text/css| | ||
| |.jpeg|image/jpeg| | ||
| |.jpg|image/jpeg| | ||
| |.js|text/javascript| | ||
| |.gif|image/gif| | ||
| |.png|image/png| | ||
| |.tar|application/x-tar| | ||
| |.ts|application/typescript| | ||
| |.xlsx|application/vnd.openxmlformats-officedocument.spreadsheetml.sheet| | ||
| |.xml|application/xml or "text/xml"| | ||
| |.zip|application/zip| |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.