Skip to content

Commit 8053f5b

Browse files
committed
Fix black formatting
1 parent becabfa commit 8053f5b

File tree

6 files changed

+77
-61
lines changed

6 files changed

+77
-61
lines changed

render_table.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ def render_product_results(results: list[dict[str, Any]], title: str, show_reran
3232
]
3333
if show_reranker:
3434
row.append(f"{doc['@search.reranker_score']:.3f}")
35-
row.extend([
36-
doc["name"],
37-
doc["description"][:80] + "..." if len(doc["description"]) > 80 else doc["description"],
38-
", ".join(doc["categories"]),
39-
f"${doc['price']:.2f}",
40-
doc["sku"],
41-
])
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+
)
4244
table.add_row(*row)
4345

4446
console.print(table)

zava_product_upload.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
def create_product_index_schema(index_name: str) -> SearchIndex:
3434
"""Create the schema for the product index.
35-
35+
3636
Args:
3737
index_name: Name of the index to create
38-
38+
3939
Returns:
4040
SearchIndex object with the schema
4141
"""
@@ -127,7 +127,7 @@ def create_product_index_schema(index_name: str) -> SearchIndex:
127127

128128
def create_index(index_client: SearchIndexClient, index_name: str) -> None:
129129
"""Create the search index if it doesn't exist.
130-
130+
131131
Args:
132132
index_client: Azure Search Index Client
133133
index_name: Name of the index to create
@@ -140,46 +140,45 @@ def create_index(index_client: SearchIndexClient, index_name: str) -> None:
140140

141141
def generate_embeddings(openai_client: OpenAI, products: list[dict[str, Any]]) -> None:
142142
"""Generate embeddings for products using OpenAI.
143-
143+
144144
Args:
145145
openai_client: OpenAI client
146146
products: List of product dictionaries (modified in place)
147147
"""
148148
print("Generating embeddings for products...")
149-
149+
150150
for i, product in enumerate(products):
151151
# Create text to embed from name, description, and categories
152152
text_to_embed = f"{product['name']} {product['description']} {' '.join(product['categories'])}"
153-
153+
154154
# Generate embedding
155155
response = openai_client.embeddings.create(
156-
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
157-
input=text_to_embed
156+
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"], input=text_to_embed
158157
)
159158
product["embedding"] = response.data[0].embedding
160-
159+
161160
if (i + 1) % 100 == 0:
162161
print(f" Generated embeddings for {i + 1}/{len(products)} products")
163-
162+
164163
print(f"Generated embeddings for all {len(products)} products.")
165164

166165

167166
def upload_products(search_client: SearchClient, products: list[dict[str, Any]]) -> None:
168167
"""Upload products to the search index.
169-
168+
170169
Args:
171170
search_client: Azure Search Client
172171
products: List of product dictionaries
173172
"""
174173
print(f"Uploading {len(products)} products...")
175-
174+
176175
# Upload in batches of 1000 (Azure AI Search limit)
177176
batch_size = 1000
178177
for i in range(0, len(products), batch_size):
179-
batch = products[i:i + batch_size]
178+
batch = products[i : i + batch_size]
180179
search_client.upload_documents(documents=batch)
181180
print(f"Uploaded batch {i // batch_size + 1} ({len(batch)} products)")
182-
181+
183182
print(f"Successfully uploaded {len(products)} products.")
184183

185184

@@ -189,40 +188,39 @@ def main() -> None:
189188
search_service = os.environ["AZURE_SEARCH_SERVICE"]
190189
index_name = "zava-products-index"
191190
tenant_id = os.environ["AZURE_TENANT_ID"]
192-
191+
193192
# Create credential
194193
azure_credential = azure.identity.AzureCliCredential(tenant_id=tenant_id)
195-
194+
196195
# Create token provider for OpenAI
197196
token_provider = azure.identity.get_bearer_token_provider(
198197
azure_credential, "https://cognitiveservices.azure.com/.default"
199198
)
200-
199+
201200
# Create clients
202201
search_endpoint = f"https://{search_service}.search.windows.net"
203202
index_client = SearchIndexClient(endpoint=search_endpoint, credential=azure_credential)
204203
search_client = SearchClient(endpoint=search_endpoint, index_name=index_name, credential=azure_credential)
205-
204+
206205
openai_client = OpenAI(
207-
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1",
208-
api_key=token_provider
206+
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1", api_key=token_provider
209207
)
210-
208+
211209
# Create the index
212210
create_index(index_client, index_name)
213-
211+
214212
# Load product data
215213
print("Loading product data from product_data_flat.json...")
216214
with open("zava_product_data/product_data_flat.json") as f:
217215
products = json.load(f)
218216
print(f"Loaded {len(products)} products.")
219-
217+
220218
# Generate embeddings
221219
generate_embeddings(openai_client, products)
222-
220+
223221
# Upload products
224222
upload_products(search_client, products)
225-
223+
226224
print("\n✓ All operations completed successfully!")
227225
print(f" - Index: {index_name}")
228226
print(f" - Products uploaded: {len(products)}")

zava_search_keyword.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020

2121
results = search_client.search(search_text=search_query, top=5)
2222

23-
render_product_results(results, f"Keyword search results for '{search_query}'")
23+
render_product_results(results, f"Keyword search results for '{search_query}'")

zava_search_reranker.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
dotenv.load_dotenv()
1212

1313
azure_credential = azure.identity.AzureCliCredential(tenant_id=os.environ["AZURE_TENANT_ID"])
14-
token_provider = azure.identity.get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure_credential, "https://cognitiveservices.azure.com/.default"
16+
)
1517

1618
openai_client = OpenAI(
17-
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1",
18-
api_key=token_provider)
19+
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1", api_key=token_provider
20+
)
1921

2022
search_client = SearchClient(
2123
f"https://{os.environ['AZURE_SEARCH_SERVICE']}.search.windows.net",
@@ -25,16 +27,18 @@
2527

2628
search_query = "100 foot hose that won't break"
2729

28-
search_vector = openai_client.embeddings.create(
29-
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
30-
input=search_query).data[0].embedding
30+
search_vector = (
31+
openai_client.embeddings.create(model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"], input=search_query)
32+
.data[0]
33+
.embedding
34+
)
3135

3236
results = search_client.search(
3337
search_query,
3438
top=5,
35-
vector_queries=[
36-
VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")],
39+
vector_queries=[VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")],
3740
query_type="semantic",
38-
semantic_configuration_name="products-semantic-config")
41+
semantic_configuration_name="products-semantic-config",
42+
)
3943

40-
render_product_results(results, f"Hybrid Search with Reranker Results for '{search_query}'", show_reranker=True)
44+
render_product_results(results, f"Hybrid Search with Reranker Results for '{search_query}'", show_reranker=True)

zava_search_rrf.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
dotenv.load_dotenv()
1212

1313
azure_credential = azure.identity.AzureCliCredential(tenant_id=os.environ["AZURE_TENANT_ID"])
14-
token_provider = azure.identity.get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure_credential, "https://cognitiveservices.azure.com/.default"
16+
)
1517

1618
openai_client = OpenAI(
17-
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1",
18-
api_key=token_provider)
19+
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1", api_key=token_provider
20+
)
1921

2022
search_client = SearchClient(
2123
f"https://{os.environ['AZURE_SEARCH_SERVICE']}.search.windows.net",
@@ -25,11 +27,16 @@
2527

2628
search_query = "100 foot hose that won't break"
2729

28-
search_vector = openai_client.embeddings.create(
29-
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
30-
input=search_query).data[0].embedding
30+
search_vector = (
31+
openai_client.embeddings.create(model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"], input=search_query)
32+
.data[0]
33+
.embedding
34+
)
3135

32-
results = search_client.search(search_query, top=5, vector_queries=[
33-
VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")])
36+
results = search_client.search(
37+
search_query,
38+
top=5,
39+
vector_queries=[VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")],
40+
)
3441

35-
render_product_results(results, f"Hybrid RRF search results for '{search_query}'")
42+
render_product_results(results, f"Hybrid RRF search results for '{search_query}'")

zava_search_vector.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
dotenv.load_dotenv()
1212

1313
azure_credential = azure.identity.AzureCliCredential(tenant_id=os.environ["AZURE_TENANT_ID"])
14-
token_provider = azure.identity.get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure_credential, "https://cognitiveservices.azure.com/.default"
16+
)
1517

1618
openai_client = OpenAI(
17-
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1",
18-
api_key=token_provider)
19+
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1", api_key=token_provider
20+
)
1921

2022
search_client = SearchClient(
2123
f"https://{os.environ['AZURE_SEARCH_SERVICE']}.search.windows.net",
@@ -25,11 +27,14 @@
2527

2628
search_query = "100 foot hose that won't break"
2729

28-
search_vector = openai_client.embeddings.create(
29-
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"],
30-
input=search_query).data[0].embedding
30+
search_vector = (
31+
openai_client.embeddings.create(model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"], input=search_query)
32+
.data[0]
33+
.embedding
34+
)
3135

32-
results = search_client.search(None, top=5, vector_queries=[
33-
VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")])
36+
results = search_client.search(
37+
None, top=5, vector_queries=[VectorizedQuery(vector=search_vector, k_nearest_neighbors=50, fields="embedding")]
38+
)
3439

35-
render_product_results(results, f"Vector search results for '{search_query}'")
40+
render_product_results(results, f"Vector search results for '{search_query}'")

0 commit comments

Comments
 (0)