Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Minor updates to dspy.ReAct (#1716)

Omar Khattabcba4a720c285
diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.py
index 81e037c57..8d73b6d3f 100644
--- a/dspy/adapters/chat_adapter.py
+++ b/dspy/adapters/chat_adapter.py
@@ -86,7 +86,7 @@ class ChatAdapter(Adapter):
def format_turn(self, signature, values, role, incomplete=False):
return format_turn(signature, values, role, incomplete)
- def format_fields(self, signature, values):
+ def format_fields(self, signature, values, role):
fields_with_values = {
FieldInfoWithName(name=field_name, info=field_info): values.get(
field_name, "Not supplied for this particular example."
diff --git a/dspy/adapters/json_adapter.py b/dspy/adapters/json_adapter.py
index f319d40fe..a1b1bedd3 100644
--- a/dspy/adapters/json_adapter.py
+++ b/dspy/adapters/json_adapter.py
@@ -90,7 +90,7 @@ class JsonAdapter(Adapter):
def format_turn(self, signature, values, role, incomplete=False):
return format_turn(signature, values, role, incomplete)
- def format_fields(self, signature, values):
+ def format_fields(self, signature, values, role):
fields_with_values = {
FieldInfoWithName(name=field_name, info=field_info): values.get(
field_name, "Not supplied for this particular example."
@@ -99,8 +99,7 @@ class JsonAdapter(Adapter):
if field_name in values
}
- return format_fields(role='user', fields_with_values=fields_with_values)
-
+ return format_fields(role=role, fields_with_values=fields_with_values)
def parse_value(value, annotation):
diff --git a/dspy/predict/react.py b/dspy/predict/react.py
index 2095d9ba6..a26f538f0 100644
--- a/dspy/predict/react.py
+++ b/dspy/predict/react.py
@@ -47,7 +47,7 @@ class ReAct(Module):
finish_desc = f"Signals that the final outputs, i.e. {outputs_}, are now available and marks the task as complete."
finish_args = {} #k: v.annotation for k, v in signature.output_fields.items()}
- tools["finish"] = Tool(func=lambda **kwargs: kwargs, name="finish", desc=finish_desc, args=finish_args)
+ tools["finish"] = Tool(func=lambda **kwargs: "Completed.", name="finish", desc=finish_desc, args=finish_args)
for idx, tool in enumerate(tools.values()):
desc = tool.desc.replace("\n", " ")
@@ -77,10 +77,8 @@ class ReAct(Module):
def format(trajectory_: dict[str, Any], last_iteration: bool):
adapter = dspy.settings.adapter or dspy.ChatAdapter()
- blob = adapter.format_fields(dspy.Signature(f"{', '.join(trajectory_.keys())} -> x"), trajectory_)
- warning = f"\n\nWarning: The maximum number of iterations ({self.max_iters}) has been reached."
- warning += " You must now produce the finish action."
- return blob + (warning if last_iteration else "")
+ signature_ = dspy.Signature(f"{', '.join(trajectory_.keys())} -> x")
+ return adapter.format_fields(signature_, trajectory_, role='user')
for idx in range(self.max_iters):
pred = self.react(**input_args, trajectory=format(trajectory, last_iteration=(idx == self.max_iters-1)))
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index d6787cc62..b17244828 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -150,7 +150,7 @@ class BootstrapFewShot(Teleprompter):
for round_idx in range(self.max_rounds):
bootstrap_attempts += 1
- if success := self._bootstrap_one_example(example, round_idx):
+ if self._bootstrap_one_example(example, round_idx):
bootstrapped[example_idx] = True
break