|
16 | 16 | }, |
17 | 17 | { |
18 | 18 | "cell_type": "code", |
19 | | - "execution_count": null, |
| 19 | + "execution_count": 8, |
20 | 20 | "metadata": {}, |
21 | 21 | "outputs": [], |
22 | 22 | "source": [ |
23 | 23 | "import os\n", |
24 | 24 | "\n", |
25 | 25 | "import azure.identity\n", |
26 | 26 | "import dotenv\n", |
27 | | - "import openai\n", |
28 | 27 | "from azure.search.documents import SearchClient\n", |
29 | 28 | "from azure.search.documents.models import VectorizedQuery\n", |
| 29 | + "from openai import OpenAI\n", |
30 | 30 | "\n", |
31 | 31 | "dotenv.load_dotenv()\n", |
32 | 32 | "\n", |
33 | | - "azure_credential = azure.identity.DefaultAzureCredential()\n", |
| 33 | + "azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv(\"AZURE_TENANT_ID\"))\n", |
34 | 34 | "\n", |
35 | 35 | "# Initialize Azure OpenAI client\n", |
36 | 36 | "AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n", |
37 | 37 | "AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\")\n", |
38 | 38 | "\n", |
39 | 39 | "token_provider = azure.identity.get_bearer_token_provider(azure_credential, \"https://cognitiveservices.azure.com/.default\")\n", |
40 | | - "openai_client = openai.AzureOpenAI(\n", |
41 | | - " api_version=\"2023-07-01-preview\",\n", |
42 | | - " azure_endpoint=f\"https://{AZURE_OPENAI_SERVICE}.openai.azure.com\",\n", |
43 | | - " azure_ad_token_provider=token_provider)\n", |
| 40 | + "openai_client = OpenAI(\n", |
| 41 | + " base_url=f\"https://{AZURE_OPENAI_SERVICE}.openai.azure.com/openai/v1\",\n", |
| 42 | + " api_key=token_provider)\n", |
44 | 43 | "\n", |
45 | 44 | "def get_embedding(text):\n", |
46 | 45 | " get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n", |
|
49 | 48 | "# Initialize Azure search client\n", |
50 | 49 | "AZURE_SEARCH_SERVICE = os.getenv(\"AZURE_SEARCH_SERVICE\")\n", |
51 | 50 | "AZURE_SEARCH_ENDPOINT = f\"https://{AZURE_SEARCH_SERVICE}.search.windows.net\"\n", |
| 51 | + "AZURE_SEARCH_FULL_INDEX = os.getenv(\"AZURE_SEARCH_INDEX\", \"gptkbindex\")\n", |
| 52 | + "AZURE_SEARCH_FIELD_NAME_EMBEDDING = os.getenv(\"AZURE_SEARCH_FIELD_NAME_EMBEDDING\", \"embedding\")\n", |
52 | 53 | "\n", |
53 | | - "AZURE_SEARCH_FULL_INDEX = \"gptkbindex\"\n", |
54 | | - "search_client = SearchClient(AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_FULL_INDEX, credential=azure_credential)\n" |
| 54 | + "search_client = SearchClient(AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_FULL_INDEX, credential=azure_credential)" |
55 | 55 | ] |
56 | 56 | }, |
57 | 57 | { |
|
65 | 65 | }, |
66 | 66 | { |
67 | 67 | "cell_type": "code", |
68 | | - "execution_count": 2, |
| 68 | + "execution_count": 9, |
69 | 69 | "metadata": {}, |
70 | 70 | "outputs": [ |
71 | 71 | { |
72 | 72 | "name": "stdout", |
73 | 73 | "output_type": "stream", |
74 | 74 | "text": [ |
75 | | - "Score: 0.81510\tMatching text: Not found\n", |
76 | | - "Score: 0.80934\tMatching text: Not found\n", |
77 | | - "Score: 0.80891\tMatching text: Not found\n" |
| 75 | + "Score: 0.58118\tMatching text: Not found\n", |
| 76 | + "Score: 0.57963\tMatching text: Not found\n", |
| 77 | + "Score: 0.57403\tMatching text: Not found\n" |
78 | 78 | ] |
79 | 79 | } |
80 | 80 | ], |
81 | 81 | "source": [ |
82 | 82 | "search_query = \"$45.00\"\n", |
83 | 83 | "search_vector = get_embedding(search_query)\n", |
84 | 84 | "r = search_client.search(None, top=3, vector_queries=[\n", |
85 | | - " VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields=\"embedding\")])\n", |
| 85 | + " VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)])\n", |
86 | 86 | "for doc in r:\n", |
87 | 87 | " found_content = \"Not found\"\n", |
88 | 88 | " if search_query.lower() in doc['content'].lower():\n", |
|
99 | 99 | }, |
100 | 100 | { |
101 | 101 | "cell_type": "code", |
102 | | - "execution_count": 3, |
| 102 | + "execution_count": 10, |
103 | 103 | "metadata": {}, |
104 | 104 | "outputs": [ |
105 | 105 | { |
106 | 106 | "name": "stdout", |
107 | 107 | "output_type": "stream", |
108 | 108 | "text": [ |
109 | | - "Score: 4.14542\tMatching text: $45.00</td><td>$55.00</td></tr><tr><td>Employee +1</td><td>$65.00</td><td>$71.00</td></tr><tr><td>Employee +2 or more</td><td>$78.00</td><td>$89.00</td></tr></table> Next Steps We hope that this information has been helpful in understanding the differences between Northwind Health Plus and Northwind Standard. We are confident that you will find the right plan for you and your family. Thank you for choosing Contoso Electronics!\n", |
110 | | - "Score: 3.37283\tMatching text: Not found\n" |
| 109 | + "Score: 2.60556\tMatching text: $45.00</td><td>$55.00</td></tr><tr><td>Employee +1</td><td>$65.00</td><td>$71.00</td></tr><tr><td>Employee +2 or more</td><td>$78.00</td><td>$89.00</td></tr></table></figure>\n" |
111 | 110 | ] |
112 | 111 | } |
113 | 112 | ], |
|
131 | 130 | }, |
132 | 131 | { |
133 | 132 | "cell_type": "code", |
134 | | - "execution_count": 4, |
| 133 | + "execution_count": 12, |
135 | 134 | "metadata": {}, |
136 | 135 | "outputs": [ |
137 | 136 | { |
138 | 137 | "name": "stdout", |
139 | 138 | "output_type": "stream", |
140 | 139 | "text": [ |
141 | | - "Score: 0.03254\tMatching text: $45.00</td><td>$55.00</td></tr><tr><td>Employee +1</td><td>$65.00</td><td>$71.00</td></tr><tr><td>Employee +2 or more</td><td>$78.00</td><td>$89.00</td></tr></table> Next Steps We hope that this information has been helpful in understanding the differences between Northwind Health Plus and Northwind Standard. We are confident that you will find the right plan for you and your family. Thank you for choosing Contoso Electronics!\n", |
142 | | - "Score: 0.03110\tMatching text: Not found\n", |
| 140 | + "Score: 0.03205\tMatching text: $45.00</td><td>$55.00</td></tr><tr><td>Employee +1</td><td>$65.00</td><td>$71.00</td></tr><tr><td>Employee +2 or more</td><td>$78.00</td><td>$89.00</td></tr></table></figure>\n", |
143 | 141 | "Score: 0.01667\tMatching text: Not found\n", |
144 | 142 | "Score: 0.01639\tMatching text: Not found\n", |
145 | 143 | "Score: 0.01613\tMatching text: Not found\n", |
| 144 | + "Score: 0.01587\tMatching text: Not found\n", |
146 | 145 | "Score: 0.01562\tMatching text: Not found\n", |
147 | | - "Score: 0.01538\tMatching text: Not found\n", |
148 | 146 | "Score: 0.01515\tMatching text: Not found\n", |
149 | 147 | "Score: 0.01493\tMatching text: Not found\n", |
| 148 | + "Score: 0.01471\tMatching text: Not found\n", |
150 | 149 | "Score: 0.01449\tMatching text: Not found\n" |
151 | 150 | ] |
152 | 151 | } |
153 | 152 | ], |
154 | 153 | "source": [ |
155 | 154 | "search_vector = get_embedding(search_query)\n", |
156 | 155 | "r = search_client.search(search_query, top=15, vector_queries=[\n", |
157 | | - " VectorizedQuery(vector=search_vector, k_nearest_neighbors=10, fields=\"embedding\")])\n", |
| 156 | + " VectorizedQuery(vector=search_vector, k_nearest_neighbors=10, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)])\n", |
158 | 157 | "for doc in r:\n", |
159 | 158 | " found_content = \"Not found\"\n", |
160 | 159 | " if search_query in doc['content']:\n", |
|
171 | 170 | }, |
172 | 171 | { |
173 | 172 | "cell_type": "code", |
174 | | - "execution_count": 5, |
| 173 | + "execution_count": 14, |
175 | 174 | "metadata": {}, |
176 | 175 | "outputs": [ |
177 | 176 | { |
178 | 177 | "name": "stdout", |
179 | 178 | "output_type": "stream", |
180 | 179 | "text": [ |
181 | | - "Score: 0.03252\t\tContent: PerksPlus is not only designed to support employees' physical health, but also their mental health. Regular exercise has been shown to reduce stress,\n", |
182 | | - "Score: 0.03105\t\tContent: Under the Northwind Health Plus plan, habilitation services are covered up to a certain dollar amount and number of visits. This amount and the numbe\n", |
183 | | - "Score: 0.02797\t\tContent: Occupational Therapy Occupational therapy helps individuals develop, maintain, or restore skills for daily living and work. It can help individuals w\n", |
184 | | - "Score: 0.02766\t\tContent: Some of the lessons covered under PerksPlus include: · Skiing and snowboarding lessons · Scuba diving lessons · Surfing lessons · Horseback riding le\n", |
185 | | - "Score: 0.02290\t\tContent: Talk to your doctor or health care provider about the trial and ask any questions you may have. · Ask about the potential risks and benefits of parti\n" |
| 180 | + "Score: 0.03137\t\tContent: PerksPlus covers a wide range of fitness activities, including but not limited to: · Gym memberships · Personal training sessions · Yoga and Pila\n", |
| 181 | + "Score: 0.02697\t\tContent: With PerksPlus, employees can choose from a variety of fitness programs to suit their individual needs and preferences. Whether you're looking to im\n", |
| 182 | + "Score: 0.02597\t\tContent: # Overview Introducing PerksPlus - the ultimate benefits program designed to support the health and wellness of employees. With PerksPlus, employees \n", |
| 183 | + "Score: 0.02379\t\tContent: 2\\. Ask questions. If the employee is unsure about any part of the plan, it is important to ask questions in order to make sure that the plan is suita\n", |
| 184 | + "Score: 0.02284\t\tContent: Seeking Pre-Approval for Care In some cases, you may need to seek pre-approval or authorization before receiving certain medical services. This is \n" |
186 | 185 | ] |
187 | 186 | } |
188 | 187 | ], |
189 | 188 | "source": [ |
190 | 189 | "search_query = \"learning about underwater activities\"\n", |
191 | 190 | "search_vector = get_embedding(search_query)\n", |
192 | 191 | "r = search_client.search(search_query, top=5, vector_queries=[\n", |
193 | | - " VectorizedQuery(vector=search_vector, k_nearest_neighbors=10, fields=\"embedding\")])\n", |
| 192 | + " VectorizedQuery(vector=search_vector, k_nearest_neighbors=10, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)])\n", |
194 | 193 | "for doc in r:\n", |
195 | 194 | " content = doc[\"content\"].replace(\"\\n\", \" \")[:150]\n", |
196 | 195 | " print(f\"Score: {doc['@search.score']:.5f}\\t\\tContent: {content}\")" |
|
205 | 204 | }, |
206 | 205 | { |
207 | 206 | "cell_type": "code", |
208 | | - "execution_count": 6, |
| 207 | + "execution_count": 16, |
209 | 208 | "metadata": {}, |
210 | 209 | "outputs": [ |
211 | 210 | { |
212 | 211 | "name": "stdout", |
213 | 212 | "output_type": "stream", |
214 | 213 | "text": [ |
215 | | - "Score: 0.02766\tReranker: 1.94936\tContent: Some of the lessons covered under PerksPlus include: · Skiing and snowboarding lessons · Scuba diving lessons · Surfing lessons · Horseback riding le\n", |
216 | | - "Score: 0.03252\tReranker: 1.77669\tContent: PerksPlus is not only designed to support employees' physical health, but also their mental health. Regular exercise has been shown to reduce stress,\n", |
217 | | - "Score: 0.02455\tReranker: 0.59237\tContent: By taking the time to research providers in-network with Northwind Health Plus and keeping track of your medical records and tests, you can make sure\n", |
218 | | - "Score: 0.03105\tReranker: 0.58931\tContent: Under the Northwind Health Plus plan, habilitation services are covered up to a certain dollar amount and number of visits. This amount and the numbe\n", |
219 | | - "Score: 0.02153\tReranker: 0.49740\tContent: This position will be responsible for designing and implementing innovative solutions to maximize product performance and optimize customer satisfact\n" |
| 214 | + "Score: 0.03137\tReranker: 2.37414\tContent: PerksPlus covers a wide range of fitness activities, including but not limited to: · Gym memberships · Personal training sessions · Yoga and Pila\n", |
| 215 | + "Score: 0.02597\tReranker: 1.33856\tContent: # Overview Introducing PerksPlus - the ultimate benefits program designed to support the health and wellness of employees. With PerksPlus, employees \n", |
| 216 | + "Score: 0.02697\tReranker: 1.30170\tContent: With PerksPlus, employees can choose from a variety of fitness programs to suit their individual needs and preferences. Whether you're looking to im\n", |
| 217 | + "Score: 0.02132\tReranker: 0.96566\tContent: Northwind Health Plus covers physical therapy services that are medically necessary. ### Occupational Therapy Occupational therapy helps individua\n", |
| 218 | + "Score: 0.01639\tReranker: 0.95006\tContent: It is designed to help individuals with disabilities learn the skills they need to live as independently as possible, including communication, proble\n" |
220 | 219 | ] |
221 | 220 | } |
222 | 221 | ], |
|
227 | 226 | " search_query,\n", |
228 | 227 | " top=5, \n", |
229 | 228 | " vector_queries=[\n", |
230 | | - " VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields=\"embedding\")],\n", |
| 229 | + " VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)],\n", |
231 | 230 | " query_type=\"semantic\",\n", |
232 | 231 | " semantic_configuration_name=\"default\")\n", |
233 | 232 | "\n", |
|
0 commit comments