Skip to content

Commit 66573d6

Browse files
authored
Merge pull request #851 from habanoz/main
fix #841, the issue with the api documentation.
2 parents 8f1becf + b5d1df6 commit 66573d6

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

docs/api/modules/ChainOfThought.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@ class ChainOfThought(Predict):
1313

1414
self.activated = activated
1515

16-
signature = self.signature
17-
*keys, last_key = signature.kwargs.keys()
18-
19-
DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
20-
desc="${produce the " + last_key + "}. We ...")
21-
22-
rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE
23-
24-
extended_kwargs = {key: signature.kwargs[key] for key in keys}
25-
extended_kwargs.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
26-
27-
self.extended_signature = dsp.Template(signature.instructions, **extended_kwargs)
16+
signature = ensure_signature(self.signature)
17+
*_keys, last_key = signature.output_fields.keys()
18+
19+
rationale_type = rationale_type or dspy.OutputField(
20+
prefix="Reasoning: Let's think step by step in order to",
21+
desc="${produce the " + last_key + "}. We ...",
22+
)
23+
24+
self.extended_signature = signature.prepend("rationale", rationale_type, type_=str)
2825
```
2926

3027
**Parameters:**
3128
- `signature` (_Any_): Signature of predictive model.
32-
- `rationale_type` (_dsp.Type_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
29+
- `rationale_type` (_dspy.OutputField_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
3330
- `activated` (_bool_, _optional_): Flag for activated chain of thought processing. Defaults to `True`.
3431
- `**config` (_dict_): Additional configuration parameters for model.
3532

@@ -64,3 +61,15 @@ pred = generate_answer(question=question)
6461
print(f"Question: {question}")
6562
print(f"Predicted Answer: {pred.answer}")
6663
```
64+
65+
The following example shows how to specify your custom rationale. Here `answer` corresponds to the last key to produce, it may be different in your case.
66+
67+
```python
68+
#define a custom rationale
69+
rationale_type = dspy.OutputField(
70+
prefix="Reasoning: Let's think step by step in order to",
71+
desc="${produce the answer}. We ...",
72+
)
73+
#Pass signature to ChainOfThought module
74+
generate_answer = dspy.ChainOfThought(BasicQA, rationale_type=rationale_type)
75+
```

docs/api/modules/ChainOfThoughtWithHint.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,23 @@ The constructor initializes the `ChainOfThoughtWithHint` class and sets up its a
88
class ChainOfThoughtWithHint(Predict):
99
def __init__(self, signature, rationale_type=None, activated=True, **config):
1010
super().__init__(signature, **config)
11-
1211
self.activated = activated
13-
1412
signature = self.signature
15-
*keys, last_key = signature.kwargs.keys()
16-
17-
DEFAULT_HINT_TYPE = dsp.Type(prefix="Hint:", desc="${hint}")
18-
19-
DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
20-
desc="${produce the " + last_key + "}. We ...")
2113

22-
rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE
23-
24-
extended_kwargs1 = {key: signature.kwargs[key] for key in keys}
25-
extended_kwargs1.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
14+
*keys, last_key = signature.fields.keys()
15+
rationale_type = rationale_type or dspy.OutputField(
16+
prefix="Reasoning: Let's think step by step in order to",
17+
desc="${produce the " + last_key + "}. We ...",
18+
)
19+
self.extended_signature1 = self.signature.insert(-2, "rationale", rationale_type, type_=str)
2620

27-
extended_kwargs2 = {key: signature.kwargs[key] for key in keys}
28-
extended_kwargs2.update({'hint': DEFAULT_HINT_TYPE, 'rationale': rationale_type, last_key: signature.kwargs[last_key]})
29-
30-
self.extended_signature1 = dsp.Template(signature.instructions, **extended_kwargs1)
31-
self.extended_signature2 = dsp.Template(signature.instructions, **extended_kwargs2)
21+
DEFAULT_HINT_TYPE = dspy.OutputField()
22+
self.extended_signature2 = self.extended_signature1.insert(-2, "hint", DEFAULT_HINT_TYPE, type_=str)
3223
```
3324

3425
**Parameters:**
3526
- `signature` (_Any_): Signature of predictive model.
36-
- `rationale_type` (_dsp.Type_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
27+
- `rationale_type` (_dspy.OutputField_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
3728
- `activated` (_bool_, _optional_): Flag for activated chain of thought processing. Defaults to `True`.
3829
- `**config` (_dict_): Additional configuration parameters for model.
3930

0 commit comments

Comments
 (0)