Skip to content

Commit cba4a72

Browse files
authored
Minor updates to dspy.ReAct (#1716)
* JsonAdapter: Handle JSON formatting in demo's outputs * Adjustmetns for JsonAdapter * Revamp ReAct, adjust Bootstrap (handle repeat calls to a module; transpose order for max_rounds), adjust ChatAdapter (handle incomplete demos better) * Remove ReAct tests (outdated format) * Remove react tests (outdated) * Minor update to dspy.ReAct
1 parent 803dff0 commit cba4a72

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

dspy/adapters/chat_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def parse(self, signature, completion, _parse_values=True):
8686
def format_turn(self, signature, values, role, incomplete=False):
8787
return format_turn(signature, values, role, incomplete)
8888

89-
def format_fields(self, signature, values):
89+
def format_fields(self, signature, values, role):
9090
fields_with_values = {
9191
FieldInfoWithName(name=field_name, info=field_info): values.get(
9292
field_name, "Not supplied for this particular example."

dspy/adapters/json_adapter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def parse(self, signature, completion, _parse_values=True):
9090
def format_turn(self, signature, values, role, incomplete=False):
9191
return format_turn(signature, values, role, incomplete)
9292

93-
def format_fields(self, signature, values):
93+
def format_fields(self, signature, values, role):
9494
fields_with_values = {
9595
FieldInfoWithName(name=field_name, info=field_info): values.get(
9696
field_name, "Not supplied for this particular example."
@@ -99,8 +99,7 @@ def format_fields(self, signature, values):
9999
if field_name in values
100100
}
101101

102-
return format_fields(role='user', fields_with_values=fields_with_values)
103-
102+
return format_fields(role=role, fields_with_values=fields_with_values)
104103

105104

106105
def parse_value(value, annotation):

dspy/predict/react.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, signature, tools: list[Callable], max_iters=5):
4747

4848
finish_desc = f"Signals that the final outputs, i.e. {outputs_}, are now available and marks the task as complete."
4949
finish_args = {} #k: v.annotation for k, v in signature.output_fields.items()}
50-
tools["finish"] = Tool(func=lambda **kwargs: kwargs, name="finish", desc=finish_desc, args=finish_args)
50+
tools["finish"] = Tool(func=lambda **kwargs: "Completed.", name="finish", desc=finish_desc, args=finish_args)
5151

5252
for idx, tool in enumerate(tools.values()):
5353
desc = tool.desc.replace("\n", " ")
@@ -77,10 +77,8 @@ def forward(self, **input_args):
7777

7878
def format(trajectory_: dict[str, Any], last_iteration: bool):
7979
adapter = dspy.settings.adapter or dspy.ChatAdapter()
80-
blob = adapter.format_fields(dspy.Signature(f"{', '.join(trajectory_.keys())} -> x"), trajectory_)
81-
warning = f"\n\nWarning: The maximum number of iterations ({self.max_iters}) has been reached."
82-
warning += " You must now produce the finish action."
83-
return blob + (warning if last_iteration else "")
80+
signature_ = dspy.Signature(f"{', '.join(trajectory_.keys())} -> x")
81+
return adapter.format_fields(signature_, trajectory_, role='user')
8482

8583
for idx in range(self.max_iters):
8684
pred = self.react(**input_args, trajectory=format(trajectory, last_iteration=(idx == self.max_iters-1)))

dspy/teleprompt/bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _bootstrap(self, *, max_bootstraps=None):
150150
for round_idx in range(self.max_rounds):
151151
bootstrap_attempts += 1
152152

153-
if success := self._bootstrap_one_example(example, round_idx):
153+
if self._bootstrap_one_example(example, round_idx):
154154
bootstrapped[example_idx] = True
155155
break
156156

0 commit comments

Comments
 (0)