Skip to content

Commit ff5d35a

Browse files
committed
sdks/python: remove examples from this PR
1 parent dbfd5f5 commit ff5d35a

2 files changed

Lines changed: 3 additions & 151 deletions

File tree

sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment.py

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -116,75 +116,3 @@ def enrichment_with_vertex_ai_legacy():
116116
| "Enrich W/ Vertex AI" >> Enrichment(vertex_ai_handler)
117117
| "Print" >> beam.Map(print))
118118
# [END enrichment_with_vertex_ai_legacy]
119-
120-
121-
def enrichment_with_milvus():
122-
# [START enrichment_with_milvus]
123-
import os
124-
import apache_beam as beam
125-
from apache_beam.ml.rag.types import Content
126-
from apache_beam.ml.rag.types import Chunk
127-
from apache_beam.ml.rag.types import Embedding
128-
from apache_beam.transforms.enrichment import Enrichment
129-
from apache_beam.ml.rag.enrichment.milvus_search import (
130-
MilvusSearchEnrichmentHandler,
131-
MilvusConnectionParameters,
132-
MilvusSearchParameters,
133-
MilvusCollectionLoadParameters,
134-
VectorSearchParameters,
135-
VectorSearchMetrics)
136-
137-
uri = os.environ.get("MILVUS_VECTOR_DB_URI")
138-
user = os.environ.get("MILVUS_VECTOR_DB_USER")
139-
password = os.environ.get("MILVUS_VECTOR_DB_PASSWORD")
140-
db_id = os.environ.get("MILVUS_VECTOR_DB_ID")
141-
token = os.environ.get("MILVUS_VECTOR_DB_TOKEN")
142-
collection_name = os.environ.get("MILVUS_VECTOR_DB_COLLECTION_NAME")
143-
144-
data = [
145-
Chunk(
146-
id="query1",
147-
embedding=Embedding(dense_embedding=[0.1, 0.2, 0.3]),
148-
content=Content())
149-
]
150-
151-
connection_parameters = MilvusConnectionParameters(
152-
uri, user, password, db_id, token)
153-
154-
# The first condition (language == "en") excludes documents in other
155-
# languages. Initially, this gives us two documents. After applying the second
156-
# condition (cost < 50), only the first document returns in search results.
157-
filter_expr = 'metadata["language"] == "en" AND cost < 50'
158-
159-
search_params = {"metric_type": VectorSearchMetrics.COSINE.value, "nprobe": 1}
160-
161-
vector_search_params = VectorSearchParameters(
162-
anns_field="dense_embedding_cosine",
163-
limit=3,
164-
filter=filter_expr,
165-
search_params=search_params)
166-
167-
search_parameters = MilvusSearchParameters(
168-
collection_name=collection_name,
169-
search_strategy=vector_search_params,
170-
output_fields=["id", "content", "domain", "cost", "metadata"],
171-
round_decimal=2)
172-
173-
# MilvusCollectionLoadParameters is optional and provides fine-grained control
174-
# over how collections are loaded into memory. For simple use cases or when
175-
# getting started, this parameter can be omitted to use default loading
176-
# behavior. Consider using it in resource-constrained environments to optimize
177-
# memory usage and query performance.
178-
collection_load_parameters = MilvusCollectionLoadParameters()
179-
180-
milvus_search_handler = MilvusSearchEnrichmentHandler(
181-
connection_parameters=connection_parameters,
182-
search_parameters=search_parameters,
183-
collection_load_parameters=collection_load_parameters)
184-
with beam.Pipeline() as p:
185-
_ = (
186-
p
187-
| "Create" >> beam.Create(data)
188-
| "Enrich W/ Milvus" >> Enrichment(milvus_search_handler)
189-
| "Print" >> beam.Map(print))
190-
# [END enrichment_with_milvus]

sdks/python/apache_beam/examples/snippets/transforms/elementwise/enrichment_test.py

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,16 @@
1818
# pytype: skip-file
1919
# pylint: disable=line-too-long
2020

21-
import os
2221
import unittest
2322
from io import StringIO
2423

2524
import mock
26-
import pytest
2725

2826
# pylint: disable=unused-import
2927
try:
30-
from apache_beam.examples.snippets.transforms.elementwise.enrichment import (
31-
enrichment_with_bigtable,
32-
enrichment_with_vertex_ai,
33-
enrichment_with_vertex_ai_legacy,
34-
enrichment_with_milvus)
35-
from apache_beam.ml.rag.enrichment.milvus_search import (
36-
MilvusConnectionParameters)
37-
from apache_beam.ml.rag.enrichment.milvus_search_it_test import (
38-
MilvusEnrichmentTestHelper,
39-
MilvusDBContainerInfo,
40-
parse_chunk_strings,
41-
assert_chunks_equivalent)
28+
from apache_beam.examples.snippets.transforms.elementwise.enrichment import enrichment_with_bigtable, \
29+
enrichment_with_vertex_ai_legacy
30+
from apache_beam.examples.snippets.transforms.elementwise.enrichment import enrichment_with_vertex_ai
4231
from apache_beam.io.requestresponse import RequestResponseIO
4332
except ImportError:
4433
raise unittest.SkipTest('RequestResponseIO dependencies are not installed')
@@ -71,15 +60,7 @@ def validate_enrichment_with_vertex_ai_legacy():
7160
return expected
7261

7362

74-
def validate_enrichment_with_milvus():
75-
expected = '''[START enrichment_with_milvus]
76-
Chunk(content=Content(text=None), id='query1', index=0, metadata={'enrichment_data': defaultdict(<class 'list'>, {'id': [1], 'distance': [1.0], 'fields': [{'content': 'This is a test document', 'cost': 49, 'domain': 'medical', 'id': 1, 'metadata': {'language': 'en'}}]})}, embedding=Embedding(dense_embedding=[0.1, 0.2, 0.3], sparse_embedding=None))
77-
[END enrichment_with_milvus]'''.splitlines()[1:-1]
78-
return expected
79-
80-
8163
@mock.patch('sys.stdout', new_callable=StringIO)
82-
@pytest.mark.uses_testcontainer
8364
class EnrichmentTest(unittest.TestCase):
8465
def test_enrichment_with_bigtable(self, mock_stdout):
8566
enrichment_with_bigtable()
@@ -102,63 +83,6 @@ def test_enrichment_with_vertex_ai_legacy(self, mock_stdout):
10283
self.maxDiff = None
10384
self.assertEqual(output, expected)
10485

105-
def test_enrichment_with_milvus(self, mock_stdout):
106-
milvus_db = None
107-
try:
108-
milvus_db = EnrichmentTestHelpers.pre_milvus_enrichment()
109-
enrichment_with_milvus()
110-
output = mock_stdout.getvalue().splitlines()
111-
expected = validate_enrichment_with_milvus()
112-
self.maxDiff = None
113-
output = parse_chunk_strings(output)
114-
expected = parse_chunk_strings(expected)
115-
assert_chunks_equivalent(output, expected)
116-
except Exception as e:
117-
self.fail(f"Test failed with unexpected error: {e}")
118-
finally:
119-
if milvus_db:
120-
EnrichmentTestHelpers.post_milvus_enrichment(milvus_db)
121-
122-
123-
class EnrichmentTestHelpers:
124-
@staticmethod
125-
def pre_milvus_enrichment() -> MilvusDBContainerInfo:
126-
# Create Milvus db container and make sure it is up and running.
127-
db = MilvusEnrichmentTestHelper.start_db_container()
128-
129-
# Construct connection parameters.
130-
connection_params = MilvusConnectionParameters(
131-
uri=db.uri,
132-
user=db.user,
133-
password=db.password,
134-
db_id=db.id,
135-
token=db.token)
136-
137-
# Initialize db with data required for testing.
138-
collection_name = MilvusEnrichmentTestHelper.initialize_db_with_data(
139-
connection_params)
140-
141-
# Setup environment variables for db and collection configuration. This will
142-
# be used downstream by the milvus enrichment handler.
143-
os.environ['MILVUS_VECTOR_DB_URI'] = db.uri
144-
os.environ['MILVUS_VECTOR_DB_USER'] = db.user
145-
os.environ['MILVUS_VECTOR_DB_PASSWORD'] = db.password
146-
os.environ['MILVUS_VECTOR_DB_ID'] = db.id
147-
os.environ['MILVUS_VECTOR_DB_TOKEN'] = db.token
148-
os.environ['MILVUS_VECTOR_DB_COLLECTION_NAME'] = collection_name
149-
150-
return db
151-
152-
@staticmethod
153-
def post_milvus_enrichment(db: MilvusDBContainerInfo):
154-
MilvusEnrichmentTestHelper.stop_db_container(db)
155-
os.environ.pop('MILVUS_VECTOR_DB_URI', None)
156-
os.environ.pop('MILVUS_VECTOR_DB_USER', None)
157-
os.environ.pop('MILVUS_VECTOR_DB_PASSWORD', None)
158-
os.environ.pop('MILVUS_VECTOR_DB_ID', None)
159-
os.environ.pop('MILVUS_VECTOR_DB_TOKEN', None)
160-
os.environ.pop('MILVUS_VECTOR_DB_COLLECTION_NAME', None)
161-
16286

16387
if __name__ == '__main__':
16488
unittest.main()

0 commit comments

Comments
 (0)