Skip to content

Commit dd971a7

Browse files
Fix missing [[ ## completed ## ]] in history and fewshot example (#8340)
1 parent 6c130fa commit dd971a7

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

dspy/adapters/chat_adapter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,14 @@ def format_assistant_message_content(
141141
outputs: dict[str, Any],
142142
missing_field_message=None,
143143
) -> str:
144-
return self.format_field_with_value(
144+
assistant_message_content = self.format_field_with_value(
145145
{
146146
FieldInfoWithName(name=k, info=v): outputs.get(k, missing_field_message)
147147
for k, v in signature.output_fields.items()
148148
},
149149
)
150+
assistant_message_content += "\n\n[[ ## completed ## ]]\n"
151+
return assistant_message_content
150152

151153
def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:
152154
sections = [(None, [])]

tests/adapters/test_chat_adapter.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ class MySignature(dspy.Signature):
244244
# 1 system message, 2 few shot examples (1 user and assistant message for each example), 1 user message
245245
assert len(messages) == 6
246246

247+
assert "[[ ## completed ## ]]\n" in messages[2]["content"]
248+
assert "[[ ## completed ## ]]\n" in messages[4]["content"]
249+
247250
assert {"type": "image_url", "image_url": {"url": "https://example.com/image1.jpg"}} in messages[1]["content"]
248251
assert {"type": "image_url", "image_url": {"url": "https://example.com/image2.jpg"}} in messages[3]["content"]
249252
assert {"type": "image_url", "image_url": {"url": "https://example.com/image3.jpg"}} in messages[5]["content"]
@@ -350,3 +353,26 @@ def get_population(country: str, year: int) -> str:
350353
# Tool arguments format should be included in the user message
351354
assert "{'city': {'type': 'string'}}" in messages[1]["content"]
352355
assert "{'country': {'type': 'string'}, 'year': {'type': 'integer'}}" in messages[1]["content"]
356+
357+
358+
def test_chat_adapter_formats_conversation_history():
359+
class MySignature(dspy.Signature):
360+
question: str = dspy.InputField()
361+
history: dspy.History = dspy.InputField()
362+
answer: str = dspy.OutputField()
363+
364+
history = dspy.History(
365+
messages=[
366+
{"question": "What is the capital of France?", "answer": "Paris"},
367+
{"question": "What is the capital of Germany?", "answer": "Berlin"},
368+
]
369+
)
370+
371+
adapter = dspy.ChatAdapter()
372+
messages = adapter.format(MySignature, [], {"question": "What is the capital of France?", "history": history})
373+
374+
assert len(messages) == 6
375+
assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?"
376+
assert messages[2]["content"] == "[[ ## answer ## ]]\nParis\n\n[[ ## completed ## ]]\n"
377+
assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?"
378+
assert messages[4]["content"] == "[[ ## answer ## ]]\nBerlin\n\n[[ ## completed ## ]]\n"

tests/adapters/test_json_adapter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,26 @@ def get_population(country: str, year: int) -> str:
419419
},
420420
},
421421
}
422+
423+
424+
def test_json_adapter_formats_conversation_history():
425+
class MySignature(dspy.Signature):
426+
question: str = dspy.InputField()
427+
history: dspy.History = dspy.InputField()
428+
answer: str = dspy.OutputField()
429+
430+
history = dspy.History(
431+
messages=[
432+
{"question": "What is the capital of France?", "answer": "Paris"},
433+
{"question": "What is the capital of Germany?", "answer": "Berlin"},
434+
]
435+
)
436+
437+
adapter = dspy.JSONAdapter()
438+
messages = adapter.format(MySignature, [], {"question": "What is the capital of France?", "history": history})
439+
440+
assert len(messages) == 6
441+
assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?"
442+
assert messages[2]["content"] == '{\n "answer": "Paris"\n}'
443+
assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?"
444+
assert messages[4]["content"] == '{\n "answer": "Berlin"\n}'

0 commit comments

Comments
 (0)