Skip to content

Commit 0d6b4c2

Browse files
authored
JsonAdapter: Handle JSON formatting in demo's outputs (#1702)
* JsonAdapter: Handle JSON formatting in demo's outputs * Adjustmetns for JsonAdapter
1 parent 2156a5e commit 0d6b4c2

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

dspy/adapters/json_adapter.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _format_field_value(field_info: FieldInfo, value: Any) -> str:
168168

169169

170170

171-
def format_fields(fields_with_values: Dict[FieldInfoWithName, Any]) -> str:
171+
def format_fields(role: str, fields_with_values: Dict[FieldInfoWithName, Any]) -> str:
172172
"""
173173
Formats the values of the specified fields according to the field's DSPy type (input or output),
174174
annotation (e.g. str, int, etc.), and the type of the value itself. Joins the formatted values
@@ -180,6 +180,13 @@ def format_fields(fields_with_values: Dict[FieldInfoWithName, Any]) -> str:
180180
Returns:
181181
The joined formatted values of the fields, represented as a string.
182182
"""
183+
184+
if role == "assistant":
185+
d = fields_with_values.items()
186+
d = {k.name: _serialize_for_json(v) for k, v in d}
187+
188+
return json.dumps(_serialize_for_json(d), indent=2)
189+
183190
output = []
184191
for field, field_value in fields_with_values.items():
185192
formatted_field_value = _format_field_value(field_info=field.info, value=field_value)
@@ -219,6 +226,7 @@ def format_turn(signature: SignatureMeta, values: Dict[str, Any], role, incomple
219226
raise ValueError(f"Expected {field_names} but got {values.keys()}")
220227

221228
formatted_fields = format_fields(
229+
role=role,
222230
fields_with_values={
223231
FieldInfoWithName(name=field_name, info=field_info): values.get(
224232
field_name, "Not supplied for this particular example."
@@ -269,7 +277,7 @@ def prepare_instructions(signature: SignatureMeta):
269277
parts = []
270278
parts.append("Your input fields are:\n" + enumerate_fields(signature.input_fields))
271279
parts.append("Your output fields are:\n" + enumerate_fields(signature.output_fields))
272-
# parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")
280+
parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")
273281

274282
def field_metadata(field_name, field_info):
275283
type_ = field_info.annotation
@@ -291,16 +299,19 @@ def field_metadata(field_name, field_info):
291299
desc = (" " * 8) + f"# note: the value you produce {desc}" if desc else ""
292300
return f"{{{field_name}}}{desc}"
293301

294-
def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo]):
302+
def format_signature_fields_for_instructions(role, fields: Dict[str, FieldInfo]):
295303
return format_fields(
304+
role=role,
296305
fields_with_values={
297306
FieldInfoWithName(name=field_name, info=field_info): field_metadata(field_name, field_info)
298307
for field_name, field_info in fields.items()
299308
}
300309
)
301-
302-
parts.append(format_signature_fields_for_instructions(signature.input_fields))
303-
parts.append(format_signature_fields_for_instructions(signature.output_fields))
310+
311+
parts.append("Inputs will have the following structure:")
312+
parts.append(format_signature_fields_for_instructions('user', signature.input_fields))
313+
parts.append("Outputs will be a JSON object with the following fields.")
314+
parts.append(format_signature_fields_for_instructions('assistant', signature.output_fields))
304315
# parts.append(format_fields({BuiltInCompletedOutputFieldInfo: ""}))
305316

306317
instructions = textwrap.dedent(signature.instructions)

0 commit comments

Comments
 (0)