4
4
5
5
import numpy as np
6
6
7
- from dspy .adapters .chat_adapter import ChatAdapter , FieldInfoWithName , field_header_pattern
7
+ from dspy .adapters .chat_adapter import FieldInfoWithName , field_header_pattern
8
8
from dspy .clients .lm import LM
9
9
from dspy .dsp .utils .utils import dotdict
10
10
from dspy .signatures .field import OutputField
@@ -67,13 +67,19 @@ class DummyLM(LM):
67
67
68
68
"""
69
69
70
- def __init__ (self , answers : list [dict [str , str ]] | dict [str , dict [str , str ]], follow_examples : bool = False ):
70
+ def __init__ (self , answers : list [dict [str , str ]] | dict [str , dict [str , str ]], follow_examples : bool = False , adapter = None ):
71
71
super ().__init__ ("dummy" , "chat" , 0.0 , 1000 , True )
72
72
self .answers = answers
73
73
if isinstance (answers , list ):
74
74
self .answers = iter (answers )
75
75
self .follow_examples = follow_examples
76
76
77
+ # Set adapter, defaulting to ChatAdapter
78
+ if adapter is None :
79
+ from dspy .adapters .chat_adapter import ChatAdapter
80
+ adapter = ChatAdapter ()
81
+ self .adapter = adapter
82
+
77
83
def _use_example (self , messages ):
78
84
# find all field names
79
85
fields = defaultdict (int )
@@ -94,12 +100,20 @@ def _use_example(self, messages):
94
100
@with_callbacks
95
101
def __call__ (self , prompt = None , messages = None , ** kwargs ):
96
102
def format_answer_fields (field_names_and_values : dict [str , Any ]):
97
- return ChatAdapter ().format_field_with_value (
98
- fields_with_values = {
99
- FieldInfoWithName (name = field_name , info = OutputField ()): value
100
- for field_name , value in field_names_and_values .items ()
101
- }
102
- )
103
+ fields_with_values = {
104
+ FieldInfoWithName (name = field_name , info = OutputField ()): value
105
+ for field_name , value in field_names_and_values .items ()
106
+ }
107
+ # The reason why DummyLM needs an adapter is because it needs to know which output format to mimic.
108
+ # Normally LMs should not have any knowledge of an adapter, because the output format is defined in the prompt.
109
+ adapter = self .adapter
110
+
111
+ # Try to use role="assistant" if the adapter supports it (like JSONAdapter)
112
+ try :
113
+ return adapter .format_field_with_value (fields_with_values , role = "assistant" )
114
+ except TypeError :
115
+ # Fallback for adapters that don't support role parameter (like ChatAdapter)
116
+ return adapter .format_field_with_value (fields_with_values )
103
117
104
118
# Build the request.
105
119
outputs = []
0 commit comments