llama-stack-api 0.4.3__py3-none-any.whl → 0.5.0rc1__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.
Files changed (117) hide show
  1. llama_stack_api/__init__.py +1100 -0
  2. llama_stack_api/admin/__init__.py +45 -0
  3. llama_stack_api/admin/api.py +72 -0
  4. llama_stack_api/admin/fastapi_routes.py +117 -0
  5. llama_stack_api/admin/models.py +113 -0
  6. llama_stack_api/agents/__init__.py +38 -0
  7. llama_stack_api/agents/api.py +52 -0
  8. llama_stack_api/agents/fastapi_routes.py +268 -0
  9. llama_stack_api/agents/models.py +181 -0
  10. llama_stack_api/batches/__init__.py +40 -0
  11. llama_stack_api/batches/api.py +53 -0
  12. llama_stack_api/batches/fastapi_routes.py +113 -0
  13. llama_stack_api/batches/models.py +78 -0
  14. llama_stack_api/benchmarks/__init__.py +43 -0
  15. llama_stack_api/benchmarks/api.py +39 -0
  16. llama_stack_api/benchmarks/fastapi_routes.py +109 -0
  17. llama_stack_api/benchmarks/models.py +109 -0
  18. llama_stack_api/common/__init__.py +5 -0
  19. llama_stack_api/common/content_types.py +101 -0
  20. llama_stack_api/common/errors.py +110 -0
  21. llama_stack_api/common/job_types.py +38 -0
  22. llama_stack_api/common/responses.py +77 -0
  23. llama_stack_api/common/training_types.py +47 -0
  24. llama_stack_api/common/type_system.py +146 -0
  25. llama_stack_api/connectors/__init__.py +38 -0
  26. llama_stack_api/connectors/api.py +50 -0
  27. llama_stack_api/connectors/fastapi_routes.py +103 -0
  28. llama_stack_api/connectors/models.py +103 -0
  29. llama_stack_api/conversations/__init__.py +61 -0
  30. llama_stack_api/conversations/api.py +44 -0
  31. llama_stack_api/conversations/fastapi_routes.py +177 -0
  32. llama_stack_api/conversations/models.py +245 -0
  33. llama_stack_api/datasetio/__init__.py +34 -0
  34. llama_stack_api/datasetio/api.py +42 -0
  35. llama_stack_api/datasetio/fastapi_routes.py +94 -0
  36. llama_stack_api/datasetio/models.py +48 -0
  37. llama_stack_api/datasets/__init__.py +61 -0
  38. llama_stack_api/datasets/api.py +35 -0
  39. llama_stack_api/datasets/fastapi_routes.py +104 -0
  40. llama_stack_api/datasets/models.py +152 -0
  41. llama_stack_api/datatypes.py +373 -0
  42. llama_stack_api/eval/__init__.py +55 -0
  43. llama_stack_api/eval/api.py +51 -0
  44. llama_stack_api/eval/compat.py +300 -0
  45. llama_stack_api/eval/fastapi_routes.py +126 -0
  46. llama_stack_api/eval/models.py +141 -0
  47. llama_stack_api/file_processors/__init__.py +27 -0
  48. llama_stack_api/file_processors/api.py +64 -0
  49. llama_stack_api/file_processors/fastapi_routes.py +78 -0
  50. llama_stack_api/file_processors/models.py +42 -0
  51. llama_stack_api/files/__init__.py +35 -0
  52. llama_stack_api/files/api.py +51 -0
  53. llama_stack_api/files/fastapi_routes.py +124 -0
  54. llama_stack_api/files/models.py +107 -0
  55. llama_stack_api/inference/__init__.py +207 -0
  56. llama_stack_api/inference/api.py +93 -0
  57. llama_stack_api/inference/fastapi_routes.py +243 -0
  58. llama_stack_api/inference/models.py +1035 -0
  59. llama_stack_api/inspect_api/__init__.py +37 -0
  60. llama_stack_api/inspect_api/api.py +25 -0
  61. llama_stack_api/inspect_api/fastapi_routes.py +76 -0
  62. llama_stack_api/inspect_api/models.py +28 -0
  63. llama_stack_api/internal/__init__.py +9 -0
  64. llama_stack_api/internal/kvstore.py +28 -0
  65. llama_stack_api/internal/sqlstore.py +81 -0
  66. llama_stack_api/models/__init__.py +47 -0
  67. llama_stack_api/models/api.py +38 -0
  68. llama_stack_api/models/fastapi_routes.py +104 -0
  69. llama_stack_api/models/models.py +157 -0
  70. llama_stack_api/openai_responses.py +1494 -0
  71. llama_stack_api/post_training/__init__.py +73 -0
  72. llama_stack_api/post_training/api.py +36 -0
  73. llama_stack_api/post_training/fastapi_routes.py +116 -0
  74. llama_stack_api/post_training/models.py +339 -0
  75. llama_stack_api/prompts/__init__.py +47 -0
  76. llama_stack_api/prompts/api.py +44 -0
  77. llama_stack_api/prompts/fastapi_routes.py +163 -0
  78. llama_stack_api/prompts/models.py +177 -0
  79. llama_stack_api/providers/__init__.py +33 -0
  80. llama_stack_api/providers/api.py +16 -0
  81. llama_stack_api/providers/fastapi_routes.py +57 -0
  82. llama_stack_api/providers/models.py +24 -0
  83. llama_stack_api/rag_tool.py +168 -0
  84. llama_stack_api/resource.py +36 -0
  85. llama_stack_api/router_utils.py +160 -0
  86. llama_stack_api/safety/__init__.py +37 -0
  87. llama_stack_api/safety/api.py +29 -0
  88. llama_stack_api/safety/datatypes.py +83 -0
  89. llama_stack_api/safety/fastapi_routes.py +55 -0
  90. llama_stack_api/safety/models.py +38 -0
  91. llama_stack_api/schema_utils.py +251 -0
  92. llama_stack_api/scoring/__init__.py +66 -0
  93. llama_stack_api/scoring/api.py +35 -0
  94. llama_stack_api/scoring/fastapi_routes.py +67 -0
  95. llama_stack_api/scoring/models.py +81 -0
  96. llama_stack_api/scoring_functions/__init__.py +50 -0
  97. llama_stack_api/scoring_functions/api.py +39 -0
  98. llama_stack_api/scoring_functions/fastapi_routes.py +108 -0
  99. llama_stack_api/scoring_functions/models.py +214 -0
  100. llama_stack_api/shields/__init__.py +41 -0
  101. llama_stack_api/shields/api.py +39 -0
  102. llama_stack_api/shields/fastapi_routes.py +104 -0
  103. llama_stack_api/shields/models.py +74 -0
  104. llama_stack_api/tools.py +226 -0
  105. llama_stack_api/validators.py +46 -0
  106. llama_stack_api/vector_io/__init__.py +88 -0
  107. llama_stack_api/vector_io/api.py +234 -0
  108. llama_stack_api/vector_io/fastapi_routes.py +447 -0
  109. llama_stack_api/vector_io/models.py +663 -0
  110. llama_stack_api/vector_stores.py +53 -0
  111. llama_stack_api/version.py +9 -0
  112. {llama_stack_api-0.4.3.dist-info → llama_stack_api-0.5.0rc1.dist-info}/METADATA +1 -1
  113. llama_stack_api-0.5.0rc1.dist-info/RECORD +115 -0
  114. llama_stack_api-0.5.0rc1.dist-info/top_level.txt +1 -0
  115. llama_stack_api-0.4.3.dist-info/RECORD +0 -4
  116. llama_stack_api-0.4.3.dist-info/top_level.txt +0 -1
  117. {llama_stack_api-0.4.3.dist-info → llama_stack_api-0.5.0rc1.dist-info}/WHEEL +0 -0
@@ -0,0 +1,37 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """Inspect API protocol and models.
8
+
9
+ This module contains the Inspect protocol definition.
10
+ Pydantic models are defined in llama_stack_api.inspect.models.
11
+ The FastAPI router is defined in llama_stack_api.inspect.fastapi_routes.
12
+ """
13
+
14
+ # Import fastapi_routes for router factory access
15
+ from . import fastapi_routes
16
+
17
+ # Import protocol for re-export
18
+ from .api import Inspect
19
+
20
+ # Import models for re-export
21
+ from .models import (
22
+ ApiFilter,
23
+ HealthInfo,
24
+ ListRoutesResponse,
25
+ RouteInfo,
26
+ VersionInfo,
27
+ )
28
+
29
+ __all__ = [
30
+ "Inspect",
31
+ "ApiFilter",
32
+ "HealthInfo",
33
+ "ListRoutesResponse",
34
+ "RouteInfo",
35
+ "VersionInfo",
36
+ "fastapi_routes",
37
+ ]
@@ -0,0 +1,25 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ from typing import Protocol, runtime_checkable
8
+
9
+ from .models import (
10
+ ApiFilter,
11
+ HealthInfo,
12
+ ListRoutesResponse,
13
+ VersionInfo,
14
+ )
15
+
16
+
17
+ @runtime_checkable
18
+ class Inspect(Protocol):
19
+ """APIs for inspecting the Llama Stack service, including health status, available API routes with methods and implementing providers."""
20
+
21
+ async def list_routes(self, api_filter: ApiFilter | None = None) -> ListRoutesResponse: ...
22
+
23
+ async def health(self) -> HealthInfo: ...
24
+
25
+ async def version(self) -> VersionInfo: ...
@@ -0,0 +1,76 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """FastAPI router for the Inspect API.
8
+
9
+ This module defines the FastAPI router for the Inspect API using standard
10
+ FastAPI route decorators.
11
+ """
12
+
13
+ from typing import Annotated
14
+
15
+ from fastapi import APIRouter, Query
16
+
17
+ from llama_stack_api.router_utils import PUBLIC_ROUTE_KEY, standard_responses
18
+ from llama_stack_api.version import LLAMA_STACK_API_V1
19
+
20
+ from .api import Inspect
21
+ from .models import (
22
+ ApiFilter,
23
+ HealthInfo,
24
+ ListRoutesResponse,
25
+ VersionInfo,
26
+ )
27
+
28
+
29
+ def create_router(impl: Inspect) -> APIRouter:
30
+ """Create a FastAPI router for the Inspect API."""
31
+ router = APIRouter(
32
+ prefix=f"/{LLAMA_STACK_API_V1}",
33
+ tags=["Inspect"],
34
+ responses=standard_responses,
35
+ )
36
+
37
+ @router.get(
38
+ "/inspect/routes",
39
+ response_model=ListRoutesResponse,
40
+ summary="List routes.",
41
+ description="List all available API routes with their methods and implementing providers.",
42
+ responses={200: {"description": "Response containing information about all available routes."}},
43
+ )
44
+ async def list_routes(
45
+ api_filter: Annotated[
46
+ ApiFilter | None,
47
+ Query(
48
+ description="Optional filter to control which routes are returned. Can be an API level ('v1', 'v1alpha', 'v1beta') to show non-deprecated routes at that level, or 'deprecated' to show deprecated routes across all levels. If not specified, returns all non-deprecated routes."
49
+ ),
50
+ ] = None,
51
+ ) -> ListRoutesResponse:
52
+ return await impl.list_routes(api_filter)
53
+
54
+ @router.get(
55
+ "/health",
56
+ response_model=HealthInfo,
57
+ summary="Get health status.",
58
+ description="Get the current health status of the service.",
59
+ responses={200: {"description": "Health information indicating if the service is operational."}},
60
+ openapi_extra={PUBLIC_ROUTE_KEY: True},
61
+ )
62
+ async def health() -> HealthInfo:
63
+ return await impl.health()
64
+
65
+ @router.get(
66
+ "/version",
67
+ response_model=VersionInfo,
68
+ summary="Get version.",
69
+ description="Get the version of the service.",
70
+ responses={200: {"description": "Version information containing the service version number."}},
71
+ openapi_extra={PUBLIC_ROUTE_KEY: True},
72
+ )
73
+ async def version() -> VersionInfo:
74
+ return await impl.version()
75
+
76
+ return router
@@ -0,0 +1,28 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """Pydantic models for Inspect API requests and responses.
8
+
9
+ This module re-exports models from llama_stack_api.admin.models to ensure
10
+ a single source of truth and avoid type conflicts.
11
+ """
12
+
13
+ # Import and re-export shared models from admin
14
+ from llama_stack_api.admin.models import (
15
+ ApiFilter,
16
+ HealthInfo,
17
+ ListRoutesResponse,
18
+ RouteInfo,
19
+ VersionInfo,
20
+ )
21
+
22
+ __all__ = [
23
+ "ApiFilter",
24
+ "RouteInfo",
25
+ "HealthInfo",
26
+ "VersionInfo",
27
+ "ListRoutesResponse",
28
+ ]
@@ -0,0 +1,9 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ # Internal subpackage for shared interfaces that are not part of the public API.
8
+
9
+ __all__: list[str] = []
@@ -0,0 +1,28 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ from datetime import datetime
8
+ from typing import Protocol
9
+
10
+
11
+ class KVStore(Protocol):
12
+ """Protocol for simple key/value storage backends."""
13
+
14
+ # TODO: make the value type bytes instead of str
15
+ async def set(self, key: str, value: str, expiration: datetime | None = None) -> None: ...
16
+
17
+ async def get(self, key: str) -> str | None: ...
18
+
19
+ async def delete(self, key: str) -> None: ...
20
+
21
+ async def values_in_range(self, start_key: str, end_key: str) -> list[str]: ...
22
+
23
+ async def keys_in_range(self, start_key: str, end_key: str) -> list[str]: ...
24
+
25
+ async def shutdown(self) -> None: ...
26
+
27
+
28
+ __all__ = ["KVStore"]
@@ -0,0 +1,81 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ from collections.abc import Mapping, Sequence
8
+ from enum import Enum
9
+ from typing import Any, Literal, Protocol
10
+
11
+ from pydantic import BaseModel
12
+
13
+ from llama_stack_api import PaginatedResponse
14
+
15
+
16
+ class ColumnType(Enum):
17
+ INTEGER = "INTEGER"
18
+ STRING = "STRING"
19
+ TEXT = "TEXT"
20
+ FLOAT = "FLOAT"
21
+ BOOLEAN = "BOOLEAN"
22
+ JSON = "JSON"
23
+ DATETIME = "DATETIME"
24
+
25
+
26
+ class ColumnDefinition(BaseModel):
27
+ type: ColumnType
28
+ primary_key: bool = False
29
+ nullable: bool = True
30
+ default: Any = None
31
+
32
+
33
+ class SqlStore(Protocol):
34
+ """Protocol for common SQL-store functionality."""
35
+
36
+ async def create_table(self, table: str, schema: Mapping[str, ColumnType | ColumnDefinition]) -> None: ...
37
+
38
+ async def insert(self, table: str, data: Mapping[str, Any] | Sequence[Mapping[str, Any]]) -> None: ...
39
+
40
+ async def upsert(
41
+ self,
42
+ table: str,
43
+ data: Mapping[str, Any],
44
+ conflict_columns: list[str],
45
+ update_columns: list[str] | None = None,
46
+ ) -> None: ...
47
+
48
+ async def fetch_all(
49
+ self,
50
+ table: str,
51
+ where: Mapping[str, Any] | None = None,
52
+ where_sql: str | None = None,
53
+ limit: int | None = None,
54
+ order_by: list[tuple[str, Literal["asc", "desc"]]] | None = None,
55
+ cursor: tuple[str, str] | None = None,
56
+ ) -> PaginatedResponse: ...
57
+
58
+ async def fetch_one(
59
+ self,
60
+ table: str,
61
+ where: Mapping[str, Any] | None = None,
62
+ where_sql: str | None = None,
63
+ order_by: list[tuple[str, Literal["asc", "desc"]]] | None = None,
64
+ ) -> dict[str, Any] | None: ...
65
+
66
+ async def update(self, table: str, data: Mapping[str, Any], where: Mapping[str, Any]) -> None: ...
67
+
68
+ async def delete(self, table: str, where: Mapping[str, Any]) -> None: ...
69
+
70
+ async def add_column_if_not_exists(
71
+ self,
72
+ table: str,
73
+ column_name: str,
74
+ column_type: ColumnType,
75
+ nullable: bool = True,
76
+ ) -> None: ...
77
+
78
+ async def shutdown(self) -> None: ...
79
+
80
+
81
+ __all__ = ["ColumnDefinition", "ColumnType", "SqlStore"]
@@ -0,0 +1,47 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """Models API protocol and models.
8
+
9
+ This module contains the Models protocol definition.
10
+ Pydantic models are defined in llama_stack_api.models.models.
11
+ The FastAPI router is defined in llama_stack_api.models.fastapi_routes.
12
+ """
13
+
14
+ # Import fastapi_routes for router factory access
15
+ from . import fastapi_routes
16
+
17
+ # Import new protocol for FastAPI router
18
+ from .api import Models
19
+
20
+ # Import models for re-export
21
+ from .models import (
22
+ CommonModelFields,
23
+ GetModelRequest,
24
+ ListModelsResponse,
25
+ Model,
26
+ ModelInput,
27
+ ModelType,
28
+ OpenAIListModelsResponse,
29
+ OpenAIModel,
30
+ RegisterModelRequest,
31
+ UnregisterModelRequest,
32
+ )
33
+
34
+ __all__ = [
35
+ "CommonModelFields",
36
+ "fastapi_routes",
37
+ "GetModelRequest",
38
+ "ListModelsResponse",
39
+ "Model",
40
+ "ModelInput",
41
+ "Models",
42
+ "ModelType",
43
+ "OpenAIListModelsResponse",
44
+ "OpenAIModel",
45
+ "RegisterModelRequest",
46
+ "UnregisterModelRequest",
47
+ ]
@@ -0,0 +1,38 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """Models API protocol definition.
8
+
9
+ This module contains the Models protocol definition.
10
+ Pydantic models are defined in llama_stack_api.models.models.
11
+ The FastAPI router is defined in llama_stack_api.models.fastapi_routes.
12
+ """
13
+
14
+ from typing import Protocol, runtime_checkable
15
+
16
+ from .models import (
17
+ GetModelRequest,
18
+ ListModelsResponse,
19
+ Model,
20
+ OpenAIListModelsResponse,
21
+ RegisterModelRequest,
22
+ UnregisterModelRequest,
23
+ )
24
+
25
+
26
+ @runtime_checkable
27
+ class Models(Protocol):
28
+ """Protocol for model management operations."""
29
+
30
+ async def list_models(self) -> ListModelsResponse: ...
31
+
32
+ async def openai_list_models(self) -> OpenAIListModelsResponse: ...
33
+
34
+ async def get_model(self, request: GetModelRequest) -> Model: ...
35
+
36
+ async def register_model(self, request: RegisterModelRequest) -> Model: ...
37
+
38
+ async def unregister_model(self, request: UnregisterModelRequest) -> None: ...
@@ -0,0 +1,104 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """FastAPI router for the Models API.
8
+
9
+ This module defines the FastAPI router for the Models API using standard
10
+ FastAPI route decorators.
11
+ """
12
+
13
+ from typing import Annotated
14
+
15
+ from fastapi import APIRouter, Body, Depends
16
+
17
+ from llama_stack_api.router_utils import create_path_dependency, standard_responses
18
+ from llama_stack_api.version import LLAMA_STACK_API_V1
19
+
20
+ from .api import Models
21
+ from .models import (
22
+ GetModelRequest,
23
+ Model,
24
+ OpenAIListModelsResponse,
25
+ RegisterModelRequest,
26
+ UnregisterModelRequest,
27
+ )
28
+
29
+ # Path parameter dependencies for single-field models
30
+ get_model_request = create_path_dependency(GetModelRequest)
31
+ unregister_model_request = create_path_dependency(UnregisterModelRequest)
32
+
33
+
34
+ def create_router(impl: Models) -> APIRouter:
35
+ """Create a FastAPI router for the Models API.
36
+
37
+ Args:
38
+ impl: The Models implementation instance
39
+
40
+ Returns:
41
+ APIRouter configured for the Models API
42
+ """
43
+ router = APIRouter(
44
+ prefix=f"/{LLAMA_STACK_API_V1}",
45
+ tags=["Models"],
46
+ responses=standard_responses,
47
+ )
48
+
49
+ @router.get(
50
+ "/models",
51
+ response_model=OpenAIListModelsResponse,
52
+ summary="List models using the OpenAI API.",
53
+ description="List models using the OpenAI API.",
54
+ responses={
55
+ 200: {"description": "A list of OpenAI model objects."},
56
+ },
57
+ )
58
+ async def openai_list_models() -> OpenAIListModelsResponse:
59
+ return await impl.openai_list_models()
60
+
61
+ @router.get(
62
+ "/models/{model_id:path}",
63
+ response_model=Model,
64
+ summary="Get a model by its identifier.",
65
+ description="Get a model by its identifier.",
66
+ responses={
67
+ 200: {"description": "The model object."},
68
+ },
69
+ )
70
+ async def get_model(
71
+ request: Annotated[GetModelRequest, Depends(get_model_request)],
72
+ ) -> Model:
73
+ return await impl.get_model(request)
74
+
75
+ @router.post(
76
+ "/models",
77
+ response_model=Model,
78
+ summary="Register a model.",
79
+ description="Register a model.",
80
+ responses={
81
+ 200: {"description": "The registered model object."},
82
+ },
83
+ deprecated=True,
84
+ )
85
+ async def register_model(
86
+ request: Annotated[RegisterModelRequest, Body(...)],
87
+ ) -> Model:
88
+ return await impl.register_model(request)
89
+
90
+ @router.delete(
91
+ "/models/{model_id:path}",
92
+ summary="Unregister a model.",
93
+ description="Unregister a model.",
94
+ responses={
95
+ 200: {"description": "The model was successfully unregistered."},
96
+ },
97
+ deprecated=True,
98
+ )
99
+ async def unregister_model(
100
+ request: Annotated[UnregisterModelRequest, Depends(unregister_model_request)],
101
+ ) -> None:
102
+ return await impl.unregister_model(request)
103
+
104
+ return router
@@ -0,0 +1,157 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the terms described in the LICENSE file in
5
+ # the root directory of this source tree.
6
+
7
+ """Pydantic models for Models API requests and responses.
8
+
9
+ This module defines the request and response models for the Models API
10
+ using Pydantic with Field descriptions for OpenAPI schema generation.
11
+ """
12
+
13
+ from enum import StrEnum
14
+ from typing import Any, Literal
15
+
16
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
17
+
18
+ from llama_stack_api.resource import Resource, ResourceType
19
+ from llama_stack_api.schema_utils import json_schema_type
20
+
21
+
22
+ @json_schema_type
23
+ class ModelType(StrEnum):
24
+ """Enumeration of supported model types in Llama Stack.
25
+
26
+ :cvar llm: Large language model for text generation and completion
27
+ :cvar embedding: Embedding model for converting text to vector representations
28
+ :cvar rerank: Reranking model for reordering documents based on their relevance to a query
29
+ """
30
+
31
+ llm = "llm"
32
+ embedding = "embedding"
33
+ rerank = "rerank"
34
+
35
+
36
+ class CommonModelFields(BaseModel):
37
+ metadata: dict[str, Any] = Field(
38
+ default_factory=dict,
39
+ description="Any additional metadata for this model",
40
+ )
41
+
42
+
43
+ @json_schema_type
44
+ class Model(CommonModelFields, Resource):
45
+ """A model resource representing an AI model registered in Llama Stack.
46
+
47
+ :param type: The resource type, always 'model' for model resources
48
+ :param model_type: The type of model (LLM or embedding model)
49
+ :param metadata: Any additional metadata for this model
50
+ :param identifier: Unique identifier for this resource in llama stack
51
+ :param provider_resource_id: Unique identifier for this resource in the provider
52
+ :param provider_id: ID of the provider that owns this resource
53
+ """
54
+
55
+ type: Literal[ResourceType.model] = ResourceType.model
56
+
57
+ @property
58
+ def model_id(self) -> str:
59
+ return self.identifier
60
+
61
+ @property
62
+ def provider_model_id(self) -> str:
63
+ assert self.provider_resource_id is not None, "Provider resource ID must be set"
64
+ return self.provider_resource_id
65
+
66
+ model_config = ConfigDict(protected_namespaces=())
67
+
68
+ model_type: ModelType = Field(default=ModelType.llm)
69
+
70
+ @field_validator("provider_resource_id")
71
+ @classmethod
72
+ def validate_provider_resource_id(cls, v):
73
+ if v is None:
74
+ raise ValueError("provider_resource_id cannot be None")
75
+ return v
76
+
77
+
78
+ class ModelInput(CommonModelFields):
79
+ model_id: str
80
+ provider_id: str | None = None
81
+ provider_model_id: str | None = None
82
+ model_type: ModelType | None = ModelType.llm
83
+ model_config = ConfigDict(protected_namespaces=())
84
+
85
+
86
+ @json_schema_type
87
+ class ListModelsResponse(BaseModel):
88
+ """Response containing a list of model objects."""
89
+
90
+ data: list[Model] = Field(..., description="List of model objects.")
91
+
92
+
93
+ @json_schema_type
94
+ class OpenAIModel(BaseModel):
95
+ """A model from OpenAI.
96
+
97
+ :id: The ID of the model
98
+ :object: The object type, which will be "model"
99
+ :created: The Unix timestamp in seconds when the model was created
100
+ :owned_by: The owner of the model
101
+ :custom_metadata: Llama Stack-specific metadata including model_type, provider info, and additional metadata
102
+ """
103
+
104
+ id: str
105
+ object: Literal["model"] = "model"
106
+ created: int
107
+ owned_by: str
108
+ custom_metadata: dict[str, Any] | None = None
109
+
110
+
111
+ @json_schema_type
112
+ class OpenAIListModelsResponse(BaseModel):
113
+ """Response containing a list of OpenAI model objects."""
114
+
115
+ data: list[OpenAIModel] = Field(..., description="List of OpenAI model objects.")
116
+
117
+
118
+ # Request models for each endpoint
119
+
120
+
121
+ @json_schema_type
122
+ class GetModelRequest(BaseModel):
123
+ """Request model for getting a model by ID."""
124
+
125
+ model_id: str = Field(..., description="The ID of the model to get.")
126
+
127
+
128
+ @json_schema_type
129
+ class RegisterModelRequest(BaseModel):
130
+ """Request model for registering a model."""
131
+
132
+ model_id: str = Field(..., description="The identifier of the model to register.")
133
+ provider_model_id: str | None = Field(default=None, description="The identifier of the model in the provider.")
134
+ provider_id: str | None = Field(default=None, description="The identifier of the provider.")
135
+ metadata: dict[str, Any] | None = Field(default=None, description="Any additional metadata for this model.")
136
+ model_type: ModelType | None = Field(default=None, description="The type of model to register.")
137
+
138
+
139
+ @json_schema_type
140
+ class UnregisterModelRequest(BaseModel):
141
+ """Request model for unregistering a model."""
142
+
143
+ model_id: str = Field(..., description="The ID of the model to unregister.")
144
+
145
+
146
+ __all__ = [
147
+ "CommonModelFields",
148
+ "GetModelRequest",
149
+ "ListModelsResponse",
150
+ "Model",
151
+ "ModelInput",
152
+ "ModelType",
153
+ "OpenAIListModelsResponse",
154
+ "OpenAIModel",
155
+ "RegisterModelRequest",
156
+ "UnregisterModelRequest",
157
+ ]