Skip to content

Commit b23612b

Browse files
authored
Merge pull request #5 from Azure-Samples/zavaproductsignite
Add Zava product data and search scripts for Ignite
2 parents de6b1ae + 8053f5b commit b23612b

15 files changed

+5079
-26
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AZURE_OPENAI_SERVICE="SERVICE-NAME"
22
AZURE_OPENAI_DEPLOYMENT_NAME=DEPLOYMENT-NAME
3-
AZURE_OPENAI_ADA_DEPLOYMENT=ADA-DEPLOYMENT
3+
AZURE_OPENAI_EMBEDDING_DEPLOYMENT=EMBEDDING-DEPLOYMENT
44
AZURE_SEARCH_SERVICE=SEARCH-SERVICE-NAME
55
AZURE_VISION_ENDPOINT=https://YOUR-RESOURCE-NAME.cognitiveservices.azure.com
66
AZURE_TENANT_ID=TENANT-ID

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ This repository contains many notebooks that explain how Azure AI Search works,
44

55
## Environment setup
66

7-
1. Run `azd up` on [azure-search-openai-demo](https://github.com/Azure-Samples/azure-search-openai-demo/) with GPT vision feature enabled. This will create the necessary resources for the Azure OpenAI, Azure AI Search services, and the Azure AI Vision service.
7+
1. Run `azd up` on [azure-search-openai-demo](https://github.com/Azure-Samples/azure-search-openai-demo/) with multimodal feature enabled. This will create the necessary resources for the Azure OpenAI, Azure AI Search services, and the Azure AI Vision service.
88

99
2. Create a .env with these variables, with the values taken from `.azure/ENV-NAME/.env` in the azure-search-openai-demo repository.
1010

1111
```shell
1212
AZURE_OPENAI_SERVICE=YOUR-SERVICE-NAME
1313
AZURE_OPENAI_DEPLOYMENT_NAME=YOUR-OPENAI-DEPLOYMENT-NAME
14-
AZURE_OPENAI_ADA_DEPLOYMENT=YOUR-EMBED-DEPLOYMENT-NAME
14+
AZURE_OPENAI_EMBEDDING_DEPLOYMENT=YOUR-EMBED-DEPLOYMENT-NAME
1515
AZURE_SEARCH_SERVICE=YOUR-SEARCH-SERVICE-NAME
1616
AZURE_COMPUTERVISION_SERVICE=YOUR-COMPUTERVISION-SERVICE-NAME
1717
AZURE_TENANT_ID=YOUR-TENANT-ID
@@ -31,9 +31,9 @@ This repository contains many notebooks that explain how Azure AI Search works,
3131
pip install -r requirements.txt
3232
```
3333

34-
## Notebooks
34+
## Search on documents
3535

36-
These are the available notebooks, in suggested order:
36+
These notebooks operate on the index from the [azure-search-openai-demo](https://github.com/Azure-Samples/azure-search-openai-demo/) repository, which contains chunked documents from a fictional company.
3737

3838
* [Vector Embeddings Notebook](./vector_embeddings.ipynb)
3939
* [Azure AI Search Notebook](./azure_ai_search.ipynb)
@@ -43,3 +43,19 @@ These are the available notebooks, in suggested order:
4343
* [RAG Evaluation](./rag_eval.ipynb)
4444

4545
You can find video recordings going through the notebooks [here](https://github.com/microsoft/aitour-rag-with-ai-search/tree/main/session-delivery-resources#video-recordings).
46+
47+
## Search on product catalog
48+
49+
You can also try out search techniques on a Zava product catalog.
50+
First, create the search index and upload the products by running:
51+
52+
```shell
53+
python zava_product_upload.py
54+
```
55+
56+
Then, explore the different search techniques with these Python scripts:
57+
58+
* [Keyword Search](./zava_search_keyword.py)
59+
* [Vector Search](./zava_search_vector.py)
60+
* [Hybrid Search with RRF](./zava_search_rrf.py)
61+
* [Hybrid Search with RRF + Reranker](./zava_search_reranker.py)

azure_ai_search.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
"# Set up OpenAI client based on environment variables\n",
191191
"dotenv.load_dotenv()\n",
192192
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
193-
"AZURE_OPENAI_ADA_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_ADA_DEPLOYMENT\")\n",
193+
"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",
196196
"openai_client = openai.AzureOpenAI(\n",
@@ -199,7 +199,7 @@
199199
" azure_ad_token_provider=token_provider)\n",
200200
"\n",
201201
"def get_embedding(text):\n",
202-
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=text)\n",
202+
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
203203
" return get_embeddings_response.data[0].embedding"
204204
]
205205
},

rag.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"\n",
3535
"# Initialize Azure OpenAI client\n",
3636
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
37-
"AZURE_OPENAI_ADA_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_ADA_DEPLOYMENT\")\n",
37+
"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",
4040
"openai_client = openai.AzureOpenAI(\n",
@@ -43,7 +43,7 @@
4343
" azure_ad_token_provider=token_provider)\n",
4444
"\n",
4545
"def get_embedding(text):\n",
46-
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=text)\n",
46+
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
4747
" return get_embeddings_response.data[0].embedding\n",
4848
"\n",
4949
"# Initialize Azure search client\n",

rag_eval.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"\n",
3535
"# Initialize Azure OpenAI client\n",
3636
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
37-
"AZURE_OPENAI_ADA_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_ADA_DEPLOYMENT\")\n",
37+
"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",
4040
"openai_client = openai.AzureOpenAI(\n",
@@ -43,7 +43,7 @@
4343
" azure_ad_token_provider=token_provider)\n",
4444
"\n",
4545
"def get_embedding(text):\n",
46-
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=text)\n",
46+
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
4747
" return get_embeddings_response.data[0].embedding\n",
4848
"\n",
4949
"# Initialize Azure search client\n",

render_table.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Utility for rendering search results as rich tables."""
2+
3+
from typing import Any
4+
5+
from rich.console import Console
6+
from rich.table import Table
7+
8+
9+
def render_product_results(results: list[dict[str, Any]], title: str, show_reranker: bool = False) -> None:
10+
"""Render product search results as a rich table.
11+
12+
Args:
13+
results: List of search result documents
14+
title: Title for the table
15+
show_reranker: Whether to show reranker score column
16+
"""
17+
console = Console()
18+
table = Table(title=title, show_lines=True)
19+
20+
table.add_column("Score", justify="right", style="cyan", width=8)
21+
if show_reranker:
22+
table.add_column("Reranker", justify="right", style="red", width=8)
23+
table.add_column("Name", style="magenta", width=25)
24+
table.add_column("Description", style="white", width=50)
25+
table.add_column("Categories", style="blue", width=20)
26+
table.add_column("Price", justify="right", style="yellow", width=8)
27+
table.add_column("SKU", style="green", width=12)
28+
29+
for doc in list(results):
30+
row = [
31+
f"{doc['@search.score']:.3f}",
32+
]
33+
if show_reranker:
34+
row.append(f"{doc['@search.reranker_score']:.3f}")
35+
row.extend(
36+
[
37+
doc["name"],
38+
doc["description"][:80] + "..." if len(doc["description"]) > 80 else doc["description"],
39+
", ".join(doc["categories"]),
40+
f"${doc['price']:.2f}",
41+
doc["sku"],
42+
]
43+
)
44+
table.add_row(*row)
45+
46+
console.print(table)

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
openai
1+
openai==1.109.1
22
numpy
33
pandas
44
azure-identity
55
azure-search-documents
66
python-dotenv
77
pillow
88
azure-ai-evaluation
9-
ipykernel
9+
ipykernel
10+
rich

search_relevance.ipynb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 1,
19+
"execution_count": null,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
@@ -30,11 +30,11 @@
3030
"\n",
3131
"dotenv.load_dotenv()\n",
3232
"\n",
33-
"azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv(\"AZURE_TENANT_ID\"))\n",
33+
"azure_credential = azure.identity.DefaultAzureCredential()\n",
3434
"\n",
3535
"# Initialize Azure OpenAI client\n",
3636
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
37-
"AZURE_OPENAI_ADA_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_ADA_DEPLOYMENT\")\n",
37+
"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",
4040
"openai_client = openai.AzureOpenAI(\n",
@@ -43,7 +43,7 @@
4343
" azure_ad_token_provider=token_provider)\n",
4444
"\n",
4545
"def get_embedding(text):\n",
46-
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=text)\n",
46+
" get_embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=text)\n",
4747
" return get_embeddings_response.data[0].embedding\n",
4848
"\n",
4949
"# Initialize Azure search client\n",
@@ -72,9 +72,9 @@
7272
"name": "stdout",
7373
"output_type": "stream",
7474
"text": [
75-
"Score: 0.81581\tMatching text: Not found\n",
76-
"Score: 0.80998\tMatching text: Not found\n",
77-
"Score: 0.80965\tMatching text: Not found\n"
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"
7878
]
7979
}
8080
],
@@ -239,7 +239,7 @@
239239
],
240240
"metadata": {
241241
"kernelspec": {
242-
"display_name": "Python 3",
242+
"display_name": ".venv (3.11.9)",
243243
"language": "python",
244244
"name": "python3"
245245
},
@@ -253,7 +253,7 @@
253253
"name": "python",
254254
"nbconvert_exporter": "python",
255255
"pygments_lexer": "ipython3",
256-
"version": "3.11.0"
256+
"version": "3.11.9"
257257
},
258258
"orig_nbformat": 4
259259
},

vector_embeddings.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"# Set up OpenAI client based on environment variables\n",
3030
"dotenv.load_dotenv()\n",
3131
"AZURE_OPENAI_SERVICE = os.getenv(\"AZURE_OPENAI_SERVICE\")\n",
32-
"AZURE_OPENAI_ADA_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_ADA_DEPLOYMENT\")\n",
32+
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\")\n",
3333
"\n",
3434
"azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv(\"AZURE_TENANT_ID\"))\n",
3535
"token_provider = azure.identity.get_bearer_token_provider(azure_credential,\n",
@@ -55,7 +55,7 @@
5555
"source": [
5656
"sentence = \"A dog just walked past my house and yipped yipped like a Martian\"\n",
5757
"\n",
58-
"response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=sentence)\n",
58+
"response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=sentence)\n",
5959
"\n",
6060
"vector = response.data[0].embedding"
6161
]
@@ -1139,7 +1139,7 @@
11391139
" 'djkshsjdkhfsjdfkhsd']\n",
11401140
"\n",
11411141
"def get_embeddings(sentences):\n",
1142-
" embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=sentences)\n",
1142+
" embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=sentences)\n",
11431143
" return [embedding_object.embedding for embedding_object in embeddings_response.data]\n",
11441144
"\n",
11451145
"embeddings1 = get_embeddings(sentences1)\n",
@@ -1277,7 +1277,7 @@
12771277
"# Compute vector for query\n",
12781278
"query = \"101 Dalmations\"\n",
12791279
"\n",
1280-
"embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_ADA_DEPLOYMENT, input=[query])\n",
1280+
"embeddings_response = openai_client.embeddings.create(model=AZURE_OPENAI_EMBEDDING_DEPLOYMENT, input=[query])\n",
12811281
"vector = embeddings_response.data[0].embedding\n",
12821282
"\n",
12831283
"# Compute cosine similarity between query and each movie title\n",

0 commit comments

Comments
 (0)