Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge from main #1

Merged
merged 81 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
d7b41cd
feat(dspy): testing initial commit
sfc-gh-alherrera Apr 17, 2024
dfae177
feat(dspy): initial cleanup of Snowflake (LM)support
sfc-gh-alherrera Apr 17, 2024
e492cfa
feat(dspy): added SnowflakeRM retriever and cleanup of Snowflake (LM)…
sfc-gh-alherrera Apr 17, 2024
f2ce44f
feat(dspy): added SnowflakeRM retriever and cleanup of Snowflake (LM)…
sfc-gh-alherrera Apr 17, 2024
6af2d47
feat(dspy): added SnowflakeRM retriever and cleanup of Snowflake (LM)…
sfc-gh-alherrera Apr 17, 2024
f42df77
feat(dspy): added SnowflakeRM retriever and cleanup of Snowflake (LM)…
sfc-gh-alherrera Apr 17, 2024
8ce1378
feat(dspy): added Snowflake Cortex (LM) and Snowflake RM support
sfc-gh-alherrera Apr 17, 2024
bbb6204
feat(dspy): added Snowflake Cortex (LM) and Snowflake RM support
sfc-gh-alherrera Apr 17, 2024
0ad59dc
Merge pull request #1 from tom-doerr/patch-4
tom-doerr Apr 18, 2024
9c286f3
remove n=1 in fallback generate logic when not all fields have been g…
Apr 28, 2024
a198363
extend the generation for all candidate generations until all have re…
Apr 28, 2024
70e630a
remove break clauses
Apr 28, 2024
5b16f10
add types
Apr 28, 2024
991d713
pick up completion from first missing field
Apr 28, 2024
67bb1a0
pick up completion from first missing field - and pop empty fields
Apr 28, 2024
cffbfff
pick up completion from first missing field - and pop empty fields
Apr 28, 2024
db4fcf3
Update FaissRM.md
arnavsinghvi11 Apr 16, 2024
5a54d59
Clean up knn related functionalities a bit
XiaoConstantine Apr 29, 2024
d75904e
Expose vectorizer on knn fewshot level
XiaoConstantine Apr 29, 2024
e85bfa2
remove extra blank line
XiaoConstantine Apr 29, 2024
f3e77d0
Added more details to the discussion of optimizers/teleprompters.
rpgoldman May 1, 2024
fc15eb2
Comments, type hints, etc. added to optimizers.
rpgoldman May 1, 2024
06bf5a7
Update 6-optimizers.md with corrections
arnavsinghvi11 May 5, 2024
34e0058
Update bootstrap.py and remove comments
arnavsinghvi11 May 5, 2024
c99df69
Update copro_optimizer.py docstring
arnavsinghvi11 May 5, 2024
c435a3f
added logic to parse instructions in ReAct signature
xyloid May 5, 2024
762751f
Remove unused PDF file.
rpgoldman May 5, 2024
e0b8a69
Remove unnecessary return value.
rpgoldman May 5, 2024
3dcddbe
feat: adds Cloudflare Workers AI
isbecker Apr 27, 2024
f21214c
Update knn notebook
XiaoConstantine May 6, 2024
b2a9258
linting
May 6, 2024
6ec0a49
Merge branch 'main' into main
isbecker May 6, 2024
657c086
Merge remote-tracking branch 'upstream/main'
tom-doerr May 6, 2024
e00d4e6
Remove graphviz file of class hierarchy.
rpgoldman May 6, 2024
33ea790
ruff fixes.
rpgoldman May 6, 2024
dc0d78b
Describe how to generate class diagram.
rpgoldman May 6, 2024
5e4df24
Update description of KNN and Instruction Optimizers.
rpgoldman May 6, 2024
285b0dc
Proposed Optuna rewrite.
rpgoldman May 6, 2024
bf9247f
fix(dspy): removing unnecessary ipykernel dependency
sfc-gh-alherrera May 6, 2024
4ed0753
fix(dspy): updating import error handling language for SnowflakeRM
sfc-gh-alherrera May 6, 2024
315d71a
fix(dspy): updating README to include Snowflake as optional supported…
sfc-gh-alherrera May 6, 2024
cfa67d6
fix(dspy): updating SnowflakeRM typos
sfc-gh-alherrera May 6, 2024
75cd65b
fix(dspy): updating sort order to be descending for retriever results
sfc-gh-alherrera May 6, 2024
c663444
docs(dspy): Adding documentation for Snowflake LM
sfc-gh-alherrera May 6, 2024
2cb77df
docs(dspy): Adding documentation for Snowflake RM
sfc-gh-alherrera May 6, 2024
daef34e
docs(dspy): Adding documentation for Snowflake RM
sfc-gh-alherrera May 6, 2024
4f10658
fix(dspy): Adding self.history definition to Snowflake LM
sfc-gh-alherrera May 6, 2024
e6afb61
Merge branch 'main' into dspy-snowflake
sfc-gh-alherrera May 6, 2024
f5e39ee
fix(dspy): Remove common indentations in signature according to Pytho…
May 7, 2024
0c14d38
test(evaluate): Update unit tests
christeefy May 7, 2024
d1075af
fix(evaluate): Display table
christeefy May 7, 2024
7acf4c8
added a test case to test instructions parsing in ReAct class
xyloid May 8, 2024
0db53e5
Update anthropic.py
4daJKong May 8, 2024
bb074a7
fix: assertion and chain-of-thought bugs
Shangyint May 8, 2024
be493e9
update test_retry
Shangyint May 8, 2024
fad2f11
fix(dspy): Adding missing LLMs supported in Snowflake and tag for ses…
sfc-gh-alherrera May 9, 2024
441fd43
fix(dspy): Adding missing LLMs supported in Snowflake and tag for ses…
sfc-gh-alherrera May 9, 2024
8775674
fix(dspy): adding language for recently released snowflake retriever …
sfc-gh-alherrera May 9, 2024
231b120
Add support for logprob output
tom-doerr May 9, 2024
4b48281
Remove unnecessary comment in GPT3 class.
tom-doerr May 9, 2024
339cb60
Merge pull request #995 from stanfordnlp/fix-assertions
Shangyint May 9, 2024
a048a51
fix(dspy): updating syntax for embeddings method call which will be d…
sfc-gh-alherrera May 9, 2024
b3af69c
fix(dspy): updating syntax for Snowflake cos similarity method which …
sfc-gh-alherrera May 9, 2024
53630da
fix(dspy): updating syntax for Snowflake cos similarity method which …
sfc-gh-alherrera May 9, 2024
b0b18a7
fix(dspy): updating syntax for Snowflake cos similarity method which …
sfc-gh-alherrera May 10, 2024
8dedd00
fix(dspy): solving for null response bug in Cortex API
sfc-gh-alherrera May 10, 2024
8371650
Update Groq.md
Frostbite22 May 10, 2024
54d9d75
Merge pull request #1010 from Frostbite22/patch-1
arnavsinghvi11 May 10, 2024
e7269e3
Merge pull request #944 from isbecker/main
arnavsinghvi11 May 11, 2024
6899b5f
Merge pull request #920 from mikeedjones/extend-generation-for-all-ca…
arnavsinghvi11 May 11, 2024
c37e534
Merge pull request #680 from aazizisoufiane/fix/bedrock_n
arnavsinghvi11 May 11, 2024
1d570ee
Merge pull request #930 from XiaoConstantine/xiao/clean-up
arnavsinghvi11 May 11, 2024
4b9bbcd
Merge pull request #987 from wullli/main
arnavsinghvi11 May 11, 2024
012b07a
Merge pull request #993 from 4daJKong/patch-1
arnavsinghvi11 May 11, 2024
32449e1
Merge pull request #990 from christeefy/bug/evaluate-display-table-arg
arnavsinghvi11 May 11, 2024
5bc17d8
Merge pull request #999 from tom-doerr/add_logprob_support
arnavsinghvi11 May 11, 2024
23cd780
fix(dspy): adding LM connection parameter update to docs, resolving r…
sfc-gh-alherrera May 11, 2024
822f932
Merge pull request #859 from sfc-gh-alherrera/dspy-snowflake
arnavsinghvi11 May 11, 2024
48c7e90
Add braggy comment.
rpgoldman May 11, 2024
bcf47c8
Merge pull request #951 from rpgoldman/teleprompter-details
arnavsinghvi11 May 11, 2024
61fd5cc
Merge pull request #992 from xyloid/fix/react_instr_from_sig
arnavsinghvi11 May 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ Or open our intro notebook in Google Colab: [<img align="center" src="https://co

By default, DSPy installs the latest `openai` from pip. However, if you install old version before OpenAI changed their API `openai~=0.28.1`, the library will use that just fine. Both are supported.

For the optional (alphabetically sorted) [Chromadb](https://github.com/chroma-core/chroma), [Qdrant](https://github.com/qdrant/qdrant), [Marqo](https://github.com/marqo-ai/marqo), Pinecone, [Weaviate](https://github.com/weaviate/weaviate),
For the optional (alphabetically sorted) [Chromadb](https://github.com/chroma-core/chroma), [Qdrant](https://github.com/qdrant/qdrant), [Marqo](https://github.com/marqo-ai/marqo), Pinecone, [Snowflake](https://github.com/snowflakedb/snowpark-python) [Weaviate](https://github.com/weaviate/weaviate),
or [Milvus](https://github.com/milvus-io/milvus) retrieval integration(s), include the extra(s) below:

```
pip install dspy-ai[chromadb] # or [qdrant] or [marqo] or [mongodb] or [pinecone] or [weaviate] or [milvus]
pip install dspy-ai[chromadb] # or [qdrant] or [marqo] or [mongodb] or [pinecone] or [snowflake] or [weaviate] or [milvus]
```

## 2) Documentation
Expand Down
42 changes: 42 additions & 0 deletions docs/api/language_model_clients/Cloudflare.md
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.
6 changes: 3 additions & 3 deletions docs/api/language_model_clients/Groq.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lm = dspy.GROQ(model='mixtral-8x7b-32768', api_key ="gsk_***" )

### Constructor

The constructor initializes the base class `LM` and verifies the provided arguments like the `api_key` for GROQ api retriver. The `kwargs` attribute is initialized with default values for relevant text generation parameters needed for communicating with the GPT API, such as `temperature`, `max_tokens`, `top_p`, `frequency_penalty`, `presence_penalty`, and `n`.
The constructor initializes the base class `LM` and verifies the provided arguments like the `api_key` for GROQ api retriver. The `kwargs` attribute is initialized with default values for relevant text generation parameters needed for communicating with the GROQ API, such as `temperature`, `max_tokens`, `top_p`, `frequency_penalty`, `presence_penalty`, and `n`.

```python
class GroqLM(LM):
Expand Down Expand Up @@ -42,10 +42,10 @@ Internally, the method handles the specifics of preparing the request prompt and
After generation, the generated content look like `choice["message"]["content"]`.

**Parameters:**
- `prompt` (_str_): Prompt to send to OpenAI.
- `prompt` (_str_): Prompt to send to GROQ.
- `only_completed` (_bool_, _optional_): Flag to return only completed responses and ignore completion due to length. Defaults to True.
- `return_sorted` (_bool_, _optional_): Flag to sort the completion choices using the returned averaged log-probabilities. Defaults to False.
- `**kwargs`: Additional keyword arguments for completion request.

**Returns:**
- `List[Dict[str, Any]]`: List of completion choices.
- `List[Dict[str, Any]]`: List of completion choices.
45 changes: 45 additions & 0 deletions docs/api/language_model_clients/Snowflake.md
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.
79 changes: 79 additions & 0 deletions docs/api/retrieval_model_clients/SnowflakeRM.md
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")
```
36 changes: 25 additions & 11 deletions docs/docs/building-blocks/6-optimizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,54 @@ DSPy programs consist of multiple calls to LMs, stacked together as [DSPy module

Given a metric, DSPy can optimize all of these three with multi-stage optimization algorithms. These can combine gradient descent (for LM weights) and discrete LM-driven optimization, i.e. for crafting/updating instructions and for creating/validating demonstrations. DSPy Demonstrations are like few-shot examples, but they're far more powerful. They can be created from scratch, given your program, and their creation and selection can be optimized in many effective ways.

In many cases, we found that compiling leads to better prompts than humans write. Not because DSPy optimizers are more creative than humans, but simply because they can try more things, much more systematically, and tune the metrics directly.
In many cases, we found that compiling leads to better prompts than human writing. Not because DSPy optimizers are more creative than humans, but simply because they can try more things, much more systematically, and tune the metrics directly.


## What DSPy Optimizers are currently available?

<!-- The following diagram was generated by: -->
<!-- 1. Running symilar on the teleprompter module to extract the python hierarchy as a Graphviz dot file -->
<!-- 2. Hand-editing the resulting dot file to remove classes that are not teleprompters/optimizers (e.g., classes for data structures manipulated by optimizers). -->
<!-- 3. Using dot to compile the `.dot` file into a PNG -->
<!-- Robert Goldman [2024/05/11:rpg] -->

[Subclasses of Teleprompter](figures/teleprompter-classes.png)

All of these can be accessed via `from dspy.teleprompt import *`.

#### Automatic Few-Shot Learning

1. **`LabeledFewShot`**: Simply constructs few-shot examples from provided labeled Q/A pairs.
These optimizers extend the signature by automatically generating and including **optimized** examples within the prompt sent to the model, implementing few-shot learning.

1. **`LabeledFewShot`**: Simply constructs few-shot examples (demos) from provided labeled input and output data points. Requires `k` (number of examples for the prompt) and `trainset` to randomly select `k` examples from.

2. **`BootstrapFewShot`**: Uses a `teacher` module (which defaults to your program) to generate complete demonstrations for every stage of your program, along with labeled examples in `trainset`. Parameters include `max_labeled_demos` (the number of demonstrations randomly selected from the `trainset`) and `max_bootstrapped_demos` (the number of additional examples generated by the `teacher`). The bootstrapping process employs the metric to validate demonstrations, including only those that pass the metric in the "compiled" prompt. Advanced: Supports using a `teacher` program that is a *different* DSPy program that has compatible structure, for harder tasks.

2. **`BootstrapFewShot`**: Uses your program to self-generate complete demonstrations for every stage of your program. Will simply use the generated demonstrations (if they pass the metric) without any further optimization. Advanced: Supports using a teacher program (a different DSPy program that has compatible structure) and a teacher LM, for harder tasks.
3. **`BootstrapFewShotWithRandomSearch`**: Applies `BootstrapFewShot` several times with random search over generated demonstrations, and selects the best program over the optimization. Parameters mirror those of `BootstrapFewShot`, with the addition of `num_candidate_programs`, which specifies the number of random programs evaluated over the optimization, including candidates of the uncompiled program, `LabeledFewShot` optimized program, `BootstrapFewShot` compiled program with unshuffled examples and `num_candidate_programs` of `BootstrapFewShot` compiled programs with randomized example sets.

3. **`BootstrapFewShotWithRandomSearch`**: Applies `BootstrapFewShot` several times with random search over generated demonstrations, and selects the best program.
4. **`BootstrapFewShotWithOptuna`**: Applies `BootstrapFewShot` with Optuna optimization across demonstration sets, running trials to maximize evaluation metrics and selecting the best demonstrations.

4. **`BootstrapFewShotWithOptuna`**: Applies `BootstrapFewShot` through Optuna hyperparameter optimization across demonstration sets, running trials to maximize evaluation metrics.
5. **`KNNFewShot`**. Selects demonstrations through k-Nearest Neighbors algorithm to pick a diverse set of examples from different clusters. Vectorizes the examples, and then clusters them, using cluster centers with `BootstrapFewShot` for bootstrapping/selection process. This will be useful when there's a lot of data over random spaces: using KNN helps optimize the `trainset` for `BootstrapFewShot`. See [this notebook](https://github.com/stanfordnlp/dspy/blob/main/examples/knn.ipynb) for an example.


#### Automatic Instruction Optimization

4. **`COPRO`**: Generates and refines new instructions for each step, and optimizes them with coordinate ascent.
These optimizers produce optimal instructions for the prompt and, in the case of MIPRO also optimize the set of few-shot demonstrations.

5. **`MIPRO`**: Generates instructions and few-shot examples in each step. The instruction generation is data-aware and demonstration-aware. Uses Bayesian Optimization to effectively search over the space of generation instructions/demonstrations across your modules.
6. **`COPRO`**: Generates and refines new instructions for each step, and optimizes them with coordinate ascent (hill-climbing using the metric function and the `trainset`). Parameters include `depth` which is the number of iterations of prompt improvement the optimizer runs over.

7. **`MIPRO`**: Generates instructions *and* few-shot examples in each step. The instruction generation is data-aware and demonstration-aware. Uses Bayesian Optimization to effectively search over the space of generation instructions/demonstrations across your modules.


#### Automatic Finetuning

This optimizer is used to fine-tune the underlying LLM(s).

6. **`BootstrapFinetune`**: Distills a prompt-based DSPy program into weight updates (for smaller LMs). The output is a DSPy program that has the same steps, but where each step is conducted by a finetuned model instead of a prompted LM.


#### Program Transformations

7. **`KNNFewShot`**. Selects demonstrations through k-Nearest Neighbors algorithm integrating `BootstrapFewShot` for bootstrapping/selection process.

8. **`Ensemble`**: Ensembles a set of DSPy programs and either uses the full set or randomly samples a subset into a single program.


Expand Down Expand Up @@ -90,7 +104,7 @@ from dspy.teleprompt import BootstrapFewShotWithRandomSearch

# Set up the optimizer: we want to "bootstrap" (i.e., self-generate) 8-shot examples of your program's steps.
# The optimizer will repeat this 10 times (plus some initial attempts) before selecting its best attempt on the devset.
config = dict(max_bootstrapped_demos=3, max_labeled_demos=3, num_candidate_programs=10, num_threads=4)
config = dict(max_bootstrapped_demos=4, max_labeled_demos=4, num_candidate_programs=10, num_threads=4)

teleprompter = BootstrapFewShotWithRandomSearch(metric=YOUR_METRIC_HERE, **config)
optimized_program = teleprompter.compile(YOUR_PROGRAM_HERE, trainset=YOUR_TRAINSET_HERE)
Expand All @@ -115,4 +129,4 @@ To load a program from a file, you can instantiate an object from that class and
```python
loaded_program = YOUR_PROGRAM_CLASS()
loaded_program.load(path=YOUR_SAVE_PATH)
```
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions dsp/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .azure_openai import AzureOpenAI
from .cache_utils import *
from .clarifai import *
from .cloudflare import *
from .cohere import *
from .colbertv2 import ColBERTv2
from .databricks import *
Expand All @@ -22,4 +23,6 @@
from .pyserini import *
from .sbert import *
from .sentence_vectorizer import *
from .snowflake import *
from .watsonx import *

2 changes: 1 addition & 1 deletion dsp/modules/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(
}
self.kwargs["model"] = model
self.history: list[dict[str, Any]] = []
self.client = Anthropic(api_key=api_key)
self.client = Anthropic(api_key=api_key, base_url=api_base)

def log_usage(self, response):
"""Log the total tokens from the Anthropic API response."""
Expand Down
41 changes: 41 additions & 0 deletions dsp/modules/aws_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
from abc import ABC, abstractmethod
from typing import Any, Optional

import backoff

try:
import boto3
from botocore.exceptions import ClientError
ERRORS = (ClientError,)

except ImportError:
ERRORS = (Exception,)


def backoff_hdlr(details):
"""Handler from https://pypi.org/project/backoff/."""
print(
"Backing off {wait:0.1f} seconds after {tries} tries "
"calling function {target} with kwargs "
"{kwargs}".format(**details),
)


def giveup_hdlr(details):
"""Wrapper function that decides when to give up on retry."""
if "max retries" in details.args[0]:
return False
return True

class AWSProvider(ABC):
"""This abstract class adds support for AWS model providers such as Bedrock and SageMaker.
Expand Down Expand Up @@ -52,6 +77,14 @@ def get_provider_name(self) -> str:
return self.__class__.__name__

@abstractmethod
@backoff.on_exception(
backoff.expo,
ERRORS,
max_time=1000,
max_tries=8,
on_backoff=backoff_hdlr,
giveup=giveup_hdlr,
)
def call_model(self, model_id: str, body: str) -> str:
"""Call the model and return the response."""

Expand Down Expand Up @@ -119,6 +152,14 @@ def __init__(
"""
super().__init__(region_name, "runtime.sagemaker", profile_name)

@backoff.on_exception(
backoff.expo,
ERRORS,
max_time=1000,
max_tries=8,
on_backoff=backoff_hdlr,
giveup=giveup_hdlr,
)
def call_model(self, model_id: str, body: str) -> str:
return self.predictor.invoke_endpoint(
EndpointName=model_id,
Expand Down
Loading