-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,083 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
sidebar_position: 10 | ||
--- | ||
|
||
# dspy.CloudflareAI | ||
|
||
### Usage | ||
|
||
```python | ||
lm = dspy.CloudflareAI(model="@hf/meta-llama/meta-llama-3-8b-instruct") | ||
``` | ||
|
||
### Constructor | ||
|
||
The constructor initializes the base class `LM` and verifies the `api_key` and `account_id` for using Cloudflare AI API. | ||
The following environment variables are expected to be set or passed as arguments: | ||
|
||
- `CLOUDFLARE_ACCOUNT_ID`: Account ID for Cloudflare. | ||
- `CLOUDFLARE_API_KEY`: API key for Cloudflare. | ||
|
||
```python | ||
class CloudflareAI(LM): | ||
def __init__( | ||
self, | ||
model: str = "@hf/meta-llama/meta-llama-3-8b-instruct", | ||
account_id: Optional[str] = None, | ||
api_key: Optional[str] = None, | ||
system_prompt: Optional[str] = None, | ||
**kwargs, | ||
): | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `model` (_str_): Model hosted on Cloudflare. Defaults to `@hf/meta-llama/meta-llama-3-8b-instruct`. | ||
- `account_id` (_Optional[str]_, _optional_): Account ID for Cloudflare. Defaults to None. Reads from environment variable `CLOUDFLARE_ACCOUNT_ID`. | ||
- `api_key` (_Optional[str]_, _optional_): API key for Cloudflare. Defaults to None. Reads from environment variable `CLOUDFLARE_API_KEY`. | ||
- `system_prompt` (_Optional[str]_, _optional_): System prompt to use for generation. | ||
|
||
### Methods | ||
|
||
Refer to [`dspy.OpenAI`](https://dspy-docs.vercel.app/api/language_model_clients/OpenAI) documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
sidebar_position: | ||
--- | ||
|
||
# dspy.Snowflake | ||
|
||
### Usage | ||
|
||
```python | ||
import dspy | ||
import os | ||
|
||
connection_parameters = { | ||
|
||
"account": os.getenv('SNOWFLAKE_ACCOUNT'), | ||
"user": os.getenv('SNOWFLAKE_USER'), | ||
"password": os.getenv('SNOWFLAKE_PASSWORD'), | ||
"role": os.getenv('SNOWFLAKE_ROLE'), | ||
"warehouse": os.getenv('SNOWFLAKE_WAREHOUSE'), | ||
"database": os.getenv('SNOWFLAKE_DATABASE'), | ||
"schema": os.getenv('SNOWFLAKE_SCHEMA')} | ||
|
||
lm = dspy.Snowflake(model="mixtral-8x7b",credentials=connection_parameters) | ||
``` | ||
|
||
### Constructor | ||
|
||
The constructor inherits from the base class `LM` and verifies the `credentials` for using Snowflake API. | ||
|
||
```python | ||
class Snowflake(LM): | ||
def __init__( | ||
self, | ||
model, | ||
credentials, | ||
**kwargs): | ||
``` | ||
|
||
**Parameters:** | ||
- `model` (_str_): model hosted by [Snowflake Cortex](https://docs.snowflake.com/en/user-guide/snowflake-cortex/llm-functions#availability). | ||
- `credentials` (_dict_): connection parameters required to initialize a [snowflake snowpark session](https://docs.snowflake.com/en/developer-guide/snowpark/reference/python/latest/api/snowflake.snowpark.Session) | ||
|
||
### Methods | ||
|
||
Refer to [`dspy.Snowflake`](https://dspy-docs.vercel.app/api/language_model_clients/Snowflake) documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
sidebar_position: | ||
--- | ||
|
||
# retrieve.SnowflakeRM | ||
|
||
### Constructor | ||
|
||
Initialize an instance of the `SnowflakeRM` class, with the option to use `e5-base-v2` or `snowflake-arctic-embed-m` embeddings or any other Snowflake Cortex supported embeddings model. | ||
|
||
```python | ||
SnowflakeRM( | ||
snowflake_table_name: str, | ||
snowflake_credentials: dict, | ||
k: int = 3, | ||
embeddings_field: str, | ||
embeddings_text_field:str, | ||
embeddings_model: str = "e5-base-v2", | ||
) | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `snowflake_table_name (str)`: The name of the Snowflake table containing embeddings. | ||
- `snowflake_credentials (dict)`: The connection parameters needed to initialize a Snowflake Snowpark Session. | ||
- `k (int, optional)`: The number of top passages to retrieve. Defaults to 3. | ||
- `embeddings_field (str)`: The name of the column in the Snowflake table containing the embeddings. | ||
- `embeddings_text_field (str)`: The name of the column in the Snowflake table containing the passages. | ||
- `embeddings_model (str)`: The model to be used to convert text to embeddings | ||
|
||
### Methods | ||
|
||
#### `forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = None) -> dspy.Prediction` | ||
|
||
Search the Snowflake table for the top `k` passages matching the given query or queries, using embeddings generated via the default `e5-base-v2` model or the specified `embedding_model`. | ||
|
||
**Parameters:** | ||
|
||
- `query_or_queries` (_Union[str, List[str]]_): The query or list of queries to search for. | ||
- `k` (_Optional[int]_, _optional_): The number of results to retrieve. If not specified, defaults to the value set during initialization. | ||
|
||
**Returns:** | ||
|
||
- `dspy.Prediction`: Contains the retrieved passages, each represented as a `dotdict` with schema `[{"id": str, "score": float, "long_text": str, "metadatas": dict }]` | ||
|
||
### Quickstart | ||
|
||
To support passage retrieval, it assumes that a Snowflake table has been created and populated with the passages in a column `embeddings_text_field` and the embeddings in another column `embeddings_field` | ||
|
||
SnowflakeRM uses `e5-base-v2` embeddings model by default or any Snowflake Cortex supported embeddings model. | ||
|
||
#### Default OpenAI Embeddings | ||
|
||
```python | ||
from dspy.retrieve.snowflake_rm import SnowflakeRM | ||
import os | ||
|
||
connection_parameters = { | ||
|
||
"account": os.getenv('SNOWFLAKE_ACCOUNT'), | ||
"user": os.getenv('SNOWFLAKE_USER'), | ||
"password": os.getenv('SNOWFLAKE_PASSWORD'), | ||
"role": os.getenv('SNOWFLAKE_ROLE'), | ||
"warehouse": os.getenv('SNOWFLAKE_WAREHOUSE'), | ||
"database": os.getenv('SNOWFLAKE_DATABASE'), | ||
"schema": os.getenv('SNOWFLAKE_SCHEMA')} | ||
|
||
retriever_model = SnowflakeRM( | ||
snowflake_table_name="<YOUR_SNOWFLAKE_TABLE_NAME>", | ||
snowflake_credentials=connection_parameters, | ||
embeddings_field="<YOUR_EMBEDDINGS_COLUMN_NAME>", | ||
embeddings_text_field= "<YOUR_PASSAGE_COLUMN_NAME>" | ||
) | ||
|
||
results = retriever_model("Explore the meaning of life", k=5) | ||
|
||
for result in results: | ||
print("Document:", result.long_text, "\n") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.