@@ -71,16 +71,15 @@ async def agent_model(
71
71
result_tools : list [ToolDefinition ],
72
72
) -> AgentModel :
73
73
return FunctionAgentModel (
74
- self .function , self .stream_function , AgentInfo (function_tools , allow_text_result , result_tools , None )
74
+ self .function ,
75
+ self .stream_function ,
76
+ AgentInfo (function_tools , allow_text_result , result_tools , None ),
75
77
)
76
78
77
79
def name (self ) -> str :
78
- labels : list [str ] = []
79
- if self .function is not None :
80
- labels .append (self .function .__name__ )
81
- if self .stream_function is not None :
82
- labels .append (f'stream-{ self .stream_function .__name__ } ' )
83
- return f'function:{ "," .join (labels )} '
80
+ function_name = self .function .__name__ if self .function is not None else ''
81
+ stream_function_name = self .stream_function .__name__ if self .stream_function is not None else ''
82
+ return f'function:{ function_name } :{ stream_function_name } '
84
83
85
84
86
85
@dataclass (frozen = True )
@@ -147,12 +146,15 @@ async def request(
147
146
agent_info = replace (self .agent_info , model_settings = model_settings )
148
147
149
148
assert self .function is not None , 'FunctionModel must receive a `function` to support non-streamed requests'
149
+ model_name = f'function:{ self .function .__name__ } '
150
+
150
151
if inspect .iscoroutinefunction (self .function ):
151
152
response = await self .function (messages , agent_info )
152
153
else :
153
154
response_ = await _utils .run_in_executor (self .function , messages , agent_info )
154
155
assert isinstance (response_ , ModelResponse ), response_
155
156
response = response_
157
+ response .model_name = model_name
156
158
# TODO is `messages` right here? Should it just be new messages?
157
159
return response , _estimate_usage (chain (messages , [response ]))
158
160
@@ -163,13 +165,15 @@ async def request_stream(
163
165
assert (
164
166
self .stream_function is not None
165
167
), 'FunctionModel must receive a `stream_function` to support streamed requests'
168
+ model_name = f'function:{ self .stream_function .__name__ } '
169
+
166
170
response_stream = PeekableAsyncStream (self .stream_function (messages , self .agent_info ))
167
171
168
172
first = await response_stream .peek ()
169
173
if isinstance (first , _utils .Unset ):
170
174
raise ValueError ('Stream function must return at least one item' )
171
175
172
- yield FunctionStreamedResponse (response_stream )
176
+ yield FunctionStreamedResponse (_model_name = model_name , _iter = response_stream )
173
177
174
178
175
179
@dataclass
0 commit comments