|
25 | 25 | "source": [ |
26 | 26 | "## Prerequisites\n", |
27 | 27 | "\n", |
28 | | - "- Azure AI Search, Basic tier or higher is recommended. Enable semantic ranking and role-based access control.\n", |
| 28 | + "- [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/how-to/create-resource)\n", |
29 | 29 | "\n", |
30 | | - "- Azure OpenAI, in the same region as Azure AI Search. Configure Azure OpenAI to use a system managed identity.\n", |
| 30 | + " - Configure Azure OpenAI to use a system managed identity.\n", |
| 31 | + " - Deploy a chat model (GPT-3.5-Turbo, GPT-4, or equivalent LLM).\n", |
31 | 32 | "\n", |
32 | | - "- Deployment GPT-3.5-Turbo, GPT-4, or equivalent LLM.\n", |
| 33 | + "- [Azure AI Search](https://learn.microsoft.com/azure/search/search-create-service-portal)\n", |
33 | 34 | "\n", |
34 | | - "- Create the hotels-sample-index using [this quickstart](https://learn.microsoft.com/azure/search/search-get-started-portal), and then modify it to use this semantic configuration:\n", |
| 35 | + " - Basic tier or higher is recommended.\n", |
| 36 | + " - Choose the same region as Azure OpenAI.\n", |
| 37 | + " - Enable semantic ranking.\n", |
| 38 | + " - Enable role-based access control.\n", |
| 39 | + "\n", |
| 40 | + "## Configure access\n", |
| 41 | + "\n", |
| 42 | + "This quickstart assumes authentication and authorization using Microsoft Entra ID and role assignments. It also assumes that you run this code from your local device.\n", |
| 43 | + "\n", |
| 44 | + "1. To create and load the sample index on Azure AI Search, you must have role assignments for: **Search Index Data Reader**, **Search Index Data Contributor**, **Search Service Contributor**.\n", |
| 45 | + "\n", |
| 46 | + "1. To send the query and results from the search engine to Azure OpenAI, the search system identity must have **Cognitive Services OpenAI User** permissions on Azure OpenAI.\n", |
| 47 | + "\n", |
| 48 | + "## Create the sample index\n", |
| 49 | + "\n", |
| 50 | + "This quickstart assumes the hotels-sample-index, which you can create in minutes using [this quickstart](https://learn.microsoft.com/azure/search/search-get-started-portal).\n", |
| 51 | + "\n", |
| 52 | + "Once the index exists, modify it in the Azure portal to use this semantic configuration:\n", |
35 | 53 | " \n", |
36 | 54 | " ```json\n", |
37 | 55 | " \"semantic\": {\n", |
|
49 | 67 | " }]},\n", |
50 | 68 | " ```\n", |
51 | 69 | "\n", |
52 | | - "- On Azure AI Search, create a role assignment for the Azure OpenAI managed identity. Required roles: **Search Index Data Reader**, **Search Service Contributor**.\n", |
| 70 | + "Now that you have your Azure resources, an index, and model in place, you can run the script to chat with the index." |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "## Run the code\n", |
| 78 | + "\n", |
| 79 | + "1. Create a virtual environment. In Visual Studio Code, press Ctrl-shift-P to open the command palette, search for \"Python: Create Environment\", and then select `Venv` to create a virtual environment in the current workspace.\n", |
| 80 | + "\n", |
| 81 | + "1. Select Quickstart-RAG\\requirements.txt for the dependencies.\n", |
53 | 82 | "\n", |
54 | | - "- On Azure OpenAI, create a role assigment for the user running the code: Required role: **Cognitive Services OpenAI User**." |
| 83 | + "It takes several minutes to create the environment. When the environment is ready, continue to the next step." |
55 | 84 | ] |
56 | 85 | }, |
57 | 86 | { |
58 | 87 | "cell_type": "code", |
59 | | - "execution_count": null, |
| 88 | + "execution_count": 6, |
60 | 89 | "metadata": {}, |
61 | 90 | "outputs": [], |
62 | 91 | "source": [ |
63 | 92 | "# Package install for quickstart\n", |
64 | | - "! pip install azure-search-documents==11.6.0b4 --quiet\n", |
65 | | - "! pip install azure-identity==1.16.0 --quiet\n", |
66 | | - "! pip install openai --quiet" |
| 93 | + "! pip install -r requirements.txt --quiet" |
67 | 94 | ] |
68 | 95 | }, |
69 | 96 | { |
70 | 97 | "cell_type": "code", |
71 | | - "execution_count": null, |
| 98 | + "execution_count": 7, |
72 | 99 | "metadata": {}, |
73 | 100 | "outputs": [], |
74 | 101 | "source": [ |
75 | | - "# Set endpoints and deployment model\n", |
76 | | - "AZURE_SEARCH_SERVICE: str = \"PUT YOUR SEARCH SERVICE ENDPOINT HERE\"\n", |
77 | | - "AZURE_OPENAI_ACCOUNT: str = \"PUT YOUR AZURE OPENAI ENDPOINT HERE\"\n", |
78 | | - "AZURE_DEPLOYMENT_MODEL: str = \"gpt-35-turbo\"" |
| 102 | + "# Set endpoints and deployment model (provide the name of the deployment)\n", |
| 103 | + " AZURE_SEARCH_SERVICE: str = \"PUT YOUR SEARCH SERVICE ENDPOINT HERE\"\n", |
| 104 | + " AZURE_OPENAI_ACCOUNT: str = \"PUT YOUR AZURE OPENAI ENDPOINT HERE\"\n", |
| 105 | + " AZURE_DEPLOYMENT_MODEL: str = \"gpt-35-turbo\"" |
79 | 106 | ] |
80 | 107 | }, |
81 | 108 | { |
82 | 109 | "cell_type": "code", |
83 | | - "execution_count": null, |
| 110 | + "execution_count": 8, |
84 | 111 | "metadata": {}, |
85 | 112 | "outputs": [], |
86 | 113 | "source": [ |
|
92 | 119 | }, |
93 | 120 | { |
94 | 121 | "cell_type": "code", |
95 | | - "execution_count": null, |
| 122 | + "execution_count": 16, |
96 | 123 | "metadata": {}, |
97 | 124 | "outputs": [], |
98 | 125 | "source": [ |
|
185 | 212 | }, |
186 | 213 | { |
187 | 214 | "cell_type": "code", |
188 | | - "execution_count": null, |
| 215 | + "execution_count": 19, |
189 | 216 | "metadata": {}, |
190 | | - "outputs": [], |
| 217 | + "outputs": [ |
| 218 | + { |
| 219 | + "name": "stdout", |
| 220 | + "output_type": "stream", |
| 221 | + "text": [ |
| 222 | + "Based on your preferences, I recommend the following hotels: \n", |
| 223 | + "\n", |
| 224 | + "- Ocean Air Motel: This hotel is oceanfront with beach access, two pools, and a private balcony with ocean views. \n", |
| 225 | + "- Marquis Plaza & Suites: This hotel has a view of the ocean and amenities like free Wi-Fi, a full kitchen, and a free breakfast buffet. \n", |
| 226 | + "- Trails End Motel: Though not directly on the ocean, this hotel does have a view and is only 8 miles from downtown. Amenities include an on-site bar/restaurant, free hot breakfast buffet, and free Wi-Fi.\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
191 | 230 | "source": [ |
192 | 231 | "# Instantiate the chat thread and run the conversation\n", |
193 | 232 | "import azure.identity.aio\n", |
|
201 | 240 | " query=\"Can you recommend a few hotels near the ocean with beach access and good views\",\n", |
202 | 241 | " search_type=SearchType(search_type),\n", |
203 | 242 | " use_semantic_reranker=use_semantic_reranker,\n", |
204 | | - " sources_to_include=sources_to_include,\n", |
205 | | - " k=k)\n", |
| 243 | + " sources_to_include=sources_to_include\n", |
| 244 | + " )\n", |
206 | 245 | " await chat_thread.get_openai_response(openai_client=openai_client, model=chat_deployment)\n", |
207 | 246 | "\n", |
208 | 247 | "print(chat_thread.get_last_message()[\"content\"])" |
209 | 248 | ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "markdown", |
| 252 | + "metadata": {}, |
| 253 | + "source": [ |
| 254 | + "If you get an authorization error message, wait a few minutes and try again. It can take several minutes for role assignments to become operational." |
| 255 | + ] |
210 | 256 | } |
211 | 257 | ], |
212 | 258 | "metadata": { |
|
0 commit comments