0_basic-agent/AzureAIAgent/bing-grounding-test.ipynb (137 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bing Grounding with Azure AI Agent\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created message, ID: msg_oEvhGM0XV8xxIWkU4mmvnBlR\n", "Message ID: msg_nSfjXXUp9RmCwkGhdnbVKoIx\n", "Role: assistant\n", "Content: Today, April 9, 2025, in Seoul, South Korea, the weather is mildly warm with a high of 62°F (about 16°C) and a low of 50°F (about 10°C). There is a slight chance of rain with expected precipitation of about 0.01 inches【3:0†source】.\n", "Created At: 1744198126\n", "Metadata: {}\n", "URL: https://www.weather25.com/asia/south-korea/seoul?page=month&month=April\n", "Title: Seoul weather in April 2025 | Seoul 14 day weather\n", "--------------------------------------------------\n" ] } ], "source": [ "import os\n", "from azure.ai.projects.models import BingGroundingTool\n", "from azure.ai.projects import AIProjectClient\n", "from azure.identity import DefaultAzureCredential\n", "USER_INPUTS = [\n", " \"What is today's weather in South Korea?\",\n", " \"What is the new hotels in NYC 2025?\",\n", "]\n", "\n", "creds = DefaultAzureCredential()\n", "\n", "project_client = AIProjectClient.from_connection_string(\n", " credential=creds,\n", " conn_str=os.getenv(\"AZURE_AI_AGENT_PROJECT_CONNECTION_STRING\"),\n", ")\n", "bing_connection = project_client.connections.get(\n", " connection_name=os.getenv(\"BING_GROUNDING_CONNECTION_NAME\"),\n", ")\n", "conn_id = bing_connection.id\n", "\n", "\n", "# 1. Create an agent with a code interpreter on the Azure AI agent service\n", "bing = BingGroundingTool(connection_id=conn_id)\n", "with project_client:\n", " agent = project_client.agents.create_agent(\n", " model=os.getenv(\"AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME\"),\n", " name=\"my-bing-agent\",\n", " instructions=\"You are a helpful assistant\",\n", " tools=bing.definitions,\n", " headers={\"x-ms-enable-preview\": \"true\"}\n", " )\n", " #if you want to use existing agent, you can use the following code\n", " # agent = project_client.agents.get_agent(agent_id=\"your agent id\")\n", " \n", " # Create thread for communication\n", " thread = project_client.agents.create_thread()\n", " \n", " # Create message to thread\n", " message = project_client.agents.create_message(\n", " thread_id=thread.id,\n", " role=\"user\",\n", " content=USER_INPUTS[0],\n", " )\n", " print(f\"Created message, ID: {message.id}\")\n", "\n", " run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)\n", " \n", " run_steps = project_client.agents.list_run_steps(run_id=run.id, thread_id=thread.id)\n", " run_steps_data = run_steps['data']\n", " \n", " if run.status == \"failed\":\n", " print(f\"Run failed: {run.last_error}\")\n", "\n", " #Delete the assistant when done\n", " project_client.agents.delete_agent(agent.id)\n", " \n", " messages = project_client.agents.list_messages(thread_id=thread.id)\n", " \n", " for msg in messages['data']:\n", " if msg['role'] == 'assistant':\n", " print(f\"Message ID: {msg['id']}\")\n", " print(f\"Role: {msg['role']}\")\n", " print(f\"Content: {msg['content'][0]['text']['value']}\")\n", " print(f\"Created At: {msg['created_at']}\")\n", " print(f\"Metadata: {msg['metadata']}\")\n", " \n", " # Print URL information if available\n", " annotations = msg['content'][0]['text'].get('annotations', [])\n", " for annotation in annotations:\n", " if annotation['type'] == 'url_citation':\n", " url_info = annotation['url_citation']\n", " print(f\"URL: {url_info['url']}\")\n", " print(f\"Title: {url_info['title']}\")\n", " print(\"-\" * 50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv_agent", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" } }, "nbformat": 4, "nbformat_minor": 2 }