Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1724294216 ## examples/notebooks/beam-ml/rag_usecase/opensearch_rag_pipeline.ipynb: ## @@ -0,0 +1,1779 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "aab0b3c8-f952-4db8-8026-74d4d0d18f2c", + "metadata": {}, + "outputs": [], + "source": [ +"# @title ## Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n", +"\n", +"# Licensed to the Apache Software Foundation (ASF) under one\n", +"# or more contributor license agreements. See the NOTICE file\n", +"# distributed with this work for additional information\n", +"# regarding copyright ownership. The ASF licenses this file\n", +"# to you under the Apache License, Version 2.0 (the\n", +"# \"License\"); you may not use this file except in compliance\n", +"# with the License. You may obtain a copy of the License at\n", +"#\n", +"# http://www.apache.org/licenses/LICENSE-2.0\n";, +"#\n", +"# Unless required by applicable law or agreed to in writing,\n", +"# software distributed under the License is distributed on an\n", +"# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n", +"# KIND, either express or implied. See the License for the\n", +"# specific language governing permissions and limitations\n", +"# under the License" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "62a8c341-3092-48f7-a4b6-c586414d2c83", + "metadata": {}, + "outputs": [ +{ + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pandas==1.4.4 in /usr/local/lib/python3.10/site-packages (1.4.4)\n", + "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (2.8.2)\n", + "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (2022.2.1)\n", + "Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (1.24.4)\n", + "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas==1.4.4) (1.16.0)\n", + "Requirement already satisfied: numpy==1.24.4 in /usr/local/lib/python3.10/site-packages (1.24.4)\n", + "Requirement already satisfied: apache_beam==2.56.0 in /usr/local/lib/python3.10/site-packages (2.56.0)\n", + "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.7)\n", + "Requirement already satisfied: orjson<4,>=3.9.7 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (3.10.0)\n", + "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.3.1.1)\n", + "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (2.2.1)\n", + "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.9.4)\n", + "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.19)\n", + "Requirement already satisfied: grpcio!=1.48.0,!=1.59.*,!=1.60.*,!=1.61.*,!=1.62.0,!=1.62.1,<2,>=1.33.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.53.0)\n", + "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (2.7.3)\n", + "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.22.0)\n", + "Requirement already satisfied: jsonschema<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (4.19.0)\n", + "Requirement already satisfied: jsonpickle<4.0.0,>=3.0.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (3.0.2)\n", + "Requirement already satisfied: numpy<1.27.0,>=1.14.3 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.24.4)\n", + "Requirement already satisfied: objsize<0.8.0,>=0.6.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.7.0)\n", + "Requirement already satisfied: packaging>=22.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (23.2)\n", + "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (4.7.2)\n", + "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.23.0)\n", + "Requirement already satisfied: protobuf!=4.0.*,!=4.21.*,!=4.22.
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1724292782 ## examples/notebooks/beam-ml/rag_usecase/opensearch_enrichment.py: ## @@ -0,0 +1,130 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""This module implements enrichment classes to implement semantic search on opensearch Vector DB. + + +opensearch :Enrichment Handler +- +:class:`opensearchEnrichmentHandler` is a ``EnrichmentSourceHandler`` that performs enrichment/search +by fetching the similar text to the user query/prompt from the knowledge base (opensearch vector DB) and returns +the similar text along with its embeddings as Beam.Row Object. + +Example usage:: + opensearch_handler = opensearchEnrichmentHandler(opensearch_host='127.0.0.1', opensearch_port=6379) + + pipeline | Enrichment(opensearch_handler) + +No backward compatibility guarantees. Everything in this module is experimental. +""" + +import logging + + +from opensearchpy import OpenSearch +from typing import Optional +import os + +import apache_beam as beam +from apache_beam.transforms.enrichment import EnrichmentSourceHandler +from apache_beam.transforms.enrichment_handlers.utils import ExceptionLevel + +__all__ = [ +'OpenSearchEnrichmentHandler', +] + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +class OpenSearchEnrichmentHandler(EnrichmentSourceHandler[beam.Row, beam.Row]): +"""A handler for :class:`apache_beam.transforms.enrichment.Enrichment` +transform to interact with opensearch vector DB. +""" +def __init__( +self, +opensearch_host: str, +opensearch_port: int, +username: Optional[str] = os.getenv("OPENSEARCH_USERNAME"), +password: Optional[str] = os.getenv("OPENSEARCH_PASSWORD"), Review Comment: Done ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,372 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle + +from typing import Optional +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + +load_dotenv() + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + +__all__ = ['InsertDocInOpenSearch', 'InsertEmbeddingInOpenSearch'] + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + username='admin', + password='admin' + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" + + +class InsertDocInOpenSearch(PTransform): +"""InsertDocInO
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1724290190 ## examples/notebooks/beam-ml/rag_usecase/opensearch_rag_pipeline.ipynb: ## @@ -0,0 +1,1779 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "aab0b3c8-f952-4db8-8026-74d4d0d18f2c", + "metadata": {}, + "outputs": [], + "source": [ +"# @title ## Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n", +"\n", +"# Licensed to the Apache Software Foundation (ASF) under one\n", +"# or more contributor license agreements. See the NOTICE file\n", +"# distributed with this work for additional information\n", +"# regarding copyright ownership. The ASF licenses this file\n", +"# to you under the Apache License, Version 2.0 (the\n", +"# \"License\"); you may not use this file except in compliance\n", +"# with the License. You may obtain a copy of the License at\n", +"#\n", +"# http://www.apache.org/licenses/LICENSE-2.0\n";, +"#\n", +"# Unless required by applicable law or agreed to in writing,\n", +"# software distributed under the License is distributed on an\n", +"# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n", +"# KIND, either express or implied. See the License for the\n", +"# specific language governing permissions and limitations\n", +"# under the License" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "62a8c341-3092-48f7-a4b6-c586414d2c83", + "metadata": {}, + "outputs": [ +{ + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pandas==1.4.4 in /usr/local/lib/python3.10/site-packages (1.4.4)\n", + "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (2.8.2)\n", + "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (2022.2.1)\n", + "Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/site-packages (from pandas==1.4.4) (1.24.4)\n", + "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas==1.4.4) (1.16.0)\n", + "Requirement already satisfied: numpy==1.24.4 in /usr/local/lib/python3.10/site-packages (1.24.4)\n", + "Requirement already satisfied: apache_beam==2.56.0 in /usr/local/lib/python3.10/site-packages (2.56.0)\n", + "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.7)\n", + "Requirement already satisfied: orjson<4,>=3.9.7 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (3.10.0)\n", + "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.3.1.1)\n", + "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (2.2.1)\n", + "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.9.4)\n", + "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.19)\n", + "Requirement already satisfied: grpcio!=1.48.0,!=1.59.*,!=1.60.*,!=1.61.*,!=1.62.0,!=1.62.1,<2,>=1.33.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.53.0)\n", + "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (2.7.3)\n", + "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.22.0)\n", + "Requirement already satisfied: jsonschema<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (4.19.0)\n", + "Requirement already satisfied: jsonpickle<4.0.0,>=3.0.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (3.0.2)\n", + "Requirement already satisfied: numpy<1.27.0,>=1.14.3 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.24.4)\n", + "Requirement already satisfied: objsize<0.8.0,>=0.6.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (0.7.0)\n", + "Requirement already satisfied: packaging>=22.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (23.2)\n", + "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (4.7.2)\n", + "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/site-packages (from apache_beam==2.56.0) (1.23.0)\n", + "Requirement already satisfied: protobuf!=4.0.*,!=4.21.*,!=4.22.
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1722584775 ## examples/notebooks/beam-ml/rag_usecase/opensearch_enrichment.py: ## @@ -0,0 +1,130 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""This module implements enrichment classes to implement semantic search on opensearch Vector DB. + + +opensearch :Enrichment Handler +- +:class:`opensearchEnrichmentHandler` is a ``EnrichmentSourceHandler`` that performs enrichment/search +by fetching the similar text to the user query/prompt from the knowledge base (opensearch vector DB) and returns +the similar text along with its embeddings as Beam.Row Object. + +Example usage:: + opensearch_handler = opensearchEnrichmentHandler(opensearch_host='127.0.0.1', opensearch_port=6379) + + pipeline | Enrichment(opensearch_handler) + +No backward compatibility guarantees. Everything in this module is experimental. +""" + +import logging + + +from opensearchpy import OpenSearch +from typing import Optional +import os + +import apache_beam as beam +from apache_beam.transforms.enrichment import EnrichmentSourceHandler +from apache_beam.transforms.enrichment_handlers.utils import ExceptionLevel + +__all__ = [ +'OpenSearchEnrichmentHandler', +] + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +class OpenSearchEnrichmentHandler(EnrichmentSourceHandler[beam.Row, beam.Row]): +"""A handler for :class:`apache_beam.transforms.enrichment.Enrichment` +transform to interact with opensearch vector DB. +""" +def __init__( +self, +opensearch_host: str, +opensearch_port: int, +username: Optional[str] = os.getenv("OPENSEARCH_USERNAME"), +password: Optional[str] = os.getenv("OPENSEARCH_PASSWORD"), +index_name: str = "embeddings-index", +vector_field: str = "text_vector", +k: int = 1, +): + +"""Args: + opensearch_host (str): opensearch Host to connect to opensearch DB + opensearch_port (int): opensearch Port to connect to opensearch DB + index_name (str): Index Name created for searching in opensearch DB + vector_field (str): vector field to compute similarity score in vector DB + k (int): Value of K in KNN algorithm for searching in opensearch +""" +self.opensearch_host = opensearch_host +self.opensearch_port = opensearch_port +self.username = username +self.password = password +self.index_name = index_name +self.vector_field = vector_field +self.k = k +self.client = None + +def __enter__(self): +"""connect to the opensearch DB using opensearch client. +""" + +if self.client is None: +http_auth = [self.username, self.password] Review Comment: No, for creating opensearch vector db client, it is mandatory to provide username and password -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1722553299 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() + + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +__all__ = ['InsertDocInOpenSearch','InsertEmbeddingInOpenSearch'] + + + + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" +class InsertDocInOpenSearch(PTransform): +"""InsertDocInOpensearch is a ``PTransform`` that writes a ``PCollection`` of +key, value tuple or 2-element array into a Opensearch server. +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +""" + +Args: +host (str, ValueProvider): The Opensearch host +port (int, ValueProvider): The Opensearch port +batch_size(int, ValueProvider): Number of key, values pairs to write at once + +Returns: +:class:`~apache_beam.transforms.ptransform.PTransform` + +""" + +self.host = host +self.port = port +self.username= username +self.password = password +self._batch_size = batch_size + +def expand(self, pcoll): +return pcoll \ + | "Reshuffle for Opensearch Insert" >> Reshuffle() \ + | "Insert document into Opensearch" >> beam.ParDo(_InsertDocOpenSearchFn(self.host, +self.port, +self.username, +self.password, +self._batch_size) + ) + + +class _InsertDocOpenSearchFn(DoFn): +"""Abstract class that takes in Opensearch +credentials to connect to Opensearch DB +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +self.host = host +self.port = port +self.username= username +self.password = password +self.batch_size = batch_size + +self.batch_counter = 0 +self.batch = list() + +self.text_col = None + + +def finish_bundle(self): +self._flush() + +def process(self, element, *args, **kwargs): +self.batch.append(element) +self.batch_counter += 1 +if self.batch_counter >= self.batch_size: +self._flush() +yield element + +def _flush(self): +if self.batch_counter == 0: +return + +with _InsertDocOpenSearchSink(self.host, self.port, self.username,self.password) as sink: + +sink.write(self.batch) +self.batch_counter = 0 +self.batch = list() + + + +class _InsertDocOpenSearchSink(object): +"""Class where we create Opensearch client +and write insertion logic in Opensearch +""" + +de
Re: [PR] Rag opensearch [beam]
jrmccluskey commented on PR #32018: URL: https://github.com/apache/beam/pull/32018#issuecomment-2284167700 I'd recommend rebasing the PR now that the initial RAG PR is merged -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1712691720 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() Review Comment: I have added the logic for reading username and password from .env file as passing the username and password directly as the parameter isn't ideal. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1712546394 ## examples/notebooks/beam-ml/rag_usecase/opensearch_enrichment.py: ## @@ -0,0 +1,138 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""This module implements enrichment classes to implement semantic search on opensearch Vector DB. + + +opensearch :Enrichment Handler +- +:class:`opensearchEnrichmentHandler` is a ``EnrichmentSourceHandler`` that performs enrichment/search +by fetching the similar text to the user query/prompt from the knowledge base (opensearch vector DB) and returns +the similar text along with its embeddings as Beam.Row Object. + +Example usage:: + opensearch_handler = opensearchEnrichmentHandler(opensearch_host='127.0.0.1', opensearch_port=6379) + + pipeline | Enrichment(opensearch_handler) + +No backward compatibility guarantees. Everything in this module is experimental. +""" + + +import logging + + +import numpy as np +from opensearchpy import OpenSearch + + +import apache_beam as beam +from apache_beam.transforms.enrichment import EnrichmentSourceHandler +from apache_beam.transforms.enrichment_handlers.utils import ExceptionLevel + +__all__ = [ +'OpenSearchEnrichmentHandler', +] + + +_LOGGER = logging.getLogger(__name__) + + +class OpenSearchEnrichmentHandler(EnrichmentSourceHandler[beam.Row, beam.Row]): + """A handler for :class:`apache_beam.transforms.enrichment.Enrichment` + transform to interact with opensearch vector DB. + + Args: +opensearch_host (str): opensearch Host to connect to opensearch DB +opensearch_port (int): opensearch Port to connect to opensearch DB +index_name (str): Index Name created for searching in opensearch DB +vector_field (str): vector field to compute similarity score in vector DB +return_fields (list): returns list of similar text and its embeddings +hybrid_fields (str): fields to be selected +k (int): Value of K in KNN algorithm for searching in opensearch + """ Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1712546077 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() + + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +__all__ = ['InsertDocInOpenSearch','InsertEmbeddingInOpenSearch'] + + + + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" +class InsertDocInOpenSearch(PTransform): +"""InsertDocInOpensearch is a ``PTransform`` that writes a ``PCollection`` of +key, value tuple or 2-element array into a Opensearch server. +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +""" + +Args: +host (str, ValueProvider): The Opensearch host +port (int, ValueProvider): The Opensearch port +batch_size(int, ValueProvider): Number of key, values pairs to write at once + +Returns: +:class:`~apache_beam.transforms.ptransform.PTransform` + +""" + +self.host = host +self.port = port +self.username= username +self.password = password +self._batch_size = batch_size + +def expand(self, pcoll): +return pcoll \ + | "Reshuffle for Opensearch Insert" >> Reshuffle() \ + | "Insert document into Opensearch" >> beam.ParDo(_InsertDocOpenSearchFn(self.host, +self.port, +self.username, +self.password, +self._batch_size) + ) + + +class _InsertDocOpenSearchFn(DoFn): +"""Abstract class that takes in Opensearch +credentials to connect to Opensearch DB +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +self.host = host +self.port = port +self.username= username +self.password = password +self.batch_size = batch_size + +self.batch_counter = 0 +self.batch = list() + +self.text_col = None + + +def finish_bundle(self): +self._flush() + +def process(self, element, *args, **kwargs): +self.batch.append(element) +self.batch_counter += 1 +if self.batch_counter >= self.batch_size: +self._flush() +yield element + +def _flush(self): +if self.batch_counter == 0: +return + +with _InsertDocOpenSearchSink(self.host, self.port, self.username,self.password) as sink: + +sink.write(self.batch) +self.batch_counter = 0 +self.batch = list() + + + +class _InsertDocOpenSearchSink(object): +"""Class where we create Opensearch client +and write insertion logic in Opensearch +""" + +de
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1712545868 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() + + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +__all__ = ['InsertDocInOpenSearch','InsertEmbeddingInOpenSearch'] + + + + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" +class InsertDocInOpenSearch(PTransform): +"""InsertDocInOpensearch is a ``PTransform`` that writes a ``PCollection`` of +key, value tuple or 2-element array into a Opensearch server. +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +""" + +Args: +host (str, ValueProvider): The Opensearch host +port (int, ValueProvider): The Opensearch port +batch_size(int, ValueProvider): Number of key, values pairs to write at once + +Returns: +:class:`~apache_beam.transforms.ptransform.PTransform` + +""" + +self.host = host +self.port = port +self.username= username +self.password = password +self._batch_size = batch_size + +def expand(self, pcoll): +return pcoll \ + | "Reshuffle for Opensearch Insert" >> Reshuffle() \ + | "Insert document into Opensearch" >> beam.ParDo(_InsertDocOpenSearchFn(self.host, +self.port, +self.username, +self.password, +self._batch_size) + ) + + +class _InsertDocOpenSearchFn(DoFn): +"""Abstract class that takes in Opensearch +credentials to connect to Opensearch DB +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +self.host = host +self.port = port +self.username= username +self.password = password +self.batch_size = batch_size + +self.batch_counter = 0 +self.batch = list() + +self.text_col = None + + +def finish_bundle(self): +self._flush() + +def process(self, element, *args, **kwargs): +self.batch.append(element) +self.batch_counter += 1 +if self.batch_counter >= self.batch_size: +self._flush() +yield element + +def _flush(self): +if self.batch_counter == 0: +return + +with _InsertDocOpenSearchSink(self.host, self.port, self.username,self.password) as sink: + +sink.write(self.batch) +self.batch_counter = 0 +self.batch = list() + + + +class _InsertDocOpenSearchSink(object): +"""Class where we create Opensearch client +and write insertion logic in Opensearch +""" + +de
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1698895682 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() + + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +__all__ = ['InsertDocInOpenSearch','InsertEmbeddingInOpenSearch'] + + + + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" +class InsertDocInOpenSearch(PTransform): +"""InsertDocInOpensearch is a ``PTransform`` that writes a ``PCollection`` of +key, value tuple or 2-element array into a Opensearch server. +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +""" + +Args: +host (str, ValueProvider): The Opensearch host +port (int, ValueProvider): The Opensearch port +batch_size(int, ValueProvider): Number of key, values pairs to write at once Review Comment: @jrmccluskey I have committed the working code, but clean up is still pending. I should have marked this PR as draft. I'll cleanup the code now but can show the working output only after previous PR is merged as I will need to update the code in beam_rag_notebook.ipynb. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
jrmccluskey commented on code in PR #32018: URL: https://github.com/apache/beam/pull/32018#discussion_r1698871123 ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() Review Comment: You don't provide a .env file and I don't see any accesses of any environment variables in the file, should be removed ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import absolute_import + +import logging + +import apache_beam as beam + +from apache_beam.transforms import DoFn +from apache_beam.transforms import PTransform +from apache_beam.transforms import Reshuffle +from apache_beam import coders + + +import typing +from opensearchpy import OpenSearch + +import os +from dotenv import load_dotenv + + +load_dotenv() + + +# Set the logging level to reduce verbose information +import logging + +logging.root.setLevel(logging.INFO) +logger = logging.getLogger(__name__) + + +__all__ = ['InsertDocInOpenSearch','InsertEmbeddingInOpenSearch'] + + + + +"""This module implements IO classes to read document in Opensearch. + + +Insert Doc in OpenSearch: +- +:class:`InsertDocInOpenSearch` is a ``PTransform`` that writes key and values to a +configured sink, and the write is conducted through a Opensearch pipeline. + +The ptransform works by getting the first and second elements from the input, +this means that inputs like `[k,v]` or `(k,v)` are valid. + +Example usage:: + + pipeline | InsertDocInOpenSearch(host='localhost', + port=6379, + batch_size=100) + + +No backward compatibility guarantees. Everything in this module is experimental. +""" +class InsertDocInOpenSearch(PTransform): +"""InsertDocInOpensearch is a ``PTransform`` that writes a ``PCollection`` of +key, value tuple or 2-element array into a Opensearch server. +""" + +def __init__(self, + host:str, + port:int, + username:str, + password:str, + batch_size:int=100 +): +""" + +Args: +host (str, ValueProvider): The Opensearch host +port (int, ValueProvider): The Opensearch port +batch_size(int, ValueProvider): Number of key, values pairs to write at once Review Comment: Arg types aren't `Union[typ, ValueProvider]`, also missing username and password args ## examples/notebooks/beam-ml/rag_usecase/opensearch_connector.py: ## @@ -0,0 +1,383 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +#
Re: [PR] Rag opensearch [beam]
github-actions[bot] commented on PR #32018: URL: https://github.com/apache/beam/pull/32018#issuecomment-2257542285 Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment `assign set of reviewers` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
Re: [PR] Rag opensearch [beam]
itsayushpandey commented on PR #32018: URL: https://github.com/apache/beam/pull/32018#issuecomment-2257540567 R: @damccorm @jrmccluskey -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org