|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": null, |
| 5 | + "execution_count": 1, |
6 | 6 | "metadata": {},
|
7 | 7 | "outputs": [],
|
8 | 8 | "source": [
|
9 | 9 | "import os\n",
|
10 | 10 | "from dotenv import load_dotenv\n",
|
11 | 11 | "\n",
|
12 | 12 | "from azure.ai.projects import AIProjectClient\n",
|
13 |
| - "from azure.ai.projects.models import CodeInterpreterTool\n", |
| 13 | + "from azure.ai.agents.models import CodeInterpreterTool\n", |
14 | 14 | "from azure.identity import DefaultAzureCredential\n",
|
15 | 15 | "from typing import Any\n",
|
16 | 16 | "from pathlib import Path\n",
|
17 |
| - "from datetime import datetime\n", |
18 |
| - "\n" |
| 17 | + "from datetime import datetime\n" |
19 | 18 | ]
|
20 | 19 | },
|
21 | 20 | {
|
22 | 21 | "cell_type": "code",
|
23 |
| - "execution_count": null, |
| 22 | + "execution_count": 2, |
24 | 23 | "metadata": {},
|
25 | 24 | "outputs": [],
|
26 | 25 | "source": [
|
27 | 26 | "load_dotenv()\n",
|
28 |
| - "project_client = AIProjectClient.from_connection_string(\n", |
29 |
| - " credential=DefaultAzureCredential(), conn_str=os.environ[\"PROJECT_CONNECTION_STRING\"]\n", |
| 27 | + "project_endpoint = os.environ[\"PROJECT_ENDPOINT\"]\n", |
| 28 | + "\n", |
| 29 | + "project_client = AIProjectClient(\n", |
| 30 | + " endpoint=project_endpoint,\n", |
| 31 | + " # Use Azure Default Credential for authentication\n", |
| 32 | + " credential=DefaultAzureCredential(),\n", |
30 | 33 | ")"
|
31 | 34 | ]
|
32 | 35 | },
|
33 | 36 | {
|
34 | 37 | "cell_type": "code",
|
35 | 38 | "execution_count": null,
|
36 | 39 | "metadata": {},
|
37 |
| - "outputs": [], |
| 40 | + "outputs": [ |
| 41 | + { |
| 42 | + "data": { |
| 43 | + "text/html": [ |
| 44 | + "<h2>Azure AI Agent Execution</h2><div><strong>Created agent</strong> with ID: asst_BehQldbWIkw0LsJPj1N38FA8</div><div><strong>Created thread</strong> with ID: thread_cvWuXbcYkne8rovyAbzV8v1N</div><div style='margin:15px 0; padding:10px; background-color:#f5f5f5; border-left:4px solid #007bff; border-radius:4px;'><strong>User:</strong><br><div style='margin-left:15px'>Could you please create a bar chart for the operating profit using the following data and provide the file to me? Bali: 100 Travelers, Paris: 356 Travelers, London: 900 Travelers, Tokyo: 850 Travellers</div></div><div style='color:#007bff'><i>Processing request...</i></div>" |
| 45 | + ], |
| 46 | + "text/plain": [ |
| 47 | + "<IPython.core.display.HTML object>" |
| 48 | + ] |
| 49 | + }, |
| 50 | + "metadata": {}, |
| 51 | + "output_type": "display_data" |
| 52 | + }, |
| 53 | + { |
| 54 | + "ename": "AttributeError", |
| 55 | + "evalue": "'AgentsClient' object has no attribute 'list_messages'", |
| 56 | + "output_type": "error", |
| 57 | + "traceback": [ |
| 58 | + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", |
| 59 | + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", |
| 60 | + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 137\u001b[39m\n\u001b[32m 134\u001b[39m display(Image(img_file))\n\u001b[32m 136\u001b[39m \u001b[38;5;66;03m# Execute the function\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m137\u001b[39m \u001b[38;5;28;01mawait\u001b[39;00m run_agent_with_visualization()\n", |
| 61 | + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 58\u001b[39m, in \u001b[36mrun_agent_with_visualization\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m 55\u001b[39m html_output += \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33m<div style=\u001b[39m\u001b[33m'\u001b[39m\u001b[33mcolor:red\u001b[39m\u001b[33m'\u001b[39m\u001b[33m><strong>Run failed:</strong> \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrun.last_error\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m</div>\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 57\u001b[39m \u001b[38;5;66;03m# Get messages from the thread\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m58\u001b[39m messages = \u001b[43mproject_client\u001b[49m\u001b[43m.\u001b[49m\u001b[43magents\u001b[49m\u001b[43m.\u001b[49m\u001b[43mlist_messages\u001b[49m(thread_id=thread.id)\n\u001b[32m 60\u001b[39m \u001b[38;5;66;03m# Format assistant response\u001b[39;00m\n\u001b[32m 61\u001b[39m html_output += \u001b[33m\"\u001b[39m\u001b[33m<div style=\u001b[39m\u001b[33m'\u001b[39m\u001b[33mmargin:15px 0; padding:10px; background-color:#f0f7ff; border-left:4px solid #28a745; border-radius:4px;\u001b[39m\u001b[33m'\u001b[39m\u001b[33m>\u001b[39m\u001b[33m\"\u001b[39m\n", |
| 62 | + "\u001b[31mAttributeError\u001b[39m: 'AgentsClient' object has no attribute 'list_messages'" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
38 | 66 | "source": [
|
39 | 67 | "from IPython.display import display, HTML, Image\n",
|
40 | 68 | "from pathlib import Path\n",
|
|
50 | 78 | " # The CodeInterpreterTool needs to be included in creation of the agent\n",
|
51 | 79 | " # Ensure to set the correct model name as deployed in Azure AI Foundry for your use case\n",
|
52 | 80 | " agent = project_client.agents.create_agent(\n",
|
53 |
| - " model=\"gpt-4o-mini\",\n", |
| 81 | + " model=\"gpt-4o\",\n", |
54 | 82 | " name=\"my-agent\",\n",
|
55 | 83 | " instructions=\"You are helpful agent\",\n",
|
56 | 84 | " tools=code_interpreter.definitions,\n",
|
57 |
| - " tool_resources=code_interpreter.resources,\n", |
58 | 85 | " )\n",
|
| 86 | + "\n", |
| 87 | + " \n", |
59 | 88 | " html_output += f\"<div><strong>Created agent</strong> with ID: {agent.id}</div>\"\n",
|
60 | 89 | "\n",
|
61 | 90 | " # Create a thread\n",
|
62 |
| - " thread = project_client.agents.create_thread()\n", |
| 91 | + " thread = project_client.agents.threads.create()\n", |
63 | 92 | " html_output += f\"<div><strong>Created thread</strong> with ID: {thread.id}</div>\"\n",
|
64 | 93 | "\n",
|
65 | 94 | " # User query - display nicely\n",
|
|
70 | 99 | " html_output += \"</div>\"\n",
|
71 | 100 | "\n",
|
72 | 101 | " # Create a message\n",
|
73 |
| - " message = project_client.agents.create_message(\n", |
| 102 | + " message = project_client.agents.messages.create(\n", |
74 | 103 | " thread_id=thread.id,\n",
|
75 | 104 | " role=\"user\",\n",
|
76 | 105 | " content=user_query,\n",
|
|
81 | 110 | " html_output + \"<div style='color:#007bff'><i>Processing request...</i></div>\"))\n",
|
82 | 111 | "\n",
|
83 | 112 | " # Execute the run\n",
|
84 |
| - " run = project_client.agents.create_and_process_run(\n", |
| 113 | + " run = project_client.agents.runs.create_and_process(\n", |
85 | 114 | " thread_id=thread.id, agent_id=agent.id)\n",
|
86 | 115 | "\n",
|
87 | 116 | " # Update status\n",
|
|
92 | 121 | " html_output += f\"<div style='color:red'><strong>Run failed:</strong> {run.last_error}</div>\"\n",
|
93 | 122 | "\n",
|
94 | 123 | " # Get messages from the thread\n",
|
95 |
| - " messages = project_client.agents.list_messages(thread_id=thread.id)\n", |
| 124 | + " messages = project_client.agents.messages.list(thread_id=thread.id)\n", |
96 | 125 | "\n",
|
97 | 126 | " # Format assistant response\n",
|
98 | 127 | " html_output += \"<div style='margin:15px 0; padding:10px; background-color:#f0f7ff; border-left:4px solid #28a745; border-radius:4px;'>\"\n",
|
|
191 | 220 | "name": "python",
|
192 | 221 | "nbconvert_exporter": "python",
|
193 | 222 | "pygments_lexer": "ipython3",
|
194 |
| - "version": "3.13.1" |
| 223 | + "version": "3.12.11" |
195 | 224 | }
|
196 | 225 | },
|
197 | 226 | "nbformat": 4,
|
|
0 commit comments