|
39 | 39 | "azure_openai_endpoint = os.environ[\"AZURE_OPENAI_ENDPOINT\"]\n", |
40 | 40 | "azure_openai_gpt_deployment = os.getenv(\"AZURE_OPENAI_GPT_DEPLOYMENT\", \"gpt-4o\")\n", |
41 | 41 | "azure_openai_gpt_model = os.getenv(\"AZURE_OPENAI_GPT_MODEL\", \"gpt-4o\")\n", |
| 42 | + "azure_openai_embedding_deployment = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\", \"text-embedding-3-large\")\n", |
| 43 | + "azure_openai_embedding_model = os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\", \"text-embedding-3-large\")\n", |
42 | 44 | "agent_name = os.getenv(\"AZURE_SEARCH_AGENT_NAME\", \"earth-search-agent\")\n", |
43 | 45 | "api_version = \"2025-05-01-Preview\"" |
44 | 46 | ] |
45 | 47 | }, |
46 | 48 | { |
47 | 49 | "cell_type": "markdown", |
48 | | - "id": "e3d0081e", |
| 50 | + "id": "ea2ecdce", |
49 | 51 | "metadata": {}, |
50 | 52 | "source": [ |
51 | | - "### 2. Create search agent" |
| 53 | + "### 2. Create Search Index" |
52 | 54 | ] |
53 | 55 | }, |
54 | 56 | { |
55 | 57 | "cell_type": "code", |
56 | 58 | "execution_count": 2, |
| 59 | + "id": "91fd6810", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "name": "stdout", |
| 64 | + "output_type": "stream", |
| 65 | + "text": [ |
| 66 | + "Index 'earth_at_night' created or updated successfully\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "from azure.search.documents.indexes.models import SearchIndex, SearchField, VectorSearch, VectorSearchProfile, HnswAlgorithmConfiguration, AzureOpenAIVectorizer, AzureOpenAIVectorizerParameters, SemanticSearch, SemanticConfiguration, SemanticPrioritizedFields, SemanticField\n", |
| 72 | + "from azure.search.documents.indexes import SearchIndexClient\n", |
| 73 | + "\n", |
| 74 | + "index = SearchIndex(\n", |
| 75 | + " name=index_name,\n", |
| 76 | + " fields=[\n", |
| 77 | + " SearchField(name=\"id\", type=\"Edm.String\", key=True, filterable=True, sortable=True, facetable=True),\n", |
| 78 | + " SearchField(name=\"page_chunk\", type=\"Edm.String\", filterable=False, sortable=False, facetable=False),\n", |
| 79 | + " SearchField(name=\"page_embedding_text_3_large\", type=\"Collection(Edm.Single)\", stored=False, vector_search_dimensions=3072, vector_search_profile_name=\"hnsw_text_3_large\"),\n", |
| 80 | + " SearchField(name=\"page_number\", type=\"Edm.Int32\", filterable=True, sortable=True, facetable=True),\n", |
| 81 | + " SearchField(name=\"chapter_number\", type=\"Edm.Int32\", filterable=True, sortable=True, facetable=True)\n", |
| 82 | + " ],\n", |
| 83 | + " vector_search=VectorSearch(\n", |
| 84 | + " profiles=[VectorSearchProfile(name=\"hnsw_text_3_large\", algorithm_configuration_name=\"alg\", vectorizer_name=\"azure_openai_text_3_large\")],\n", |
| 85 | + " algorithms=[HnswAlgorithmConfiguration(name=\"alg\")],\n", |
| 86 | + " vectorizers=[\n", |
| 87 | + " AzureOpenAIVectorizer(\n", |
| 88 | + " vectorizer_name=\"azure_openai_text_3_large\",\n", |
| 89 | + " parameters=AzureOpenAIVectorizerParameters(\n", |
| 90 | + " resource_url=azure_openai_endpoint,\n", |
| 91 | + " deployment_name=azure_openai_embedding_deployment,\n", |
| 92 | + " model_name=azure_openai_embedding_model\n", |
| 93 | + " )\n", |
| 94 | + " )\n", |
| 95 | + " ]\n", |
| 96 | + " ),\n", |
| 97 | + " semantic_search=SemanticSearch(\n", |
| 98 | + " default_configuration_name=\"semantic_config\",\n", |
| 99 | + " configurations=[\n", |
| 100 | + " SemanticConfiguration(\n", |
| 101 | + " name=\"semantic_config\",\n", |
| 102 | + " prioritized_fields=SemanticPrioritizedFields(\n", |
| 103 | + " content_fields=[\n", |
| 104 | + " SemanticField(field_name=\"page_chunk\")\n", |
| 105 | + " ]\n", |
| 106 | + " )\n", |
| 107 | + " )\n", |
| 108 | + " ]\n", |
| 109 | + " )\n", |
| 110 | + ")\n", |
| 111 | + "\n", |
| 112 | + "index_client = SearchIndexClient(endpoint=endpoint, credential=credential)\n", |
| 113 | + "index_client.create_or_update_index(index)\n", |
| 114 | + "print(f\"Index '{index_name}' created or updated successfully\")\n" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "id": "376b9785", |
| 120 | + "metadata": {}, |
| 121 | + "source": [ |
| 122 | + "### 3. Upload sample documents" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 3, |
| 128 | + "id": "f98f31e7", |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "name": "stdout", |
| 133 | + "output_type": "stream", |
| 134 | + "text": [ |
| 135 | + "Documents uploaded to index 'earth_at_night'\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "import requests\n", |
| 141 | + "from azure.search.documents import SearchIndexingBufferedSender\n", |
| 142 | + "from azure.search.documents import SearchClient\n", |
| 143 | + "\n", |
| 144 | + "url = \"https://raw.githubusercontent.com/Azure-Samples/azure-search-sample-data/refs/heads/main/nasa-e-book/earth-at-night-json/documents.json\"\n", |
| 145 | + "documents = requests.get(url).json()\n", |
| 146 | + "\n", |
| 147 | + "with SearchIndexingBufferedSender(endpoint=endpoint, index_name=index_name, credential=credential) as client:\n", |
| 148 | + " client.upload_documents(documents=documents)\n", |
| 149 | + "\n", |
| 150 | + "print(f\"Documents uploaded to index '{index_name}'\")\n" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "e3d0081e", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "### 4. Create search agent" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 4, |
57 | 164 | "id": "fbe31e32", |
58 | 165 | "metadata": {}, |
59 | 166 | "outputs": [], |
|
89 | 196 | "id": "ff845de0", |
90 | 197 | "metadata": {}, |
91 | 198 | "source": [ |
92 | | - "### 3. Create AI Agent" |
| 199 | + "### 5. Create AI Agent" |
93 | 200 | ] |
94 | 201 | }, |
95 | 202 | { |
96 | 203 | "cell_type": "code", |
97 | | - "execution_count": null, |
| 204 | + "execution_count": 10, |
98 | 205 | "id": "aa363122", |
99 | 206 | "metadata": {}, |
100 | 207 | "outputs": [ |
|
103 | 210 | "output_type": "stream", |
104 | 211 | "text": [ |
105 | 212 | "{\n", |
106 | | - " \"id\": \"asst_ndZgxVpaNU0pmCH4Ot9S45qa\",\n", |
| 213 | + " \"id\": \"asst_tybwnQVSaJkQOuKHkFSll4Bb\",\n", |
107 | 214 | " \"object\": \"assistant\",\n", |
108 | | - " \"created_at\": 1745984682,\n", |
| 215 | + " \"created_at\": 1746141946,\n", |
109 | 216 | " \"name\": \"earth-search-agent\",\n", |
110 | 217 | " \"description\": null,\n", |
111 | 218 | " \"model\": \"gpt-4o\",\n", |
|
143 | 250 | "id": "f12a051e", |
144 | 251 | "metadata": {}, |
145 | 252 | "source": [ |
146 | | - "### 4. Add Agentic Retrieval tool to AI Agent" |
| 253 | + "### 6. Add Agentic Retrieval tool to AI Agent" |
147 | 254 | ] |
148 | 255 | }, |
149 | 256 | { |
150 | 257 | "cell_type": "code", |
151 | | - "execution_count": 4, |
| 258 | + "execution_count": 11, |
152 | 259 | "id": "de2ee775", |
153 | 260 | "metadata": {}, |
154 | 261 | "outputs": [], |
|
192 | 299 | " # Return the grounding response to the agent\n", |
193 | 300 | " return result[\"response\"][0][\"content\"][0][\"text\"]\n", |
194 | 301 | "\n", |
| 302 | + "# Link to AI Agent Service function calling tutorial\n", |
195 | 303 | "user_functions = { agentic_retrieval }\n", |
196 | 304 | "functions = FunctionTool(user_functions)\n", |
197 | 305 | "toolset = ToolSet()\n", |
|
204 | 312 | "id": "6bf5b621", |
205 | 313 | "metadata": {}, |
206 | 314 | "source": [ |
207 | | - "### 5. Start a chat with the agent" |
| 315 | + "### 7. Start a chat with the agent" |
208 | 316 | ] |
209 | 317 | }, |
210 | 318 | { |
211 | 319 | "cell_type": "code", |
212 | | - "execution_count": 5, |
| 320 | + "execution_count": null, |
213 | 321 | "id": "f1fc04fc", |
214 | 322 | "metadata": {}, |
215 | | - "outputs": [ |
216 | | - { |
217 | | - "name": "stdout", |
218 | | - "output_type": "stream", |
219 | | - "text": [ |
220 | | - "Agent response: ### Why do suburban belts display larger December brightening than urban cores, despite higher absolute light levels downtown?\n", |
221 | | - "\n", |
222 | | - "Suburban belts often display larger December brightening compared to urban cores due to several factors:\n", |
223 | | - "1\n", |
224 | | - " **Residential Lighting:** Suburban areas typically have more residential homes and less commercial or industrial areas compared to urban cores\n", |
225 | | - " During December, longer nights coupled with holiday lighting decorations and increased residential lighting contribute more significantly to the overall brightness\n", |
226 | | - "\n", |
227 | | - "2\n", |
228 | | - " **Diffusion of Light:** Light pollution in urban cores is already high, and additional light during December has a marginal impact\n", |
229 | | - " In suburban belts, where the baseline light levels are lower, the addition of holiday lights and longer residential lighting hours has a more pronounced effect, becoming more noticeable when viewed from space\n", |
230 | | - "\n", |
231 | | - " \n", |
232 | | - "### Why is the Phoenix nighttime street grid so sharply visible from space, whereas large stretches of the interstate between Midwestern cities remain comparatively dim?\n", |
233 | | - "\n", |
234 | | - "The nighttime visibility of the Phoenix street grid compared to the interstates in the Midwest can be attributed to the following:\n", |
235 | | - "1\n", |
236 | | - " **Urban Grid Layout:** The Phoenix metropolitan area is designed with a regular grid of city blocks and streets, which is distinctly visible at night due to the extensive and uniform street lighting\n", |
237 | | - " The bright illumination of commercial properties, shopping centers, and intersections contributes to the clear visibility of the grid pattern from space ([ref: 0, 1])\n", |
238 | | - "\n", |
239 | | - "2\n", |
240 | | - " **Density of Lighting:** The density of streetlights, commercial lighting, and residential lighting in urban areas like Phoenix is much higher than along rural interstates\n", |
241 | | - " Interstate highways in the Midwest tend to have sparse lighting, primarily focusing on major intersections or urban exits, resulting in less overall brightness compared to a city grid system\n", |
242 | | - "\n", |
243 | | - "3\n", |
244 | | - " **Low Earth Orbit Perspective:** From the vantage point of the International Space Station (ISS), the grid layout of cities like Phoenix, with its consistent lighting, stands out sharply against the darker background of less densely-lit rural areas and interstates\n", |
245 | | - " Agricultural and undeveloped lands appear much dimmer in comparison ([ref: 0, 1])\n", |
246 | | - "\n", |
247 | | - "\n", |
248 | | - "These factors combine to make the urban grid of Phoenix very distinct in nighttime satellite imagery, unlike the less consistently illuminated stretches of interstate highways\n", |
249 | | - "\n" |
250 | | - ] |
251 | | - } |
252 | | - ], |
| 323 | + "outputs": [], |
253 | 324 | "source": [ |
254 | 325 | "message = project_client.agents.create_message(\n", |
255 | 326 | " thread_id=thread.id,\n", |
|
273 | 344 | "id": "aa88be35", |
274 | 345 | "metadata": {}, |
275 | 346 | "source": [ |
276 | | - "### 5.1: Review retrieval activity and results" |
| 347 | + "### 7.1: Review retrieval activity and results" |
277 | 348 | ] |
278 | 349 | }, |
279 | 350 | { |
280 | 351 | "cell_type": "code", |
281 | | - "execution_count": 6, |
| 352 | + "execution_count": null, |
282 | 353 | "id": "7b90fefa", |
283 | 354 | "metadata": {}, |
284 | 355 | "outputs": [ |
|
365 | 436 | "id": "4b6bfb9f", |
366 | 437 | "metadata": {}, |
367 | 438 | "source": [ |
368 | | - "### 6. Continue the conversation" |
| 439 | + "### 8. Continue the conversation" |
369 | 440 | ] |
370 | 441 | }, |
371 | 442 | { |
372 | 443 | "cell_type": "code", |
373 | | - "execution_count": 7, |
| 444 | + "execution_count": null, |
374 | 445 | "id": "d9478191", |
375 | 446 | "metadata": {}, |
376 | 447 | "outputs": [ |
|
425 | 496 | "id": "b366ed37", |
426 | 497 | "metadata": {}, |
427 | 498 | "source": [ |
428 | | - "### 6.1: Review retrieval activity and results" |
| 499 | + "### 8.1: Review retrieval activity and results" |
429 | 500 | ] |
430 | 501 | }, |
431 | 502 | { |
432 | 503 | "cell_type": "code", |
433 | | - "execution_count": 8, |
| 504 | + "execution_count": null, |
434 | 505 | "id": "c063c45e", |
435 | 506 | "metadata": {}, |
436 | 507 | "outputs": [ |
|
0 commit comments