Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

feat(dspy): handle case where type origin doesn't have a __name__ attr

Michael Jones6d7c968bd11b
diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.py
index e2c3abeba..261c15667 100644
--- a/dspy/adapters/chat_adapter.py
+++ b/dspy/adapters/chat_adapter.py
@@ -1,13 +1,14 @@
-import re
import ast
import json
+import re
import textwrap
+from typing import get_args, get_origin
from pydantic import TypeAdapter
+
from .base import Adapter
-from typing import get_origin, get_args
-field_header_pattern = re.compile(r'\[\[ ## (\w+) ## \]\]')
+field_header_pattern = re.compile(r"\[\[ ## (\w+) ## \]\]")
class ChatAdapter(Adapter):
@@ -20,9 +21,11 @@ class ChatAdapter(Adapter):
# Extract demos where some of the output_fields are not filled in.
incomplete_demos = [demo for demo in demos if not all(k in demo for k in signature.fields)]
complete_demos = [demo for demo in demos if demo not in incomplete_demos]
- incomplete_demos = [demo for demo in incomplete_demos \
- if any(k in demo for k in signature.input_fields) and \
- any(k in demo for k in signature.output_fields)]
+ incomplete_demos = [
+ demo
+ for demo in incomplete_demos
+ if any(k in demo for k in signature.input_fields) and any(k in demo for k in signature.output_fields)
+ ]
demos = incomplete_demos + complete_demos
@@ -31,20 +34,22 @@ class ChatAdapter(Adapter):
for demo in demos:
messages.append(format_turn(signature, demo, role="user", incomplete=demo in incomplete_demos))
messages.append(format_turn(signature, demo, role="assistant", incomplete=demo in incomplete_demos))
-
+
messages.append(format_turn(signature, inputs, role="user"))
return messages
-
+
def parse(self, signature, completion, _parse_values=True):
sections = [(None, [])]
for line in completion.splitlines():
match = field_header_pattern.match(line.strip())
- if match: sections.append((match.group(1), []))
- else: sections[-1][1].append(line)
+ if match:
+ sections.append((match.group(1), []))
+ else:
+ sections[-1][1].append(line)
- sections = [(k, '\n'.join(v).strip()) for k, v in sections]
+ sections = [(k, "\n".join(v).strip()) for k, v in sections]
fields = {}
for k, v in sections:
@@ -52,23 +57,29 @@ class ChatAdapter(Adapter):
try:
fields[k] = parse_value(v, signature.output_fields[k].annotation) if _parse_values else v
except Exception as e:
- raise ValueError(f"Error parsing field {k}: {e}.\n\n\t\tOn attempting to parse the value\n```\n{v}\n```")
+ raise ValueError(
+ f"Error parsing field {k}: {e}.\n\n\t\tOn attempting to parse the value\n```\n{v}\n```"
+ )
if fields.keys() != signature.output_fields.keys():
raise ValueError(f"Expected {signature.output_fields.keys()} but got {fields.keys()}")
return fields
+
def format_blob(blob):
- if '\n' not in blob and "«" not in blob and "»" not in blob: return f"«{blob}»"
+ if "\n" not in blob and "«" not in blob and "»" not in blob:
+ return f"«{blob}»"
- modified_blob = blob.replace('\n', '\n ')
+ modified_blob = blob.replace("\n", "\n ")
return f"«««\n {modified_blob}\n»»»"
def format_list(items):
- if len(items) == 0: return "N/A"
- if len(items) == 1: return format_blob(items[0])
+ if len(items) == 0:
+ return "N/A"
+ if len(items) == 1:
+ return format_blob(items[0])
return "\n".join([f"[{idx+1}] {format_blob(txt)}" for idx, txt in enumerate(items)])
@@ -79,21 +90,25 @@ def format_fields(fields):
v = v if not isinstance(v, list) else format_list(v)
output.append(f"[[ ## {k} ## ]]\n{v}")
- return '\n\n'.join(output).strip()
-
+ return "\n\n".join(output).strip()
+
def parse_value(value, annotation):
- if annotation is str: return str(value)
+ if annotation is str:
+ return str(value)
parsed_value = value
if isinstance(value, str):
- try: parsed_value = json.loads(value)
+ try:
+ parsed_value = json.loads(value)
except json.JSONDecodeError:
- try: parsed_value = ast.literal_eval(value)
- except (ValueError, SyntaxError): parsed_value = value
+ try:
+ parsed_value = ast.literal_eval(value)
+ except (ValueError, SyntaxError):
+ parsed_value = value
return TypeAdapter(annotation).validate_python(parsed_value)
-def format_turn(signature, values, role, incomplete=False):
+def format_turn(signature, values, role, incomplete=False):
content = []
if role == "user":
@@ -101,42 +116,46 @@ def format_turn(signature, values, role, incomplete=False):
if incomplete:
content.append("This is an example of the task, though some input or output fields are not supplied.")
else:
- field_names, values = list(signature.output_fields.keys()) + ['completed'], {**values, 'completed': ''}
+ field_names, values = list(signature.output_fields.keys()) + ["completed"], {**values, "completed": ""}
if not incomplete:
if not set(values).issuperset(set(field_names)):
raise ValueError(f"Expected {field_names} but got {values.keys()}")
-
+
content.append(format_fields({k: values.get(k, "Not supplied for this particular example.") for k in field_names}))
if role == "user":
- content.append("Respond with the corresponding output fields, starting with the field " +
- ", then ".join(f"`{f}`" for f in signature.output_fields) +
- ", and then ending with the marker for `completed`.")
+ content.append(
+ "Respond with the corresponding output fields, starting with the field "
+ + ", then ".join(f"`{f}`" for f in signature.output_fields)
+ + ", and then ending with the marker for `completed`."
+ )
- return {"role": role, "content": '\n\n'.join(content).strip()}
+ return {"role": role, "content": "\n\n".join(content).strip()}
def get_annotation_name(annotation):
origin = get_origin(annotation)
args = get_args(annotation)
if origin is None:
- if hasattr(annotation, '__name__'):
+ if hasattr(annotation, "__name__"):
return annotation.__name__
else:
return str(annotation)
else:
- args_str = ', '.join(get_annotation_name(arg) for arg in args)
- return f"{origin.__name__}[{args_str}]"
+ args_str = ", ".join(get_annotation_name(arg) for arg in args)
+ return f"{get_annotation_name(origin)}[{args_str}]"
+
def enumerate_fields(fields):
parts = []
for idx, (k, v) in enumerate(fields.items()):
parts.append(f"{idx+1}. `{k}`")
parts[-1] += f" ({get_annotation_name(v.annotation)})"
- parts[-1] += f": {v.json_schema_extra['desc']}" if v.json_schema_extra['desc'] != f'${{{k}}}' else ''
+ parts[-1] += f": {v.json_schema_extra['desc']}" if v.json_schema_extra["desc"] != f"${{{k}}}" else ""
+
+ return "\n".join(parts).strip()
- return '\n'.join(parts).strip()
def prepare_instructions(signature):
parts = []
@@ -144,12 +163,12 @@ def prepare_instructions(signature):
parts.append("Your output fields are:\n" + enumerate_fields(signature.output_fields))
parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")
- parts.append(format_fields({f : f"{{{f}}}" for f in signature.input_fields}))
- parts.append(format_fields({f : f"{{{f}}}" for f in signature.output_fields}))
- parts.append(format_fields({'completed' : ""}))
+ parts.append(format_fields({f: f"{{{f}}}" for f in signature.input_fields}))
+ parts.append(format_fields({f: f"{{{f}}}" for f in signature.output_fields}))
+ parts.append(format_fields({"completed": ""}))
instructions = textwrap.dedent(signature.instructions)
- objective = ('\n' + ' ' * 8).join([''] + instructions.splitlines())
+ objective = ("\n" + " " * 8).join([""] + instructions.splitlines())
parts.append(f"In adhering to this structure, your objective is: {objective}")
# parts.append("You will receive some input fields in each interaction. " +
@@ -157,4 +176,4 @@ def prepare_instructions(signature):
# ", then ".join(f"`{f}`" for f in signature.output_fields) +
# ", and then ending with the marker for `completed`.")
- return '\n\n'.join(parts).strip()
+ return "\n\n".join(parts).strip()
diff --git a/tests/functional/test_functional.py b/tests/functional/test_functional.py
index 01bc9bc53..0341bdede 100644
--- a/tests/functional/test_functional.py
+++ b/tests/functional/test_functional.py
@@ -435,7 +435,7 @@ def test_multi_errors():
[[ ## completed ## ]]
- In adhering to this structure, your objective is:
+ In adhering to this structure, your objective is:
Given the fields `email`, produce the fields `flight_information`."""
), # noqa
},
@@ -508,7 +508,7 @@ def test_field_validator():
[[ ## completed ## ]]
- In adhering to this structure, your objective is:
+ In adhering to this structure, your objective is:
Make a very succinct json object that validates with the following schema"""
), # noqa
},
@@ -590,8 +590,7 @@ def test_parse_type_string():
def test_literal():
- pytest.skip("This test is not working as Literal type does not have a __name__ attribute")
- lm = DummyLM(['[[ ## output ## ]]\n"2"', '[[ ## output ## ]]\n"3"'])
+ lm = DummyLM(['[[ ## f ## ]]\n"2"', '[[ ## f ## ]]\n"3"'])
dspy.settings.configure(lm=lm)
@predictor
@@ -602,8 +601,7 @@ def test_literal():
def test_literal_missmatch():
- pytest.skip("This test is not working as Literal type does not have a __name__ attribute")
- lm = DummyLM([f'[[ ## output ## ]]\n"{i}"' for i in range(5, 100)])
+ lm = DummyLM([f'[[ ## f ## ]]\n"{i}"' for i in range(5, 100)])
dspy.settings.configure(lm=lm)
@predictor(max_retries=1)
@@ -617,8 +615,7 @@ def test_literal_missmatch():
def test_literal_int():
- pytest.skip("This test is not working as Literal type does not have a __name__ attribute")
- lm = DummyLM(["[[ ## output ## ]]\n2", "[[ ## output ## ]]\n3"])
+ lm = DummyLM(["[[ ## f ## ]]\n2", "[[ ## f ## ]]\n3"])
dspy.settings.configure(lm=lm)
@predictor
@@ -629,8 +626,7 @@ def test_literal_int():
def test_literal_int_missmatch():
- pytest.skip("This test is not working as Literal type does not have a __name__ attribute")
- lm = DummyLM([f"[[ ## output ## ]]\n{i}" for i in range(5, 100)])
+ lm = DummyLM([f"[[ ## f ## ]]\n{i}" for i in range(5, 100)])
dspy.settings.configure(lm=lm)
@predictor(max_retries=1)
@@ -753,7 +749,7 @@ def test_list_input2():
[[ ## completed ## ]]
- In adhering to this structure, your objective is:
+ In adhering to this structure, your objective is:
Given the fields `attempted_signatures`, produce the fields `proposed_signature`."""
), # noqa
},
@@ -821,7 +817,7 @@ def test_custom_reasoning_field():
[[ ## completed ## ]]
- In adhering to this structure, your objective is:
+ In adhering to this structure, your objective is:
Given the fields `topic`, produce the fields `question`."""
), # noqa
},
@@ -976,7 +972,7 @@ def test_demos():
[[ ## completed ## ]]
- In adhering to this structure, your objective is:
+ In adhering to this structure, your objective is:
Given the fields `input`, produce the fields `output`."""
), # noqa
},