Skip to content

Commit 2d845de

Browse files
authored
Update 8-typed_predictors.md
A few fixes to the typed docs
1 parent 734a345 commit 2d845de

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

docs/docs/building-blocks/8-typed_predictors.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ Let's take a simple task as an example i.e. given the `context` and `query`, the
1616
from pydantic import BaseModel, Field
1717

1818
class Input(BaseModel):
19-
context: str = Field(..., description="The context for the question")
20-
query: str = Field(..., description="The question to be answered")
19+
context: str = Field(description="The context for the question")
20+
query: str = Field(description="The question to be answered")
2121

2222
class Output(BaseModel):
23-
answer: str = Field(..., description="The answer for the question")
24-
factual_: float = Field(..., description="The confidence score for the answer")
23+
answer: str = Field(description="The answer for the question")
24+
confidence: float = Field(ge=0, le=1, description="The confidence score for the answer")
2525
```
2626

2727
As you can see, we can describe the attributes by defining a simple Signature that takes in the input and returns the output.
@@ -46,6 +46,11 @@ predictor = dspy.TypedPredictor(QASignature)
4646

4747
Similar to other modules, we pass the `QASignature` to `dspy.TypedPredictor` which enforces the typed constraints.
4848

49+
And similarly to `dspy.Predict`, we can also use a "string signature", which we type as:
50+
```python
51+
predictor = dspy.TypedPredictor("input:Input -> output:Output")
52+
```
53+
4954
### I/O in Typed Predictors
5055

5156
Now let's test out the Typed Predictor by providing some sample input to the predictor and verifying the output type. We can create an `Input` instance and pass it to the predictor to get a dictionary of the output.
@@ -62,8 +67,8 @@ prediction = predictor(input=doc_query_pair)
6267
Let's see the output and its type.
6368

6469
```python
65-
answer = prediction['answer']
66-
confidence_score = prediction['confidence_score']
70+
answer = prediction.answer
71+
confidence_score = prediction.confidence
6772

6873
print(f"Prediction: {prediction}\n\n")
6974
print(f"Answer: {answer}, Answer Type: {type(answer)}")
@@ -89,18 +94,18 @@ prediction = cot_predictor(input=doc_query_pair)
8994

9095
While the `dspy.TypedPredictor` and `dspy.TypedChainOfThought` provide a convenient way to use typed predictors, you can also use them as decorators to enforce type constraints on the inputs and outputs of the function. This relies on the internal definitions of the Signature class and its function arguments, outputs, and docstrings.
9196

92-
```
93-
# Function name is output key
94-
97+
```python
9598
@dspy.predictor
96-
def qa_function(doc_query_pair: Input) -> Output:
97-
"""Answer the question based on the context and query provided, and on the scale of 10 tell how confident you are about the answer."""
99+
def answer(doc_query_pair: Input) -> Output:
100+
"""Answer the question based on the context and query provided, and on the scale of 0-1 tell how confident you are about the answer."""
98101
pass
99102

100103
@dspy.cot
101-
def qa_function(doc_query_pair: Input) -> Output:
102-
"""Answer the question based on the context and query provided, and on the scale of 10 tell how confident you are about the answer."""
104+
def answer(doc_query_pair: Input) -> Output:
105+
"""Answer the question based on the context and query provided, and on the scale of 0-1 tell how confident you are about the answer."""
103106
pass
107+
108+
prediction = answer(doc_query_pair=doc_query_pair)
104109
```
105110

106111
## Composing Functional Typed Predictors in `dspy.Module`

0 commit comments

Comments
 (0)