Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

refactor(dspy): updating snowflake LM and RM implementations

alejandrob155ce7432d9
diff --git a/docs/api/retrieval_model_clients/SnowflakeRM.md b/docs/api/retrieval_model_clients/SnowflakeRM.md
index e1f27086e..ac502d728 100644
--- a/docs/api/retrieval_model_clients/SnowflakeRM.md
+++ b/docs/api/retrieval_model_clients/SnowflakeRM.md
@@ -14,6 +14,8 @@ SnowflakeRM(
cortex_search_service: str,
snowflake_database: str,
snowflake_schema: dict,
+ retrieval_columns: list,
+ search_filter: dict = None,
k: int = 3,
)
```
@@ -24,6 +26,8 @@ SnowflakeRM(
- `cortex_search_service (str)`: The name of the Cortex Search service to be used.
- `snowflake_database (str)`: The name of the Snowflake database to be used with the Cortex Search service.
- `snowflake_schema (str)`: The name of the Snowflake schema to be used with the Cortex Search service.
+- `retrieval_columns (str)`: A list of columns to return for each relevant result in the response.
+- `search_filter (dict, optional)`: Optional filter object used for filtering results based on data in the ATTRIBUTES columns. See [Filter syntax](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-search/query-cortex-search-service#filter-syntax)
- `k (int, optional)`: The number of top passages to retrieve. Defaults to 3.
### Methods
@@ -35,8 +39,6 @@ Query the Cortex Search service to retrieve the top k relevant results given a q
**Parameters:**
- `query_or_queries` (_Union[str, List[str]]_): The query or list of queries to search for.
-- `response_columns` (_List[str]_): A list of columns to return for each relevant result in the response.
-- `filters` (_Optional[dict]_): An optional filter object for filtering results based on data in the ATTRIBUTES columns. See [Filter syntax](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-search/query-cortex-search-service#filter-syntax)
- `k` (_Optional[int]_): The number of results to retrieve. If not specified, defaults to the value set during initialization.
**Returns:**
diff --git a/dspy/retrieve/snowflake_rm.py b/dspy/retrieve/snowflake_rm.py
index 8ed2ef062..adcfea7e4 100644
--- a/dspy/retrieve/snowflake_rm.py
+++ b/dspy/retrieve/snowflake_rm.py
@@ -24,6 +24,8 @@ class SnowflakeRM(dspy.Retrieve):
cortex_search_service(str): Name of the Cortex Search service to be used.
snowflake_database (str): The name of the Snowflake table containing document embeddings.
snowflake_schema (str): The name of the Snowflake table containing document embeddings.
+ search_columns (list): A comma-separated list of columns to return for each relevant result in the response. These columns must be included in the source query for the service.
+ search_filter (dict): A filter object for filtering results based on data in the ATTRIBUTES columns. See Filter syntax.
k (int, optional): The default number of top passages to retrieve. Defaults to 3.
"""
@@ -33,45 +35,45 @@ class SnowflakeRM(dspy.Retrieve):
cortex_search_service: str,
snowflake_database: str,
snowflake_schema: str,
+ retrieval_columns: list,
+ search_filter=None,
k: int = 3,
):
super().__init__(k=k)
self.k = k
self.cortex_search_service_name = cortex_search_service
+ self.retrieval_columns = retrieval_columns
+ self.search_filter = search_filter
self.client = self._fetch_cortex_service(
snowflake_session, snowflake_database, snowflake_schema, cortex_search_service
)
- def forward(
- self,
- query_or_queries: Union[str, list[str]],
- response_columns: list[str],
- filters: dict = None,
- k: Optional[int] = None,
- ) -> dspy.Prediction:
+ def forward(self, query_or_queries: Union[str, list[str]], k: Optional[int] = None) -> dspy.Prediction:
"""Query Cortex Search endpoint for top relevant passages.
Args:
query_or_queries (Union[str, List[str]]): The query or queries to search for.
- repsonse_columns (List[str]): A comma-separated list of columns to return for each relevant result in the response. These columns must be included in the source query for the service.
- filters (dict): A filter object for filtering results based on data in the ATTRIBUTES columns. See Filter syntax.
k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.
Returns:
dspy.Prediction: An object containing the retrieved passages.
"""
- k = k if k is not None else self.k
+ k = self.k if k is None else k
queries = [query_or_queries] if isinstance(query_or_queries, str) else query_or_queries
queries = [q for q in queries if q]
passages = []
for cortex_query in queries:
response_chunks = self._query_cortex_search(
- cortex_search_service=self.client, query=cortex_query, columns=response_columns, filter=filters, k=k
+ cortex_search_service=self.client,
+ query=cortex_query,
+ columns=self.retrieval_columns,
+ filter=self.search_filter,
+ k=k,
)
- if len(response_columns) == 1:
+ if len(self.retrieval_columns) == 1:
passages.extend(
- dotdict({"long_text": passage[response_columns[0]]}) for passage in response_chunks["results"]
+ dotdict({"long_text": passage[self.retrieval_columns[0]]}) for passage in response_chunks["results"]
)
else:
passages.extend(dotdict({"long_text": str(passage)}) for passage in response_chunks["results"])