Skip to content

Commit 0ad59dc

Browse files
authored
Merge pull request #1 from tom-doerr/patch-4
Patch 4
2 parents 1a56e69 + a9d173f commit 0ad59dc

37 files changed

+2110
-857
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ 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.
@@ -95,7 +101,7 @@ The DSPy documentation is divided into **tutorials** (step-by-step illustration
95101

96102
- [DSPy talk at ScaleByTheBay Nov 2023](https://www.youtube.com/watch?v=Dt3H2ninoeY).
97103
- [DSPy webinar with MLOps Learners](https://www.youtube.com/watch?v=im7bCLW2aM4), a bit longer with Q&A.
98-
- 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)
99105
- 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.
100106
- **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)
101107
- [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.
@@ -142,6 +148,7 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
142148
- [DSPy on BIG-Bench Hard Example, by Chris Levy](https://drchrislevy.github.io/posts/dspy/dspy.html)
143149
- [Using Ollama with DSPy for Mistral (quantized) by @jrknox1977](https://gist.github.com/jrknox1977/78c17e492b5a75ee5bbaf9673aee4641)
144150
- [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/)
151+
- [Optimizing Performance of Open Source LM for Text-to-SQL using DSPy and vLLM, by Juan Ovalle](https://github.com/jjovalle99/DSPy-Text2SQL)
145152
- Typed DSPy (contributed by [@normal-computing](https://github.com/normal-computing))
146153
- [Using DSPy to train Gpt 3.5 on HumanEval by Thomas Ahle](https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb)
147154
- [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)
@@ -414,7 +421,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for a quickstart guide to contributing to
414421
415422
**DSPy** is led by **Omar Khattab** at Stanford NLP with **Chris Potts** and **Matei Zaharia**.
416423
417-
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**.
424+
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**.
418425
419426
**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.
420427

docs/api/retrieval_model_clients/FaissRM.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The **FaissRM** module provides a retriever that uses an in-memory Faiss vector
4040

4141
```python
4242
import dspy
43-
from dspy.retrieve import FaissRM
43+
from dspy.retrieve.faiss_rm import FaissRM
4444

4545
document_chunks = [
4646
"The superbowl this year was played between the San Francisco 49ers and the Kanasas City Chiefs",
@@ -59,4 +59,4 @@ frm = FaissRM(document_chunks)
5959
turbo = dspy.OpenAI(model="gpt-3.5-turbo")
6060
dspy.settings.configure(lm=turbo, rm=frm)
6161
print(frm(["I am in the mood for Chinese food"]))
62-
```
62+
```

docs/docs/building-blocks/1-language_models.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,25 @@ You need to host these models on your own GPU(s). Below, we include pointers for
145145
1. `dspy.HFClientTGI`: for HuggingFace models through the Text Generation Inference (TGI) system. [Tutorial: How do I install and launch the TGI server?](https://dspy-docs.vercel.app/docs/deep-dive/language_model_clients/local_models/HFClientTGI)
146146

147147
```python
148-
tgi_llama2 = dspy.HFClientTGI(model="meta-llama/Llama-2-7b-hf", port=8080, url="http://localhost")
148+
tgi_mistral = dspy.HFClientTGI(model="mistralai/Mistral-7B-Instruct-v0.2", port=8080, url="http://localhost")
149149
```
150150

151151
2. `dspy.HFClientVLLM`: for HuggingFace models through vLLM. [Tutorial: How do I install and launch the vLLM server?](https://dspy-docs.vercel.app/docs/deep-dive/language_model_clients/local_models/HFClientVLLM)
152152

153153
```python
154-
vllm_llama2 = dspy.HFClientVLLM(model="meta-llama/Llama-2-7b-hf", port=8080, url="http://localhost")
154+
vllm_mistral = dspy.HFClientVLLM(model="mistralai/Mistral-7B-Instruct-v0.2", port=8080, url="http://localhost")
155155
```
156156

157157
3. `dspy.HFModel` (experimental) [Tutorial: How do I initialize models using HFModel](https://dspy-docs.vercel.app/api/local_language_model_clients/HFModel)
158158

159159
```python
160-
llama = dspy.HFModel(model = 'meta-llama/Llama-2-7b-hf')
160+
mistral = dspy.HFModel(model = 'mistralai/Mistral-7B-Instruct-v0.2')
161161
```
162162

163163
4. `dspy.Ollama` (experimental) for open source models through [Ollama](https://ollama.com). [Tutorial: How do I install and use Ollama on a local computer?](https://dspy-docs.vercel.app/api/local_language_model_clients/Ollama)\n",
164164

165165
```python
166-
mistral_ollama = dspy.OllamaLocal(model='mistral')
166+
ollama_mistral = dspy.OllamaLocal(model='mistral')
167167
```
168168

169169
5. `dspy.ChatModuleClient` (experimental): [How do I install and use MLC?](https://dspy-docs.vercel.app/api/local_language_model_clients/MLC)

docs/docs/cheatsheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ print(f"Question: {question}")
177177
print(f"Final Predicted Answer (after ReAct process): {result.answer}")
178178
```
179179

180-
### dspy.Retreive
180+
### dspy.Retrieve
181181

182182
```python
183183
colbertv2_wiki17_abstracts = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')

docs/docs/deep-dive/retrieval_models_clients/Azure.mdx

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,59 @@ The constructor initializes an instance of the `AzureAISearchRM` class and sets
2222
- `search_api_key` (str): The API key for accessing the Azure AI Search service.
2323
- `search_index_name` (str): The name of the search index in the Azure AI Search service.
2424
- `field_text` (str): The name of the field containing text content in the search index. This field will be mapped to the "content" field in the dsp framework.
25+
- `field_vector` (Optional[str]): The name of the field containing vector content in the search index.
2526
- `k` (int, optional): The default number of top passages to retrieve. Defaults to 3.
27+
- `azure_openai_client` (Optional[openai.AzureOpenAI]): An instance of the AzureOpenAI client. Either openai_client or embedding_func must be provided. Defaults to None.
28+
- `openai_embed_model` (Optional[str]): The name of the OpenAI embedding model. Defaults to "text-embedding-ada-002".
29+
- `embedding_func` (Optional[Callable]): A function for generating embeddings. Either openai_client or embedding_func must be provided. Defaults to None.
2630
- `semantic_ranker` (bool, optional): Whether to use semantic ranking. Defaults to False.
2731
- `filter` (str, optional): Additional filter query. Defaults to None.
2832
- `query_language` (str, optional): The language of the query. Defaults to "en-Us".
2933
- `query_speller` (str, optional): The speller mode. Defaults to "lexicon".
3034
- `use_semantic_captions` (bool, optional): Whether to use semantic captions. Defaults to False.
3135
- `query_type` (Optional[QueryType], optional): The type of query. Defaults to QueryType.FULL.
3236
- `semantic_configuration_name` (str, optional): The name of the semantic configuration. Defaults to None.
37+
- `is_vector_search` (Optional[bool]): Whether to enable vector search. Defaults to False.
38+
- `is_hybrid_search` (Optional[bool]): Whether to enable hybrid search. Defaults to False.
39+
- `is_fulltext_search` (Optional[bool]): Whether to enable fulltext search. Defaults to True.
40+
- `vector_filter_mode` (Optional[VectorFilterMode]): The vector filter mode. Defaults to None.
3341

34-
Available Query Types:
3542

36-
SIMPLE
43+
**Available Query Types:**
44+
45+
- SIMPLE
3746
"""Uses the simple query syntax for searches. Search text is interpreted using a simple query
3847
#: language that allows for symbols such as +, * and "". Queries are evaluated across all
3948
#: searchable fields by default, unless the searchFields parameter is specified."""
40-
FULL
49+
- FULL
4150
"""Uses the full Lucene query syntax for searches. Search text is interpreted using the Lucene
4251
#: query language which allows field-specific and weighted searches, as well as other advanced
4352
#: features."""
44-
SEMANTIC
53+
- SEMANTIC
4554
"""Best suited for queries expressed in natural language as opposed to keywords. Improves
4655
#: precision of search results by re-ranking the top search results using a ranking model trained
4756
#: on the Web corpus.""
4857

4958
More Details: https://learn.microsoft.com/en-us/azure/search/search-query-overview
5059

60+
**Available Vector Filter Mode:**
61+
62+
- POST_FILTER = "postFilter"
63+
"""The filter will be applied after the candidate set of vector results is returned. Depending on
64+
#: the filter selectivity, this can result in fewer results than requested by the parameter 'k'."""
65+
66+
- PRE_FILTER = "preFilter"
67+
"""The filter will be applied before the search query."""
68+
69+
More Details: https://learn.microsoft.com/en-us/azure/search/vector-search-filters
70+
71+
**Note**
72+
73+
- The `AzureAISearchRM` client allows you to perform Vector search, Hybrid search, or Full text search.
74+
- By default, the `AzureAISearchRM` client uses the Azure OpenAI Client for generating embeddings. If you want to use something else, you can provide your custom embedding_func, but either the openai_client or embedding_func must be provided.
75+
- If you need to enable semantic search, either with vector, hybrid, or full text search, then set the `semantic_ranker` flag to True.
76+
- If `semantic_ranker` is True, always set the `query_type` to QueryType.SEMANTIC and always provide the `semantic_configuration_name`.
77+
5178
Example of the AzureAISearchRM constructor:
5279

5380
```python
@@ -56,14 +83,22 @@ AzureAISearchRM(
5683
search_api_key: str,
5784
search_index_name: str,
5885
field_text: str,
86+
field_vector: Optional[str] = None,
5987
k: int = 3,
88+
azure_openai_client: Optional[openai.AzureOpenAI] = None,
89+
openai_embed_model: Optional[str] = "text-embedding-ada-002",
90+
embedding_func: Optional[Callable] = None,
6091
semantic_ranker: bool = False,
6192
filter: str = None,
6293
query_language: str = "en-Us",
6394
query_speller: str = "lexicon",
6495
use_semantic_captions: bool = False,
6596
query_type: Optional[QueryType] = QueryType.FULL,
66-
semantic_configuration_name: str = None
97+
semantic_configuration_name: str = None,
98+
is_vector_search: Optional[bool] = False,
99+
is_hybrid_search: Optional[bool] = False,
100+
is_fulltext_search: Optional[bool] = True,
101+
vector_filter_mode: Optional[VectorFilterMode.PRE_FILTER] = None
67102
)
68103
```
69104

@@ -128,6 +163,31 @@ for result in retrieval_response:
128163
print("Text:", result.long_text, "\n")
129164
```
130165

166+
3. Example of Semantic Hybrid Search.
167+
168+
```python
169+
from dspy.retrieve.azureaisearch_rm import AzureAISearchRM
170+
171+
azure_search = AzureAISearchRM(
172+
search_service_name="search_service_name",
173+
search_api_key="search_api_key",
174+
search_index_name="search_index_name",
175+
field_text="field_text",
176+
field_vector="field_vector",
177+
k=3,
178+
azure_openai_client="azure_openai_client",
179+
openai_embed_model="text-embedding-ada-002"
180+
semantic_ranker=True,
181+
query_type=QueryType.SEMANTIC,
182+
semantic_configuration_name="semantic_configuration_name",
183+
is_hybrid_search=True,
184+
)
185+
186+
retrieval_response = azure_search("What is Thermodynamics", k=3)
187+
for result in retrieval_response:
188+
print("Text:", result.long_text, "\n")
189+
```
190+
131191
***
132192

133193
<AuthorDetails name="Prajapati Harishkumar Kishorkumar"/>

docs/docs/deep-dive/teleprompter/signature-optimizer.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Now we have the baseline pipeline ready to use, so let's try using the `COPRO` t
8282
Let's start by importing and initializing our teleprompter, for the metric we'll be using the same `validate_context_and_answer` imported and used above:
8383

8484
```python
85-
from dspy.teleprompt import COPRP
85+
from dspy.teleprompt import COPRO
8686

8787
teleprompter = COPRO(
8888
metric=validate_context_and_answer,

docs/docs/faqs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Open source libraries such as [RAGautouille](https://github.com/bclavie/ragatoui
7070

7171
- **How do I turn off the cache? How do I export the cache?**
7272

73-
You can turn off the cache by setting the [`cache_turn_on` flag to `False`](https://github.com/stanfordnlp/dspy/blob/9d8a40c477b9dd6dcdc007647b5b9ddad2b5657a/dsp/modules/cache_utils.py#L10).
73+
You can turn off the cache by setting the [`DSP_CACHEBOOL`](https://github.com/stanfordnlp/dspy/blob/main/dsp/modules/cache_utils.py#L9) environment variable to `False`, which disables the `cache_turn_on` flag.
7474

7575
Your local cache will be saved to the global env directory `os.environ["DSP_NOTEBOOK_CACHEDIR"]` which you can usually set to `os.path.join(repo_path, 'cache')` and export this cache from here.
7676

docs/docs/quick-start/minimal-example.mdx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import AuthorDetails from '@site/src/components/AuthorDetails';
88

99
In this post, we walk you through a minimal working example using the DSPy library.
1010

11-
We make use of the GSM8K dataset and the OpenAI GPT-3.5-turbo model to simulate prompting tasks within DSPy.
11+
We make use of the [GSM8K dataset](https://huggingface.co/datasets/gsm8k) and the OpenAI GPT-3.5-turbo model to simulate prompting tasks within DSPy.
1212

1313
## Setup
1414

@@ -27,9 +27,17 @@ gsm8k = GSM8K()
2727
gsm8k_trainset, gsm8k_devset = gsm8k.train[:10], gsm8k.dev[:10]
2828
```
2929

30+
Let's take a look at what `gsm8k_trainset` and `gsm8k_devset` are:
31+
32+
```python
33+
print(gsm8k_trainset)
34+
```
35+
36+
The `gsm8k_trainset` and `gsm8k_devset` datasets contain a list of Examples with each example having `question` and `answer` field. We'll use these datasets to train and evaluate our model.
37+
3038
## Define the Module
3139

32-
With our environment set up, let's define a custom program that utilizes the `ChainOfThought` module to perform step-by-step reasoning to generate answers:
40+
With our environment set up, let's define a custom program that utilizes the [`ChainOfThought`](/api/modules/ChainOfThought) module to perform step-by-step reasoning to generate answers:
3341

3442
```python
3543
class CoT(dspy.Module):
@@ -43,7 +51,7 @@ class CoT(dspy.Module):
4351

4452
## Compile and Evaluate the Model
4553

46-
With our simple program in place, let's move on to optimizing it using the `BootstrapFewShotWithRandomSearch` teleprompter:
54+
With our simple program in place, let's move on to optimizing it using the [`BootstrapFewShot`](/api/optimizers/BootstrapFewShot) teleprompter:
4755

4856
```python
4957
from dspy.teleprompt import BootstrapFewShot

docs/docs/tutorials/other_tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ sidebar_position: 99999
2222

2323
- [DSPy talk at ScaleByTheBay Nov 2023](https://www.youtube.com/watch?v=Dt3H2ninoeY).
2424
- [DSPy webinar with MLOps Learners](https://www.youtube.com/watch?v=im7bCLW2aM4), a bit longer with Q&A.
25-
- 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)
25+
- 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)
2626
- 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.
2727
- **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)

docs/docs/tutorials/rag.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ RAG ensures LLMs can dynamically utilize real-time knowledge even if not origina
1010

1111
## Configuring LM and RM
1212

13-
We'll start by setting up the language model (LM) and retrieval model (RM), which **DSPy** supports through multiple [LM](https://dspy-docs.vercel.app/docs/category/language-model-clients) and [RM](https://dspy-docs.vercel.app/docs/category/retrieval-model-clients) APIs and [local models hosting](https://dspy-docs.vercel.app/docs/category/local-language-model-clients).
13+
We'll start by setting up the language model (LM) and retrieval model (RM), which **DSPy** supports through multiple [LM](https://dspy-docs.vercel.app/docs/category/language-model-clients) and [RM](https://dspy-docs.vercel.app/docs/category/retrieval-model-clients) APIs and [local models hosting](https://dspy-docs.vercel.app/docs/deep-dive/language_model_clients/local_models/HFClientTGI).
1414

1515
In this notebook, we'll work with GPT-3.5 (`gpt-3.5-turbo`) and the `ColBERTv2` retriever (a free server hosting a Wikipedia 2017 "abstracts" search index containing the first paragraph of each article from this [2017 dump](https://hotpotqa.github.io/wiki-readme.html)). We configure the LM and RM within DSPy, allowing DSPy to internally call the respective module when needed for generation or retrieval.
1616

0 commit comments

Comments
 (0)