|
19 | 19 | from collections import defaultdict |
20 | 20 | from collections.abc import AsyncIterator |
21 | 21 | from contextlib import asynccontextmanager |
22 | | -from typing import Any, Optional, cast |
| 22 | +from typing import Any, Final, Optional, cast |
23 | 23 |
|
| 24 | +from llama_stack.core.library_client import AsyncLlamaStackAsLibraryClient |
| 25 | +from llama_stack_client import AsyncLlamaStackClient |
24 | 26 | from openai import AsyncStream |
25 | 27 | from openai.types import responses |
26 | 28 | from pydantic_ai import UnexpectedModelBehavior |
|
38 | 40 | OpenAIResponsesStreamedResponse, |
39 | 41 | _map_api_errors, |
40 | 42 | ) |
| 43 | +from pydantic_ai.profiles import ModelProfileSpec |
41 | 44 | from pydantic_ai.settings import ModelSettings |
42 | 45 |
|
43 | 46 | from log import get_logger |
| 47 | +from models.common.responses.responses_api_params import ResponsesApiParams |
| 48 | +from pydantic_ai_lightspeed.llamastack._provider import LlamaStackProvider |
44 | 49 |
|
45 | 50 | logger = get_logger(__name__) |
46 | 51 |
|
| 52 | +_LLS_RESPONSES_EXTRA_FIELDS: Final[frozenset[str]] = frozenset( |
| 53 | + { |
| 54 | + "conversation", |
| 55 | + "max_infer_iters", |
| 56 | + "tools", |
| 57 | + "tool_choice", |
| 58 | + "include", |
| 59 | + "text", |
| 60 | + "reasoning", |
| 61 | + "prompt", |
| 62 | + "metadata", |
| 63 | + "max_tool_calls", |
| 64 | + "safety_identifier", |
| 65 | + } |
| 66 | +) |
| 67 | + |
| 68 | + |
| 69 | +def _model_settings_from_responses_params( |
| 70 | + responses_params: ResponsesApiParams, |
| 71 | +) -> OpenAIResponsesModelSettings: |
| 72 | + """Map ``ResponsesApiParams`` into Pydantic AI OpenAI Responses model settings.""" |
| 73 | + payload = responses_params.model_dump(exclude_none=True) |
| 74 | + extra_body = {k: v for k, v in payload.items() if k in _LLS_RESPONSES_EXTRA_FIELDS} |
| 75 | + settings_dict: dict[str, Any] = {} |
| 76 | + if extra_body: |
| 77 | + settings_dict["extra_body"] = extra_body |
| 78 | + if responses_params.max_output_tokens is not None: |
| 79 | + settings_dict["max_tokens"] = responses_params.max_output_tokens |
| 80 | + if responses_params.temperature is not None: |
| 81 | + settings_dict["temperature"] = responses_params.temperature |
| 82 | + if responses_params.parallel_tool_calls is not None: |
| 83 | + settings_dict["parallel_tool_calls"] = responses_params.parallel_tool_calls |
| 84 | + if responses_params.extra_headers: |
| 85 | + settings_dict["extra_headers"] = dict(responses_params.extra_headers) |
| 86 | + settings_dict["openai_store"] = responses_params.store |
| 87 | + if responses_params.previous_response_id is not None: |
| 88 | + settings_dict["openai_previous_response_id"] = ( |
| 89 | + responses_params.previous_response_id |
| 90 | + ) |
| 91 | + return cast(OpenAIResponsesModelSettings, settings_dict) |
| 92 | + |
47 | 93 |
|
48 | 94 | class _FilteredResponseStream: |
49 | 95 | """Wraps an OpenAI AsyncStream to reorder spurious events from Llama Stack. |
@@ -307,3 +353,53 @@ async def request_stream( # pylint: disable=unused-argument |
307 | 353 | else None |
308 | 354 | ), |
309 | 355 | ) |
| 356 | + |
| 357 | + @staticmethod |
| 358 | + def from_llama_stack_client( |
| 359 | + model_name: str, |
| 360 | + client: AsyncLlamaStackClient | AsyncLlamaStackAsLibraryClient, |
| 361 | + *, |
| 362 | + responses_params: ResponsesApiParams | None = None, |
| 363 | + model_settings: ModelSettings | None = None, |
| 364 | + profile: ModelProfileSpec | None = None, |
| 365 | + ) -> LlamaStackResponsesModel: |
| 366 | + """Create a ``LlamaStackResponsesModel`` from a Llama Stack client. |
| 367 | +
|
| 368 | + Mirrors ``OpenAIResponsesModel.__init__`` parameters, but accepts a |
| 369 | + Llama Stack client instead of a provider. Exactly one of |
| 370 | + ``responses_params`` or ``model_settings`` may be provided. |
| 371 | +
|
| 372 | + Args: |
| 373 | + model_name: The model name/ID to use. |
| 374 | + client: Llama Stack client to build the provider from. |
| 375 | + responses_params: Optional ``ResponsesApiParams``, converted to |
| 376 | + ``OpenAIResponsesModelSettings`` internally. Mutually |
| 377 | + exclusive with ``model_settings``. |
| 378 | + model_settings: Optional raw ``ModelSettings`` passed through |
| 379 | + directly. Mutually exclusive with ``responses_params``. |
| 380 | + profile: Optional model profile specification. |
| 381 | +
|
| 382 | + Raises: |
| 383 | + ValueError: If both ``responses_params`` and ``model_settings`` |
| 384 | + are provided. |
| 385 | +
|
| 386 | + Returns: |
| 387 | + Configured ``LlamaStackResponsesModel`` instance. |
| 388 | + """ |
| 389 | + provider = LlamaStackProvider.from_llama_stack_client(client) |
| 390 | + |
| 391 | + if responses_params is not None and model_settings is not None: |
| 392 | + raise ValueError( |
| 393 | + "You can only pass either ResponsesApiParams or ModelSetting not both." |
| 394 | + ) |
| 395 | + |
| 396 | + _settings: OpenAIResponsesModelSettings | ModelSettings | None = None |
| 397 | + |
| 398 | + if responses_params is not None: |
| 399 | + _settings = _model_settings_from_responses_params(responses_params) |
| 400 | + elif model_settings is not None: |
| 401 | + _settings = model_settings |
| 402 | + |
| 403 | + return LlamaStackResponsesModel( |
| 404 | + model_name, provider=provider, profile=profile, settings=_settings |
| 405 | + ) |
0 commit comments