mirascope 2.0.0a3__py3-none-any.whl → 2.0.0a4__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/api/_generated/__init__.py +62 -6
- mirascope/api/_generated/client.py +8 -0
- mirascope/api/_generated/errors/__init__.py +11 -1
- mirascope/api/_generated/errors/conflict_error.py +15 -0
- mirascope/api/_generated/errors/forbidden_error.py +15 -0
- mirascope/api/_generated/errors/internal_server_error.py +15 -0
- mirascope/api/_generated/errors/not_found_error.py +15 -0
- mirascope/api/_generated/organizations/__init__.py +25 -0
- mirascope/api/_generated/organizations/client.py +380 -0
- mirascope/api/_generated/organizations/raw_client.py +876 -0
- mirascope/api/_generated/organizations/types/__init__.py +23 -0
- mirascope/api/_generated/organizations/types/organizations_create_response.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_create_response_role.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_get_response.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_get_response_role.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_list_response_item.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_list_response_item_role.py +7 -0
- mirascope/api/_generated/organizations/types/organizations_update_response.py +24 -0
- mirascope/api/_generated/organizations/types/organizations_update_response_role.py +7 -0
- mirascope/api/_generated/projects/__init__.py +17 -0
- mirascope/api/_generated/projects/client.py +458 -0
- mirascope/api/_generated/projects/raw_client.py +1016 -0
- mirascope/api/_generated/projects/types/__init__.py +15 -0
- mirascope/api/_generated/projects/types/projects_create_response.py +30 -0
- mirascope/api/_generated/projects/types/projects_get_response.py +30 -0
- mirascope/api/_generated/projects/types/projects_list_response_item.py +30 -0
- mirascope/api/_generated/projects/types/projects_update_response.py +30 -0
- mirascope/api/_generated/reference.md +586 -0
- mirascope/api/_generated/types/__init__.py +20 -4
- mirascope/api/_generated/types/already_exists_error.py +24 -0
- mirascope/api/_generated/types/already_exists_error_tag.py +5 -0
- mirascope/api/_generated/types/database_error.py +24 -0
- mirascope/api/_generated/types/database_error_tag.py +5 -0
- mirascope/api/_generated/types/http_api_decode_error.py +1 -3
- mirascope/api/_generated/types/issue.py +1 -5
- mirascope/api/_generated/types/not_found_error_body.py +24 -0
- mirascope/api/_generated/types/not_found_error_tag.py +5 -0
- mirascope/api/_generated/types/permission_denied_error.py +24 -0
- mirascope/api/_generated/types/permission_denied_error_tag.py +7 -0
- mirascope/api/_generated/types/property_key.py +2 -2
- mirascope/api/_generated/types/{property_key_tag.py → property_key_key.py} +3 -5
- mirascope/api/_generated/types/{property_key_tag_tag.py → property_key_key_tag.py} +1 -1
- mirascope/llm/__init__.py +4 -0
- mirascope/llm/providers/__init__.py +6 -0
- mirascope/llm/providers/anthropic/__init__.py +6 -1
- mirascope/llm/providers/anthropic/_utils/__init__.py +15 -5
- mirascope/llm/providers/anthropic/_utils/beta_decode.py +271 -0
- mirascope/llm/providers/anthropic/_utils/beta_encode.py +216 -0
- mirascope/llm/providers/anthropic/_utils/decode.py +39 -7
- mirascope/llm/providers/anthropic/_utils/encode.py +156 -64
- mirascope/llm/providers/anthropic/beta_provider.py +322 -0
- mirascope/llm/providers/anthropic/model_id.py +10 -27
- mirascope/llm/providers/anthropic/model_info.py +87 -0
- mirascope/llm/providers/anthropic/provider.py +127 -145
- mirascope/llm/providers/base/_utils.py +15 -1
- mirascope/llm/providers/google/_utils/decode.py +55 -3
- mirascope/llm/providers/google/_utils/encode.py +14 -6
- mirascope/llm/providers/google/model_id.py +7 -13
- mirascope/llm/providers/google/model_info.py +62 -0
- mirascope/llm/providers/google/provider.py +8 -4
- mirascope/llm/providers/load_provider.py +8 -2
- mirascope/llm/providers/mlx/_utils.py +23 -1
- mirascope/llm/providers/mlx/encoding/transformers.py +17 -1
- mirascope/llm/providers/mlx/provider.py +4 -0
- mirascope/llm/providers/ollama/__init__.py +19 -0
- mirascope/llm/providers/ollama/provider.py +71 -0
- mirascope/llm/providers/openai/completions/__init__.py +6 -1
- mirascope/llm/providers/openai/completions/_utils/decode.py +57 -5
- mirascope/llm/providers/openai/completions/_utils/encode.py +9 -8
- mirascope/llm/providers/openai/completions/base_provider.py +513 -0
- mirascope/llm/providers/openai/completions/provider.py +13 -447
- mirascope/llm/providers/openai/model_info.py +57 -0
- mirascope/llm/providers/openai/provider.py +16 -4
- mirascope/llm/providers/openai/responses/_utils/decode.py +55 -4
- mirascope/llm/providers/openai/responses/_utils/encode.py +9 -9
- mirascope/llm/providers/openai/responses/provider.py +20 -21
- mirascope/llm/providers/provider_id.py +11 -1
- mirascope/llm/providers/provider_registry.py +3 -1
- mirascope/llm/providers/together/__init__.py +19 -0
- mirascope/llm/providers/together/provider.py +40 -0
- mirascope/llm/responses/__init__.py +3 -0
- mirascope/llm/responses/base_response.py +4 -0
- mirascope/llm/responses/base_stream_response.py +25 -1
- mirascope/llm/responses/finish_reason.py +1 -0
- mirascope/llm/responses/response.py +9 -0
- mirascope/llm/responses/root_response.py +5 -1
- mirascope/llm/responses/usage.py +95 -0
- {mirascope-2.0.0a3.dist-info → mirascope-2.0.0a4.dist-info}/METADATA +3 -3
- {mirascope-2.0.0a3.dist-info → mirascope-2.0.0a4.dist-info}/RECORD +91 -50
- mirascope/llm/providers/openai/shared/__init__.py +0 -7
- mirascope/llm/providers/openai/shared/_utils.py +0 -59
- {mirascope-2.0.0a3.dist-info → mirascope-2.0.0a4.dist-info}/WHEEL +0 -0
- {mirascope-2.0.0a3.dist-info → mirascope-2.0.0a4.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
"""Base class for OpenAI Completions-compatible providers."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
from typing import ClassVar
|
|
6
|
+
from typing_extensions import Unpack
|
|
7
|
+
|
|
8
|
+
from openai import AsyncOpenAI, OpenAI
|
|
9
|
+
|
|
10
|
+
from ....context import Context, DepsT
|
|
11
|
+
from ....formatting import Format, FormattableT
|
|
12
|
+
from ....messages import Message
|
|
13
|
+
from ....responses import (
|
|
14
|
+
AsyncContextResponse,
|
|
15
|
+
AsyncContextStreamResponse,
|
|
16
|
+
AsyncResponse,
|
|
17
|
+
AsyncStreamResponse,
|
|
18
|
+
ContextResponse,
|
|
19
|
+
ContextStreamResponse,
|
|
20
|
+
Response,
|
|
21
|
+
StreamResponse,
|
|
22
|
+
)
|
|
23
|
+
from ....tools import (
|
|
24
|
+
AsyncContextTool,
|
|
25
|
+
AsyncContextToolkit,
|
|
26
|
+
AsyncTool,
|
|
27
|
+
AsyncToolkit,
|
|
28
|
+
ContextTool,
|
|
29
|
+
ContextToolkit,
|
|
30
|
+
Tool,
|
|
31
|
+
Toolkit,
|
|
32
|
+
)
|
|
33
|
+
from ...base import BaseProvider, Params
|
|
34
|
+
from ..model_id import model_name as openai_model_name
|
|
35
|
+
from . import _utils
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class BaseOpenAICompletionsProvider(BaseProvider[OpenAI]):
|
|
39
|
+
"""Base class for providers that use OpenAI Completions-compatible APIs."""
|
|
40
|
+
|
|
41
|
+
id: ClassVar[str]
|
|
42
|
+
default_scope: ClassVar[str | list[str]]
|
|
43
|
+
default_base_url: ClassVar[str | None] = None
|
|
44
|
+
api_key_env_var: ClassVar[str]
|
|
45
|
+
api_key_required: ClassVar[bool] = True
|
|
46
|
+
provider_name: ClassVar[str | None] = None
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
*,
|
|
51
|
+
api_key: str | None = None,
|
|
52
|
+
base_url: str | None = None,
|
|
53
|
+
) -> None:
|
|
54
|
+
"""Initialize the OpenAI Completions-compatible provider client."""
|
|
55
|
+
resolved_api_key = api_key or os.environ.get(self.api_key_env_var)
|
|
56
|
+
|
|
57
|
+
if self.api_key_required and not resolved_api_key:
|
|
58
|
+
name = self.provider_name or self.id.split(":")[0].capitalize()
|
|
59
|
+
raise ValueError(
|
|
60
|
+
f"{name} API key is required. "
|
|
61
|
+
f"Set the {self.api_key_env_var} environment variable "
|
|
62
|
+
f"or pass the api_key parameter to register_provider()."
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
resolved_base_url = base_url or self.default_base_url
|
|
66
|
+
|
|
67
|
+
effective_api_key: str | None = resolved_api_key
|
|
68
|
+
if resolved_base_url is not None and not effective_api_key:
|
|
69
|
+
effective_api_key = "not-needed"
|
|
70
|
+
|
|
71
|
+
self.client = OpenAI(
|
|
72
|
+
api_key=effective_api_key,
|
|
73
|
+
base_url=resolved_base_url,
|
|
74
|
+
)
|
|
75
|
+
self.async_client = AsyncOpenAI(
|
|
76
|
+
api_key=effective_api_key,
|
|
77
|
+
base_url=resolved_base_url,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
def _model_name(self, model_id: str) -> str:
|
|
81
|
+
"""Extract the model name to send to the API."""
|
|
82
|
+
return openai_model_name(model_id, None)
|
|
83
|
+
|
|
84
|
+
def _provider_model_name(self, model_id: str) -> str:
|
|
85
|
+
"""Get the model name for tracking in Response."""
|
|
86
|
+
return self._model_name(model_id)
|
|
87
|
+
|
|
88
|
+
def _call(
|
|
89
|
+
self,
|
|
90
|
+
*,
|
|
91
|
+
model_id: str,
|
|
92
|
+
messages: Sequence[Message],
|
|
93
|
+
tools: Sequence[Tool] | Toolkit | None = None,
|
|
94
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
95
|
+
**params: Unpack[Params],
|
|
96
|
+
) -> Response | Response[FormattableT]:
|
|
97
|
+
"""Generate an `llm.Response` by synchronously calling the API.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
model_id: Model identifier to use.
|
|
101
|
+
messages: Messages to send to the LLM.
|
|
102
|
+
tools: Optional tools that the model may invoke.
|
|
103
|
+
format: Optional response format specifier.
|
|
104
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
An `llm.Response` object containing the LLM-generated content.
|
|
108
|
+
"""
|
|
109
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
110
|
+
model_id=model_id,
|
|
111
|
+
messages=messages,
|
|
112
|
+
tools=tools,
|
|
113
|
+
format=format,
|
|
114
|
+
params=params,
|
|
115
|
+
)
|
|
116
|
+
kwargs["model"] = self._model_name(model_id)
|
|
117
|
+
|
|
118
|
+
openai_response = self.client.chat.completions.create(**kwargs)
|
|
119
|
+
|
|
120
|
+
assistant_message, finish_reason, usage = _utils.decode_response(
|
|
121
|
+
openai_response,
|
|
122
|
+
model_id,
|
|
123
|
+
self.id,
|
|
124
|
+
self._provider_model_name(model_id),
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
return Response(
|
|
128
|
+
raw=openai_response,
|
|
129
|
+
provider_id=self.id,
|
|
130
|
+
model_id=model_id,
|
|
131
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
132
|
+
params=params,
|
|
133
|
+
tools=tools,
|
|
134
|
+
input_messages=input_messages,
|
|
135
|
+
assistant_message=assistant_message,
|
|
136
|
+
finish_reason=finish_reason,
|
|
137
|
+
usage=usage,
|
|
138
|
+
format=format,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
def _context_call(
|
|
142
|
+
self,
|
|
143
|
+
*,
|
|
144
|
+
ctx: Context[DepsT],
|
|
145
|
+
model_id: str,
|
|
146
|
+
messages: Sequence[Message],
|
|
147
|
+
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
148
|
+
| ContextToolkit[DepsT]
|
|
149
|
+
| None = None,
|
|
150
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
151
|
+
**params: Unpack[Params],
|
|
152
|
+
) -> ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]:
|
|
153
|
+
"""Generate an `llm.ContextResponse` by synchronously calling the API.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
ctx: Context object with dependencies for tools.
|
|
157
|
+
model_id: Model identifier to use.
|
|
158
|
+
messages: Messages to send to the LLM.
|
|
159
|
+
tools: Optional tools that the model may invoke.
|
|
160
|
+
format: Optional response format specifier.
|
|
161
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
An `llm.ContextResponse` object containing the LLM-generated content.
|
|
165
|
+
"""
|
|
166
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
167
|
+
model_id=model_id,
|
|
168
|
+
messages=messages,
|
|
169
|
+
tools=tools,
|
|
170
|
+
format=format,
|
|
171
|
+
params=params,
|
|
172
|
+
)
|
|
173
|
+
kwargs["model"] = self._model_name(model_id)
|
|
174
|
+
|
|
175
|
+
openai_response = self.client.chat.completions.create(**kwargs)
|
|
176
|
+
|
|
177
|
+
assistant_message, finish_reason, usage = _utils.decode_response(
|
|
178
|
+
openai_response,
|
|
179
|
+
model_id,
|
|
180
|
+
self.id,
|
|
181
|
+
self._provider_model_name(model_id),
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return ContextResponse(
|
|
185
|
+
raw=openai_response,
|
|
186
|
+
provider_id=self.id,
|
|
187
|
+
model_id=model_id,
|
|
188
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
189
|
+
params=params,
|
|
190
|
+
tools=tools,
|
|
191
|
+
input_messages=input_messages,
|
|
192
|
+
assistant_message=assistant_message,
|
|
193
|
+
finish_reason=finish_reason,
|
|
194
|
+
usage=usage,
|
|
195
|
+
format=format,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
async def _call_async(
|
|
199
|
+
self,
|
|
200
|
+
*,
|
|
201
|
+
model_id: str,
|
|
202
|
+
messages: Sequence[Message],
|
|
203
|
+
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
204
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
205
|
+
**params: Unpack[Params],
|
|
206
|
+
) -> AsyncResponse | AsyncResponse[FormattableT]:
|
|
207
|
+
"""Generate an `llm.AsyncResponse` by asynchronously calling the API.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
model_id: Model identifier to use.
|
|
211
|
+
messages: Messages to send to the LLM.
|
|
212
|
+
tools: Optional tools that the model may invoke.
|
|
213
|
+
format: Optional response format specifier.
|
|
214
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
215
|
+
|
|
216
|
+
Returns:
|
|
217
|
+
An `llm.AsyncResponse` object containing the LLM-generated content.
|
|
218
|
+
"""
|
|
219
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
220
|
+
model_id=model_id,
|
|
221
|
+
params=params,
|
|
222
|
+
messages=messages,
|
|
223
|
+
tools=tools,
|
|
224
|
+
format=format,
|
|
225
|
+
)
|
|
226
|
+
kwargs["model"] = self._model_name(model_id)
|
|
227
|
+
|
|
228
|
+
openai_response = await self.async_client.chat.completions.create(**kwargs)
|
|
229
|
+
|
|
230
|
+
assistant_message, finish_reason, usage = _utils.decode_response(
|
|
231
|
+
openai_response,
|
|
232
|
+
model_id,
|
|
233
|
+
self.id,
|
|
234
|
+
self._provider_model_name(model_id),
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
return AsyncResponse(
|
|
238
|
+
raw=openai_response,
|
|
239
|
+
provider_id=self.id,
|
|
240
|
+
model_id=model_id,
|
|
241
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
242
|
+
params=params,
|
|
243
|
+
tools=tools,
|
|
244
|
+
input_messages=input_messages,
|
|
245
|
+
assistant_message=assistant_message,
|
|
246
|
+
finish_reason=finish_reason,
|
|
247
|
+
usage=usage,
|
|
248
|
+
format=format,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
async def _context_call_async(
|
|
252
|
+
self,
|
|
253
|
+
*,
|
|
254
|
+
ctx: Context[DepsT],
|
|
255
|
+
model_id: str,
|
|
256
|
+
messages: Sequence[Message],
|
|
257
|
+
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
258
|
+
| AsyncContextToolkit[DepsT]
|
|
259
|
+
| None = None,
|
|
260
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
261
|
+
**params: Unpack[Params],
|
|
262
|
+
) -> AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]:
|
|
263
|
+
"""Generate an `llm.AsyncContextResponse` by asynchronously calling the API.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
ctx: Context object with dependencies for tools.
|
|
267
|
+
model_id: Model identifier to use.
|
|
268
|
+
messages: Messages to send to the LLM.
|
|
269
|
+
tools: Optional tools that the model may invoke.
|
|
270
|
+
format: Optional response format specifier.
|
|
271
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
An `llm.AsyncContextResponse` object containing the LLM-generated content.
|
|
275
|
+
"""
|
|
276
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
277
|
+
model_id=model_id,
|
|
278
|
+
params=params,
|
|
279
|
+
messages=messages,
|
|
280
|
+
tools=tools,
|
|
281
|
+
format=format,
|
|
282
|
+
)
|
|
283
|
+
kwargs["model"] = self._model_name(model_id)
|
|
284
|
+
|
|
285
|
+
openai_response = await self.async_client.chat.completions.create(**kwargs)
|
|
286
|
+
|
|
287
|
+
assistant_message, finish_reason, usage = _utils.decode_response(
|
|
288
|
+
openai_response,
|
|
289
|
+
model_id,
|
|
290
|
+
self.id,
|
|
291
|
+
self._provider_model_name(model_id),
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
return AsyncContextResponse(
|
|
295
|
+
raw=openai_response,
|
|
296
|
+
provider_id=self.id,
|
|
297
|
+
model_id=model_id,
|
|
298
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
299
|
+
params=params,
|
|
300
|
+
tools=tools,
|
|
301
|
+
input_messages=input_messages,
|
|
302
|
+
assistant_message=assistant_message,
|
|
303
|
+
finish_reason=finish_reason,
|
|
304
|
+
usage=usage,
|
|
305
|
+
format=format,
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
def _stream(
|
|
309
|
+
self,
|
|
310
|
+
*,
|
|
311
|
+
model_id: str,
|
|
312
|
+
messages: Sequence[Message],
|
|
313
|
+
tools: Sequence[Tool] | Toolkit | None = None,
|
|
314
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
315
|
+
**params: Unpack[Params],
|
|
316
|
+
) -> StreamResponse | StreamResponse[FormattableT]:
|
|
317
|
+
"""Generate an `llm.StreamResponse` by synchronously streaming from the API.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
model_id: Model identifier to use.
|
|
321
|
+
messages: Messages to send to the LLM.
|
|
322
|
+
tools: Optional tools that the model may invoke.
|
|
323
|
+
format: Optional response format specifier.
|
|
324
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
325
|
+
|
|
326
|
+
Returns:
|
|
327
|
+
An `llm.StreamResponse` object for iterating over the LLM-generated content.
|
|
328
|
+
"""
|
|
329
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
330
|
+
model_id=model_id,
|
|
331
|
+
messages=messages,
|
|
332
|
+
tools=tools,
|
|
333
|
+
format=format,
|
|
334
|
+
params=params,
|
|
335
|
+
)
|
|
336
|
+
kwargs["model"] = self._model_name(model_id)
|
|
337
|
+
|
|
338
|
+
openai_stream = self.client.chat.completions.create(
|
|
339
|
+
**kwargs,
|
|
340
|
+
stream=True,
|
|
341
|
+
stream_options={"include_usage": True},
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
chunk_iterator = _utils.decode_stream(openai_stream)
|
|
345
|
+
|
|
346
|
+
return StreamResponse(
|
|
347
|
+
provider_id=self.id,
|
|
348
|
+
model_id=model_id,
|
|
349
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
350
|
+
params=params,
|
|
351
|
+
tools=tools,
|
|
352
|
+
input_messages=input_messages,
|
|
353
|
+
chunk_iterator=chunk_iterator,
|
|
354
|
+
format=format,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
def _context_stream(
|
|
358
|
+
self,
|
|
359
|
+
*,
|
|
360
|
+
ctx: Context[DepsT],
|
|
361
|
+
model_id: str,
|
|
362
|
+
messages: Sequence[Message],
|
|
363
|
+
tools: Sequence[Tool | ContextTool[DepsT]]
|
|
364
|
+
| ContextToolkit[DepsT]
|
|
365
|
+
| None = None,
|
|
366
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
367
|
+
**params: Unpack[Params],
|
|
368
|
+
) -> ContextStreamResponse[DepsT] | ContextStreamResponse[DepsT, FormattableT]:
|
|
369
|
+
"""Generate an `llm.ContextStreamResponse` by synchronously streaming from the API.
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
ctx: Context object with dependencies for tools.
|
|
373
|
+
model_id: Model identifier to use.
|
|
374
|
+
messages: Messages to send to the LLM.
|
|
375
|
+
tools: Optional tools that the model may invoke.
|
|
376
|
+
format: Optional response format specifier.
|
|
377
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
378
|
+
|
|
379
|
+
Returns:
|
|
380
|
+
An `llm.ContextStreamResponse` object for iterating over the LLM-generated content.
|
|
381
|
+
"""
|
|
382
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
383
|
+
model_id=model_id,
|
|
384
|
+
messages=messages,
|
|
385
|
+
tools=tools,
|
|
386
|
+
format=format,
|
|
387
|
+
params=params,
|
|
388
|
+
)
|
|
389
|
+
kwargs["model"] = self._model_name(model_id)
|
|
390
|
+
|
|
391
|
+
openai_stream = self.client.chat.completions.create(
|
|
392
|
+
**kwargs,
|
|
393
|
+
stream=True,
|
|
394
|
+
stream_options={"include_usage": True},
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
chunk_iterator = _utils.decode_stream(openai_stream)
|
|
398
|
+
|
|
399
|
+
return ContextStreamResponse(
|
|
400
|
+
provider_id=self.id,
|
|
401
|
+
model_id=model_id,
|
|
402
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
403
|
+
params=params,
|
|
404
|
+
tools=tools,
|
|
405
|
+
input_messages=input_messages,
|
|
406
|
+
chunk_iterator=chunk_iterator,
|
|
407
|
+
format=format,
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
async def _stream_async(
|
|
411
|
+
self,
|
|
412
|
+
*,
|
|
413
|
+
model_id: str,
|
|
414
|
+
messages: Sequence[Message],
|
|
415
|
+
tools: Sequence[AsyncTool] | AsyncToolkit | None = None,
|
|
416
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
417
|
+
**params: Unpack[Params],
|
|
418
|
+
) -> AsyncStreamResponse | AsyncStreamResponse[FormattableT]:
|
|
419
|
+
"""Generate an `llm.AsyncStreamResponse` by asynchronously streaming from the API.
|
|
420
|
+
|
|
421
|
+
Args:
|
|
422
|
+
model_id: Model identifier to use.
|
|
423
|
+
messages: Messages to send to the LLM.
|
|
424
|
+
tools: Optional tools that the model may invoke.
|
|
425
|
+
format: Optional response format specifier.
|
|
426
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
427
|
+
|
|
428
|
+
Returns:
|
|
429
|
+
An `llm.AsyncStreamResponse` object for iterating over the LLM-generated content.
|
|
430
|
+
"""
|
|
431
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
432
|
+
model_id=model_id,
|
|
433
|
+
messages=messages,
|
|
434
|
+
tools=tools,
|
|
435
|
+
format=format,
|
|
436
|
+
params=params,
|
|
437
|
+
)
|
|
438
|
+
kwargs["model"] = self._model_name(model_id)
|
|
439
|
+
|
|
440
|
+
openai_stream = await self.async_client.chat.completions.create(
|
|
441
|
+
**kwargs,
|
|
442
|
+
stream=True,
|
|
443
|
+
stream_options={"include_usage": True},
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
chunk_iterator = _utils.decode_async_stream(openai_stream)
|
|
447
|
+
|
|
448
|
+
return AsyncStreamResponse(
|
|
449
|
+
provider_id=self.id,
|
|
450
|
+
model_id=model_id,
|
|
451
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
452
|
+
params=params,
|
|
453
|
+
tools=tools,
|
|
454
|
+
input_messages=input_messages,
|
|
455
|
+
chunk_iterator=chunk_iterator,
|
|
456
|
+
format=format,
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
async def _context_stream_async(
|
|
460
|
+
self,
|
|
461
|
+
*,
|
|
462
|
+
ctx: Context[DepsT],
|
|
463
|
+
model_id: str,
|
|
464
|
+
messages: Sequence[Message],
|
|
465
|
+
tools: Sequence[AsyncTool | AsyncContextTool[DepsT]]
|
|
466
|
+
| AsyncContextToolkit[DepsT]
|
|
467
|
+
| None = None,
|
|
468
|
+
format: type[FormattableT] | Format[FormattableT] | None = None,
|
|
469
|
+
**params: Unpack[Params],
|
|
470
|
+
) -> (
|
|
471
|
+
AsyncContextStreamResponse[DepsT]
|
|
472
|
+
| AsyncContextStreamResponse[DepsT, FormattableT]
|
|
473
|
+
):
|
|
474
|
+
"""Generate an `llm.AsyncContextStreamResponse` by asynchronously streaming from the API.
|
|
475
|
+
|
|
476
|
+
Args:
|
|
477
|
+
ctx: Context object with dependencies for tools.
|
|
478
|
+
model_id: Model identifier to use.
|
|
479
|
+
messages: Messages to send to the LLM.
|
|
480
|
+
tools: Optional tools that the model may invoke.
|
|
481
|
+
format: Optional response format specifier.
|
|
482
|
+
**params: Additional parameters to configure output (e.g. temperature). See `llm.Params`.
|
|
483
|
+
|
|
484
|
+
Returns:
|
|
485
|
+
An `llm.AsyncContextStreamResponse` object for iterating over the LLM-generated content.
|
|
486
|
+
"""
|
|
487
|
+
input_messages, format, kwargs = _utils.encode_request(
|
|
488
|
+
model_id=model_id,
|
|
489
|
+
messages=messages,
|
|
490
|
+
tools=tools,
|
|
491
|
+
format=format,
|
|
492
|
+
params=params,
|
|
493
|
+
)
|
|
494
|
+
kwargs["model"] = self._model_name(model_id)
|
|
495
|
+
|
|
496
|
+
openai_stream = await self.async_client.chat.completions.create(
|
|
497
|
+
**kwargs,
|
|
498
|
+
stream=True,
|
|
499
|
+
stream_options={"include_usage": True},
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
chunk_iterator = _utils.decode_async_stream(openai_stream)
|
|
503
|
+
|
|
504
|
+
return AsyncContextStreamResponse(
|
|
505
|
+
provider_id=self.id,
|
|
506
|
+
model_id=model_id,
|
|
507
|
+
provider_model_name=self._provider_model_name(model_id),
|
|
508
|
+
params=params,
|
|
509
|
+
tools=tools,
|
|
510
|
+
input_messages=input_messages,
|
|
511
|
+
chunk_iterator=chunk_iterator,
|
|
512
|
+
format=format,
|
|
513
|
+
)
|