Skip to content

Commit c2b4126

Browse files
committed
Merge branch 'main' of https://github.com/stanfordnlp/dspy into stanfordnlp-main
2 parents 4897967 + b05394b commit c2b4126

File tree

91 files changed

+6547
-1832
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+6547
-1832
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ assertion.log
4747
*.log
4848
*.db
4949
/.devcontainer/.personalization.sh
50+
51+
.mypy_cache

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,25 @@ Ditto! **DSPy** gives you the right general-purpose modules (e.g., `ChainOfThoug
5858

5959
All you need is:
6060

61-
```
61+
```bash
6262
pip install dspy-ai
6363
```
6464

65+
To install the very latest from `main`:
66+
67+
```bash
68+
pip install git+https://github.com/stanfordnlp/dspy.git
69+
````
70+
6571
Or open our intro notebook in Google Colab: [<img align="center" src="https://colab.research.google.com/assets/colab-badge.svg" />](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/intro.ipynb)
6672

6773
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.
6874

69-
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, or [Weaviate](https://github.com/weaviate/weaviate) retrieval integration(s), include the extra(s) below:
75+
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),
76+
or [Milvus](https://github.com/milvus-io/milvus) retrieval integration(s), include the extra(s) below:
7077

7178
```
72-
pip install dspy-ai[chromadb] # or [qdrant] or [marqo] or [mongodb] or [pinecone] or [weaviate]
79+
pip install dspy-ai[chromadb] # or [qdrant] or [marqo] or [mongodb] or [pinecone] or [weaviate] or [milvus]
7380
```
7481

7582
## 2) Documentation
@@ -94,10 +101,11 @@ The DSPy documentation is divided into **tutorials** (step-by-step illustration
94101

95102
- [DSPy talk at ScaleByTheBay Nov 2023](https://www.youtube.com/watch?v=Dt3H2ninoeY).
96103
- [DSPy webinar with MLOps Learners](https://www.youtube.com/watch?v=im7bCLW2aM4), a bit longer with Q&A.
97-
- Hands-on Overviews of DSPy by the community: [DSPy Explained! by Connor Shorten](https://www.youtube.com/watch?v=41EfOY0Ldkc), [DSPy explained by code_your_own_ai](https://www.youtube.com/watch?v=ycfnKPxBMck)
104+
- Hands-on Overviews of DSPy by the community: [DSPy Explained! by Connor Shorten](https://www.youtube.com/watch?v=41EfOY0Ldkc), [DSPy explained by code_your_own_ai](https://www.youtube.com/watch?v=ycfnKPxBMck), [DSPy Crash Course by AI Bites](https://youtu.be/5-zgASQKkKQ?si=3gnmVouT5_rpk_nu)
98105
- Interviews: [Weaviate Podcast in-person](https://www.youtube.com/watch?v=CDung1LnLbY), and you can find 6-7 other remote podcasts on YouTube from a few different perspectives/audiences.
99106
- **Tracing in DSPy** with Arize Phoenix: [Tutorial for tracing your prompts and the steps of your DSPy programs](https://colab.research.google.com/github/Arize-ai/phoenix/blob/main/tutorials/tracing/dspy_tracing_tutorial.ipynb)
100107
- [DSPy: Not Your Average Prompt Engineering](https://jina.ai/news/dspy-not-your-average-prompt-engineering), why it's crucial for future prompt engineering, and yet why it is challenging for prompt engineers to learn.
108+
- **Tracing & Optimization Tracking in DSPy** with Parea AI: [Tutorial on tracing & evaluating a DSPy RAG program](https://docs.parea.ai/tutorials/dspy-rag-trace-evaluate/tutorial)
101109
102110
### B) Guides
103111
@@ -129,22 +137,29 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
129137

130138
**Some other examples (not exhaustive, feel free to add more via PR):**
131139

140+
141+
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/tasks)
142+
- [Sophisticated Extreme Multi-Class Classification, IReRa, by Karel D’Oosterlinck](https://github.com/KarelDO/xmc.dspy)
143+
- [Haize Lab's Red Teaming with DSPy](https://blog.haizelabs.com/posts/dspy/) and see [their DSPy code](https://github.com/haizelabs/dspy-redteam)
132144
- Applying DSPy Assertions
133145
- [Long-form Answer Generation with Citations, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/longformqa/longformqa_assertions.ipynb)
134146
- [Generating Answer Choices for Quiz Questions, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/quiz/quiz_assertions.ipynb)
135147
- [Generating Tweets for QA, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/tweets/tweets_assertions.ipynb)
136148
- [Compiling LCEL runnables from LangChain in DSPy](https://github.com/stanfordnlp/dspy/blob/main/examples/tweets/compiling_langchain.ipynb)
137149
- [AI feedback, or writing LM-based metrics in DSPy](https://github.com/stanfordnlp/dspy/blob/main/examples/tweets/tweet_metric.py)
138-
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/tasks)
150+
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/README.md)
139151
- [Indian Languages NLI with gains due to compiling by Saiful Haq](https://github.com/saifulhaq95/DSPy-Indic/blob/main/indicxlni.ipynb)
140-
- [Sophisticated Extreme Multi-Class Classification, IReRa, by Karel D’Oosterlinck](https://github.com/KarelDO/xmc.dspy)
141152
- [DSPy on BIG-Bench Hard Example, by Chris Levy](https://drchrislevy.github.io/posts/dspy/dspy.html)
142153
- [Using Ollama with DSPy for Mistral (quantized) by @jrknox1977](https://gist.github.com/jrknox1977/78c17e492b5a75ee5bbaf9673aee4641)
143-
- [Using DSPy, "The Unreasonable Effectiveness of Eccentric Automatic Prompts" (paper) by VMware's Rick Battle & Teja Gollapudi, and interview at TheRegister](https://www.theregister.com/2024/02/22/prompt_engineering_ai_models/)
154+
- [Using DSPy, "The Unreasonable Effectiveness of Eccentric Automatic Prompts" (paper) by VMware's Rick Battle & Teja Gollapudi](https://arxiv.org/abs/2402.10949), and [interview at TheRegister](https://www.theregister.com/2024/02/22/prompt_engineering_ai_models/)
155+
- [Optimizing Performance of Open Source LM for Text-to-SQL using DSPy and vLLM, by Juan Ovalle](https://github.com/jjovalle99/DSPy-Text2SQL)
144156
- Typed DSPy (contributed by [@normal-computing](https://github.com/normal-computing))
145157
- [Using DSPy to train Gpt 3.5 on HumanEval by Thomas Ahle](https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb)
146158
- [Building a chess playing agent using DSPy by Franck SN](https://medium.com/thoughts-on-machine-learning/building-a-chess-playing-agent-using-dspy-9b87c868f71e)
147159

160+
161+
TODO: Add links to the state-of-the-art results by the University of Toronto on Clinical NLP, on Theory of Mind (ToM) by Plastic Labs, and the DSPy pipeline from Replit.
162+
148163
There are also recent cool examples at [Weaviate's DSPy cookbook](https://github.com/weaviate/recipes/tree/main/integrations/dspy) by Connor Shorten. [See tutorial on YouTube](https://www.youtube.com/watch?v=CEuUG4Umfxs).
149164
150165
## 3) Syntax: You're in charge of the workflow—it's free-form Python code!
@@ -411,7 +426,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for a quickstart guide to contributing to
411426
412427
**DSPy** is led by **Omar Khattab** at Stanford NLP with **Chris Potts** and **Matei Zaharia**.
413428
414-
Key contributors and team members include **Arnav Singhvi**, **Krista Opsahl-Ong**, **Michael Ryan**, **Karel D'Oosterlinck**, **Shangyin Tan**, **Manish Shetty**, **Paridhi Maheshwari**, **Keshav Santhanam**, **Sri Vardhamanan**, **Eric Zhang**, **Hanna Moazam**, **Thomas Joshi**, **Saiful Haq**, **Ashutosh Sharma**, and **Herumb Shandilya**.
429+
Key contributors and team members include **Arnav Singhvi**, **Krista Opsahl-Ong**, **Michael Ryan**, **Cyrus Nouroozi**, **Kyle Caverly**, **Amir Mehr**, **Karel D'Oosterlinck**, **Shangyin Tan**, **Manish Shetty**, **Herumb Shandilya**, **Paridhi Maheshwari**, **Keshav Santhanam**, **Sri Vardhamanan**, **Eric Zhang**, **Hanna Moazam**, **Thomas Joshi**, **Saiful Haq**, and **Ashutosh Sharma**.
415430
416431
**DSPy** includes important contributions from **Rick Battle** and **Igor Kotenkov**. It reflects discussions with **Peter Zhong**, **Haoze He**, **Lisa Li**, **David Hall**, **Ashwin Paranjape**, **Heather Miller**, **Chris Manning**, **Percy Liang**, and many others.
417432

docs/api/language_model_clients/AzureOpenAI.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ lm = dspy.AzureOpenAI(api_base='...', api_version='2023-12-01-preview', model='g
1414

1515
The constructor initializes the base class `LM` and verifies the provided arguments like the `api_provider`, `api_key`, and `api_base` to set up OpenAI request retrieval through Azure. 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`.
1616

17+
Azure requires that the deployment id of the Azure deployment to be also provided using the argument `deployment_id`.
18+
1719
```python
1820
class AzureOpenAI(LM):
1921
def __init__(
@@ -53,4 +55,4 @@ After generation, the completions are post-processed based on the `model_type` p
5355
- `**kwargs`: Additional keyword arguments for completion request.
5456

5557
**Returns:**
56-
- `List[Dict[str, Any]]`: List of completion choices.
58+
- `List[Dict[str, Any]]`: List of completion choices.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# GoogleVertexAI Usage Guide
2+
3+
This guide provides instructions on how to use the `GoogleVertexAI` class to interact with Google Vertex AI's API for text and code generation.
4+
5+
## Requirements
6+
7+
- Python 3.10 or higher.
8+
- The `vertexai` package installed, which can be installed via pip.
9+
- A Google Cloud account and a configured project with access to Vertex AI.
10+
11+
## Installation
12+
13+
Ensure you have installed the `vertexai` package along with other necessary dependencies:
14+
15+
```bash
16+
pip install dspy-ai[google-vertex-ai]
17+
```
18+
19+
## Configuration
20+
21+
Before using the `GoogleVertexAI` class, you need to set up access to Google Cloud:
22+
23+
1. Create a project in Google Cloud Platform (GCP).
24+
2. Enable the Vertex AI API for your project.
25+
3. Create authentication credentials and save them in a JSON file.
26+
27+
## Usage
28+
29+
Here's an example of how to instantiate the `GoogleVertexAI` class and send a text generation request:
30+
31+
```python
32+
from dsp.modules import GoogleVertexAI # Import the GoogleVertexAI class
33+
34+
# Initialize the class with the model name and parameters for Vertex AI
35+
vertex_ai = GoogleVertexAI(
36+
model_name="text-bison@002",
37+
project="your-google-cloud-project-id",
38+
location="us-central1",
39+
credentials="path-to-your-service-account-file.json"
40+
)
41+
```
42+
43+
## Customizing Requests
44+
45+
You can customize requests by passing additional parameters such as `temperature`, `max_output_tokens`, and others supported by the Vertex AI API. This allows you to control the behavior of the text generation.
46+
47+
## Important Notes
48+
49+
- Make sure you have correctly set up access to Google Cloud to avoid authentication issues.
50+
- Be aware of the quotas and limits of the Vertex AI API to prevent unexpected interruptions in service.
51+
52+
With this guide, you're ready to use `GoogleVertexAI` for interacting with Google Vertex AI's text and code generation services.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Watsonx Usage Guide
2+
3+
This guide provides instructions on how to use the `Watsonx` class to interact with IBM Watsonx.ai API for text and code generation.
4+
5+
## Requirements
6+
7+
- Python 3.10 or higher.
8+
- The `ibm-watsonx-ai` package installed, which can be installed via pip.
9+
- An IBM Cloud account and a Watsonx configured project.
10+
11+
## Installation
12+
13+
Ensure you have installed the `ibm-watsonx-ai` package along with other necessary dependencies:
14+
15+
## Configuration
16+
17+
Before using the `Watsonx` class, you need to set up access to IBM Cloud:
18+
19+
1. Create an IBM Cloud account
20+
2. Enable a Watsonx service from the catalog
21+
3. Create a new project and associate a Watson Machine Learning service instance.
22+
4. Create an IAM authentication credentials and save them in a JSON file.
23+
24+
## Usage
25+
26+
Here's an example of how to instantiate the `Watsonx` class and send a generation request:
27+
28+
```python
29+
import dspy
30+
31+
''' Initialize the class with the model name and parameters for Watsonx.ai
32+
You can choose between many different models:
33+
* (Mistral) mistralai/mixtral-8x7b-instruct-v01
34+
* (Meta) meta-llama/llama-3-70b-instruct
35+
* (IBM) ibm/granite-13b-instruct-v2
36+
* and many others.
37+
'''
38+
watsonx=dspy.Watsonx(
39+
model='mistralai/mixtral-8x7b-instruct-v01',
40+
credentials={
41+
"apikey": "your-api-key",
42+
"url": "https://us-south.ml.cloud.ibm.com"
43+
},
44+
project_id="your-watsonx-project-id",
45+
max_new_tokens=500,
46+
max_tokens=1000
47+
)
48+
49+
dspy.settings.configure(lm=watsonx)
50+
```
51+
52+
## Customizing Requests
53+
54+
You can customize requests by passing additional parameters such as `decoding_method`,`max_new_tokens`, `stop_sequences`, `repetition_penalty`, and others supported by the Watsonx.ai API. This allows you to control the behavior of the generation.
55+
Refer to [`ibm-watsonx-ai library`](https://ibm.github.io/watsonx-ai-python-sdk/index.html) documentation.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
sidebar_position: 9
3+
---
4+
5+
# dspy.AWSMistral, dspy.AWSAnthropic, dspy.AWSMeta
6+
7+
### Usage
8+
9+
```python
10+
# Notes:
11+
# 1. Install boto3 to use AWS models.
12+
# 2. Configure your AWS credentials with the AWS CLI before using these models
13+
14+
# initialize the bedrock aws provider
15+
bedrock = dspy.Bedrock(region_name="us-west-2")
16+
# For mixtral on Bedrock
17+
lm = dspy.AWSMistral(bedrock, "mistral.mixtral-8x7b-instruct-v0:1", **kwargs)
18+
# For haiku on Bedrock
19+
lm = dspy.AWSAnthropic(bedrock, "anthropic.claude-3-haiku-20240307-v1:0", **kwargs)
20+
# For llama2 on Bedrock
21+
lm = dspy.AWSMeta(bedrock, "meta.llama2-13b-chat-v1", **kwargs)
22+
23+
# initialize the sagemaker aws provider
24+
sagemaker = dspy.Sagemaker(region_name="us-west-2")
25+
# For mistral on Sagemaker
26+
# Note: you need to create a Sagemaker endpoint for the mistral model first
27+
lm = dspy.AWSMistral(sagemaker, "<YOUR_MISTRAL_ENDPOINT_NAME>", **kwargs)
28+
29+
```
30+
31+
### Constructor
32+
33+
The `AWSMistral` constructor initializes the base class `AWSModel` which itself inherits from the `LM` class.
34+
35+
```python
36+
class AWSMistral(AWSModel):
37+
"""Mistral family of models."""
38+
39+
def __init__(
40+
self,
41+
aws_provider: AWSProvider,
42+
model: str,
43+
max_context_size: int = 32768,
44+
max_new_tokens: int = 1500,
45+
**kwargs
46+
) -> None:
47+
```
48+
49+
**Parameters:**
50+
- `aws_provider` (AWSProvider): The aws provider to use. One of `dspy.Bedrock` or `dspy.Sagemaker`.
51+
- `model` (_str_): Mistral AI pretrained models. For Bedrock, this is the Model ID in https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns. For Sagemaker, this is the endpoint name.
52+
- `max_context_size` (_Optional[int]_, _optional_): Max context size for this model. Defaults to 32768.
53+
- `max_new_tokens` (_Optional[int]_, _optional_): Max new tokens possible for this model. Defaults to 1500.
54+
- `**kwargs`: Additional language model arguments to pass to the API provider.
55+
56+
### Methods
57+
58+
```python
59+
def _format_prompt(self, raw_prompt: str) -> str:
60+
```
61+
This function formats the prompt for the model. Refer to the model card for the specific formatting required.
62+
63+
<br/>
64+
65+
```python
66+
def _create_body(self, prompt: str, **kwargs) -> tuple[int, dict[str, str | float]]:
67+
```
68+
This function creates the body of the request to the model. It takes the prompt and any additional keyword arguments and returns a tuple of the number of tokens to generate and a dictionary of keys including the prompt used to create the body of the request.
69+
70+
<br/>
71+
72+
```python
73+
def _call_model(self, body: str) -> str:
74+
```
75+
This function calls the model using the provider `call_model()` function and extracts the generated text (completion) from the provider-specific response.
76+
77+
<br/>
78+
79+
The above model-specific methods are called by the `AWSModel::basic_request()` method, which is the main method for querying the model. This method takes the prompt and any additional keyword arguments and calls the `AWSModel::_simple_api_call()` which then delegates to the model-specific `_create_body()` and `_call_model()` methods to create the body of the request, call the model and extract the generated text.
80+
81+
82+
Refer to [`dspy.OpenAI`](https://dspy-docs.vercel.app/api/language_model_clients/OpenAI) documentation for information on the `LM` base class functionality.
83+
84+
<br/>
85+
86+
`AWSAnthropic` and `AWSMeta` work exactly the same as `AWSMistral`.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
sidebar_position: 9
3+
---
4+
5+
# dspy.Bedrock, dspy.Sagemaker
6+
7+
### Usage
8+
9+
The `AWSProvider` class is the base class for the AWS providers - `dspy.Bedrock` and `dspy.Sagemaker`. An instance of one of these providers is passed to the constructor when creating an instance of an AWS model class (e.g., `dspy.AWSMistral`) that is ultimately used to query the model.
10+
11+
```python
12+
# Notes:
13+
# 1. Install boto3 to use AWS models.
14+
# 2. Configure your AWS credentials with the AWS CLI before using these models
15+
16+
# initialize the bedrock aws provider
17+
bedrock = dspy.Bedrock(region_name="us-west-2")
18+
19+
# initialize the sagemaker aws provider
20+
sagemaker = dspy.Sagemaker(region_name="us-west-2")
21+
```
22+
23+
### Constructor
24+
25+
The `Bedrock` constructor initializes the base class `AWSProvider`.
26+
27+
```python
28+
class Bedrock(AWSProvider):
29+
"""This class adds support for Bedrock models."""
30+
31+
def __init__(
32+
self,
33+
region_name: str,
34+
profile_name: Optional[str] = None,
35+
batch_n_enabled: bool = False, # This has to be setup manually on Bedrock.
36+
) -> None:
37+
```
38+
39+
**Parameters:**
40+
- `region_name` (str): The AWS region where this LM is hosted.
41+
- `profile_name` (str, optional): boto3 credentials profile.
42+
- `batch_n_enabled` (bool): If False, call the LM N times rather than batching.
43+
44+
### Methods
45+
46+
```python
47+
def call_model(self, model_id: str, body: str) -> str:
48+
```
49+
This function implements the actual invocation of the model on AWS using the boto3 provider.
50+
51+
<br/>
52+
53+
`Sagemaker` works exactly the same as `Bedrock`.

0 commit comments

Comments
 (0)