mirascope 1.21.3__py3-none-any.whl → 1.21.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mirascope/llm/_call.py +51 -36
- mirascope/llm/_context.py +8 -5
- mirascope/llm/call_response.py +6 -0
- {mirascope-1.21.3.dist-info → mirascope-1.21.5.dist-info}/METADATA +1 -1
- {mirascope-1.21.3.dist-info → mirascope-1.21.5.dist-info}/RECORD +7 -7
- {mirascope-1.21.3.dist-info → mirascope-1.21.5.dist-info}/WHEEL +0 -0
- {mirascope-1.21.3.dist-info → mirascope-1.21.5.dist-info}/licenses/LICENSE +0 -0
mirascope/llm/_call.py
CHANGED
|
@@ -20,7 +20,11 @@ from ..core.base import (
|
|
|
20
20
|
from ..core.base._utils import fn_is_async
|
|
21
21
|
from ..core.base.stream_config import StreamConfig
|
|
22
22
|
from ..core.base.types import LocalProvider, Provider
|
|
23
|
-
from ._context import
|
|
23
|
+
from ._context import (
|
|
24
|
+
CallArgs,
|
|
25
|
+
apply_context_overrides_to_call_args,
|
|
26
|
+
get_current_context,
|
|
27
|
+
)
|
|
24
28
|
from ._protocols import (
|
|
25
29
|
AsyncLLMFunctionDecorator,
|
|
26
30
|
CallDecorator,
|
|
@@ -235,55 +239,66 @@ def _call(
|
|
|
235
239
|
| Awaitable[(_ResponseModelT | CallResponse)],
|
|
236
240
|
]:
|
|
237
241
|
if fn_is_async(fn):
|
|
238
|
-
|
|
242
|
+
# Create a wrapper function that captures the current context when called
|
|
239
243
|
@wraps(fn)
|
|
240
|
-
|
|
244
|
+
def wrapper_with_context(
|
|
241
245
|
*args: _P.args, **kwargs: _P.kwargs
|
|
242
|
-
) ->
|
|
246
|
+
) -> Awaitable[
|
|
243
247
|
CallResponse
|
|
244
248
|
| Stream
|
|
245
249
|
| _ResponseModelT
|
|
246
250
|
| _ParsedOutputT
|
|
247
251
|
| (_ResponseModelT | CallResponse)
|
|
248
|
-
|
|
249
|
-
#
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
252
|
+
]:
|
|
253
|
+
# Capture the context at call time
|
|
254
|
+
current_context = get_current_context()
|
|
255
|
+
|
|
256
|
+
# Define an async function that uses the captured context
|
|
257
|
+
async def context_bound_inner_async() -> (
|
|
258
|
+
CallResponse
|
|
259
|
+
| Stream
|
|
260
|
+
| _ResponseModelT
|
|
261
|
+
| _ParsedOutputT
|
|
262
|
+
| (_ResponseModelT | CallResponse)
|
|
263
|
+
):
|
|
264
|
+
# Apply any context overrides to the original call args
|
|
265
|
+
effective_call_args = apply_context_overrides_to_call_args(
|
|
266
|
+
original_call_args, context_override=current_context
|
|
267
|
+
)
|
|
253
268
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
269
|
+
# Get the appropriate provider call function with the possibly overridden provider
|
|
270
|
+
effective_provider = effective_call_args["provider"]
|
|
271
|
+
effective_client = effective_call_args["client"]
|
|
257
272
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
273
|
+
if effective_provider in get_args(LocalProvider):
|
|
274
|
+
provider_call, effective_client = _get_local_provider_call(
|
|
275
|
+
cast(LocalProvider, effective_provider),
|
|
276
|
+
effective_client,
|
|
277
|
+
True,
|
|
278
|
+
)
|
|
279
|
+
effective_call_args["client"] = effective_client
|
|
280
|
+
else:
|
|
281
|
+
provider_call = _get_provider_call(
|
|
282
|
+
cast(Provider, effective_provider)
|
|
283
|
+
)
|
|
269
284
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
"provider"
|
|
274
|
-
] # Remove provider as it's not a parameter to provider_call
|
|
285
|
+
# Use the provider-specific call function with overridden args
|
|
286
|
+
call_kwargs = dict(effective_call_args)
|
|
287
|
+
del call_kwargs["provider"] # Not a parameter to provider_call
|
|
275
288
|
|
|
276
|
-
|
|
277
|
-
|
|
289
|
+
# Get decorated function using provider_call
|
|
290
|
+
decorated = provider_call(**call_kwargs)(fn)
|
|
278
291
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
292
|
+
# Call the decorated function and wrap the result
|
|
293
|
+
result = await decorated(*args, **kwargs)
|
|
294
|
+
return _wrap_result(result)
|
|
295
|
+
|
|
296
|
+
return context_bound_inner_async()
|
|
282
297
|
|
|
283
|
-
|
|
284
|
-
|
|
298
|
+
wrapper_with_context._original_call_args = original_call_args # pyright: ignore [reportAttributeAccessIssue]
|
|
299
|
+
wrapper_with_context._original_fn = fn # pyright: ignore [reportAttributeAccessIssue]
|
|
285
300
|
|
|
286
|
-
return
|
|
301
|
+
return wrapper_with_context # pyright: ignore [reportReturnType]
|
|
287
302
|
else:
|
|
288
303
|
|
|
289
304
|
@wraps(fn)
|
mirascope/llm/_context.py
CHANGED
|
@@ -114,10 +114,10 @@ def _context(
|
|
|
114
114
|
client: Any | None = None, # noqa: ANN401
|
|
115
115
|
call_params: CommonCallParams | Any | None = None, # noqa: ANN401
|
|
116
116
|
) -> LLMContext:
|
|
117
|
-
"""Context manager for
|
|
117
|
+
"""Context manager for LLM API calls.
|
|
118
118
|
|
|
119
119
|
This is an internal method that allows both setting and structural overrides
|
|
120
|
-
for
|
|
120
|
+
for LLM functions.
|
|
121
121
|
|
|
122
122
|
Unfortunately we have not yet identified a way to properly type hint this because
|
|
123
123
|
providing no structural overrides means the return type is that of the original
|
|
@@ -138,7 +138,7 @@ def _context(
|
|
|
138
138
|
client: The client to use for the LLM API call.
|
|
139
139
|
call_params: The call parameters for the LLM API call.
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
Returns:
|
|
142
142
|
The context object that can be used to apply the context to a function.
|
|
143
143
|
"""
|
|
144
144
|
old_context: LLMContext | None = getattr(_current_context_local, "context", None)
|
|
@@ -171,16 +171,19 @@ def _context(
|
|
|
171
171
|
)
|
|
172
172
|
|
|
173
173
|
|
|
174
|
-
def apply_context_overrides_to_call_args(
|
|
174
|
+
def apply_context_overrides_to_call_args(
|
|
175
|
+
call_args: CallArgs, context_override: LLMContext | None = None
|
|
176
|
+
) -> CallArgs:
|
|
175
177
|
"""Apply any active context overrides to the call arguments.
|
|
176
178
|
|
|
177
179
|
Args:
|
|
178
180
|
call_args: The original call arguments.
|
|
181
|
+
context_override: Optional explicit context to use instead of the current thread context.
|
|
179
182
|
|
|
180
183
|
Returns:
|
|
181
184
|
The call arguments with any context overrides applied.
|
|
182
185
|
"""
|
|
183
|
-
context = get_current_context()
|
|
186
|
+
context = context_override or get_current_context()
|
|
184
187
|
if not context:
|
|
185
188
|
return call_args
|
|
186
189
|
|
mirascope/llm/call_response.py
CHANGED
|
@@ -64,6 +64,7 @@ class CallResponse(
|
|
|
64
64
|
def __getattribute__(self, name: str) -> Any: # noqa: ANN401
|
|
65
65
|
special_names = {
|
|
66
66
|
"_response",
|
|
67
|
+
"common_messages",
|
|
67
68
|
"finish_reasons",
|
|
68
69
|
"usage",
|
|
69
70
|
"message_param",
|
|
@@ -113,6 +114,11 @@ class CallResponse(
|
|
|
113
114
|
def message_param(self) -> BaseMessageParam:
|
|
114
115
|
return self._response.common_message_param # pyright: ignore [reportReturnType]
|
|
115
116
|
|
|
117
|
+
@computed_field
|
|
118
|
+
@cached_property
|
|
119
|
+
def common_messages(self) -> list[BaseMessageParam]: # pyright: ignore [reportIncompatibleMethodOverride]
|
|
120
|
+
return self._response.common_messages
|
|
121
|
+
|
|
116
122
|
@cached_property
|
|
117
123
|
def tools(self) -> list[Tool] | None: # pyright: ignore [reportIncompatibleVariableOverride]
|
|
118
124
|
return self._response.common_tools
|
|
@@ -332,12 +332,12 @@ mirascope/integrations/otel/_utils.py,sha256=SCVb3MpcpqLpCpumJEbEdINceNdusnyT6iu
|
|
|
332
332
|
mirascope/integrations/otel/_with_hyperdx.py,sha256=f17uxXQk5zZPtyj6zwPwJz5i7atsnUPOoq1LqT8JO0E,1637
|
|
333
333
|
mirascope/integrations/otel/_with_otel.py,sha256=tbjd6BEbcSfnsm5CWHBoHwbRNrHt6-t4or-SYGQSD-w,1659
|
|
334
334
|
mirascope/llm/__init__.py,sha256=zo7QD39zQ6HIU_N2yHrzd6bbcT9YmlikZ84JN_r5k4E,397
|
|
335
|
-
mirascope/llm/_call.py,sha256=
|
|
336
|
-
mirascope/llm/_context.py,sha256=
|
|
335
|
+
mirascope/llm/_call.py,sha256=zPULSy5GLlJszphRvc3WUSFEsmP83Abp0FzbZefTa8Y,13848
|
|
336
|
+
mirascope/llm/_context.py,sha256=vtHJkLlFfUwyR_hYEHXAw3xunpHhLn67k4kuFw50GR8,12481
|
|
337
337
|
mirascope/llm/_override.py,sha256=m4MdOhM-aJRIGP7NBJhscq3ISNct6FBPn3jjmryFo_Q,112292
|
|
338
338
|
mirascope/llm/_protocols.py,sha256=HXspRAC0PduGqbh2BM0CGe5iVj7CC3ZKMPAYvFvbDNQ,16406
|
|
339
339
|
mirascope/llm/_response_metaclass.py,sha256=6DLQb5IrqMldyEXHT_pAsr2DlUVc9CmZuZiBXG37WK8,851
|
|
340
|
-
mirascope/llm/call_response.py,sha256=
|
|
340
|
+
mirascope/llm/call_response.py,sha256=4w_2Dgt325lEjMaFlg7Iq2AGp8cI0uSaqCa8MR_S5GY,5171
|
|
341
341
|
mirascope/llm/call_response_chunk.py,sha256=bZwO43ipc6PO1VLgGSaAPRqCIUyZD_Ty5oxdJX62yno,1966
|
|
342
342
|
mirascope/llm/stream.py,sha256=GtUKyLBlKqqZTOKjdL9FLInCXJ0ZOEAe6nymbjKwyTQ,5293
|
|
343
343
|
mirascope/llm/tool.py,sha256=MQRJBPhP1d-OyOz3PE_VsKmSXca0chySyYO1U9OW8ck,1824
|
|
@@ -368,7 +368,7 @@ mirascope/v0/base/ops_utils.py,sha256=1Qq-VIwgHBaYutiZsS2MUQ4OgPC3APyywI5bTiTAmA
|
|
|
368
368
|
mirascope/v0/base/prompts.py,sha256=FM2Yz98cSnDceYogiwPrp4BALf3_F3d4fIOCGAkd-SE,1298
|
|
369
369
|
mirascope/v0/base/types.py,sha256=ZfatJoX0Yl0e3jhv0D_MhiSVHLYUeJsdN3um3iE10zY,352
|
|
370
370
|
mirascope/v0/base/utils.py,sha256=XREPENRQTu8gpMhHU8RC8qH_am3FfGUvY-dJ6x8i-mw,681
|
|
371
|
-
mirascope-1.21.
|
|
372
|
-
mirascope-1.21.
|
|
373
|
-
mirascope-1.21.
|
|
374
|
-
mirascope-1.21.
|
|
371
|
+
mirascope-1.21.5.dist-info/METADATA,sha256=bvRDe9mKqx1E8dnJnoEz5KQ4FjHkPjMp3rIkMVCzWrc,8730
|
|
372
|
+
mirascope-1.21.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
373
|
+
mirascope-1.21.5.dist-info/licenses/LICENSE,sha256=LAs5Q8mdawTsVdONpDGukwsoc4KEUBmmonDEL39b23Y,1072
|
|
374
|
+
mirascope-1.21.5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|