Databricks Langchain Integrations Python API๏ƒ

Setup:

Install databricks-langchain.

pip install -U databricks-langchain

If you are outside Databricks, set the Databricks workspace hostname and personal access token to environment variables:

export DATABRICKS_HOSTNAME="https://your-databricks-workspace"
export DATABRICKS_TOKEN="your-personal-access-token"
class databricks_langchain.ChatDatabricks๏ƒ

Bases: BaseChatModel

Databricks chat model integration.

Instantiate:

from databricks_langchain import ChatDatabricks

llm = ChatDatabricks(
    model="databricks-meta-llama-3-1-405b-instruct",
    temperature=0,
    max_tokens=500,
)

Invoke:

messages = [
    ("system", "You are a helpful translator. Translate the user sentence to French."),
    ("human", "I love programming."),
]
llm.invoke(messages)
AIMessage(
    content="J'adore la programmation.",
    response_metadata={"prompt_tokens": 32, "completion_tokens": 9, "total_tokens": 41},
    id="run-64eebbdd-88a8-4a25-b508-21e9a5f146c5-0",
)

Stream:

for chunk in llm.stream(messages):
    print(chunk)
content='J' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content="'" id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content='ad' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content='ore' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content=' la' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content=' programm' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content='ation' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content='.' id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
content='' response_metadata={'finish_reason': 'stop'} id='run-609b8f47-e580-4691-9ee4-e2109f53155e'
stream = llm.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full
AIMessageChunk(
    content="J'adore la programmation.",
    response_metadata={"finish_reason": "stop"},
    id="run-4cef851f-6223-424f-ad26-4a54e5852aa5",
)

To get token usage returned when streaming, pass the stream_usage kwarg:

stream = llm.stream(messages, stream_usage=True)
next(stream).usage_metadata
{"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}

Alternatively, setting stream_usage when instantiating the model can be useful when incorporating ChatDatabricks into LCEL chainsโ€“ or when using methods like .with_structured_output, which generate chains under the hood.

llm = ChatDatabricks(model="databricks-meta-llama-3-1-405b-instruct", stream_usage=True)
structured_llm = llm.with_structured_output(...)

Async:

await llm.ainvoke(messages)

# stream:
# async for chunk in llm.astream(messages)

# batch:
# await llm.abatch([messages])
AIMessage(
    content="J'adore la programmation.",
    response_metadata={"prompt_tokens": 32, "completion_tokens": 9, "total_tokens": 41},
    id="run-e4bb043e-772b-4e1d-9f98-77ccc00c0271-0",
)

Tool calling:

from pydantic import BaseModel, Field


class GetWeather(BaseModel):
    '''Get the current weather in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")


class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")


llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
)
ai_msg.tool_calls
[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_ea0a6004-8e64-4ae8-a192-a40e295bfa24",
        "type": "tool_call",
    }
]

To use tool calls, your model endpoint must support tools parameter. See [Function calling on Databricks](https://python.langchain.com/docs/integrations/chat/databricks/#function-calling-on-databricks) for more information.

param extra_params: Dict[str, Any] | None = None๏ƒ

Whether to include usage metadata in streaming output. If True, additional message chunks will be generated during the stream including usage metadata.

param max_tokens: int | None = None๏ƒ

The maximum number of tokens to generate.

param model: str [Required] (alias 'endpoint')๏ƒ

Name of Databricks Model Serving endpoint to query.

param n: int = 1๏ƒ

The number of completion choices to generate.

param stop: List[str] | None = None๏ƒ

List of strings to stop generation at.

param stream_usage: bool = False๏ƒ

Any extra parameters to pass to the endpoint.

param target_uri: str = 'databricks'๏ƒ

The target URI to use. Defaults to databricks.

param temperature: float = 0.0๏ƒ

Sampling temperature. Higher values make the model more creative.

bind_tools(tools: Sequence[Dict[str, Any] | Type[BaseModel] | Callable | BaseTool], *, tool_choice: dict | str | Literal['auto', 'none', 'required', 'any'] | bool | None = None, **kwargs: Any) Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], BaseMessage]๏ƒ

Bind tool-like objects to this chat model.

Assumes model is compatible with OpenAI tool-calling API.

Parameters:
  • tools โ€“ A list of tool definitions to bind to this chat model. Can be a dictionary, pydantic model, callable, or BaseTool. Pydantic models, callables, and BaseTools will be automatically converted to their schema dictionary representation.

  • tool_choice โ€“

    Which tool to require the model to call. Options are:

    • name of the tool (str): Calls corresponding tool.

    • โ€autoโ€: Automatically selects a tool (including no tool).

    • โ€noneโ€: Model does not generate any tool calls and instead must generate a standard assistant message.

    • โ€requiredโ€: The model picks the most relevant tool in tools and must generate a tool call or a dictionary of the form:

      {
          "type": "function",
          "function": {
              "name": "<<tool_name>>"
          }
      }
      

  • **kwargs โ€“ Any additional parameters to pass to the Runnable constructor.

with_structured_output(schema: Dict | Type | None = None, *, method: Literal['function_calling', 'json_mode', 'json_schema'] = 'function_calling', include_raw: bool = False, **kwargs: Any) Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], Dict | BaseModel]๏ƒ

Model wrapper that returns outputs formatted to match the given schema.

Assumes model is compatible with OpenAI tool-calling API.

Parameters:
  • schema โ€“ The output schema as a dict or a Pydantic class. If a Pydantic class then the model output will be an object of that class. If a dict then the model output will be a dict. With a Pydantic class the returned attributes will be validated, whereas with a dict they will not be. If method is โ€œfunction_callingโ€ and schema is a dict, then the dict must match the OpenAI function-calling spec or be a valid JSON schema with top level โ€˜titleโ€™ and โ€˜descriptionโ€™ keys specified.

  • method โ€“ The method for steering model generation, either โ€œfunction_callingโ€ or โ€œjson_modeโ€. If โ€œfunction_callingโ€ then the schema will be converted to an OpenAI function and the returned model will make use of the function-calling API. If โ€œjson_modeโ€ then OpenAIโ€™s JSON mode will be used. Note that if using โ€œjson_modeโ€ then you must include instructions for formatting the output into the desired schema into the model call.

  • include_raw โ€“ If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys โ€œrawโ€, โ€œparsedโ€, and โ€œparsing_errorโ€.

Returns:

If include_raw is False and schema is a Pydantic class, Runnable outputs an instance of schema (i.e., a Pydantic object).

Otherwise, if include_raw is False then Runnable outputs a dict.

If include_raw is True, then Runnable outputs a dict with keys:
  • "raw": BaseMessage

  • "parsed": None if there was a parsing error, otherwise the type depends on the schema as described above.

  • "parsing_error": Optional[BaseException]

Return type:

A Runnable that takes any ChatModel input and returns as output

Examples:

Function-calling, Pydantic schema (method=โ€function_callingโ€, include_raw=False)

from databricks_langchain import ChatDatabricks
from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatDatabricks(model="databricks-meta-llama-3-1-70b-instruct")
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )

Function-calling, Pydantic schema (method=โ€function_callingโ€, include_raw=True):

from databricks_langchain import ChatDatabricks
from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatDatabricks(model="databricks-meta-llama-3-1-70b-instruct")
structured_llm = llm.with_structured_output(AnswerWithJustification, include_raw=True)

structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }

Function-calling, dict schema (method=โ€function_callingโ€, include_raw=False):

from databricks_langchain import ChatDatabricks
from langchain_core.utils.function_calling import convert_to_openai_tool
from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatDatabricks(model="databricks-meta-llama-3-1-70b-instruct")
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

JSON mode, Pydantic schema (method=โ€json_modeโ€, include_raw=True):

from databricks_langchain import ChatDatabricks
from pydantic import BaseModel

class AnswerWithJustification(BaseModel):
    answer: str
    justification: str

llm = ChatDatabricks(model="databricks-meta-llama-3-1-70b-instruct")
structured_llm = llm.with_structured_output(
    AnswerWithJustification,
    method="json_mode",
    include_raw=True
)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'."
    "What's heavier a pound of bricks or a pound of feathers?"
)
# -> {
#     'raw': AIMessage(content='{    "answer": "They are both the same weight.",    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." }'),
#     'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'),
#     'parsing_error': None
# }

JSON mode, no schema (schema=None, method=โ€json_modeโ€, include_raw=True):

structured_llm = llm.with_structured_output(method="json_mode", include_raw=True)

structured_llm.invoke(
    "Answer the following question. "
    "Make sure to return a JSON blob with keys 'answer' and 'justification'."
    "What's heavier a pound of bricks or a pound of feathers?"
)
# -> {
#     'raw': AIMessage(content='{    "answer": "They are both the same weight.",    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." }'),
#     'parsed': {
#         'answer': 'They are both the same weight.',
#         'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'
#     },
#     'parsing_error': None
# }
property endpoint: str๏ƒ
class databricks_langchain.DatabricksEmbeddings๏ƒ

Bases: Embeddings, BaseModel

Databricks embedding model integration.

Instantiate:

from databricks_langchain import DatabricksEmbeddings

embed = DatabricksEmbeddings(
    endpoint="databricks-bge-large-en",
)

Embed single text:

input_text = "The meaning of life is 42"
embed.embed_query(input_text)
[0.01605224609375, -0.0298309326171875, ...]
param documents_params: Dict[str, Any] = {}๏ƒ

The parameters to use for documents.

param endpoint: str [Required]๏ƒ

Name of Databricks Model Serving endpoint to query.

param query_params: Dict[str, Any] = {}๏ƒ

The parameters to use for the query.

param target_uri: str = 'databricks'๏ƒ

The target URI to use. Defaults to databricks

embed_documents(texts: List[str]) List[List[float]]๏ƒ

Embed search docs.

Parameters:

texts โ€“ List of text to embed.

Returns:

List of embeddings.

embed_query(text: str) List[float]๏ƒ

Embed query text.

Parameters:

text โ€“ Text to embed.

Returns:

Embedding.

class databricks_langchain.DatabricksVectorSearch(index_name: str, endpoint: str | None = None, embedding: Embeddings | None = None, text_column: str | None = None, columns: List[str] | None = None, client_args: Dict[str, Any] | None = None)๏ƒ

Bases: VectorStore

Databricks vector store integration.

Parameters:
  • index_name โ€“ The name of the index to use. Format: โ€œcatalog.schema.indexโ€.

  • endpoint โ€“

    The name of the Databricks Vector Search endpoint. If not specified, the endpoint name is automatically inferred based on the index name.

    Note

    If you are using databricks-vectorsearch version < 0.35, the endpoint parameter is required when initializing the vector store.

    vector_store = DatabricksVectorSearch(
        endpoint="<your-endpoint-name>",
        index_name="<your-index-name>",
        ...
    )
    

  • embedding โ€“ The embedding model. Required for direct-access index or delta-sync index with self-managed embeddings.

  • text_column โ€“ The name of the text column to use for the embeddings. Required for direct-access index or delta-sync index with self-managed embeddings. Make sure the text column specified is in the index.

  • columns โ€“ The list of column names to get when doing the search. Defaults to [primary_key, text_column].

  • client_args โ€“ Additional arguments to pass to the VectorSearchClient. Allows you to pass in values like service_principal_client_id and service_principal_client_secret to allow for service principal authentication instead of personal access token authentication.

Instantiate:

DatabricksVectorSearch supports two types of indexes:

  • Delta Sync Index automatically syncs with a source Delta Table, automatically and incrementally updating the index as the underlying data in the Delta Table changes.

  • Direct Vector Access Index supports direct read and write of vectors and metadata. The user is responsible for updating this table using the REST API or the Python SDK.

Also for delta-sync index, you can choose to use Databricks-managed embeddings or self-managed embeddings (via LangChain embeddings classes).

If you are using a delta-sync index with Databricks-managed embeddings:

from databricks_langchain.vectorstores import DatabricksVectorSearch

vector_store = DatabricksVectorSearch(index_name="<your-index-name>")

If you are using a direct-access index or a delta-sync index with self-managed embeddings, you also need to provide the embedding model and text column in your source table to use for the embeddings:

from langchain_openai import OpenAIEmbeddings

vector_store = DatabricksVectorSearch(
    index_name="<your-index-name>",
    embedding=OpenAIEmbeddings(),
    text_column="document_content",
)

Add Documents:

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="thud", metadata={"bar": "baz"})
document_3 = Document(page_content="i will be deleted :(")
documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)

Delete Documents:

vector_store.delete(ids=["3"])

Note

The delete method is only supported for direct-access index.

Search:

results = vector_store.similarity_search(query="thud", k=1)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
*thud[{"id": "2"}]

Search with filter:

results = vector_store.similarity_search(query="thud", k=1, filter={"bar": "baz"})
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
*thud[{"id": "2"}]

Search with score:

results = vector_store.similarity_search_with_score(query="qux", k=1)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.748804] foo [{'id': '1'}]

Async:

# add documents
await vector_store.aadd_documents(documents=documents, ids=ids)
# delete documents
await vector_store.adelete(ids=["3"])
# search
results = vector_store.asimilarity_search(query="thud", k=1)
# search with score
results = await vector_store.asimilarity_search_with_score(query="qux", k=1)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.748807] foo [{'id': '1'}]

Use as Retriever:

retriever = vector_store.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
[Document(metadata={"id": "2"}, page_content="thud")]
property embeddings: Embeddings | None๏ƒ

Access the query embedding object if available.

classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: List[Dict] | None = None, **kwargs: Any) VST๏ƒ

Return VectorStore initialized from texts and embeddings.

Parameters:
  • texts โ€“ Texts to add to the vectorstore.

  • embedding โ€“ Embedding function to use.

  • metadatas โ€“ Optional list of metadatas associated with the texts. Default is None.

  • ids โ€“ Optional list of IDs associated with the texts.

  • kwargs โ€“ Additional keyword arguments.

Returns:

VectorStore initialized from texts and embeddings.

Return type:

VectorStore

add_texts(texts: Iterable[str], metadatas: List[Dict] | None = None, ids: List[Any] | None = None, **kwargs: Any) List[str]๏ƒ

Add texts to the index.

Note

This method is only supported for a direct-access index.

Parameters:
  • texts โ€“ List of texts to add.

  • metadatas โ€“ List of metadata for each text. Defaults to None.

  • ids โ€“ List of ids for each text. Defaults to None. If not provided, a random uuid will be generated for each text.

Returns:

List of ids from adding the texts into the index.

async aadd_texts(texts: Iterable[str], metadatas: List[dict] | None = None, **kwargs: Any) List[str]๏ƒ

Async run more texts through the embeddings and add to the vectorstore.

Parameters:
  • texts โ€“ Iterable of strings to add to the vectorstore.

  • metadatas โ€“ Optional list of metadatas associated with the texts. Default is None.

  • ids โ€“ Optional list

  • **kwargs โ€“ vectorstore specific parameters.

Returns:

List of ids from adding the texts into the vectorstore.

Raises:
  • 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.

delete(ids: List[Any] | None = None, **kwargs: Any) bool | None๏ƒ

Delete documents from the index.

Note

This method is only supported for a direct-access index.

Parameters:

ids โ€“ List of ids of documents to delete.

Returns:

True if successful.

Return docs most similar to query.

Parameters:
  • query โ€“ Text to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents most similar to the embedding.

Async return docs most similar to query.

Parameters:
  • query โ€“ Input text.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • **kwargs โ€“ Arguments to pass to the search method.

Returns:

List of Documents most similar to the query.

similarity_search_with_score(query: str, k: int = 4, filter: Dict[str, Any] | None = None, *, query_type: str | None = None, **kwargs: Any) List[Tuple[Document, float]]๏ƒ

Return docs most similar to query, along with scores.

Parameters:
  • query โ€“ Text to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents most similar to the embedding and score for each.

async asimilarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]]๏ƒ

Async run similarity search with distance.

Parameters:
  • *args โ€“ Arguments to pass to the search method.

  • **kwargs โ€“ Arguments to pass to the search method.

Returns:

List of Tuples of (doc, similarity_score).

similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Any | None = None, *, query_type: str | None = None, query: str | None = None, **kwargs: Any) List[Document]๏ƒ

Return docs most similar to embedding vector.

Parameters:
  • embedding โ€“ Embedding to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents most similar to the embedding.

async asimilarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document]๏ƒ

Async return docs most similar to embedding vector.

Parameters:
  • embedding โ€“ Embedding to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • **kwargs โ€“ Arguments to pass to the search method.

Returns:

List of Documents most similar to the query vector.

similarity_search_by_vector_with_score(embedding: List[float], k: int = 4, filter: Any | None = None, *, query_type: str | None = None, query: str | None = None, **kwargs: Any) List[Tuple[Document, float]]๏ƒ

Return docs most similar to embedding vector, along with scores.

Note

This method is not supported for index with Databricks-managed embeddings.

Parameters:
  • embedding โ€“ Embedding to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents most similar to the embedding and score for each.

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Note

This method is not supported for index with Databricks-managed embeddings.

Parameters:
  • query โ€“ Text to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • fetch_k โ€“ Number of Documents to fetch to pass to MMR algorithm.

  • 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. Defaults to 0.5.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents selected by maximal marginal relevance.

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:
  • query โ€“ Text to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • fetch_k โ€“ Number of Documents to fetch to pass to MMR algorithm. Default is 20.

  • 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. Defaults to 0.5.

Returns:

List of Documents selected by maximal marginal relevance.

max_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Any | None = None, *, query_type: str | 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.

Note

This method is not supported for index with Databricks-managed embeddings.

Parameters:
  • embedding โ€“ Embedding to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • fetch_k โ€“ Number of Documents to fetch to pass to MMR algorithm.

  • 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. Defaults to 0.5.

  • filter โ€“ Filters to apply to the query. Defaults to None.

  • query_type โ€“ The type of this query. Supported values are โ€œANNโ€ and โ€œHYBRIDโ€.

Returns:

List of Documents selected by maximal marginal relevance.

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.

Parameters:
  • embedding โ€“ Embedding to look up documents similar to.

  • k โ€“ Number of Documents to return. Defaults to 4.

  • fetch_k โ€“ Number of Documents to fetch to pass to MMR algorithm. Default is 20.

  • 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. Defaults to 0.5.

  • **kwargs โ€“ Arguments to pass to the search method.

Returns:

List of Documents selected by maximal marginal relevance.

databricks_langchain.GenieAgent(genie_space_id, genie_agent_name: str = 'Genie', description: str = '')๏ƒ

Create a genie agent that can be used to query the API

class databricks_langchain.VectorSearchRetrieverTool๏ƒ

Bases: BaseTool, VectorSearchRetrieverToolMixin

A utility class to create a vector search-based retrieval tool for querying indexed embeddings. This class integrates with Databricks Vector Search and provides a convenient interface for building a retriever tool for agents.

param args_schema: Type[BaseModel] = <class 'databricks_ai_bridge.vector_search_retriever_tool.VectorSearchRetrieverToolInput'>๏ƒ

Pydantic model class to validate and parse the toolโ€™s input arguments.

Args schema should be either:

  • A subclass of pydantic.BaseModel.

or - A subclass of pydantic.v1.BaseModel if accessing v1 namespace in pydantic 2

param description: str = ''๏ƒ

Used to tell the model how/when/why to use the tool.

You can provide few-shot examples as a part of the description.

The description of the tool

param embedding: Embeddings | None = None๏ƒ

Embedding model for self-managed embeddings.

param name: str = ''๏ƒ

The unique name of the tool that clearly communicates its purpose.

The name of the tool

param text_column: str | None = None๏ƒ

The name of the text column to use for the embeddings. Required for direct-access index or delta-sync index with self-managed embeddings.