llama-stack-api 0.4.2__py3-none-any.whl → 0.4.4__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.
- llama_stack_api/__init__.py +945 -0
- llama_stack_api/admin/__init__.py +45 -0
- llama_stack_api/admin/api.py +72 -0
- llama_stack_api/admin/fastapi_routes.py +117 -0
- llama_stack_api/admin/models.py +113 -0
- llama_stack_api/agents.py +173 -0
- llama_stack_api/batches/__init__.py +40 -0
- llama_stack_api/batches/api.py +53 -0
- llama_stack_api/batches/fastapi_routes.py +113 -0
- llama_stack_api/batches/models.py +78 -0
- llama_stack_api/benchmarks/__init__.py +43 -0
- llama_stack_api/benchmarks/api.py +39 -0
- llama_stack_api/benchmarks/fastapi_routes.py +109 -0
- llama_stack_api/benchmarks/models.py +109 -0
- llama_stack_api/common/__init__.py +5 -0
- llama_stack_api/common/content_types.py +101 -0
- llama_stack_api/common/errors.py +95 -0
- llama_stack_api/common/job_types.py +38 -0
- llama_stack_api/common/responses.py +77 -0
- llama_stack_api/common/training_types.py +47 -0
- llama_stack_api/common/type_system.py +146 -0
- llama_stack_api/connectors.py +146 -0
- llama_stack_api/conversations.py +270 -0
- llama_stack_api/datasetio.py +55 -0
- llama_stack_api/datasets/__init__.py +61 -0
- llama_stack_api/datasets/api.py +35 -0
- llama_stack_api/datasets/fastapi_routes.py +104 -0
- llama_stack_api/datasets/models.py +152 -0
- llama_stack_api/datatypes.py +373 -0
- llama_stack_api/eval.py +137 -0
- llama_stack_api/file_processors/__init__.py +27 -0
- llama_stack_api/file_processors/api.py +64 -0
- llama_stack_api/file_processors/fastapi_routes.py +78 -0
- llama_stack_api/file_processors/models.py +42 -0
- llama_stack_api/files/__init__.py +35 -0
- llama_stack_api/files/api.py +51 -0
- llama_stack_api/files/fastapi_routes.py +124 -0
- llama_stack_api/files/models.py +107 -0
- llama_stack_api/inference.py +1169 -0
- llama_stack_api/inspect_api/__init__.py +37 -0
- llama_stack_api/inspect_api/api.py +25 -0
- llama_stack_api/inspect_api/fastapi_routes.py +76 -0
- llama_stack_api/inspect_api/models.py +28 -0
- llama_stack_api/internal/__init__.py +9 -0
- llama_stack_api/internal/kvstore.py +28 -0
- llama_stack_api/internal/sqlstore.py +81 -0
- llama_stack_api/models.py +171 -0
- llama_stack_api/openai_responses.py +1468 -0
- llama_stack_api/post_training.py +370 -0
- llama_stack_api/prompts.py +203 -0
- llama_stack_api/providers/__init__.py +33 -0
- llama_stack_api/providers/api.py +16 -0
- llama_stack_api/providers/fastapi_routes.py +57 -0
- llama_stack_api/providers/models.py +24 -0
- llama_stack_api/rag_tool.py +168 -0
- llama_stack_api/resource.py +37 -0
- llama_stack_api/router_utils.py +160 -0
- llama_stack_api/safety.py +132 -0
- llama_stack_api/schema_utils.py +208 -0
- llama_stack_api/scoring.py +93 -0
- llama_stack_api/scoring_functions.py +211 -0
- llama_stack_api/shields.py +93 -0
- llama_stack_api/tools.py +226 -0
- llama_stack_api/vector_io.py +941 -0
- llama_stack_api/vector_stores.py +53 -0
- llama_stack_api/version.py +9 -0
- {llama_stack_api-0.4.2.dist-info → llama_stack_api-0.4.4.dist-info}/METADATA +1 -1
- llama_stack_api-0.4.4.dist-info/RECORD +70 -0
- {llama_stack_api-0.4.2.dist-info → llama_stack_api-0.4.4.dist-info}/WHEEL +1 -1
- llama_stack_api-0.4.4.dist-info/top_level.txt +1 -0
- llama_stack_api-0.4.2.dist-info/RECORD +0 -4
- llama_stack_api-0.4.2.dist-info/top_level.txt +0 -1
|
@@ -0,0 +1,370 @@
|
|
|
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 enum import Enum
|
|
9
|
+
from typing import Annotated, Any, Literal, Protocol
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel, Field
|
|
12
|
+
|
|
13
|
+
from llama_stack_api.common.content_types import URL
|
|
14
|
+
from llama_stack_api.common.job_types import JobStatus
|
|
15
|
+
from llama_stack_api.common.training_types import Checkpoint
|
|
16
|
+
from llama_stack_api.schema_utils import json_schema_type, register_schema, webmethod
|
|
17
|
+
from llama_stack_api.version import LLAMA_STACK_API_V1ALPHA
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@json_schema_type
|
|
21
|
+
class OptimizerType(Enum):
|
|
22
|
+
"""Available optimizer algorithms for training.
|
|
23
|
+
:cvar adam: Adaptive Moment Estimation optimizer
|
|
24
|
+
:cvar adamw: AdamW optimizer with weight decay
|
|
25
|
+
:cvar sgd: Stochastic Gradient Descent optimizer
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
adam = "adam"
|
|
29
|
+
adamw = "adamw"
|
|
30
|
+
sgd = "sgd"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@json_schema_type
|
|
34
|
+
class DatasetFormat(Enum):
|
|
35
|
+
"""Format of the training dataset.
|
|
36
|
+
:cvar instruct: Instruction-following format with prompt and completion
|
|
37
|
+
:cvar dialog: Multi-turn conversation format with messages
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
instruct = "instruct"
|
|
41
|
+
dialog = "dialog"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@json_schema_type
|
|
45
|
+
class DataConfig(BaseModel):
|
|
46
|
+
"""Configuration for training data and data loading.
|
|
47
|
+
|
|
48
|
+
:param dataset_id: Unique identifier for the training dataset
|
|
49
|
+
:param batch_size: Number of samples per training batch
|
|
50
|
+
:param shuffle: Whether to shuffle the dataset during training
|
|
51
|
+
:param data_format: Format of the dataset (instruct or dialog)
|
|
52
|
+
:param validation_dataset_id: (Optional) Unique identifier for the validation dataset
|
|
53
|
+
:param packed: (Optional) Whether to pack multiple samples into a single sequence for efficiency
|
|
54
|
+
:param train_on_input: (Optional) Whether to compute loss on input tokens as well as output tokens
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
dataset_id: str
|
|
58
|
+
batch_size: int
|
|
59
|
+
shuffle: bool
|
|
60
|
+
data_format: DatasetFormat
|
|
61
|
+
validation_dataset_id: str | None = None
|
|
62
|
+
packed: bool | None = False
|
|
63
|
+
train_on_input: bool | None = False
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@json_schema_type
|
|
67
|
+
class OptimizerConfig(BaseModel):
|
|
68
|
+
"""Configuration parameters for the optimization algorithm.
|
|
69
|
+
|
|
70
|
+
:param optimizer_type: Type of optimizer to use (adam, adamw, or sgd)
|
|
71
|
+
:param lr: Learning rate for the optimizer
|
|
72
|
+
:param weight_decay: Weight decay coefficient for regularization
|
|
73
|
+
:param num_warmup_steps: Number of steps for learning rate warmup
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
optimizer_type: OptimizerType
|
|
77
|
+
lr: float
|
|
78
|
+
weight_decay: float
|
|
79
|
+
num_warmup_steps: int
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@json_schema_type
|
|
83
|
+
class EfficiencyConfig(BaseModel):
|
|
84
|
+
"""Configuration for memory and compute efficiency optimizations.
|
|
85
|
+
|
|
86
|
+
:param enable_activation_checkpointing: (Optional) Whether to use activation checkpointing to reduce memory usage
|
|
87
|
+
:param enable_activation_offloading: (Optional) Whether to offload activations to CPU to save GPU memory
|
|
88
|
+
:param memory_efficient_fsdp_wrap: (Optional) Whether to use memory-efficient FSDP wrapping
|
|
89
|
+
:param fsdp_cpu_offload: (Optional) Whether to offload FSDP parameters to CPU
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
enable_activation_checkpointing: bool | None = False
|
|
93
|
+
enable_activation_offloading: bool | None = False
|
|
94
|
+
memory_efficient_fsdp_wrap: bool | None = False
|
|
95
|
+
fsdp_cpu_offload: bool | None = False
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@json_schema_type
|
|
99
|
+
class TrainingConfig(BaseModel):
|
|
100
|
+
"""Comprehensive configuration for the training process.
|
|
101
|
+
|
|
102
|
+
:param n_epochs: Number of training epochs to run
|
|
103
|
+
:param max_steps_per_epoch: Maximum number of steps to run per epoch
|
|
104
|
+
:param gradient_accumulation_steps: Number of steps to accumulate gradients before updating
|
|
105
|
+
:param max_validation_steps: (Optional) Maximum number of validation steps per epoch
|
|
106
|
+
:param data_config: (Optional) Configuration for data loading and formatting
|
|
107
|
+
:param optimizer_config: (Optional) Configuration for the optimization algorithm
|
|
108
|
+
:param efficiency_config: (Optional) Configuration for memory and compute optimizations
|
|
109
|
+
:param dtype: (Optional) Data type for model parameters (bf16, fp16, fp32)
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
n_epochs: int
|
|
113
|
+
max_steps_per_epoch: int = 1
|
|
114
|
+
gradient_accumulation_steps: int = 1
|
|
115
|
+
max_validation_steps: int | None = 1
|
|
116
|
+
data_config: DataConfig | None = None
|
|
117
|
+
optimizer_config: OptimizerConfig | None = None
|
|
118
|
+
efficiency_config: EfficiencyConfig | None = None
|
|
119
|
+
dtype: str | None = "bf16"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@json_schema_type
|
|
123
|
+
class LoraFinetuningConfig(BaseModel):
|
|
124
|
+
"""Configuration for Low-Rank Adaptation (LoRA) fine-tuning.
|
|
125
|
+
|
|
126
|
+
:param type: Algorithm type identifier, always "LoRA"
|
|
127
|
+
:param lora_attn_modules: List of attention module names to apply LoRA to
|
|
128
|
+
:param apply_lora_to_mlp: Whether to apply LoRA to MLP layers
|
|
129
|
+
:param apply_lora_to_output: Whether to apply LoRA to output projection layers
|
|
130
|
+
:param rank: Rank of the LoRA adaptation (lower rank = fewer parameters)
|
|
131
|
+
:param alpha: LoRA scaling parameter that controls adaptation strength
|
|
132
|
+
:param use_dora: (Optional) Whether to use DoRA (Weight-Decomposed Low-Rank Adaptation)
|
|
133
|
+
:param quantize_base: (Optional) Whether to quantize the base model weights
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
type: Literal["LoRA"] = "LoRA"
|
|
137
|
+
lora_attn_modules: list[str]
|
|
138
|
+
apply_lora_to_mlp: bool
|
|
139
|
+
apply_lora_to_output: bool
|
|
140
|
+
rank: int
|
|
141
|
+
alpha: int
|
|
142
|
+
use_dora: bool | None = False
|
|
143
|
+
quantize_base: bool | None = False
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@json_schema_type
|
|
147
|
+
class QATFinetuningConfig(BaseModel):
|
|
148
|
+
"""Configuration for Quantization-Aware Training (QAT) fine-tuning.
|
|
149
|
+
|
|
150
|
+
:param type: Algorithm type identifier, always "QAT"
|
|
151
|
+
:param quantizer_name: Name of the quantization algorithm to use
|
|
152
|
+
:param group_size: Size of groups for grouped quantization
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
type: Literal["QAT"] = "QAT"
|
|
156
|
+
quantizer_name: str
|
|
157
|
+
group_size: int
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
AlgorithmConfig = Annotated[LoraFinetuningConfig | QATFinetuningConfig, Field(discriminator="type")]
|
|
161
|
+
register_schema(AlgorithmConfig, name="AlgorithmConfig")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@json_schema_type
|
|
165
|
+
class PostTrainingJobLogStream(BaseModel):
|
|
166
|
+
"""Stream of logs from a finetuning job.
|
|
167
|
+
|
|
168
|
+
:param job_uuid: Unique identifier for the training job
|
|
169
|
+
:param log_lines: List of log message strings from the training process
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
job_uuid: str
|
|
173
|
+
log_lines: list[str]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@json_schema_type
|
|
177
|
+
class RLHFAlgorithm(Enum):
|
|
178
|
+
"""Available reinforcement learning from human feedback algorithms.
|
|
179
|
+
:cvar dpo: Direct Preference Optimization algorithm
|
|
180
|
+
"""
|
|
181
|
+
|
|
182
|
+
dpo = "dpo"
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@json_schema_type
|
|
186
|
+
class DPOLossType(Enum):
|
|
187
|
+
sigmoid = "sigmoid"
|
|
188
|
+
hinge = "hinge"
|
|
189
|
+
ipo = "ipo"
|
|
190
|
+
kto_pair = "kto_pair"
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@json_schema_type
|
|
194
|
+
class DPOAlignmentConfig(BaseModel):
|
|
195
|
+
"""Configuration for Direct Preference Optimization (DPO) alignment.
|
|
196
|
+
|
|
197
|
+
:param beta: Temperature parameter for the DPO loss
|
|
198
|
+
:param loss_type: The type of loss function to use for DPO
|
|
199
|
+
"""
|
|
200
|
+
|
|
201
|
+
beta: float
|
|
202
|
+
loss_type: DPOLossType = DPOLossType.sigmoid
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@json_schema_type
|
|
206
|
+
class PostTrainingRLHFRequest(BaseModel):
|
|
207
|
+
"""Request to finetune a model using reinforcement learning from human feedback.
|
|
208
|
+
|
|
209
|
+
:param job_uuid: Unique identifier for the training job
|
|
210
|
+
:param finetuned_model: URL or path to the base model to fine-tune
|
|
211
|
+
:param dataset_id: Unique identifier for the training dataset
|
|
212
|
+
:param validation_dataset_id: Unique identifier for the validation dataset
|
|
213
|
+
:param algorithm: RLHF algorithm to use for training
|
|
214
|
+
:param algorithm_config: Configuration parameters for the RLHF algorithm
|
|
215
|
+
:param optimizer_config: Configuration parameters for the optimization algorithm
|
|
216
|
+
:param training_config: Configuration parameters for the training process
|
|
217
|
+
:param hyperparam_search_config: Configuration for hyperparameter search
|
|
218
|
+
:param logger_config: Configuration for training logging
|
|
219
|
+
"""
|
|
220
|
+
|
|
221
|
+
job_uuid: str
|
|
222
|
+
|
|
223
|
+
finetuned_model: URL
|
|
224
|
+
|
|
225
|
+
dataset_id: str
|
|
226
|
+
validation_dataset_id: str
|
|
227
|
+
|
|
228
|
+
algorithm: RLHFAlgorithm
|
|
229
|
+
algorithm_config: DPOAlignmentConfig
|
|
230
|
+
|
|
231
|
+
optimizer_config: OptimizerConfig
|
|
232
|
+
training_config: TrainingConfig
|
|
233
|
+
|
|
234
|
+
# TODO: define these
|
|
235
|
+
hyperparam_search_config: dict[str, Any]
|
|
236
|
+
logger_config: dict[str, Any]
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@json_schema_type
|
|
240
|
+
class PostTrainingJob(BaseModel):
|
|
241
|
+
job_uuid: str
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@json_schema_type
|
|
245
|
+
class PostTrainingJobStatusResponse(BaseModel):
|
|
246
|
+
"""Status of a finetuning job.
|
|
247
|
+
|
|
248
|
+
:param job_uuid: Unique identifier for the training job
|
|
249
|
+
:param status: Current status of the training job
|
|
250
|
+
:param scheduled_at: (Optional) Timestamp when the job was scheduled
|
|
251
|
+
:param started_at: (Optional) Timestamp when the job execution began
|
|
252
|
+
:param completed_at: (Optional) Timestamp when the job finished, if completed
|
|
253
|
+
:param resources_allocated: (Optional) Information about computational resources allocated to the job
|
|
254
|
+
:param checkpoints: List of model checkpoints created during training
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
job_uuid: str
|
|
258
|
+
status: JobStatus
|
|
259
|
+
|
|
260
|
+
scheduled_at: datetime | None = None
|
|
261
|
+
started_at: datetime | None = None
|
|
262
|
+
completed_at: datetime | None = None
|
|
263
|
+
|
|
264
|
+
resources_allocated: dict[str, Any] | None = None
|
|
265
|
+
|
|
266
|
+
checkpoints: list[Checkpoint] = Field(default_factory=list)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
@json_schema_type
|
|
270
|
+
class ListPostTrainingJobsResponse(BaseModel):
|
|
271
|
+
data: list[PostTrainingJob]
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
@json_schema_type
|
|
275
|
+
class PostTrainingJobArtifactsResponse(BaseModel):
|
|
276
|
+
"""Artifacts of a finetuning job.
|
|
277
|
+
|
|
278
|
+
:param job_uuid: Unique identifier for the training job
|
|
279
|
+
:param checkpoints: List of model checkpoints created during training
|
|
280
|
+
"""
|
|
281
|
+
|
|
282
|
+
job_uuid: str
|
|
283
|
+
checkpoints: list[Checkpoint] = Field(default_factory=list)
|
|
284
|
+
|
|
285
|
+
# TODO(ashwin): metrics, evals
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class PostTraining(Protocol):
|
|
289
|
+
@webmethod(route="/post-training/supervised-fine-tune", method="POST", level=LLAMA_STACK_API_V1ALPHA)
|
|
290
|
+
async def supervised_fine_tune(
|
|
291
|
+
self,
|
|
292
|
+
job_uuid: str,
|
|
293
|
+
training_config: TrainingConfig,
|
|
294
|
+
hyperparam_search_config: dict[str, Any],
|
|
295
|
+
logger_config: dict[str, Any],
|
|
296
|
+
model: str | None = Field(
|
|
297
|
+
default=None,
|
|
298
|
+
description="Model descriptor for training if not in provider config`",
|
|
299
|
+
),
|
|
300
|
+
checkpoint_dir: str | None = None,
|
|
301
|
+
algorithm_config: AlgorithmConfig | None = None,
|
|
302
|
+
) -> PostTrainingJob:
|
|
303
|
+
"""Run supervised fine-tuning of a model.
|
|
304
|
+
|
|
305
|
+
:param job_uuid: The UUID of the job to create.
|
|
306
|
+
:param training_config: The training configuration.
|
|
307
|
+
:param hyperparam_search_config: The hyperparam search configuration.
|
|
308
|
+
:param logger_config: The logger configuration.
|
|
309
|
+
:param model: The model to fine-tune.
|
|
310
|
+
:param checkpoint_dir: The directory to save checkpoint(s) to.
|
|
311
|
+
:param algorithm_config: The algorithm configuration.
|
|
312
|
+
:returns: A PostTrainingJob.
|
|
313
|
+
"""
|
|
314
|
+
...
|
|
315
|
+
|
|
316
|
+
@webmethod(route="/post-training/preference-optimize", method="POST", level=LLAMA_STACK_API_V1ALPHA)
|
|
317
|
+
async def preference_optimize(
|
|
318
|
+
self,
|
|
319
|
+
job_uuid: str,
|
|
320
|
+
finetuned_model: str,
|
|
321
|
+
algorithm_config: DPOAlignmentConfig,
|
|
322
|
+
training_config: TrainingConfig,
|
|
323
|
+
hyperparam_search_config: dict[str, Any],
|
|
324
|
+
logger_config: dict[str, Any],
|
|
325
|
+
) -> PostTrainingJob:
|
|
326
|
+
"""Run preference optimization of a model.
|
|
327
|
+
|
|
328
|
+
:param job_uuid: The UUID of the job to create.
|
|
329
|
+
:param finetuned_model: The model to fine-tune.
|
|
330
|
+
:param algorithm_config: The algorithm configuration.
|
|
331
|
+
:param training_config: The training configuration.
|
|
332
|
+
:param hyperparam_search_config: The hyperparam search configuration.
|
|
333
|
+
:param logger_config: The logger configuration.
|
|
334
|
+
:returns: A PostTrainingJob.
|
|
335
|
+
"""
|
|
336
|
+
...
|
|
337
|
+
|
|
338
|
+
@webmethod(route="/post-training/jobs", method="GET", level=LLAMA_STACK_API_V1ALPHA)
|
|
339
|
+
async def get_training_jobs(self) -> ListPostTrainingJobsResponse:
|
|
340
|
+
"""Get all training jobs.
|
|
341
|
+
|
|
342
|
+
:returns: A ListPostTrainingJobsResponse.
|
|
343
|
+
"""
|
|
344
|
+
...
|
|
345
|
+
|
|
346
|
+
@webmethod(route="/post-training/job/status", method="GET", level=LLAMA_STACK_API_V1ALPHA)
|
|
347
|
+
async def get_training_job_status(self, job_uuid: str) -> PostTrainingJobStatusResponse:
|
|
348
|
+
"""Get the status of a training job.
|
|
349
|
+
|
|
350
|
+
:param job_uuid: The UUID of the job to get the status of.
|
|
351
|
+
:returns: A PostTrainingJobStatusResponse.
|
|
352
|
+
"""
|
|
353
|
+
...
|
|
354
|
+
|
|
355
|
+
@webmethod(route="/post-training/job/cancel", method="POST", level=LLAMA_STACK_API_V1ALPHA)
|
|
356
|
+
async def cancel_training_job(self, job_uuid: str) -> None:
|
|
357
|
+
"""Cancel a training job.
|
|
358
|
+
|
|
359
|
+
:param job_uuid: The UUID of the job to cancel.
|
|
360
|
+
"""
|
|
361
|
+
...
|
|
362
|
+
|
|
363
|
+
@webmethod(route="/post-training/job/artifacts", method="GET", level=LLAMA_STACK_API_V1ALPHA)
|
|
364
|
+
async def get_training_job_artifacts(self, job_uuid: str) -> PostTrainingJobArtifactsResponse:
|
|
365
|
+
"""Get the artifacts of a training job.
|
|
366
|
+
|
|
367
|
+
:param job_uuid: The UUID of the job to get the artifacts of.
|
|
368
|
+
:returns: A PostTrainingJobArtifactsResponse.
|
|
369
|
+
"""
|
|
370
|
+
...
|
|
@@ -0,0 +1,203 @@
|
|
|
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
|
+
import re
|
|
8
|
+
import secrets
|
|
9
|
+
from typing import Protocol, runtime_checkable
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
12
|
+
|
|
13
|
+
from llama_stack_api.schema_utils import json_schema_type, webmethod
|
|
14
|
+
from llama_stack_api.version import LLAMA_STACK_API_V1
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@json_schema_type
|
|
18
|
+
class Prompt(BaseModel):
|
|
19
|
+
"""A prompt resource representing a stored OpenAI Compatible prompt template in Llama Stack.
|
|
20
|
+
|
|
21
|
+
:param prompt: The system prompt text with variable placeholders. Variables are only supported when using the Responses API.
|
|
22
|
+
:param version: Version (integer starting at 1, incremented on save)
|
|
23
|
+
:param prompt_id: Unique identifier formatted as 'pmpt_<48-digit-hash>'
|
|
24
|
+
:param variables: List of prompt variable names that can be used in the prompt template
|
|
25
|
+
:param is_default: Boolean indicating whether this version is the default version for this prompt
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
prompt: str | None = Field(default=None, description="The system prompt with variable placeholders")
|
|
29
|
+
version: int = Field(description="Version (integer starting at 1, incremented on save)", ge=1)
|
|
30
|
+
prompt_id: str = Field(description="Unique identifier in format 'pmpt_<48-digit-hash>'")
|
|
31
|
+
variables: list[str] = Field(
|
|
32
|
+
default_factory=list, description="List of variable names that can be used in the prompt template"
|
|
33
|
+
)
|
|
34
|
+
is_default: bool = Field(
|
|
35
|
+
default=False, description="Boolean indicating whether this version is the default version"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
@field_validator("prompt_id")
|
|
39
|
+
@classmethod
|
|
40
|
+
def validate_prompt_id(cls, prompt_id: str) -> str:
|
|
41
|
+
if not isinstance(prompt_id, str):
|
|
42
|
+
raise TypeError("prompt_id must be a string in format 'pmpt_<48-digit-hash>'")
|
|
43
|
+
|
|
44
|
+
if not prompt_id.startswith("pmpt_"):
|
|
45
|
+
raise ValueError("prompt_id must start with 'pmpt_' prefix")
|
|
46
|
+
|
|
47
|
+
hex_part = prompt_id[5:]
|
|
48
|
+
if len(hex_part) != 48:
|
|
49
|
+
raise ValueError("prompt_id must be in format 'pmpt_<48-digit-hash>' (48 lowercase hex chars)")
|
|
50
|
+
|
|
51
|
+
for char in hex_part:
|
|
52
|
+
if char not in "0123456789abcdef":
|
|
53
|
+
raise ValueError("prompt_id hex part must contain only lowercase hex characters [0-9a-f]")
|
|
54
|
+
|
|
55
|
+
return prompt_id
|
|
56
|
+
|
|
57
|
+
@field_validator("version")
|
|
58
|
+
@classmethod
|
|
59
|
+
def validate_version(cls, prompt_version: int) -> int:
|
|
60
|
+
if prompt_version < 1:
|
|
61
|
+
raise ValueError("version must be >= 1")
|
|
62
|
+
return prompt_version
|
|
63
|
+
|
|
64
|
+
@model_validator(mode="after")
|
|
65
|
+
def validate_prompt_variables(self):
|
|
66
|
+
"""Validate that all variables used in the prompt are declared in the variables list."""
|
|
67
|
+
if not self.prompt:
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
prompt_variables = set(re.findall(r"{{\s*(\w+)\s*}}", self.prompt))
|
|
71
|
+
declared_variables = set(self.variables)
|
|
72
|
+
|
|
73
|
+
undeclared = prompt_variables - declared_variables
|
|
74
|
+
if undeclared:
|
|
75
|
+
raise ValueError(f"Prompt contains undeclared variables: {sorted(undeclared)}")
|
|
76
|
+
|
|
77
|
+
return self
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def generate_prompt_id(cls) -> str:
|
|
81
|
+
# Generate 48 hex characters (24 bytes)
|
|
82
|
+
random_bytes = secrets.token_bytes(24)
|
|
83
|
+
hex_string = random_bytes.hex()
|
|
84
|
+
return f"pmpt_{hex_string}"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@json_schema_type
|
|
88
|
+
class ListPromptsResponse(BaseModel):
|
|
89
|
+
"""Response model to list prompts."""
|
|
90
|
+
|
|
91
|
+
data: list[Prompt]
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@runtime_checkable
|
|
95
|
+
class Prompts(Protocol):
|
|
96
|
+
"""Prompts
|
|
97
|
+
|
|
98
|
+
Protocol for prompt management operations."""
|
|
99
|
+
|
|
100
|
+
@webmethod(route="/prompts", method="GET", level=LLAMA_STACK_API_V1)
|
|
101
|
+
async def list_prompts(self) -> ListPromptsResponse:
|
|
102
|
+
"""List all prompts.
|
|
103
|
+
|
|
104
|
+
:returns: A ListPromptsResponse containing all prompts.
|
|
105
|
+
"""
|
|
106
|
+
...
|
|
107
|
+
|
|
108
|
+
@webmethod(route="/prompts/{prompt_id}/versions", method="GET", level=LLAMA_STACK_API_V1)
|
|
109
|
+
async def list_prompt_versions(
|
|
110
|
+
self,
|
|
111
|
+
prompt_id: str,
|
|
112
|
+
) -> ListPromptsResponse:
|
|
113
|
+
"""List prompt versions.
|
|
114
|
+
|
|
115
|
+
List all versions of a specific prompt.
|
|
116
|
+
|
|
117
|
+
:param prompt_id: The identifier of the prompt to list versions for.
|
|
118
|
+
:returns: A ListPromptsResponse containing all versions of the prompt.
|
|
119
|
+
"""
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
@webmethod(route="/prompts/{prompt_id}", method="GET", level=LLAMA_STACK_API_V1)
|
|
123
|
+
async def get_prompt(
|
|
124
|
+
self,
|
|
125
|
+
prompt_id: str,
|
|
126
|
+
version: int | None = None,
|
|
127
|
+
) -> Prompt:
|
|
128
|
+
"""Get prompt.
|
|
129
|
+
|
|
130
|
+
Get a prompt by its identifier and optional version.
|
|
131
|
+
|
|
132
|
+
:param prompt_id: The identifier of the prompt to get.
|
|
133
|
+
:param version: The version of the prompt to get (defaults to latest).
|
|
134
|
+
:returns: A Prompt resource.
|
|
135
|
+
"""
|
|
136
|
+
...
|
|
137
|
+
|
|
138
|
+
@webmethod(route="/prompts", method="POST", level=LLAMA_STACK_API_V1)
|
|
139
|
+
async def create_prompt(
|
|
140
|
+
self,
|
|
141
|
+
prompt: str,
|
|
142
|
+
variables: list[str] | None = None,
|
|
143
|
+
) -> Prompt:
|
|
144
|
+
"""Create prompt.
|
|
145
|
+
|
|
146
|
+
Create a new prompt.
|
|
147
|
+
|
|
148
|
+
:param prompt: The prompt text content with variable placeholders.
|
|
149
|
+
:param variables: List of variable names that can be used in the prompt template.
|
|
150
|
+
:returns: The created Prompt resource.
|
|
151
|
+
"""
|
|
152
|
+
...
|
|
153
|
+
|
|
154
|
+
@webmethod(route="/prompts/{prompt_id}", method="PUT", level=LLAMA_STACK_API_V1)
|
|
155
|
+
async def update_prompt(
|
|
156
|
+
self,
|
|
157
|
+
prompt_id: str,
|
|
158
|
+
prompt: str,
|
|
159
|
+
version: int,
|
|
160
|
+
variables: list[str] | None = None,
|
|
161
|
+
set_as_default: bool = True,
|
|
162
|
+
) -> Prompt:
|
|
163
|
+
"""Update prompt.
|
|
164
|
+
|
|
165
|
+
Update an existing prompt (increments version).
|
|
166
|
+
|
|
167
|
+
:param prompt_id: The identifier of the prompt to update.
|
|
168
|
+
:param prompt: The updated prompt text content.
|
|
169
|
+
:param version: The current version of the prompt being updated.
|
|
170
|
+
:param variables: Updated list of variable names that can be used in the prompt template.
|
|
171
|
+
:param set_as_default: Set the new version as the default (default=True).
|
|
172
|
+
:returns: The updated Prompt resource with incremented version.
|
|
173
|
+
"""
|
|
174
|
+
...
|
|
175
|
+
|
|
176
|
+
@webmethod(route="/prompts/{prompt_id}", method="DELETE", level=LLAMA_STACK_API_V1)
|
|
177
|
+
async def delete_prompt(
|
|
178
|
+
self,
|
|
179
|
+
prompt_id: str,
|
|
180
|
+
) -> None:
|
|
181
|
+
"""Delete prompt.
|
|
182
|
+
|
|
183
|
+
Delete a prompt.
|
|
184
|
+
|
|
185
|
+
:param prompt_id: The identifier of the prompt to delete.
|
|
186
|
+
"""
|
|
187
|
+
...
|
|
188
|
+
|
|
189
|
+
@webmethod(route="/prompts/{prompt_id}/set-default-version", method="PUT", level=LLAMA_STACK_API_V1)
|
|
190
|
+
async def set_default_version(
|
|
191
|
+
self,
|
|
192
|
+
prompt_id: str,
|
|
193
|
+
version: int,
|
|
194
|
+
) -> Prompt:
|
|
195
|
+
"""Set prompt version.
|
|
196
|
+
|
|
197
|
+
Set which version of a prompt should be the default in get_prompt (latest).
|
|
198
|
+
|
|
199
|
+
:param prompt_id: The identifier of the prompt.
|
|
200
|
+
:param version: The version to set as default.
|
|
201
|
+
:returns: The prompt with the specified version now set as default.
|
|
202
|
+
"""
|
|
203
|
+
...
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
"""Providers API protocol and models.
|
|
8
|
+
|
|
9
|
+
This module contains the Providers protocol definition.
|
|
10
|
+
Pydantic models are defined in llama_stack_api.providers.models.
|
|
11
|
+
The FastAPI router is defined in llama_stack_api.providers.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 Providers
|
|
19
|
+
|
|
20
|
+
# Import models for re-export
|
|
21
|
+
from .models import (
|
|
22
|
+
InspectProviderRequest,
|
|
23
|
+
ListProvidersResponse,
|
|
24
|
+
ProviderInfo,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"Providers",
|
|
29
|
+
"ProviderInfo",
|
|
30
|
+
"ListProvidersResponse",
|
|
31
|
+
"InspectProviderRequest",
|
|
32
|
+
"fastapi_routes",
|
|
33
|
+
]
|
|
@@ -0,0 +1,16 @@
|
|
|
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 InspectProviderRequest, ListProvidersResponse, ProviderInfo
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@runtime_checkable
|
|
13
|
+
class Providers(Protocol):
|
|
14
|
+
async def list_providers(self) -> ListProvidersResponse: ...
|
|
15
|
+
|
|
16
|
+
async def inspect_provider(self, request: InspectProviderRequest) -> ProviderInfo: ...
|
|
@@ -0,0 +1,57 @@
|
|
|
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 Providers API.
|
|
8
|
+
|
|
9
|
+
This module defines the FastAPI router for the Providers API using standard
|
|
10
|
+
FastAPI route decorators.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from typing import Annotated
|
|
14
|
+
|
|
15
|
+
from fastapi import APIRouter, 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 Providers
|
|
21
|
+
from .models import InspectProviderRequest, ListProvidersResponse, ProviderInfo
|
|
22
|
+
|
|
23
|
+
# Path parameter dependencies for single-field models
|
|
24
|
+
get_inspect_provider_request = create_path_dependency(InspectProviderRequest)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def create_router(impl: Providers) -> APIRouter:
|
|
28
|
+
"""Create a FastAPI router for the Providers API."""
|
|
29
|
+
router = APIRouter(
|
|
30
|
+
prefix=f"/{LLAMA_STACK_API_V1}",
|
|
31
|
+
tags=["Providers"],
|
|
32
|
+
responses=standard_responses,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
@router.get(
|
|
36
|
+
"/providers",
|
|
37
|
+
response_model=ListProvidersResponse,
|
|
38
|
+
summary="List providers.",
|
|
39
|
+
description="List all available providers.",
|
|
40
|
+
responses={200: {"description": "A ListProvidersResponse containing information about all providers."}},
|
|
41
|
+
)
|
|
42
|
+
async def list_providers() -> ListProvidersResponse:
|
|
43
|
+
return await impl.list_providers()
|
|
44
|
+
|
|
45
|
+
@router.get(
|
|
46
|
+
"/providers/{provider_id}",
|
|
47
|
+
response_model=ProviderInfo,
|
|
48
|
+
summary="Get provider.",
|
|
49
|
+
description="Get detailed information about a specific provider.",
|
|
50
|
+
responses={200: {"description": "A ProviderInfo object containing the provider's details."}},
|
|
51
|
+
)
|
|
52
|
+
async def inspect_provider(
|
|
53
|
+
request: Annotated[InspectProviderRequest, Depends(get_inspect_provider_request)],
|
|
54
|
+
) -> ProviderInfo:
|
|
55
|
+
return await impl.inspect_provider(request)
|
|
56
|
+
|
|
57
|
+
return router
|