Skip to content

Commit e256a7b

Browse files
committed
Upgrade to OpenAI and latest RAGchat settings
1 parent 210e45a commit e256a7b

13 files changed

+149087
-1202
lines changed

azure_ai_search.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
"source": [
180180
"import azure.identity\n",
181181
"import dotenv\n",
182-
"import openai\n",
182+
"from openai import OpenAI\n",
183183
"\n",
184184
"dotenv.load_dotenv()\n",
185185
"\n",
@@ -193,10 +193,9 @@
193193
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\")\n",
194194
"\n",
195195
"token_provider = azure.identity.get_bearer_token_provider(azure_credential, \"https://cognitiveservices.azure.com/.default\")\n",
196-
"openai_client = openai.AzureOpenAI(\n",
197-
" api_version=\"2023-07-01-preview\",\n",
198-
" azure_endpoint=f\"https://{AZURE_OPENAI_SERVICE}.openai.azure.com\",\n",
199-
" azure_ad_token_provider=token_provider)\n",
196+
"openai_client = OpenAI(\n",
197+
" base_url=f\"https://{AZURE_OPENAI_SERVICE}.openai.azure.com/openai/v1\",\n",
198+
" api_key=token_provider)\n",
200199
"\n",
201200
"def get_embedding(text):\n",
202201
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
@@ -205,29 +204,30 @@
205204
},
206205
{
207206
"cell_type": "code",
208-
"execution_count": 9,
207+
"execution_count": 8,
209208
"metadata": {},
210209
"outputs": [
211210
{
212211
"name": "stdout",
213212
"output_type": "stream",
214213
"text": [
215-
"Score: 0.80732\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.79299\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.79254\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",
218-
"Score: 0.78812\tContent: It is important to understand which type of therapy is best suited for the individual's needs and goals. It is also important to note that habilitati\n",
219-
"Score: 0.78661\tContent: Occupational Therapy Occupational therapy helps individuals develop, maintain, or restore skills for daily living and work. It can help individuals w\n"
214+
"Score: 0.58048\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.56032\tContent: It is designed to help individuals with disabilities learn the skills they need to live as independently as possible, including communication, proble\n",
216+
"Score: 0.55442\tContent:4\\. Review your policy regularly. It is important to review your policy periodically to make sure you understand all of the details of your plan. 5\\.\n",
217+
"Score: 0.55293\tContent:# Overview Introducing PerksPlus - the ultimate benefits program designed to support the health and wellness of employees. With PerksPlus, employees \n",
218+
"Score: 0.55057\tContent: Knowing which providers are in-network and out-of- network can help you make sure you are getting the most out of your Northwind Standard plan. 2\\. \n"
220219
]
221220
}
222221
],
223222
"source": [
224-
"AZURE_SEARCH_FULL_INDEX = \"gptkbindex\"\n",
223+
"AZURE_SEARCH_FULL_INDEX = os.getenv(\"AZURE_SEARCH_INDEX\", \"gptkbindex\")\n",
224+
"AZURE_SEARCH_FIELD_NAME_EMBEDDING = os.getenv(\"AZURE_SEARCH_FIELD_NAME_EMBEDDING\", \"embedding\")\n",
225225
"search_client = SearchClient(AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_FULL_INDEX, credential=azure_credential)\n",
226226
"\n",
227227
"search_query = \"learning about underwater activities\"\n",
228228
"search_vector = get_embedding(search_query)\n",
229229
"r = search_client.search(search_text=None, top=5, vector_queries=[\n",
230-
" VectorizedQuery(vector=search_vector, k_nearest_neighbors=5, fields=\"embedding\")])\n",
230+
" VectorizedQuery(vector=search_vector, k_nearest_neighbors=5, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)])\n",
231231
"for doc in r:\n",
232232
" content = doc[\"content\"].replace(\"\\n\", \" \")[:150]\n",
233233
" print(f\"Score: {doc['@search.score']:.5f}\\tContent:{content}\")"

rag.ipynb

Lines changed: 130 additions & 76 deletions
Large diffs are not rendered by default.

rag_eval.ipynb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"\n",
2525
"import azure.identity\n",
2626
"import dotenv\n",
27-
"import openai\n",
2827
"from azure.search.documents import SearchClient\n",
2928
"from azure.search.documents.models import VectorizedQuery\n",
29+
"from openai import OpenAI\n",
3030
"\n",
3131
"dotenv.load_dotenv()\n",
3232
"\n",
@@ -37,10 +37,9 @@
3737
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\")\n",
3838
"\n",
3939
"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",
4443
"\n",
4544
"def get_embedding(text):\n",
4645
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
@@ -49,9 +48,10 @@
4948
"# Initialize Azure search client\n",
5049
"AZURE_SEARCH_SERVICE = os.getenv(\"AZURE_SEARCH_SERVICE\")\n",
5150
"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",
5253
"\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)"
5555
]
5656
},
5757
{
@@ -63,14 +63,14 @@
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": 2,
66+
"execution_count": 4,
6767
"metadata": {},
6868
"outputs": [
6969
{
7070
"name": "stdout",
7171
"output_type": "stream",
7272
"text": [
73-
"A product manager is responsible for leading the product management team, developing product strategies, collaborating with internal teams and external partners, and ensuring successful product execution. They monitor industry trends, develop product life-cycle management processes, research customer needs, and create customer-centric product roadmaps. They also manage the product development budget, oversee product performance, and ensure product quality and customer satisfaction [role_library.pdf#page=29] [role_library.pdf#page=12].\n"
73+
"A product manager at Contoso Electronics is responsible for developing and implementing product strategies and plans that support business objectives and growth. They lead a team in executing these strategies, research market trends and customer needs, oversee product roadmaps, manage product life cycles from concept to launch, and monitor product performance and customer feedback. They also identify strategic partnership opportunities, manage product budgets, develop pricing and promotional strategies, and collaborate with cross-functional teams to ensure products meet customer needs. The role requires analyzing competitors and market trends to keep products competitive and successful in the marketplace [role_library.pdf#page=17, #page=23, #page=29].\n"
7474
]
7575
}
7676
],
@@ -82,7 +82,7 @@
8282
" user_question,\n",
8383
" top=5, \n",
8484
" vector_queries=[\n",
85-
" VectorizedQuery(vector=user_question_vector, k_nearest_neighbors=50, fields=\"embedding\")],\n",
85+
" VectorizedQuery(vector=user_question_vector, k_nearest_neighbors=50, fields=AZURE_SEARCH_FIELD_NAME_EMBEDDING)],\n",
8686
" query_type=\"semantic\",\n",
8787
" semantic_configuration_name=\"default\")\n",
8888
"\n",

search_relevance.ipynb

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": null,
19+
"execution_count": 8,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
2323
"import os\n",
2424
"\n",
2525
"import azure.identity\n",
2626
"import dotenv\n",
27-
"import openai\n",
2827
"from azure.search.documents import SearchClient\n",
2928
"from azure.search.documents.models import VectorizedQuery\n",
29+
"from openai import OpenAI\n",
3030
"\n",
3131
"dotenv.load_dotenv()\n",
3232
"\n",
33-
"azure_credential = azure.identity.DefaultAzureCredential()\n",
33+
"azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv(\"AZURE_TENANT_ID\"))\n",
3434
"\n",
3535
"# Initialize Azure OpenAI client\n",
3636
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
3737
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\")\n",
3838
"\n",
3939
"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",
4443
"\n",
4544
"def get_embedding(text):\n",
4645
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
@@ -49,9 +48,10 @@
4948
"# Initialize Azure search client\n",
5049
"AZURE_SEARCH_SERVICE = os.getenv(\"AZURE_SEARCH_SERVICE\")\n",
5150
"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",
5253
"\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)"
5555
]
5656
},
5757
{
@@ -65,24 +65,24 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 2,
68+
"execution_count": 9,
6969
"metadata": {},
7070
"outputs": [
7171
{
7272
"name": "stdout",
7373
"output_type": "stream",
7474
"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"
7878
]
7979
}
8080
],
8181
"source": [
8282
"search_query = \"$45.00\"\n",
8383
"search_vector = get_embedding(search_query)\n",
8484
"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",
8686
"for doc in r:\n",
8787
" found_content = \"Not found\"\n",
8888
" if search_query.lower() in doc['content'].lower():\n",
@@ -99,15 +99,14 @@
9999
},
100100
{
101101
"cell_type": "code",
102-
"execution_count": 3,
102+
"execution_count": 10,
103103
"metadata": {},
104104
"outputs": [
105105
{
106106
"name": "stdout",
107107
"output_type": "stream",
108108
"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"
111110
]
112111
}
113112
],
@@ -131,30 +130,30 @@
131130
},
132131
{
133132
"cell_type": "code",
134-
"execution_count": 4,
133+
"execution_count": 12,
135134
"metadata": {},
136135
"outputs": [
137136
{
138137
"name": "stdout",
139138
"output_type": "stream",
140139
"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",
143141
"Score: 0.01667\tMatching text: Not found\n",
144142
"Score: 0.01639\tMatching text: Not found\n",
145143
"Score: 0.01613\tMatching text: Not found\n",
144+
"Score: 0.01587\tMatching text: Not found\n",
146145
"Score: 0.01562\tMatching text: Not found\n",
147-
"Score: 0.01538\tMatching text: Not found\n",
148146
"Score: 0.01515\tMatching text: Not found\n",
149147
"Score: 0.01493\tMatching text: Not found\n",
148+
"Score: 0.01471\tMatching text: Not found\n",
150149
"Score: 0.01449\tMatching text: Not found\n"
151150
]
152151
}
153152
],
154153
"source": [
155154
"search_vector = get_embedding(search_query)\n",
156155
"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",
158157
"for doc in r:\n",
159158
" found_content = \"Not found\"\n",
160159
" if search_query in doc['content']:\n",
@@ -171,26 +170,26 @@
171170
},
172171
{
173172
"cell_type": "code",
174-
"execution_count": 5,
173+
"execution_count": 14,
175174
"metadata": {},
176175
"outputs": [
177176
{
178177
"name": "stdout",
179178
"output_type": "stream",
180179
"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"
186185
]
187186
}
188187
],
189188
"source": [
190189
"search_query = \"learning about underwater activities\"\n",
191190
"search_vector = get_embedding(search_query)\n",
192191
"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",
194193
"for doc in r:\n",
195194
" content = doc[\"content\"].replace(\"\\n\", \" \")[:150]\n",
196195
" print(f\"Score: {doc['@search.score']:.5f}\\t\\tContent: {content}\")"
@@ -205,18 +204,18 @@
205204
},
206205
{
207206
"cell_type": "code",
208-
"execution_count": 6,
207+
"execution_count": 16,
209208
"metadata": {},
210209
"outputs": [
211210
{
212211
"name": "stdout",
213212
"output_type": "stream",
214213
"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"
220219
]
221220
}
222221
],
@@ -227,7 +226,7 @@
227226
" search_query,\n",
228227
" top=5, \n",
229228
" 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",
231230
" query_type=\"semantic\",\n",
232231
" semantic_configuration_name=\"default\")\n",
233232
"\n",

0 commit comments

Comments
 (0)