langchain_community.storage.sql
.SQLBaseStore¶
- class langchain_community.storage.sql.SQLBaseStore(connection_string: str, collection_name: str = 'langchain', collection_metadata: Optional[dict] = None, pre_delete_collection: bool = False, connection: Optional[Connection] = None, engine_args: Optional[dict[str, Any]] = None)[source]¶
SQL storage
- Parameters
connection_string – SQL connection string that will be passed to SQLAlchemy.
collection_name – The name of the collection to use. (default: langchain) NOTE: Collections are useful to isolate your data in a given a database. This is not the name of the table, but the name of the collection. The tables will be created when initializing the store (if not exists) So, make sure the user has the right permissions to create tables.
pre_delete_collection – If True, will delete the collection if it exists. (default: False). Useful for testing.
engine_args – SQLAlchemy’s create engine arguments.
Example
from langchain_community.storage import SQLDocStore from langchain_community.embeddings.openai import OpenAIEmbeddings # example using an SQLDocStore to store Document objects for # a ParentDocumentRetriever CONNECTION_STRING = "postgresql+psycopg2://user:pass@localhost:5432/db" COLLECTION_NAME = "state_of_the_union_test" docstore = SQLDocStore( collection_name=COLLECTION_NAME, connection_string=CONNECTION_STRING, ) child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) vectorstore = ... retriever = ParentDocumentRetriever( vectorstore=vectorstore, docstore=docstore, child_splitter=child_splitter, ) # example using an SQLStrStore to store strings # same example as in "InMemoryStore" but using SQL persistence store = SQLDocStore( collection_name=COLLECTION_NAME, connection_string=CONNECTION_STRING, ) store.mset([('key1', 'value1'), ('key2', 'value2')]) store.mget(['key1', 'key2']) # ['value1', 'value2'] store.mdelete(['key1']) list(store.yield_keys()) # ['key2'] list(store.yield_keys(prefix='k')) # ['key2'] # delete the COLLECTION_NAME collection docstore.delete_collection()
Methods
__init__
(connection_string[, ...])mdelete
(keys)Delete the given keys and their associated values.
mget
(keys)Get the values associated with the given keys.
mset
(key_value_pairs)Set the values for the given keys.
yield_keys
([prefix])Get an iterator over keys that match the given prefix.
- __init__(connection_string: str, collection_name: str = 'langchain', collection_metadata: Optional[dict] = None, pre_delete_collection: bool = False, connection: Optional[Connection] = None, engine_args: Optional[dict[str, Any]] = None) None [source]¶
- mdelete(keys: Sequence[str]) None [source]¶
Delete the given keys and their associated values.
- Parameters
keys (Sequence[str]) – A sequence of keys to delete.
- mget(keys: Sequence[str]) List[Optional[V]] [source]¶
Get the values associated with the given keys.
- Parameters
keys (Sequence[str]) – A sequence of keys.
- Returns
A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.