Skip to content

refactor: Refactor AlloyDBEngine to depend on PGEngine #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 126 additions & 3 deletions docs/vector_store.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@
"source": [
"from langchain_google_alloydb_pg import Column\n",
"\n",
"\n",
"# Set table name\n",
"TABLE_NAME = \"vectorstore_custom\"\n",
"# SCHEMA_NAME = \"my_schema\"\n",
Expand Down Expand Up @@ -649,8 +650,45 @@
"all_texts = [\"Apples and oranges\", \"Cars and airplanes\", \"Pineapple\", \"Train\", \"Banana\"]\n",
"metadatas = [{\"len\": len(t)} for t in all_texts]\n",
"ids = [str(uuid.uuid4()) for _ in all_texts]\n",
"await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)\n",
"await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### For v0.13.0+\n",
"\n",
"**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use filter on search\n",
"docs = await custom_store.asimilarity_search(query, filter={\"len\": {\"$gte\": 6}})\n",
"\n",
"print(docs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### For v0.12.0 and under\n",
"\n",
"You can make use of the string filters to filter on metadata"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use filter on search\n",
"docs = await custom_store.asimilarity_search(query, filter=\"len >= 6\")\n",
"\n",
Expand Down Expand Up @@ -765,6 +803,37 @@
"Since price_usd is one of the metadata_columns, we can use price filter while searching"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### For v0.13.0+\n",
"\n",
"**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import uuid\n",
"\n",
"docs = await custom_store.asimilarity_search(query, filter={\"price_usd\": {\"$gte\": 100}})\n",
"\n",
"print(docs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### For v0.12.0 and under\n",
"\n",
"You can make use of the string filters to filter on metadata"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -782,8 +851,62 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Search for documents with json filter\n",
"Since category is added in json metadata, we can use filter on JSON fields while searching\n"
"### Search for documents with json filter\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### For v0.13.0+\n",
"\n",
"**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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "sql"
}
},
"outputs": [],
"source": [
"ALTER TABLE vectorstore_table ADD COLUMN category VARCHAR;\n",
"UPDATE vectorstore_table\n",
"SET\n",
" category = metadata ->> 'category';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import uuid\n",
"\n",
"docs = await custom_store.asimilarity_search(query, filter={\"category\": \"Electronics\"})\n",
"\n",
"print(docs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"#### For v0.12.0 and under\n",
"\n",
"Since category is added in json metadata, we can use filter on JSON fields using string filters while searching."
]
},
{
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ authors = [
dependencies = [
"google-cloud-alloydb-connector[asyncpg]>=1.2.0, <2.0.0",
"google-cloud-storage>=2.18.2, <4.0.0",
"langchain-core>=0.2.36, <1.0.0",
"numpy>=1.24.4, <3.0.0; python_version >= '3.11'",
"numpy>=1.24.4, <=2.2.6; python_version == '3.10'",
"numpy>=1.24.4, <=2.0.2; python_version <= '3.9'",
"pgvector>=0.2.5, <1.0.0",
"SQLAlchemy[asyncio]>=2.0.25, <3.0.0"
"langchain-postgres>=0.0.15",
]

classifiers = [
Expand Down
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
google-cloud-alloydb-connector[asyncpg]==1.9.0
google-cloud-storage==3.1.1
langchain-core==0.3.67
numpy==2.3.1; python_version >= "3.11"
numpy==2.2.6; python_version == "3.10"
numpy==2.0.2; python_version <= "3.9"
pgvector==0.4.1
SQLAlchemy[asyncio]==2.0.41
langgraph==0.6.0
langchain-postgres==0.0.15
2 changes: 1 addition & 1 deletion samples/langchain_quick_start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@
},
"outputs": [],
"source": [
"from langchain_google_alloydb_pg import AlloyDBEngine, Column, AlloyDBLoader\n",
"from langchain_google_alloydb_pg import AlloyDBEngine, AlloyDBLoader, Column\n",
"\n",
"engine = AlloyDBEngine.from_instance(\n",
" project_id=project_id,\n",
Expand Down
4 changes: 3 additions & 1 deletion src/langchain_google_alloydb_pg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from langchain_postgres import Column

from .chat_message_history import AlloyDBChatMessageHistory
from .checkpoint import AlloyDBSaver
from .embeddings import AlloyDBEmbeddings
from .engine import AlloyDBEngine, Column
from .engine import AlloyDBEngine
from .loader import AlloyDBDocumentSaver, AlloyDBLoader
from .model_manager import AlloyDBModel, AlloyDBModelManager
from .vectorstore import AlloyDBVectorStore
Expand Down
Loading