Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix

dbczumar328d7fa125c8
diff --git a/dspy/retrieve/databricks_rm.py b/dspy/retrieve/databricks_rm.py
index 15d20711e..df597a2a3 100644
--- a/dspy/retrieve/databricks_rm.py
+++ b/dspy/retrieve/databricks_rm.py
@@ -1,25 +1,20 @@
import json
import os
-from typing import List, Union, Any, Dict
+from importlib.util import find_spec
+from typing import Any, Dict, List, Optional, Union
+
import requests
+
import dspy
from dspy.primitives.prediction import Prediction
+_databricks_sdk_installed = find_spec("databricks.sdk") is not None
+
class DatabricksRM(dspy.Retrieve):
"""
A retrieval module that uses Databricks Vector Search Endpoint to return the top-k embeddings for a given query.
- Args:
- databricks_index_name (str): Databricks vector search index to query
- databricks_endpoint (str): Databricks index endpoint url
- databricks_token (str): Databricks authentication token
- columns (list[str]): Column names to include in response
- filters_json (str, optional): JSON string for query filters
- k (int, optional): Number of top embeddings to retrieve. Defaults to 3.
- docs_id_column_name (str, optional): Column name for retrieved doc_ids to return.
- text_column_name (str, optional): Column name for retrieved text to return.
-
Examples:
Below is a code snippet that shows how to configure Databricks Vector Search endpoints:
@@ -62,46 +57,56 @@ class DatabricksRM(dspy.Retrieve):
Below is a code snippet that shows how to query the Databricks Direct Vector Access Index using the forward() function.
```python
- self.retrieve = DatabricksRM(query=[1, 2, 3], query_type = 'vector')
+ self.retrieve = DatabricksRM(query=[1, 2, 3])
```
"""
def __init__(
self,
- databricks_index_name=None,
- databricks_endpoint=None,
- databricks_token=None,
- columns=None,
- filters_json=None,
- k=3,
- docs_id_column_name="id",
- text_column_name="text",
+ databricks_index_name: str,
+ databricks_endpoint: Optional[str] = None,
+ databricks_token: Optional[str] = None,
+ columns: List[str] = None,
+ filters_json: str = None,
+ k: int = 3,
+ docs_id_column_name: str = "id",
+ text_column_name: str = "text",
):
+ """
+ Args:
+ databricks_index_name (str): The name of the Databricks Vector Search Index to query.
+ databricks_endpoint (str, optional): The Databricks workspace URL containing the
+ Vector Search Index. Defaults to the value of the ``DATABRICKS_HOST`` environment
+ variable. If unspecified, the Databricks SDK is used to identify the endpoint
+ based on the current environment.
+ databricks_token (str, optional): The Databricks workspace authentication token to use
+ when querying the Vector Search Index. Defaults to the value of the
+ ``DATABRICKS_TOKEN`` environment variable. If unspecified, the Databricks SDK is
+ used to identify the token based on the current environment.
+ columns (list[str], optional): Extra column names to include in response,
+ in addition to the document id and text columns specified by
+ ``docs_id_column_name`` and ``text_column_name``.
+ filters_json (str, optional): A JSON string specifying additional query filters.
+ k (int): The number of documents to retrieve.
+ docs_id_column_name (str): The name of the column in the Databricks Vector Search Index
+ containing document IDs.
+ text_column_name (str): The name of the column in the Databricks Vector Search Index
+ containing document text to retrieve.
+ """
super().__init__(k=k)
- if not databricks_token and not os.environ.get("DATABRICKS_TOKEN"):
- raise ValueError(
- "You must supply databricks_token or set environment variable DATABRICKS_TOKEN"
- )
- if not databricks_endpoint and not os.environ.get("DATABRICKS_HOST"):
- raise ValueError(
- "You must supply databricks_endpoint or set environment variable DATABRICKS_HOST"
- )
- if not databricks_index_name:
- raise ValueError("You must supply vector index name")
- if not columns:
- raise ValueError(
- "You must specify a list of column names to be included in the response"
- )
- self.databricks_token = (
- databricks_token if databricks_token else os.environ["DATABRICKS_TOKEN"]
- )
+ self.databricks_token = databricks_token if databricks_token is not None else os.environ.get("DATABRICKS_TOKEN")
self.databricks_endpoint = (
- databricks_endpoint
- if databricks_endpoint
- else os.environ["DATABRICKS_HOST"]
+ databricks_endpoint if databricks_endpoint is not None else os.environ.get("DATABRICKS_HOST")
)
+ if not _databricks_sdk_installed and (self.databricks_token, self.databricks_endpoint).count(None) > 0:
+ raise ValueError(
+ "To retrieve documents with Databricks Vector Search, you must install the"
+ " databricks-sdk Python library, supply the databricks_token and"
+ " databricks_endpoint parameters, or set the DATABRICKS_TOKEN and DATABRICKS_HOST"
+ " environment variables."
+ )
self.databricks_index_name = databricks_index_name
- self.columns = columns
+ self.columns = list({docs_id_column_name, text_column_name, *(columns or [])})
self.filters_json = filters_json
self.k = k
self.docs_id_column_name = docs_id_column_name
@@ -128,71 +133,64 @@ class DatabricksRM(dspy.Retrieve):
Returns:
Dict[str, Any]: Search result column values, excluding the "text" and not "id" columns.
"""
- extra_columns = {
- k: v
- for k, v in item.items()
- if k not in [self.docs_id_column_name, self.text_column_name]
- }
+ extra_columns = {k: v for k, v in item.items() if k not in [self.docs_id_column_name, self.text_column_name]}
if self.docs_id_column_name == "metadata":
extra_columns = {
**extra_columns,
- **{
- "metadata": {
- k: v
- for k, v in json.loads(item["metadata"]).items()
- if k != "document_id"
- }
- },
+ **{"metadata": {k: v for k, v in json.loads(item["metadata"]).items() if k != "document_id"}},
}
return extra_columns
def forward(
self,
query: Union[str, List[float]],
- query_type: str = "text",
+ query_type: str = "ANN",
filters_json: str = None,
) -> dspy.Prediction:
"""Search with Databricks Vector Search Client for self.k top results for query
Args:
- query (Union[str, List[float]]): query to search for.
- query_type (str): 'vector' for Direct Vector Access Index and Delta Sync Index using self-managed vectors or 'text' for Delta Sync Index using model endpoint.
+ query (Union[str, List[float]]): Query text or numeric query vector to for which to
+ find relevant documents.
+ query_type (str): The type of search query to perform against the Databricks Vector
+ Search Index. Must be either 'ANN' (approximate nearest neighbor) or 'HYBRID'
+ (hybrid search).
Returns:
dspy.Prediction: An object containing the retrieved results.
"""
- headers = {
- "Authorization": f"Bearer {self.databricks_token}",
- "Content-Type": "application/json",
- }
- payload = {
- "columns": self.columns,
- "num_results": self.k,
- }
- if query_type == "vector":
- if not isinstance(query, list):
- raise ValueError("Query must be a list of floats for query_vector")
- payload["query_vector"] = query
- elif query_type == "text":
- if not isinstance(query, str):
- raise ValueError("Query must be a string for query_text")
- payload["query_text"] = query
+ if isinstance(query, str):
+ query_text = query
+ query_vector = None
+ elif isinstance(query, list):
+ query_vector = query
+ query_text = None
else:
- raise ValueError("Invalid query type specified. Use 'vector' or 'text'.")
-
- payload["filters_json"] = filters_json if filters_json else self.filters_json
+ raise ValueError("Query must be a string or a list of floats.")
- response = requests.post(
- f"{self.databricks_endpoint}/api/2.0/vector-search/indexes/{self.databricks_index_name}/query",
- json=payload,
- headers=headers,
- )
- results = response.json()
-
- # Check for errors from REST API call
- if response.json().get("error_code", None) != None:
- raise Exception(
- f"ERROR: {response.json()['error_code']} -- {response.json()['message']}"
+ if _databricks_sdk_installed:
+ results = self._query_via_databricks_sdk(
+ index_name=self.databricks_index_name,
+ k=self.k,
+ columns=self.columns,
+ query_type=query_type,
+ query_text=query_text,
+ query_vector=query_vector,
+ databricks_token=self.databricks_token,
+ databricks_endpoint=self.databricks_endpoint,
+ filters_json=filters_json,
+ )
+ else:
+ results = self._query_via_requests(
+ index_name=self.databricks_index_name,
+ k=self.k,
+ columns=self.columns,
+ databricks_token=self.databricks_token,
+ databricks_endpoint=self.databricks_endpoint,
+ query_type=query_type,
+ query_text=query_text,
+ query_vector=query_vector,
+ filters_json=filters_json,
)
# Checking if defined columns are present in the index columns
@@ -204,9 +202,7 @@ class DatabricksRM(dspy.Retrieve):
)
if self.text_column_name not in col_names:
- raise Exception(
- f"text_column_name: '{self.text_column_name}' is not in the index columns: \n {col_names}"
- )
+ raise Exception(f"text_column_name: '{self.text_column_name}' is not in the index columns: \n {col_names}")
# Extracting the results
items = []
@@ -217,14 +213,117 @@ class DatabricksRM(dspy.Retrieve):
items += [item]
# Sorting results by score in descending order
- sorted_docs = sorted(items, key=lambda x: x["score"], reverse=True)[:self.k]
+ sorted_docs = sorted(items, key=lambda x: x["score"], reverse=True)[: self.k]
# Returning the prediction
return Prediction(
docs=[doc[self.text_column_name] for doc in sorted_docs],
- doc_ids=[
- self._extract_doc_ids(doc)
- for doc in sorted_docs
- ],
+ doc_ids=[self._extract_doc_ids(doc) for doc in sorted_docs],
extra_columns=[self._get_extra_columns(item) for item in sorted_docs],
)
+
+ @staticmethod
+ def _query_via_requests(
+ index_name: str,
+ k: int,
+ columns: List[str],
+ databricks_token: str,
+ databricks_endpoint: str,
+ query_type: str,
+ query_text: Optional[str],
+ query_vector: Optional[List[float]],
+ filters_json: Optional[str],
+ ) -> List[str]:
+ """
+ Query a Databricks Vector Search Index via the Python requests library.
+
+ Args:
+ index_name (str): Name of the Databricks vector search index to query
+ k (int): Number of relevant documents to retrieve.
+ columns (List[str]): Column names to include in response.
+ databricks_token (str): Databricks authentication token.
+ databricks_endpoint (str): Databricks index endpoint url.
+ query_text (str, optional): Text query for which to find relevant documents. Exactly
+ one of query_text or query_vector must be specified.
+ query_vector (List[float], optional): Numeric query vector for which to find relevant
+ documents. Exactly one of query_text or query_vector must be specified.
+ filters_json (str, optional): JSON string representing additional query filters.
+
+ Returns:
+ List[str]: List of top-k retrieved documents. TODO - UPDATE THIS
+ """
+ if (query_text, query_vector).count(None) != 1:
+ raise ValueError("Exactly one of query_text or query_vector must be specified.")
+
+ headers = {
+ "Authorization": f"Bearer {databricks_token}",
+ "Content-Type": "application/json",
+ }
+ payload = {
+ "columns": columns,
+ "num_results": k,
+ "query_type": query_type,
+ }
+ if filters_json is not None:
+ payload["filters_json"] = filters_json
+ if query_text is not None:
+ payload["query_text"] = query_text
+ elif query_vector is not None:
+ payload["query_vector"] = query_vector
+ response = requests.post(
+ f"{databricks_endpoint}/api/2.0/vector-search/indexes/{index_name}/query",
+ json=payload,
+ headers=headers,
+ )
+ results = response.json()
+ if "error_code" in results:
+ raise Exception(f"ERROR: {results['error_code']} -- {results['message']}")
+ return results
+
+ @staticmethod
+ def _query_via_databricks_sdk(
+ index_name: str,
+ k: int,
+ columns: List[str],
+ query_type: str,
+ query_text: Optional[str],
+ query_vector: Optional[List[float]],
+ databricks_token: Optional[str],
+ databricks_endpoint: Optional[str],
+ filters_json: Optional[str],
+ ) -> List[str]:
+ """
+ Query a Databricks Vector Search Index via the Databricks SDK.
+ Assumes that the databricks-sdk Python library is installed.
+
+ Args:
+ index_name (str): Name of the Databricks vector search index to query
+ k (int): Number of relevant documents to retrieve.
+ columns (List[str]): Column names to include in response.
+ query_text (str, optional): Text query for which to find relevant documents. Exactly
+ one of query_text or query_vector must be specified.
+ query_vector (List[float], optional): Numeric query vector for which to find relevant
+ documents. Exactly one of query_text or query_vector must be specified.
+ filters_json (str, optional): JSON string representing additional query filters.
+ databricks_token (str): Databricks authentication token. If not specified,
+ the token is resolved from the current environment.
+ databricks_endpoint (str): Databricks index endpoint url. If not specified,
+ the endpoint is resolved from the current environment.
+ Returns:
+ List[str]: List of top-k retrieved documents. TODO - UPDATE THIS
+ """
+ from databricks.sdk import WorkspaceClient
+
+ if (query_text, query_vector).count(None) != 1:
+ raise ValueError("Exactly one of query_text or query_vector must be specified.")
+
+ databricks_client = WorkspaceClient(host=databricks_endpoint, token=databricks_token)
+ return databricks_client.vector_search_indexes.query_index(
+ index_name=index_name,
+ query_type=query_type,
+ query_text=query_text,
+ query_vector=query_vector,
+ columns=columns,
+ filters_json=filters_json,
+ num_results=k,
+ ).as_dict()