DSPy: The framework for programming—not prompting—language models
Python
WIP for raw_demos in ChatAdapter
Omar Khattab2b32abd0d1f5
diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.pyindex f7928ed17..627f8322a 100644--- a/dspy/adapters/chat_adapter.py+++ b/dspy/adapters/chat_adapter.py@@ -1,7 +1,7 @@import refrom .base import Adapter-field_header_pattern = re.compile(r'\[\[\[\[ #### (\w+) #### \]\]\]\]')+field_header_pattern = re.compile(r'\[\[\[ ### (\w+) ### \]\]\]')class ChatAdapter(Adapter):@@ -10,7 +10,15 @@ class ChatAdapter(Adapter):def format(self, signature, demos, inputs):messages = []++ # TODO: Extract `raw_demos` out of `demos`, i.e. demos where some of the output_fields are not filled in.+ # raw_demos = [demo for demo in demos if not all(k in demo for k in signature.output_fields)]+ # demos = [demo for demo in demos if demo not in raw_demos]+messages.append({"role": "system", "content": prepare_instructions(signature)})+ # messages.append({"role": "system", "content": prepare_instructions(signature, raw_demos)})++ # TODO: Remove the raw_demos from demos.for demo in demos:output_fields_, demo_ = list(signature.output_fields.keys()) + ['completed'], {**demo, 'completed': ''}@@ -42,13 +50,14 @@ class ChatAdapter(Adapter):def format_fields(fields):- return '\n\n'.join([f"[[[[ #### {k} #### ]]]]\n{v}" for k, v in fields.items()]).strip()+ return '\n\n'.join([f"[[[ ### {k} ### ]]]\n{v}" for k, v in fields.items()]).strip()def format_chat_turn(field_names, values):- if not set(values).issuperset(set(field_names)):- raise ValueError(f"Expected {field_names} but got {values.keys()}")+ # TODO: Reinstate validation after dealing with raw_demos in the system messages.+ # if not set(values).issuperset(set(field_names)):+ # raise ValueError(f"Expected {field_names} but got {values.keys()}")- return format_fields({k: values[k] for k in field_names})+ return format_fields({k: values.get(k, "Not supplied for this particular example.") for k in field_names})def enumerate_fields(fields):parts = []@@ -69,12 +78,12 @@ def prepare_instructions(signature):parts.append(format_fields({f : f"{{{f}}}" for f in signature.output_fields}))parts.append(format_fields({'completed' : ""}))+ objective = ('\n' + ' ' * 8).join([''] + signature.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. " +"Respond only 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`.")-- objective = ('\n' + ' ' * 8).join([''] + signature.instructions.splitlines())- parts.append(f"In adhering to this structure, your objective is: {objective}")return '\n\n'.join(parts).strip()diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.pyindex 16e1ba3e6..a7aa7b9a8 100644--- a/dspy/predict/chain_of_thought.py+++ b/dspy/predict/chain_of_thought.py@@ -18,7 +18,11 @@ class ChainOfThought(Module):*_keys, last_key = signature.output_fields.keys()prefix = "Reasoning: Let's think step by step in order to"- if dspy.settings.experimental:++ import dspy+ if isinstance(dspy.settings.lm, dspy.LM):+ desc = "${reasoning}"+ elif dspy.settings.experimental:desc = "${produce the output fields}. We ..."else:desc = f"${{produce the {last_key}}}. We ..."