Skip to content

Commit d08fa38

Browse files
committed
Merge branch 'main' of https://github.com/drawal1/dspy into main
2 parents b65e17f + 4326033 commit d08fa38

File tree

10 files changed

+47
-29
lines changed

10 files changed

+47
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
146146
- [Using DSPy to train Gpt 3.5 on HumanEval by Thomas Ahle](https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb)
147147
- [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)
148148

149+
TODO: Add links to the state-of-the-art results on Theory of Mind (ToM) by Plastic Labs, the results by Haize Labs for Red Teaming with DSPy, and the DSPy pipeline from Replit.
150+
149151
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).
150152

151153
## 3) Syntax: You're in charge of the workflow—it's free-form Python code!

docs/api/language_model_clients/aws_models.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 9
33
---
44

5-
# dsp.AWSMistral, dsp.AWSAnthropic, dsp.AWSMeta
5+
# dspy.AWSMistral, dspy.AWSAnthropic, dspy.AWSMeta
66

77
### Usage
88

@@ -83,4 +83,4 @@ Refer to [`dspy.OpenAI`](https://dspy-docs.vercel.app/api/language_model_clients
8383

8484
<br/>
8585

86-
`AWSAnthropic` and `AWSMeta` work exactly the same as `AWSMistral`.
86+
`AWSAnthropic` and `AWSMeta` work exactly the same as `AWSMistral`.

dsp/modules/aws_providers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class AWSProvider(ABC):
88
"""This abstract class adds support for AWS model providers such as Bedrock and SageMaker.
99
The subclasses such as Bedrock and Sagemaker implement the abstract method _call_model and work in conjunction with the AWSModel classes.
1010
Usage Example:
11-
bedrock = Bedrock(region_name="us-west-2")
12-
bedrock_mixtral = AWSMistral(bedrock, "mistral.mixtral-8x7b-instruct-v0:1", **kwargs)
13-
bedrock_haiku = AWSAnthropic(bedrock, "anthropic.claude-3-haiku-20240307-v1:0", **kwargs)
14-
bedrock_llama2 = AWSMeta(bedrock, "meta.llama2-13b-chat-v1", **kwargs)
11+
bedrock = dspy.Bedrock(region_name="us-west-2")
12+
bedrock_mixtral = dspy.AWSMistral(bedrock, "mistral.mixtral-8x7b-instruct-v0:1", **kwargs)
13+
bedrock_haiku = dspy.AWSAnthropic(bedrock, "anthropic.claude-3-haiku-20240307-v1:0", **kwargs)
14+
bedrock_llama2 = dspy.AWSMeta(bedrock, "meta.llama2-13b-chat-v1", **kwargs)
1515
16-
sagemaker = Sagemaker(region_name="us-west-2")
17-
sagemaker_model = AWSMistral(sagemaker, "<YOUR_ENDPOINT_NAME>", **kwargs)
16+
sagemaker = dspy.Sagemaker(region_name="us-west-2")
17+
sagemaker_model = dspy.AWSMistral(sagemaker, "<YOUR_ENDPOINT_NAME>", **kwargs)
1818
"""
1919

2020
def __init__(

dsp/modules/azure_openai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AzureOpenAI(LM):
4444
Args:
4545
api_base (str): Azure URL endpoint for model calling, often called 'azure_endpoint'.
4646
api_version (str): Version identifier for API.
47-
model (str, optional): OpenAI or Azure supported LLM model to use. Defaults to "text-davinci-002".
47+
model (str, optional): OpenAI or Azure supported LLM model to use. Defaults to "gpt-3.5-turbo-instruct".
4848
api_key (Optional[str], optional): API provider Authentication token. use Defaults to None.
4949
model_type (Literal["chat", "text"], optional): The type of model that was specified. Mainly to decide the optimal prompting strategy. Defaults to "chat".
5050
**kwargs: Additional arguments to pass to the API provider.

dsp/modules/gpt3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class GPT3(LM):
4040
"""Wrapper around OpenAI's GPT API.
4141
4242
Args:
43-
model (str, optional): OpenAI supported LLM model to use. Defaults to "text-davinci-002".
43+
model (str, optional): OpenAI supported LLM model to use. Defaults to "gpt-3.5-turbo-instruct".
4444
api_key (Optional[str], optional): API provider Authentication token. use Defaults to None.
4545
api_provider (Literal["openai"], optional): The API provider to use. Defaults to "openai".
4646
model_type (Literal["chat", "text"], optional): The type of model that was specified. Mainly to decide the optimal prompting strategy. Defaults to "text".

dsp/modules/lm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def inspect_history(self, n: int = 1, skip: int = 0):
4646
prompt = x["prompt"]
4747

4848
if prompt != last_prompt:
49-
if provider == "clarifai" or provider == "google" or provider == "Bedrock" or provider == "Sagemaker":
49+
if provider == "clarifai" or provider == "google" or provider == "groq" or provider == "Bedrock" or provider == "Sagemaker":
5050
printed.append((prompt, x["response"]))
5151
elif provider == "anthropic":
5252
blocks = [{"text": block.text} for block in x["response"].content if block.type == "text"]

dspy/evaluate/evaluate.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,17 @@ def wrapped_program(example_idx, example):
166166
num_threads,
167167
display_progress,
168168
)
169-
if return_outputs: # Handle the return_outputs logic
170-
results = [(example, prediction, score)
171-
for _, example, prediction, score in reordered_devset]
172169

173170
if display:
174171
print(
175172
f"Average Metric: {ncorrect} / {ntotal} ({round(100 * ncorrect / ntotal, 1)}%)")
176173

177174
predicted_devset = sorted(reordered_devset)
178175

176+
if return_outputs: # Handle the return_outputs logic
177+
results = [(example, prediction, score)
178+
for _, example, prediction, score in predicted_devset]
179+
179180
# data = [{**example, **prediction, 'correct': score} for example, prediction, score in zip(reordered_devset, preds, scores)]
180181
data = [
181182
merge_dicts(example, prediction) | {"correct": score} for _, example, prediction, score in predicted_devset
@@ -222,9 +223,9 @@ def wrapped_program(example_idx, example):
222223
ipython_display(HTML(message))
223224

224225
if return_all_scores and return_outputs:
225-
return round(100 * ncorrect / ntotal, 2), results, [score for *_, score in reordered_devset]
226+
return round(100 * ncorrect / ntotal, 2), results, [score for *_, score in predicted_devset]
226227
elif return_all_scores:
227-
return round(100 * ncorrect / ntotal, 2), [score for *_, score in reordered_devset]
228+
return round(100 * ncorrect / ntotal, 2), [score for *_, score in predicted_devset]
228229
elif return_outputs:
229230
return round(100 * ncorrect / ntotal, 2), results
230231

dspy/functional/functional.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,32 @@
1414
from dspy.signatures.signature import ensure_signature, make_signature
1515

1616

17-
def predictor(func) -> dspy.Module:
18-
"""Decorator that creates a predictor module based on the provided function."""
19-
signature = _func_to_signature(func)
20-
*_, output_key = signature.output_fields.keys()
21-
return _StripOutput(TypedPredictor(signature), output_key)
22-
23-
24-
def cot(func) -> dspy.Module:
25-
"""Decorator that creates a chain of thought module based on the provided function."""
26-
signature = _func_to_signature(func)
27-
*_, output_key = signature.output_fields.keys()
28-
return _StripOutput(TypedChainOfThought(signature), output_key)
17+
def predictor(*args, **kwargs):
18+
def _predictor(func) -> dspy.Module:
19+
"""Decorator that creates a predictor module based on the provided function."""
20+
signature = _func_to_signature(func)
21+
*_, output_key = signature.output_fields.keys()
22+
return _StripOutput(TypedPredictor(signature, **kwargs), output_key)
23+
24+
# if we have only a single callable argument, the decorator was invoked with no key word arguments
25+
# so we just return the wrapped function
26+
if len(args) == 1 and callable(args[0]) and len(kwargs) == 0:
27+
return _predictor(args[0])
28+
return _predictor
29+
30+
31+
def cot(*args, **kwargs):
32+
def _cot(func) -> dspy.Module:
33+
"""Decorator that creates a chain of thought module based on the provided function."""
34+
signature = _func_to_signature(func)
35+
*_, output_key = signature.output_fields.keys()
36+
return _StripOutput(TypedChainOfThought(signature, **kwargs), output_key)
37+
38+
# if we have only a single callable argument, the decorator was invoked with no key word arguments
39+
# so we just return the wrapped function
40+
if len(args) == 1 and callable(args[0]) and len(kwargs) == 0:
41+
return _cot(args[0])
42+
return _cot
2943

3044

3145
class _StripOutput(dspy.Module):

dspy/retrieve/qdrant_rm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353

5454
super().__init__(k=k)
5555

56-
def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int],**kwargs) -> dspy.Prediction:
56+
def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = None,**kwargs) -> dspy.Prediction:
5757
"""Search with Qdrant for self.k top passages for query
5858
5959
Args:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies = [
3636
"requests",
3737
"optuna",
3838
"pydantic>=2.5.0,<=2.7",
39+
"structlog"
3940
]
4041

4142
[project.optional-dependencies]

0 commit comments

Comments
 (0)