Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
A few more typed examples
  • Loading branch information
thomasahle authored Mar 4, 2024
1 parent 2bd6fda commit 7a20578
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Assume, for example, you need to find

```python
from pydantic import BaseModel, Field
from dspy.functional import TypedPredictor

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

predictor = dspy.TypedPredictor(TravelSignature)
predictor = TypedPredictor(TravelSignature)
predictor(email='...')
```

Which will output a list of `TravelInformation` objects.

There are other ways to create typed signatures too. Such as
```python
predictor = TypedChainOfThought("question:str -> answer:int")
```
which applies chain of thought, and is guaranteed to return an int.

There's even an approach inspired by [tanuki.py](https://github.com/Tanuki/tanuki.py), which can be convenient when defining modules:
```python
from dspy.functional import FunctionalModule, predictor, cot

class MyModule(FunctionalModule):
@predictor
def hard_question(possible_topics: list[str]) -> str:
"""Write a hard question based on one of the topics. It should be answerable by a number."""

@cot
def answer(question: str) -> float:
pass

def forward(possible_topics: list[str]):
q = hard_question(possible_topics=possible_topics)
a = answer(question=q)
return (q, a)
```

For more examples, see [the list above](https://github.com/stanfordnlp/dspy#:~:text=Typed%20DSPy),
as well as [the unit tests](https://github.com/stanfordnlp/dspy/blob/main/tests/functional/test_functional.py) for the module.

## 6) FAQ: Is DSPy right for me?

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.
Expand Down

0 comments on commit 7a20578

Please sign in to comment.