Skip to content

Commit 7a20578

Browse files
authored
Update README.md
A few more typed examples
1 parent 2bd6fda commit 7a20578

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ Assume, for example, you need to find
283283

284284
```python
285285
from pydantic import BaseModel, Field
286+
from dspy.functional import TypedPredictor
286287

287288
class TravelInformation(BaseModel):
288289
origin: str = Field(pattern=r"^[A-Z]{3}$")
@@ -295,12 +296,40 @@ class TravelSignature(Signature):
295296
email: str = InputField()
296297
flight_information: list[TravelInformation] = OutputField()
297298

298-
predictor = dspy.TypedPredictor(TravelSignature)
299+
predictor = TypedPredictor(TravelSignature)
299300
predictor(email='...')
300301
```
301302

302303
Which will output a list of `TravelInformation` objects.
303304

305+
There are other ways to create typed signatures too. Such as
306+
```python
307+
predictor = TypedChainOfThought("question:str -> answer:int")
308+
```
309+
which applies chain of thought, and is guaranteed to return an int.
310+
311+
There's even an approach inspired by [tanuki.py](https://github.com/Tanuki/tanuki.py), which can be convenient when defining modules:
312+
```python
313+
from dspy.functional import FunctionalModule, predictor, cot
314+
315+
class MyModule(FunctionalModule):
316+
@predictor
317+
def hard_question(possible_topics: list[str]) -> str:
318+
"""Write a hard question based on one of the topics. It should be answerable by a number."""
319+
320+
@cot
321+
def answer(question: str) -> float:
322+
pass
323+
324+
def forward(possible_topics: list[str]):
325+
q = hard_question(possible_topics=possible_topics)
326+
a = answer(question=q)
327+
return (q, a)
328+
```
329+
330+
For more examples, see [the list above](https://github.com/stanfordnlp/dspy#:~:text=Typed%20DSPy),
331+
as well as [the unit tests](https://github.com/stanfordnlp/dspy/blob/main/tests/functional/test_functional.py) for the module.
332+
304333
## 6) FAQ: Is DSPy right for me?
305334

306335
The **DSPy** philosophy and abstraction differ significantly from other libraries and frameworks, so it's usually straightforward to decide when **DSPy** is (or isn't) the right framework for your usecase.

0 commit comments

Comments
 (0)