Skip to content

Add the use_jsonb parameter to PGEngine for storing metadata using JSONB #222

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 5 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion langchain_postgres/v2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ async def _ainit_vectorstore_table(
id_column: Union[str, Column, ColumnDict] = "langchain_id",
overwrite_existing: bool = False,
store_metadata: bool = True,
use_jsonb: bool = False,
hybrid_search_config: Optional[HybridSearchConfig] = None,
) -> None:
"""
Expand All @@ -181,6 +182,8 @@ async def _ainit_vectorstore_table(
overwrite_existing (bool): Whether to drop existing table. Default: False.
store_metadata (bool): Whether to store metadata in the table.
Default: True.
use_jsonb (bool): Whether to use JSONB for metadata storage.
Default: False.
hybrid_search_config (HybridSearchConfig): Hybrid search configuration.
Default: None.

Expand Down Expand Up @@ -256,7 +259,10 @@ async def _ainit_vectorstore_table(
nullable = "NOT NULL" if not column["nullable"] else ""
query += f',\n"{column["name"]}" {column["data_type"]} {nullable}'
if store_metadata:
query += f""",\n"{metadata_json_column}" JSON"""
if use_jsonb:
query += f""",\n"{metadata_json_column}" JSONB"""
else:
query += f""",\n"{metadata_json_column}" JSON"""
query += "\n);"

async with self._pool.connect() as conn:
Expand All @@ -276,6 +282,7 @@ async def ainit_vectorstore_table(
id_column: Union[str, Column, ColumnDict] = "langchain_id",
overwrite_existing: bool = False,
store_metadata: bool = True,
use_jsonb: bool = False,
hybrid_search_config: Optional[HybridSearchConfig] = None,
) -> None:
"""
Expand All @@ -299,6 +306,8 @@ async def ainit_vectorstore_table(
overwrite_existing (bool): Whether to drop existing table. Default: False.
store_metadata (bool): Whether to store metadata in the table.
Default: True.
use_jsonb (bool): Whether to use JSONB for metadata storage.
Default: False.
hybrid_search_config (HybridSearchConfig): Hybrid search configuration.
Note that queries might be slow if the hybrid search column does not exist.
For best hybrid search performance, consider creating a TSV column and adding GIN index.
Expand All @@ -316,6 +325,7 @@ async def ainit_vectorstore_table(
id_column=id_column,
overwrite_existing=overwrite_existing,
store_metadata=store_metadata,
use_jsonb=use_jsonb,
hybrid_search_config=hybrid_search_config,
)
)
Expand All @@ -333,6 +343,7 @@ def init_vectorstore_table(
id_column: Union[str, Column, ColumnDict] = "langchain_id",
overwrite_existing: bool = False,
store_metadata: bool = True,
use_jsonb: bool = False,
hybrid_search_config: Optional[HybridSearchConfig] = None,
) -> None:
"""
Expand All @@ -356,6 +367,8 @@ def init_vectorstore_table(
overwrite_existing (bool): Whether to drop existing table. Default: False.
store_metadata (bool): Whether to store metadata in the table.
Default: True.
use_jsonb (bool): Whether to use JSONB for metadata storage.
Default: False.
hybrid_search_config (HybridSearchConfig): Hybrid search configuration.
Note that queries might be slow if the hybrid search column does not exist.
For best hybrid search performance, consider creating a TSV column and adding GIN index.
Expand All @@ -373,6 +386,7 @@ def init_vectorstore_table(
id_column=id_column,
overwrite_existing=overwrite_existing,
store_metadata=store_metadata,
use_jsonb=use_jsonb,
hybrid_search_config=hybrid_search_config,
)
)
Expand Down