Skip to content

Commit 2293bd7

Browse files
refactor!: Refactor AlloyDBVectorStore and AlloyDBEngine to depend on PGVectorstore and PGEngine respectively (#434)
* chore: Refactor AlloyDBEngine to depend on PGEngine * fix tests * dependency fux * dependency fix * change imports * change imports * change imports * fix breaking imports * linter fix and add vector index tests * refactor!: Refactor AlloyDBVectorStore to depend on PGVectorstore (#435) * refactor!: Refactor AlloyDBVectorStore to depend on PGVectorstore * Linter fix * Fix tests * Fix tests * fix tests * linter fix * fix vectorstore * add all existing tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix tests * fix dependecies * Add tests for hybrid search * linter fix * fix test * Add better documentation for breaking change * minor fix * update deps * Re-expose hybrid search config * add header * linter fix * re-expose hybrid search configs from init * re-expose hybrid search configs from init --------- Co-authored-by: Averi Kitsch <[email protected]>
1 parent 4ccc09a commit 2293bd7

19 files changed

+917
-2403
lines changed

docs/vector_store.ipynb

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@
608608
"source": [
609609
"from langchain_google_alloydb_pg import Column\n",
610610
"\n",
611+
"\n",
611612
"# Set table name\n",
612613
"TABLE_NAME = \"vectorstore_custom\"\n",
613614
"# SCHEMA_NAME = \"my_schema\"\n",
@@ -649,8 +650,45 @@
649650
"all_texts = [\"Apples and oranges\", \"Cars and airplanes\", \"Pineapple\", \"Train\", \"Banana\"]\n",
650651
"metadatas = [{\"len\": len(t)} for t in all_texts]\n",
651652
"ids = [str(uuid.uuid4()) for _ in all_texts]\n",
652-
"await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)\n",
653+
"await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)"
654+
]
655+
},
656+
{
657+
"cell_type": "markdown",
658+
"metadata": {},
659+
"source": [
660+
"#### For v0.13.0+\n",
653661
"\n",
662+
"**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters."
663+
]
664+
},
665+
{
666+
"cell_type": "code",
667+
"execution_count": null,
668+
"metadata": {},
669+
"outputs": [],
670+
"source": [
671+
"# Use filter on search\n",
672+
"docs = await custom_store.asimilarity_search(query, filter={\"len\": {\"$gte\": 6}})\n",
673+
"\n",
674+
"print(docs)"
675+
]
676+
},
677+
{
678+
"cell_type": "markdown",
679+
"metadata": {},
680+
"source": [
681+
"#### For v0.12.0 and under\n",
682+
"\n",
683+
"You can make use of the string filters to filter on metadata"
684+
]
685+
},
686+
{
687+
"cell_type": "code",
688+
"execution_count": null,
689+
"metadata": {},
690+
"outputs": [],
691+
"source": [
654692
"# Use filter on search\n",
655693
"docs = await custom_store.asimilarity_search(query, filter=\"len >= 6\")\n",
656694
"\n",
@@ -765,6 +803,37 @@
765803
"Since price_usd is one of the metadata_columns, we can use price filter while searching"
766804
]
767805
},
806+
{
807+
"cell_type": "markdown",
808+
"metadata": {},
809+
"source": [
810+
"#### For v0.13.0+\n",
811+
"\n",
812+
"**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters."
813+
]
814+
},
815+
{
816+
"cell_type": "code",
817+
"execution_count": null,
818+
"metadata": {},
819+
"outputs": [],
820+
"source": [
821+
"import uuid\n",
822+
"\n",
823+
"docs = await custom_store.asimilarity_search(query, filter={\"price_usd\": {\"$gte\": 100}})\n",
824+
"\n",
825+
"print(docs)"
826+
]
827+
},
828+
{
829+
"cell_type": "markdown",
830+
"metadata": {},
831+
"source": [
832+
"#### For v0.12.0 and under\n",
833+
"\n",
834+
"You can make use of the string filters to filter on metadata"
835+
]
836+
},
768837
{
769838
"cell_type": "code",
770839
"execution_count": null,
@@ -782,8 +851,62 @@
782851
"cell_type": "markdown",
783852
"metadata": {},
784853
"source": [
785-
"### Search for documents with json filter\n",
786-
"Since category is added in json metadata, we can use filter on JSON fields while searching\n"
854+
"### Search for documents with json filter\n"
855+
]
856+
},
857+
{
858+
"cell_type": "markdown",
859+
"metadata": {},
860+
"source": [
861+
"#### For v0.13.0+\n",
862+
"\n",
863+
"**Important Update:** Support for string filters has been deprecated. To filter data on the JSON metadata, you must first create a new column for the specific key you wish to filter on. Use the following SQL command to set this up."
864+
]
865+
},
866+
{
867+
"cell_type": "code",
868+
"execution_count": null,
869+
"metadata": {
870+
"vscode": {
871+
"languageId": "sql"
872+
}
873+
},
874+
"outputs": [],
875+
"source": [
876+
"ALTER TABLE vectorstore_table ADD COLUMN category VARCHAR;\n",
877+
"UPDATE vectorstore_table\n",
878+
"SET\n",
879+
" category = metadata ->> 'category';"
880+
]
881+
},
882+
{
883+
"cell_type": "markdown",
884+
"metadata": {},
885+
"source": [
886+
"Now that you've added the new column, you must update the Vectorstore instance to recognize it. After which the new column is available for filtering operations."
887+
]
888+
},
889+
{
890+
"cell_type": "code",
891+
"execution_count": null,
892+
"metadata": {},
893+
"outputs": [],
894+
"source": [
895+
"import uuid\n",
896+
"\n",
897+
"docs = await custom_store.asimilarity_search(query, filter={\"category\": \"Electronics\"})\n",
898+
"\n",
899+
"print(docs)"
900+
]
901+
},
902+
{
903+
"cell_type": "markdown",
904+
"metadata": {},
905+
"source": [
906+
"\n",
907+
"#### For v0.12.0 and under\n",
908+
"\n",
909+
"Since category is added in json metadata, we can use filter on JSON fields using string filters while searching."
787910
]
788911
},
789912
{

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ authors = [
1111
dependencies = [
1212
"google-cloud-alloydb-connector[asyncpg]>=1.2.0, <2.0.0",
1313
"google-cloud-storage>=2.18.2, <4.0.0",
14-
"langchain-core>=0.2.36, <1.0.0",
1514
"numpy>=1.24.4, <3.0.0; python_version >= '3.11'",
1615
"numpy>=1.24.4, <=2.2.6; python_version == '3.10'",
1716
"numpy>=1.24.4, <=2.0.2; python_version <= '3.9'",
18-
"pgvector>=0.2.5, <1.0.0",
19-
"SQLAlchemy[asyncio]>=2.0.25, <3.0.0"
17+
"langchain-postgres>=0.0.15",
2018
]
2119

2220
classifiers = [

requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
google-cloud-alloydb-connector[asyncpg]==1.9.0
22
google-cloud-storage==3.1.1
3-
langchain-core==0.3.67
43
numpy==2.3.1; python_version >= "3.11"
54
numpy==2.2.6; python_version == "3.10"
65
numpy==2.0.2; python_version <= "3.9"
7-
pgvector==0.4.1
8-
SQLAlchemy[asyncio]==2.0.41
96
langgraph==0.6.0
7+
langchain-postgres==0.0.15

samples/langchain_quick_start.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@
601601
},
602602
"outputs": [],
603603
"source": [
604-
"from langchain_google_alloydb_pg import AlloyDBEngine, Column, AlloyDBLoader\n",
604+
"from langchain_google_alloydb_pg import AlloyDBEngine, AlloyDBLoader, Column\n",
605605
"\n",
606606
"engine = AlloyDBEngine.from_instance(\n",
607607
" project_id=project_id,\n",

src/langchain_google_alloydb_pg/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from langchain_postgres import Column
16+
from langchain_postgres.v2.hybrid_search_config import (
17+
HybridSearchConfig,
18+
reciprocal_rank_fusion,
19+
weighted_sum_ranking,
20+
)
21+
1522
from .chat_message_history import AlloyDBChatMessageHistory
1623
from .checkpoint import AlloyDBSaver
1724
from .embeddings import AlloyDBEmbeddings
18-
from .engine import AlloyDBEngine, Column
25+
from .engine import AlloyDBEngine
1926
from .loader import AlloyDBDocumentSaver, AlloyDBLoader
2027
from .model_manager import AlloyDBModel, AlloyDBModelManager
2128
from .vectorstore import AlloyDBVectorStore
@@ -32,5 +39,8 @@
3239
"AlloyDBModelManager",
3340
"AlloyDBModel",
3441
"AlloyDBSaver",
42+
"HybridSearchConfig",
43+
"reciprocal_rank_fusion",
44+
"weighted_sum_ranking",
3545
"__version__",
3646
]

0 commit comments

Comments
 (0)