langchain-db2¶
Reference docs
This page contains reference documentation for IBM DB2. See the docs for conceptual guides, tutorials, and examples on using DB2 modules.
langchain_db2
¶
DB2VS
¶
Bases: VectorStore
DB2VS vector store.
| PARAMETER | DESCRIPTION |
|---|---|
embedding_function
|
The embedding backend used to generate vectors for stored texts and queries
TYPE:
|
table_name
|
DB2 table name
TYPE:
|
client
|
Existing DB2 connection. Required if
TYPE:
|
distance_strategy
|
Similarity metric used by Db2
TYPE:
|
query
|
Probe text used once to infer embedding dimension
TYPE:
|
params
|
Extra options |
connection_args
|
Connection parameters used when |
text_field
|
Column name for the raw text (CLOB)
TYPE:
|
Setup
To use, you should have:
- the
langchain_db2python package installed - a connection to db2 database with vector store feature (v12.1.2+)
Instantiate
Create a Vector Store instance with ibm_db_dbi.Connection object
from langchain_db2 import DB2VS
db2vs = DB2VS(
embedding_function=embeddings, table_name=table_name, client=db_client
)
Create a Vector Store instance with connection_args
| METHOD | DESCRIPTION |
|---|---|
get_by_ids |
Get documents by their IDs. |
aget_by_ids |
Async get documents by their IDs. |
adelete |
Async delete by vector ID or other criteria. |
aadd_texts |
Async run more texts through the embeddings and add to the |
add_documents |
Add or update documents in the |
aadd_documents |
Async run more documents through the embeddings and add to the |
search |
Return docs most similar to query using a specified search type. |
asearch |
Async return docs most similar to query using a specified search type. |
asimilarity_search_with_score |
Async run similarity search with distance. |
similarity_search_with_relevance_scores |
Return docs and relevance scores in the range |
asimilarity_search_with_relevance_scores |
Async return docs and relevance scores in the range |
asimilarity_search |
Async return docs most similar to query. |
asimilarity_search_by_vector |
Async return docs most similar to embedding vector. |
amax_marginal_relevance_search |
Async return docs selected using the maximal marginal relevance. |
amax_marginal_relevance_search_by_vector |
Async return docs selected using the maximal marginal relevance. |
from_documents |
Return |
afrom_documents |
Async return |
afrom_texts |
Async return |
as_retriever |
Return |
__init__ |
|
get_embedding_dimension |
Embed the single document by wrapping it in a list. |
add_texts |
Add more texts to the vectorstore. |
similarity_search |
Return docs most similar to query. |
similarity_search_by_vector |
Return documents most similar to a query embedding. |
similarity_search_with_score |
Return the top-k documents most similar to a text query, with scores. |
similarity_search_by_vector_with_relevance_scores |
Return top-k documents for a query embedding, with relevance scores. |
similarity_search_by_vector_returning_embeddings |
Return top-k documents, their distances, and stored embeddings. |
max_marginal_relevance_search_with_score_by_vector |
Return docs and their similarity scores selected. |
max_marginal_relevance_search_by_vector |
Return docs selected using the maximal marginal relevance. |
max_marginal_relevance_search |
Return docs selected using the maximal marginal relevance. |
delete |
Delete by vector IDs. |
from_texts |
Return VectorStore initialized from texts and embeddings. |
get_pks |
Get primary keys, optionally filtered by expr. |
embeddings
property
¶
embeddings: Embeddings | None
A property that returns an Embeddings instance.
| RETURNS | DESCRIPTION |
|---|---|
Embeddings | None
|
Embeddings instance if embedding_function is an instance of Embeddings, otherwise returns None |
get_by_ids
¶
Get documents by their IDs.
The returned documents are expected to have the ID field set to the ID of the document in the vector store.
Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.
Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.
This method should NOT raise exceptions if no documents are found for some IDs.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of IDs to retrieve. |
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
aget_by_ids
async
¶
Async get documents by their IDs.
The returned documents are expected to have the ID field set to the ID of the document in the vector store.
Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.
Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.
This method should NOT raise exceptions if no documents are found for some IDs.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of IDs to retrieve. |
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
adelete
async
¶
Async delete by vector ID or other criteria.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of IDs to delete. If |
**kwargs
|
Other keyword arguments that subclasses might use.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool | None
|
|
aadd_texts
async
¶
aadd_texts(
texts: Iterable[str],
metadatas: list[dict] | None = None,
*,
ids: list[str] | None = None,
**kwargs: Any,
) -> list[str]
Async run more texts through the embeddings and add to the VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Iterable of strings to add to the |
metadatas
|
Optional list of metadatas associated with the texts. |
ids
|
Optional list |
**kwargs
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of IDs from adding the texts into the |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the number of metadatas does not match the number of texts. |
ValueError
|
If the number of IDs does not match the number of texts. |
add_documents
¶
Add or update documents in the VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
Documents to add to the |
**kwargs
|
Additional keyword arguments. If kwargs contains IDs and documents contain ids, the IDs in the kwargs will receive precedence.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of IDs of the added texts. |
aadd_documents
async
¶
search
¶
Return docs most similar to query using a specified search type.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
search_type
|
Type of search to perform. Can be
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
asearch
async
¶
Async return docs most similar to query using a specified search type.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
search_type
|
Type of search to perform. Can be
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
asimilarity_search_with_score
async
¶
similarity_search_with_relevance_scores
¶
similarity_search_with_relevance_scores(
query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]
Return docs and relevance scores in the range [0, 1].
0 is dissimilar, 1 is most similar.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Kwargs to be passed to similarity search. Should include
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
List of tuples of |
asimilarity_search_with_relevance_scores
async
¶
asimilarity_search_with_relevance_scores(
query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]
Async return docs and relevance scores in the range [0, 1].
0 is dissimilar, 1 is most similar.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Kwargs to be passed to similarity search. Should include
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
List of tuples of |
asimilarity_search
async
¶
Async return docs most similar to query.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
asimilarity_search_by_vector
async
¶
Async return docs most similar to embedding vector.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
amax_marginal_relevance_search
async
¶
amax_marginal_relevance_search(
query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any
) -> list[Document]
Async return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to.
TYPE:
|
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
amax_marginal_relevance_search_by_vector
async
¶
amax_marginal_relevance_search_by_vector(
embedding: list[float],
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
**kwargs: Any,
) -> list[Document]
Async return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
from_documents
classmethod
¶
from_documents(documents: list[Document], embedding: Embeddings, **kwargs: Any) -> Self
Return VectorStore initialized from documents and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
List of |
embedding
|
Embedding function to use.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
|
afrom_documents
async
classmethod
¶
afrom_documents(
documents: list[Document], embedding: Embeddings, **kwargs: Any
) -> Self
Async return VectorStore initialized from documents and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
List of |
embedding
|
Embedding function to use.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
|
afrom_texts
async
classmethod
¶
afrom_texts(
texts: list[str],
embedding: Embeddings,
metadatas: list[dict] | None = None,
*,
ids: list[str] | None = None,
**kwargs: Any,
) -> Self
Async return VectorStore initialized from texts and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Texts to add to the |
embedding
|
Embedding function to use.
TYPE:
|
metadatas
|
Optional list of metadatas associated with the texts. |
ids
|
Optional list of IDs associated with the texts. |
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
|
as_retriever
¶
as_retriever(**kwargs: Any) -> VectorStoreRetriever
Return VectorStoreRetriever initialized from this VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Keyword arguments to pass to the search function. Can include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
VectorStoreRetriever
|
Retriever class for |
Examples:
# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)
# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50})
# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"score_threshold": 0.8},
)
# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})
# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)
__init__
¶
__init__(
embedding_function: Callable[[str], list[float]] | Embeddings,
table_name: str,
client: Connection | None = None,
distance_strategy: DistanceStrategy = EUCLIDEAN_DISTANCE,
query: str | None = "What is a Db2 database",
params: dict[str, Any] | None = None,
connection_args: dict[str, Any] | None = None,
text_field: str = "text",
)
DB2VS vector store.
get_embedding_dimension
¶
get_embedding_dimension() -> int
Embed the single document by wrapping it in a list.
add_texts
¶
add_texts(
texts: Iterable[str],
metadatas: list[dict[Any, Any]] | None = None,
ids: list[str] | None = None,
**kwargs: Any,
) -> list[str]
Add more texts to the vectorstore.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Iterable of strings to add to the vectorstore |
metadatas
|
Optional list of metadatas associated with the texts |
ids
|
Optional list of ids for the texts that are being added to the vector store |
kwargs
|
vectorstore specific parameters
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of ids from adding the texts into the vectorstore |
similarity_search
¶
similarity_search(
query: str, k: int = 4, filter: dict[str, Any] | None = None, **kwargs: Any
) -> list[Document]
Return docs most similar to query.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
The natural-language text to search for
TYPE:
|
k
|
Number of Documents to return
TYPE:
|
filter
|
Filter by metadata |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
Documents most similar to a query |
similarity_search_by_vector
¶
similarity_search_by_vector(
embedding: list[float],
k: int = 4,
filter: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[Document]
Return documents most similar to a query embedding.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to |
k
|
Number of Documents to return
TYPE:
|
filter
|
Filter by metadata |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
Documents ordered from most to least similar |
similarity_search_with_score
¶
similarity_search_with_score(
query: str, k: int = 4, filter: dict[str, Any] | None = None, **kwargs: Any
) -> list[tuple[Document, float]]
Return the top-k documents most similar to a text query, with scores.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Natural-language query to embed and search with
TYPE:
|
k
|
Number of results to return
TYPE:
|
filter
|
Filter by metadata |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
A list of (document, score) pairs ordered by similarity. The score is the vector distance; lower values indicate closer matches. |
similarity_search_by_vector_with_relevance_scores
¶
similarity_search_by_vector_with_relevance_scores(
embedding: list[float], k: int = 4, filter: dict[str, Any] | None = None
) -> list[tuple[Document, float]]
Return top-k documents for a query embedding, with relevance scores.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to |
k
|
Number of Documents to return
TYPE:
|
filter
|
Filter by metadata |
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
A list of |
similarity_search_by_vector_returning_embeddings
¶
similarity_search_by_vector_returning_embeddings(
embedding: list[float], k: int, filter: dict[str, Any] | None = None
) -> list[tuple[Document, float, ndarray]]
Return top-k documents, their distances, and stored embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to |
k
|
Number of Documents to return
TYPE:
|
filter
|
Filter by metadata |
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float, ndarray]]
|
Tuples of |
max_marginal_relevance_search_with_score_by_vector
¶
max_marginal_relevance_search_with_score_by_vector(
embedding: list[float],
*,
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
filter: dict[str, Any] | None = None,
) -> list[tuple[Document, float]]
Return docs and their similarity scores selected.
Return docs and their similarity scores selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to |
k
|
Number of Documents to return
TYPE:
|
fetch_k
|
Number of Documents to fetch before filtering to pass to MMR algorithm
TYPE:
|
lambda_mult
|
Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity
TYPE:
|
filter
|
Filter by metadata |
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
List of Documents and similarity scores selected by maximal marginal relevance and score for each. |
max_marginal_relevance_search_by_vector
¶
max_marginal_relevance_search_by_vector(
embedding: list[float],
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
filter: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[Document]
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to |
k
|
Number of Documents to return
TYPE:
|
fetch_k
|
Number of Documents to fetch to pass to MMR algorithm.
TYPE:
|
lambda_mult
|
Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity
TYPE:
|
filter
|
Filter by metadata |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of Documents selected by maximal marginal relevance |
max_marginal_relevance_search
¶
max_marginal_relevance_search(
query: str,
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
filter: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[Document]
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to
TYPE:
|
k
|
Number of Documents to return
TYPE:
|
fetch_k
|
Number of Documents to fetch to pass to MMR algorithm.
TYPE:
|
lambda_mult
|
Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity
TYPE:
|
filter
|
Filter by metadata |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of Documents selected by maximal marginal relevance |
max_marginal_relevance_search requires that query returns matched
embeddings alongside the match documents.
delete
¶
from_texts
classmethod
¶
from_texts(
texts: Iterable[str],
embedding: Embeddings,
metadatas: list[dict] | None = None,
**kwargs: Any,
) -> DB2VS
Return VectorStore initialized from texts and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Iterable of strings to add to the vectorstore |
embedding
|
Embedding to look up documents similar to
TYPE:
|
metadatas
|
Optional list of metadatas associated with the texts |
kwargs
|
Additional keyword args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DB2VS
|
A ready-to-use vector store with the provided texts loaded |