From bd2bbd79802aeccaf5ceeabd2206c8a627a82ea1 Mon Sep 17 00:00:00 2001 From: habanoz Date: Wed, 17 Apr 2024 09:07:14 +0300 Subject: [PATCH 1/2] fix rational type in the api documentation. --- docs/api/modules/ChainOfThought.md | 35 ++++++++++++++-------- docs/api/modules/ChainOfThoughtWithHint.md | 27 ++++++----------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/docs/api/modules/ChainOfThought.md b/docs/api/modules/ChainOfThought.md index ab9bd0e5fa..1c73657b1c 100644 --- a/docs/api/modules/ChainOfThought.md +++ b/docs/api/modules/ChainOfThought.md @@ -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` (_dsp.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. @@ -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. + +```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) +``` \ No newline at end of file diff --git a/docs/api/modules/ChainOfThoughtWithHint.md b/docs/api/modules/ChainOfThoughtWithHint.md index 1b4b964e74..5236b5156a 100644 --- a/docs/api/modules/ChainOfThoughtWithHint.md +++ b/docs/api/modules/ChainOfThoughtWithHint.md @@ -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` (_dsp.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. From b5d1df6d7b50649cb3818d0d329057b45ab1d8a7 Mon Sep 17 00:00:00 2001 From: habanoz Date: Thu, 25 Apr 2024 11:46:35 +0300 Subject: [PATCH 2/2] corrections after code review --- docs/api/modules/ChainOfThought.md | 4 ++-- docs/api/modules/ChainOfThoughtWithHint.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api/modules/ChainOfThought.md b/docs/api/modules/ChainOfThought.md index 1c73657b1c..9d7939283e 100644 --- a/docs/api/modules/ChainOfThought.md +++ b/docs/api/modules/ChainOfThought.md @@ -26,7 +26,7 @@ class ChainOfThought(Predict): **Parameters:** - `signature` (_Any_): Signature of predictive model. -- `rationale_type` (_dsp.OutputField_, _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. @@ -62,7 +62,7 @@ print(f"Question: {question}") print(f"Predicted Answer: {pred.answer}") ``` -The following example shows how to specify your custom rationale. +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 diff --git a/docs/api/modules/ChainOfThoughtWithHint.md b/docs/api/modules/ChainOfThoughtWithHint.md index 5236b5156a..26a41814d6 100644 --- a/docs/api/modules/ChainOfThoughtWithHint.md +++ b/docs/api/modules/ChainOfThoughtWithHint.md @@ -24,7 +24,7 @@ class ChainOfThoughtWithHint(Predict): **Parameters:** - `signature` (_Any_): Signature of predictive model. -- `rationale_type` (_dsp.OutputField_, _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.