From c435a3f099fad67caba14da1be8d48689cb8009c Mon Sep 17 00:00:00 2001 From: Yilin Xu Date: Sat, 4 May 2024 23:23:53 -0700 Subject: [PATCH 1/2] added logic to parse instructions in ReAct signature --- dspy/predict/react.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dspy/predict/react.py b/dspy/predict/react.py index e358abd46..21a299c1c 100644 --- a/dspy/predict/react.py +++ b/dspy/predict/react.py @@ -28,11 +28,16 @@ def __init__(self, signature, max_iters=5, num_results=3, tools=None): inputs_ = ", ".join([f"`{k}`" for k in self.input_fields.keys()]) outputs_ = ", ".join([f"`{k}`" for k in self.output_fields.keys()]) - instr = [ + instr = [] + + if self.signature.instructions is not None: + instr.append(f"{self.signature.instructions}\n") + + instr.extend([ f"You will be given {inputs_} and you will respond with {outputs_}.\n", "To do this, you will interleave Thought, Action, and Observation steps.\n", "Thought can reason about the current situation, and Action can be the following types:\n", - ] + ]) self.tools["Finish"] = dspy.Example( name="Finish", From 7acf4c8a5dbb5bcd18d925abaf94ce0620706cea Mon Sep 17 00:00:00 2001 From: Yilin Xu Date: Tue, 7 May 2024 22:28:29 -0700 Subject: [PATCH 2/2] added a test case to test instructions parsing in ReAct class --- tests/predict/test_react.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/predict/test_react.py b/tests/predict/test_react.py index 79f8e0bdf..9b187471b 100644 --- a/tests/predict/test_react.py +++ b/tests/predict/test_react.py @@ -154,3 +154,16 @@ def test_custom_tools(): "Thought 3: Even more thoughts\n\n" "Action 3: Finish[baz]" ) + + +def test_signature_instructions(): + class ExampleSignature(dspy.Signature): + """You are going to generate output based on input.""" + + input = dspy.InputField() + output = dspy.OutputField() + + react = dspy.ReAct(ExampleSignature) + + assert react.react[0].signature.instructions is not None + assert react.react[0].signature.instructions.startswith("You are going to generate output based on input.")