Skip to content

Commit

Permalink
Update the example based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
Amnah199 committed Oct 28, 2024
1 parent d84ced9 commit c42c1f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 0 additions & 1 deletion .github/workflows/azure_ai_search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 1
matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
Expand Down
19 changes: 15 additions & 4 deletions integrations/azure_ai_search/example/embedding_retrieval.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from haystack import Document, Pipeline
from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
from haystack.components.writers import DocumentWriter
from haystack.document_stores.types import DuplicatePolicy

from haystack_integrations.components.retrievers.azure_ai_search import AzureAISearchEmbeddingRetriever
from haystack_integrations.document_stores.azure_ai_search import AzureAISearchDocumentStore

"""
This example demonstrates how to use the AzureAISearchEmbeddingRetriever to retrieve documents based on a query.
This example demonstrates how to use the AzureAISearchEmbeddingRetriever to retrieve documents using embeddings based on a query.
To run this example, you'll need an Azure Search service endpoint and API key, which can either be
set as environment variables (AZURE_SEARCH_SERVICE_ENDPOINT and AZURE_SEARCH_API_KEY) or
provided directly to AzureAISearchDocumentStore(as params "api_key", "azure_endpoint").
Otherwise you can use DefaultAzureCredential to authenticate with Azure services.
See more details at https://learn.microsoft.com/en-us/azure/search/keyless-connections?tabs=python%2Cazure-cli
"""

document_store = AzureAISearchDocumentStore()
document_store = AzureAISearchDocumentStore(index_name="retrieval-example")

model = "sentence-transformers/all-mpnet-base-v2"

Expand All @@ -32,8 +33,18 @@

document_embedder = SentenceTransformersDocumentEmbedder(model=model)
document_embedder.warm_up()
documents_with_embeddings = document_embedder.run(documents)
document_store.write_documents(documents_with_embeddings.get("documents"), policy=DuplicatePolicy.SKIP)

# Indexing Pipeline
indexing_pipeline = Pipeline()
indexing_pipeline.add_component(instance=document_embedder, name="doc_embedder")
indexing_pipeline.add_component(
instance=DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP), name="doc_writer"
)
indexing_pipeline.connect("doc_embedder", "doc_writer")

indexing_pipeline.run({"doc_embedder": {"documents": documents}})

# Query Pipeline
query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder(model=model))
query_pipeline.add_component("retriever", AzureAISearchEmbeddingRetriever(document_store=document_store))
Expand Down
6 changes: 4 additions & 2 deletions integrations/azure_ai_search/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import uuid

import pytest
from azure.core.credentials import AzureKeyCredential
Expand All @@ -10,7 +11,7 @@
from haystack_integrations.document_stores.azure_ai_search import AzureAISearchDocumentStore

# This is the approximate time in seconds it takes for the documents to be available in Azure Search index
SLEEP_TIME_IN_SECONDS = 10
SLEEP_TIME_IN_SECONDS = 5


@pytest.fixture()
Expand All @@ -24,7 +25,8 @@ def document_store(request):
This is the most basic requirement for the child class: provide
an instance of this document store so the base class can use it.
"""
index_name = "haystack_test_integration"
index_name = f"haystack_test_{uuid.uuid4().hex}"
print (index_name)
metadata_fields = getattr(request, "param", {}).get("metadata_fields", None)

azure_endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"]
Expand Down

0 comments on commit c42c1f4

Please sign in to comment.