Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions docs/api/modules/ChainOfThought.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ class ChainOfThought(Predict):

self.activated = activated

signature = self.signature
*keys, last_key = signature.kwargs.keys()

DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
desc="${produce the " + last_key + "}. We ...")

rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE

extended_kwargs = {key: signature.kwargs[key] for key in keys}
extended_kwargs.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})

self.extended_signature = dsp.Template(signature.instructions, **extended_kwargs)
signature = ensure_signature(self.signature)
*_keys, last_key = signature.output_fields.keys()

rationale_type = rationale_type or dspy.OutputField(
prefix="Reasoning: Let's think step by step in order to",
desc="${produce the " + last_key + "}. We ...",
)

self.extended_signature = signature.prepend("rationale", rationale_type, type_=str)
```

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

Expand Down Expand Up @@ -64,3 +61,15 @@ pred = generate_answer(question=question)
print(f"Question: {question}")
print(f"Predicted Answer: {pred.answer}")
```

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.

```python
#define a custom rationale
rationale_type = dspy.OutputField(
prefix="Reasoning: Let's think step by step in order to",
desc="${produce the answer}. We ...",
)
#Pass signature to ChainOfThought module
generate_answer = dspy.ChainOfThought(BasicQA, rationale_type=rationale_type)
```
27 changes: 9 additions & 18 deletions docs/api/modules/ChainOfThoughtWithHint.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,23 @@ The constructor initializes the `ChainOfThoughtWithHint` class and sets up its a
class ChainOfThoughtWithHint(Predict):
def __init__(self, signature, rationale_type=None, activated=True, **config):
super().__init__(signature, **config)

self.activated = activated

signature = self.signature
*keys, last_key = signature.kwargs.keys()

DEFAULT_HINT_TYPE = dsp.Type(prefix="Hint:", desc="${hint}")

DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
desc="${produce the " + last_key + "}. We ...")

rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE

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

extended_kwargs2 = {key: signature.kwargs[key] for key in keys}
extended_kwargs2.update({'hint': DEFAULT_HINT_TYPE, 'rationale': rationale_type, last_key: signature.kwargs[last_key]})

self.extended_signature1 = dsp.Template(signature.instructions, **extended_kwargs1)
self.extended_signature2 = dsp.Template(signature.instructions, **extended_kwargs2)
DEFAULT_HINT_TYPE = dspy.OutputField()
self.extended_signature2 = self.extended_signature1.insert(-2, "hint", DEFAULT_HINT_TYPE, type_=str)
```

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

Expand Down