llama-stack-api 0.4.3__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.3.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.4.dist-info/top_level.txt +1 -0
- llama_stack_api-0.4.3.dist-info/RECORD +0 -4
- llama_stack_api-0.4.3.dist-info/top_level.txt +0 -1
- {llama_stack_api-0.4.3.dist-info → llama_stack_api-0.4.4.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,270 @@
|
|
|
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 enum import StrEnum
|
|
8
|
+
from typing import Annotated, Literal, Protocol, runtime_checkable
|
|
9
|
+
|
|
10
|
+
from pydantic import BaseModel, Field
|
|
11
|
+
|
|
12
|
+
from llama_stack_api.openai_responses import (
|
|
13
|
+
OpenAIResponseInputFunctionToolCallOutput,
|
|
14
|
+
OpenAIResponseMCPApprovalRequest,
|
|
15
|
+
OpenAIResponseMCPApprovalResponse,
|
|
16
|
+
OpenAIResponseMessage,
|
|
17
|
+
OpenAIResponseOutputMessageFileSearchToolCall,
|
|
18
|
+
OpenAIResponseOutputMessageFunctionToolCall,
|
|
19
|
+
OpenAIResponseOutputMessageMCPCall,
|
|
20
|
+
OpenAIResponseOutputMessageMCPListTools,
|
|
21
|
+
OpenAIResponseOutputMessageWebSearchToolCall,
|
|
22
|
+
)
|
|
23
|
+
from llama_stack_api.schema_utils import json_schema_type, register_schema, webmethod
|
|
24
|
+
from llama_stack_api.version import LLAMA_STACK_API_V1
|
|
25
|
+
|
|
26
|
+
Metadata = dict[str, str]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@json_schema_type
|
|
30
|
+
class Conversation(BaseModel):
|
|
31
|
+
"""OpenAI-compatible conversation object."""
|
|
32
|
+
|
|
33
|
+
id: str = Field(..., description="The unique ID of the conversation.")
|
|
34
|
+
object: Literal["conversation"] = Field(
|
|
35
|
+
default="conversation", description="The object type, which is always conversation."
|
|
36
|
+
)
|
|
37
|
+
created_at: int = Field(
|
|
38
|
+
..., description="The time at which the conversation was created, measured in seconds since the Unix epoch."
|
|
39
|
+
)
|
|
40
|
+
metadata: Metadata | None = Field(
|
|
41
|
+
default=None,
|
|
42
|
+
description="Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard.",
|
|
43
|
+
)
|
|
44
|
+
items: list[dict] | None = Field(
|
|
45
|
+
default=None,
|
|
46
|
+
description="Initial items to include in the conversation context. You may add up to 20 items at a time.",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@json_schema_type
|
|
51
|
+
class ConversationMessage(BaseModel):
|
|
52
|
+
"""OpenAI-compatible message item for conversations."""
|
|
53
|
+
|
|
54
|
+
id: str = Field(..., description="unique identifier for this message")
|
|
55
|
+
content: list[dict] = Field(..., description="message content")
|
|
56
|
+
role: str = Field(..., description="message role")
|
|
57
|
+
status: str = Field(..., description="message status")
|
|
58
|
+
type: Literal["message"] = "message"
|
|
59
|
+
object: Literal["message"] = "message"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
ConversationItem = Annotated[
|
|
63
|
+
OpenAIResponseMessage
|
|
64
|
+
| OpenAIResponseOutputMessageWebSearchToolCall
|
|
65
|
+
| OpenAIResponseOutputMessageFileSearchToolCall
|
|
66
|
+
| OpenAIResponseOutputMessageFunctionToolCall
|
|
67
|
+
| OpenAIResponseInputFunctionToolCallOutput
|
|
68
|
+
| OpenAIResponseMCPApprovalRequest
|
|
69
|
+
| OpenAIResponseMCPApprovalResponse
|
|
70
|
+
| OpenAIResponseOutputMessageMCPCall
|
|
71
|
+
| OpenAIResponseOutputMessageMCPListTools
|
|
72
|
+
| OpenAIResponseOutputMessageMCPCall
|
|
73
|
+
| OpenAIResponseOutputMessageMCPListTools,
|
|
74
|
+
Field(discriminator="type"),
|
|
75
|
+
]
|
|
76
|
+
register_schema(ConversationItem, name="ConversationItem")
|
|
77
|
+
|
|
78
|
+
# Using OpenAI types directly caused issues but some notes for reference:
|
|
79
|
+
# Note that ConversationItem is a Annotated Union of the types below:
|
|
80
|
+
# from openai.types.responses import *
|
|
81
|
+
# from openai.types.responses.response_item import *
|
|
82
|
+
# from openai.types.conversations import ConversationItem
|
|
83
|
+
# f = [
|
|
84
|
+
# ResponseFunctionToolCallItem,
|
|
85
|
+
# ResponseFunctionToolCallOutputItem,
|
|
86
|
+
# ResponseFileSearchToolCall,
|
|
87
|
+
# ResponseFunctionWebSearch,
|
|
88
|
+
# ImageGenerationCall,
|
|
89
|
+
# ResponseComputerToolCall,
|
|
90
|
+
# ResponseComputerToolCallOutputItem,
|
|
91
|
+
# ResponseReasoningItem,
|
|
92
|
+
# ResponseCodeInterpreterToolCall,
|
|
93
|
+
# LocalShellCall,
|
|
94
|
+
# LocalShellCallOutput,
|
|
95
|
+
# McpListTools,
|
|
96
|
+
# McpApprovalRequest,
|
|
97
|
+
# McpApprovalResponse,
|
|
98
|
+
# McpCall,
|
|
99
|
+
# ResponseCustomToolCall,
|
|
100
|
+
# ResponseCustomToolCallOutput
|
|
101
|
+
# ]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@json_schema_type
|
|
105
|
+
class ConversationDeletedResource(BaseModel):
|
|
106
|
+
"""Response for deleted conversation."""
|
|
107
|
+
|
|
108
|
+
id: str = Field(..., description="The deleted conversation identifier")
|
|
109
|
+
object: str = Field(default="conversation.deleted", description="Object type")
|
|
110
|
+
deleted: bool = Field(default=True, description="Whether the object was deleted")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@json_schema_type
|
|
114
|
+
class ConversationItemCreateRequest(BaseModel):
|
|
115
|
+
"""Request body for creating conversation items."""
|
|
116
|
+
|
|
117
|
+
items: list[ConversationItem] = Field(
|
|
118
|
+
...,
|
|
119
|
+
description="Items to include in the conversation context. You may add up to 20 items at a time.",
|
|
120
|
+
max_length=20,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class ConversationItemInclude(StrEnum):
|
|
125
|
+
"""
|
|
126
|
+
Specify additional output data to include in the model response.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
web_search_call_action_sources = "web_search_call.action.sources"
|
|
130
|
+
code_interpreter_call_outputs = "code_interpreter_call.outputs"
|
|
131
|
+
computer_call_output_output_image_url = "computer_call_output.output.image_url"
|
|
132
|
+
file_search_call_results = "file_search_call.results"
|
|
133
|
+
message_input_image_image_url = "message.input_image.image_url"
|
|
134
|
+
message_output_text_logprobs = "message.output_text.logprobs"
|
|
135
|
+
reasoning_encrypted_content = "reasoning.encrypted_content"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@json_schema_type
|
|
139
|
+
class ConversationItemList(BaseModel):
|
|
140
|
+
"""List of conversation items with pagination."""
|
|
141
|
+
|
|
142
|
+
object: str = Field(default="list", description="Object type")
|
|
143
|
+
data: list[ConversationItem] = Field(..., description="List of conversation items")
|
|
144
|
+
first_id: str | None = Field(default=None, description="The ID of the first item in the list")
|
|
145
|
+
last_id: str | None = Field(default=None, description="The ID of the last item in the list")
|
|
146
|
+
has_more: bool = Field(default=False, description="Whether there are more items available")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@json_schema_type
|
|
150
|
+
class ConversationItemDeletedResource(BaseModel):
|
|
151
|
+
"""Response for deleted conversation item."""
|
|
152
|
+
|
|
153
|
+
id: str = Field(..., description="The deleted item identifier")
|
|
154
|
+
object: str = Field(default="conversation.item.deleted", description="Object type")
|
|
155
|
+
deleted: bool = Field(default=True, description="Whether the object was deleted")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@runtime_checkable
|
|
159
|
+
class Conversations(Protocol):
|
|
160
|
+
"""Conversations
|
|
161
|
+
|
|
162
|
+
Protocol for conversation management operations."""
|
|
163
|
+
|
|
164
|
+
@webmethod(route="/conversations", method="POST", level=LLAMA_STACK_API_V1)
|
|
165
|
+
async def create_conversation(
|
|
166
|
+
self, items: list[ConversationItem] | None = None, metadata: Metadata | None = None
|
|
167
|
+
) -> Conversation:
|
|
168
|
+
"""Create a conversation.
|
|
169
|
+
|
|
170
|
+
Create a conversation.
|
|
171
|
+
|
|
172
|
+
:param items: Initial items to include in the conversation context.
|
|
173
|
+
:param metadata: Set of key-value pairs that can be attached to an object.
|
|
174
|
+
:returns: The created conversation object.
|
|
175
|
+
"""
|
|
176
|
+
...
|
|
177
|
+
|
|
178
|
+
@webmethod(route="/conversations/{conversation_id}", method="GET", level=LLAMA_STACK_API_V1)
|
|
179
|
+
async def get_conversation(self, conversation_id: str) -> Conversation:
|
|
180
|
+
"""Retrieve a conversation.
|
|
181
|
+
|
|
182
|
+
Get a conversation with the given ID.
|
|
183
|
+
|
|
184
|
+
:param conversation_id: The conversation identifier.
|
|
185
|
+
:returns: The conversation object.
|
|
186
|
+
"""
|
|
187
|
+
...
|
|
188
|
+
|
|
189
|
+
@webmethod(route="/conversations/{conversation_id}", method="POST", level=LLAMA_STACK_API_V1)
|
|
190
|
+
async def update_conversation(self, conversation_id: str, metadata: Metadata) -> Conversation:
|
|
191
|
+
"""Update a conversation.
|
|
192
|
+
|
|
193
|
+
Update a conversation's metadata with the given ID.
|
|
194
|
+
|
|
195
|
+
:param conversation_id: The conversation identifier.
|
|
196
|
+
:param metadata: Set of key-value pairs that can be attached to an object.
|
|
197
|
+
:returns: The updated conversation object.
|
|
198
|
+
"""
|
|
199
|
+
...
|
|
200
|
+
|
|
201
|
+
@webmethod(route="/conversations/{conversation_id}", method="DELETE", level=LLAMA_STACK_API_V1)
|
|
202
|
+
async def openai_delete_conversation(self, conversation_id: str) -> ConversationDeletedResource:
|
|
203
|
+
"""Delete a conversation.
|
|
204
|
+
|
|
205
|
+
Delete a conversation with the given ID.
|
|
206
|
+
|
|
207
|
+
:param conversation_id: The conversation identifier.
|
|
208
|
+
:returns: The deleted conversation resource.
|
|
209
|
+
"""
|
|
210
|
+
...
|
|
211
|
+
|
|
212
|
+
@webmethod(route="/conversations/{conversation_id}/items", method="POST", level=LLAMA_STACK_API_V1)
|
|
213
|
+
async def add_items(self, conversation_id: str, items: list[ConversationItem]) -> ConversationItemList:
|
|
214
|
+
"""Create items.
|
|
215
|
+
|
|
216
|
+
Create items in the conversation.
|
|
217
|
+
|
|
218
|
+
:param conversation_id: The conversation identifier.
|
|
219
|
+
:param items: Items to include in the conversation context.
|
|
220
|
+
:returns: List of created items.
|
|
221
|
+
"""
|
|
222
|
+
...
|
|
223
|
+
|
|
224
|
+
@webmethod(route="/conversations/{conversation_id}/items/{item_id}", method="GET", level=LLAMA_STACK_API_V1)
|
|
225
|
+
async def retrieve(self, conversation_id: str, item_id: str) -> ConversationItem:
|
|
226
|
+
"""Retrieve an item.
|
|
227
|
+
|
|
228
|
+
Retrieve a conversation item.
|
|
229
|
+
|
|
230
|
+
:param conversation_id: The conversation identifier.
|
|
231
|
+
:param item_id: The item identifier.
|
|
232
|
+
:returns: The conversation item.
|
|
233
|
+
"""
|
|
234
|
+
...
|
|
235
|
+
|
|
236
|
+
@webmethod(route="/conversations/{conversation_id}/items", method="GET", level=LLAMA_STACK_API_V1)
|
|
237
|
+
async def list_items(
|
|
238
|
+
self,
|
|
239
|
+
conversation_id: str,
|
|
240
|
+
after: str | None = None,
|
|
241
|
+
include: list[ConversationItemInclude] | None = None,
|
|
242
|
+
limit: int | None = None,
|
|
243
|
+
order: Literal["asc", "desc"] | None = None,
|
|
244
|
+
) -> ConversationItemList:
|
|
245
|
+
"""List items.
|
|
246
|
+
|
|
247
|
+
List items in the conversation.
|
|
248
|
+
|
|
249
|
+
:param conversation_id: The conversation identifier.
|
|
250
|
+
:param after: An item ID to list items after, used in pagination.
|
|
251
|
+
:param include: Specify additional output data to include in the response.
|
|
252
|
+
:param limit: A limit on the number of objects to be returned (1-100, default 20).
|
|
253
|
+
:param order: The order to return items in (asc or desc, default desc).
|
|
254
|
+
:returns: List of conversation items.
|
|
255
|
+
"""
|
|
256
|
+
...
|
|
257
|
+
|
|
258
|
+
@webmethod(route="/conversations/{conversation_id}/items/{item_id}", method="DELETE", level=LLAMA_STACK_API_V1)
|
|
259
|
+
async def openai_delete_conversation_item(
|
|
260
|
+
self, conversation_id: str, item_id: str
|
|
261
|
+
) -> ConversationItemDeletedResource:
|
|
262
|
+
"""Delete an item.
|
|
263
|
+
|
|
264
|
+
Delete a conversation item.
|
|
265
|
+
|
|
266
|
+
:param conversation_id: The conversation identifier.
|
|
267
|
+
:param item_id: The item identifier.
|
|
268
|
+
:returns: The deleted item resource.
|
|
269
|
+
"""
|
|
270
|
+
...
|
|
@@ -0,0 +1,55 @@
|
|
|
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 Any, Protocol, runtime_checkable
|
|
8
|
+
|
|
9
|
+
from llama_stack_api.common.responses import PaginatedResponse
|
|
10
|
+
from llama_stack_api.datasets import Dataset
|
|
11
|
+
from llama_stack_api.schema_utils import webmethod
|
|
12
|
+
from llama_stack_api.version import LLAMA_STACK_API_V1BETA
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class DatasetStore(Protocol):
|
|
16
|
+
def get_dataset(self, dataset_id: str) -> Dataset: ...
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@runtime_checkable
|
|
20
|
+
class DatasetIO(Protocol):
|
|
21
|
+
# keeping for aligning with inference/safety, but this is not used
|
|
22
|
+
dataset_store: DatasetStore
|
|
23
|
+
|
|
24
|
+
@webmethod(route="/datasetio/iterrows/{dataset_id:path}", method="GET", level=LLAMA_STACK_API_V1BETA)
|
|
25
|
+
async def iterrows(
|
|
26
|
+
self,
|
|
27
|
+
dataset_id: str,
|
|
28
|
+
start_index: int | None = None,
|
|
29
|
+
limit: int | None = None,
|
|
30
|
+
) -> PaginatedResponse:
|
|
31
|
+
"""Get a paginated list of rows from a dataset.
|
|
32
|
+
|
|
33
|
+
Uses offset-based pagination where:
|
|
34
|
+
- start_index: The starting index (0-based). If None, starts from beginning.
|
|
35
|
+
- limit: Number of items to return. If None or -1, returns all items.
|
|
36
|
+
|
|
37
|
+
The response includes:
|
|
38
|
+
- data: List of items for the current page.
|
|
39
|
+
- has_more: Whether there are more items available after this set.
|
|
40
|
+
|
|
41
|
+
:param dataset_id: The ID of the dataset to get the rows from.
|
|
42
|
+
:param start_index: Index into dataset for the first row to get. Get all rows if None.
|
|
43
|
+
:param limit: The number of rows to get.
|
|
44
|
+
:returns: A PaginatedResponse.
|
|
45
|
+
"""
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
@webmethod(route="/datasetio/append-rows/{dataset_id:path}", method="POST", level=LLAMA_STACK_API_V1BETA)
|
|
49
|
+
async def append_rows(self, dataset_id: str, rows: list[dict[str, Any]]) -> None:
|
|
50
|
+
"""Append rows to a dataset.
|
|
51
|
+
|
|
52
|
+
:param dataset_id: The ID of the dataset to append the rows to.
|
|
53
|
+
:param rows: The rows to append to the dataset.
|
|
54
|
+
"""
|
|
55
|
+
...
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
"""Datasets API protocol and models.
|
|
8
|
+
|
|
9
|
+
This module contains the Datasets protocol definition.
|
|
10
|
+
Pydantic models are defined in llama_stack_api.datasets.models.
|
|
11
|
+
The FastAPI router is defined in llama_stack_api.datasets.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 Datasets
|
|
19
|
+
|
|
20
|
+
# Import models for re-export
|
|
21
|
+
from .models import (
|
|
22
|
+
CommonDatasetFields,
|
|
23
|
+
Dataset,
|
|
24
|
+
DatasetPurpose,
|
|
25
|
+
DatasetType,
|
|
26
|
+
DataSource,
|
|
27
|
+
GetDatasetRequest,
|
|
28
|
+
ListDatasetsResponse,
|
|
29
|
+
RegisterDatasetRequest,
|
|
30
|
+
RowsDataSource,
|
|
31
|
+
UnregisterDatasetRequest,
|
|
32
|
+
URIDataSource,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Define DatasetInput for backward compatibility
|
|
37
|
+
class DatasetInput(CommonDatasetFields):
|
|
38
|
+
"""Input parameters for dataset operations.
|
|
39
|
+
|
|
40
|
+
:param dataset_id: Unique identifier for the dataset
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
dataset_id: str
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"Datasets",
|
|
48
|
+
"Dataset",
|
|
49
|
+
"CommonDatasetFields",
|
|
50
|
+
"DatasetPurpose",
|
|
51
|
+
"DataSource",
|
|
52
|
+
"DatasetInput",
|
|
53
|
+
"DatasetType",
|
|
54
|
+
"RowsDataSource",
|
|
55
|
+
"URIDataSource",
|
|
56
|
+
"ListDatasetsResponse",
|
|
57
|
+
"RegisterDatasetRequest",
|
|
58
|
+
"GetDatasetRequest",
|
|
59
|
+
"UnregisterDatasetRequest",
|
|
60
|
+
"fastapi_routes",
|
|
61
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
"""Datasets API protocol definition.
|
|
8
|
+
|
|
9
|
+
This module contains the Datasets protocol definition.
|
|
10
|
+
Pydantic models are defined in llama_stack_api.datasets.models.
|
|
11
|
+
The FastAPI router is defined in llama_stack_api.datasets.fastapi_routes.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from typing import Protocol, runtime_checkable
|
|
15
|
+
|
|
16
|
+
from .models import (
|
|
17
|
+
Dataset,
|
|
18
|
+
GetDatasetRequest,
|
|
19
|
+
ListDatasetsResponse,
|
|
20
|
+
RegisterDatasetRequest,
|
|
21
|
+
UnregisterDatasetRequest,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@runtime_checkable
|
|
26
|
+
class Datasets(Protocol):
|
|
27
|
+
"""Protocol for dataset management operations."""
|
|
28
|
+
|
|
29
|
+
async def register_dataset(self, request: RegisterDatasetRequest) -> Dataset: ...
|
|
30
|
+
|
|
31
|
+
async def get_dataset(self, request: GetDatasetRequest) -> Dataset: ...
|
|
32
|
+
|
|
33
|
+
async def list_datasets(self) -> ListDatasetsResponse: ...
|
|
34
|
+
|
|
35
|
+
async def unregister_dataset(self, request: UnregisterDatasetRequest) -> 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 Datasets API.
|
|
8
|
+
|
|
9
|
+
This module defines the FastAPI router for the Datasets 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_V1BETA
|
|
19
|
+
|
|
20
|
+
from .api import Datasets
|
|
21
|
+
from .models import (
|
|
22
|
+
Dataset,
|
|
23
|
+
GetDatasetRequest,
|
|
24
|
+
ListDatasetsResponse,
|
|
25
|
+
RegisterDatasetRequest,
|
|
26
|
+
UnregisterDatasetRequest,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
# Path parameter dependencies for single-field models
|
|
30
|
+
get_dataset_request = create_path_dependency(GetDatasetRequest)
|
|
31
|
+
unregister_dataset_request = create_path_dependency(UnregisterDatasetRequest)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def create_router(impl: Datasets) -> APIRouter:
|
|
35
|
+
"""Create a FastAPI router for the Datasets API.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
impl: The Datasets implementation instance
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
APIRouter configured for the Datasets API
|
|
42
|
+
"""
|
|
43
|
+
router = APIRouter(
|
|
44
|
+
prefix=f"/{LLAMA_STACK_API_V1BETA}",
|
|
45
|
+
tags=["Datasets"],
|
|
46
|
+
responses=standard_responses,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
@router.post(
|
|
50
|
+
"/datasets",
|
|
51
|
+
response_model=Dataset,
|
|
52
|
+
summary="Register a new dataset.",
|
|
53
|
+
description="Register a new dataset.",
|
|
54
|
+
responses={
|
|
55
|
+
200: {"description": "The registered dataset object."},
|
|
56
|
+
},
|
|
57
|
+
deprecated=True,
|
|
58
|
+
)
|
|
59
|
+
async def register_dataset(
|
|
60
|
+
request: Annotated[RegisterDatasetRequest, Body(...)],
|
|
61
|
+
) -> Dataset:
|
|
62
|
+
return await impl.register_dataset(request)
|
|
63
|
+
|
|
64
|
+
@router.get(
|
|
65
|
+
"/datasets/{dataset_id:path}",
|
|
66
|
+
response_model=Dataset,
|
|
67
|
+
summary="Get a dataset by its ID.",
|
|
68
|
+
description="Get a dataset by its ID.",
|
|
69
|
+
responses={
|
|
70
|
+
200: {"description": "The dataset object."},
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
async def get_dataset(
|
|
74
|
+
request: Annotated[GetDatasetRequest, Depends(get_dataset_request)],
|
|
75
|
+
) -> Dataset:
|
|
76
|
+
return await impl.get_dataset(request)
|
|
77
|
+
|
|
78
|
+
@router.get(
|
|
79
|
+
"/datasets",
|
|
80
|
+
response_model=ListDatasetsResponse,
|
|
81
|
+
summary="List all datasets.",
|
|
82
|
+
description="List all datasets.",
|
|
83
|
+
responses={
|
|
84
|
+
200: {"description": "A list of dataset objects."},
|
|
85
|
+
},
|
|
86
|
+
)
|
|
87
|
+
async def list_datasets() -> ListDatasetsResponse:
|
|
88
|
+
return await impl.list_datasets()
|
|
89
|
+
|
|
90
|
+
@router.delete(
|
|
91
|
+
"/datasets/{dataset_id:path}",
|
|
92
|
+
summary="Unregister a dataset by its ID.",
|
|
93
|
+
description="Unregister a dataset by its ID.",
|
|
94
|
+
responses={
|
|
95
|
+
200: {"description": "The dataset was successfully unregistered."},
|
|
96
|
+
},
|
|
97
|
+
deprecated=True,
|
|
98
|
+
)
|
|
99
|
+
async def unregister_dataset(
|
|
100
|
+
request: Annotated[UnregisterDatasetRequest, Depends(unregister_dataset_request)],
|
|
101
|
+
) -> None:
|
|
102
|
+
return await impl.unregister_dataset(request)
|
|
103
|
+
|
|
104
|
+
return router
|
|
@@ -0,0 +1,152 @@
|
|
|
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 Datasets API requests and responses.
|
|
8
|
+
|
|
9
|
+
This module defines the request and response models for the Datasets API
|
|
10
|
+
using Pydantic with Field descriptions for OpenAPI schema generation.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from enum import Enum, StrEnum
|
|
14
|
+
from typing import Annotated, Any, Literal
|
|
15
|
+
|
|
16
|
+
from pydantic import BaseModel, Field
|
|
17
|
+
|
|
18
|
+
from llama_stack_api.resource import Resource, ResourceType
|
|
19
|
+
from llama_stack_api.schema_utils import json_schema_type, register_schema
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DatasetPurpose(StrEnum):
|
|
23
|
+
"""Purpose of the dataset. Each purpose has a required input data schema."""
|
|
24
|
+
|
|
25
|
+
post_training_messages = "post-training/messages"
|
|
26
|
+
"""The dataset contains messages used for post-training."""
|
|
27
|
+
eval_question_answer = "eval/question-answer"
|
|
28
|
+
"""The dataset contains a question column and an answer column."""
|
|
29
|
+
eval_messages_answer = "eval/messages-answer"
|
|
30
|
+
"""The dataset contains a messages column with list of messages and an answer column."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class DatasetType(Enum):
|
|
34
|
+
"""Type of the dataset source."""
|
|
35
|
+
|
|
36
|
+
uri = "uri"
|
|
37
|
+
"""The dataset can be obtained from a URI."""
|
|
38
|
+
rows = "rows"
|
|
39
|
+
"""The dataset is stored in rows."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@json_schema_type
|
|
43
|
+
class URIDataSource(BaseModel):
|
|
44
|
+
"""A dataset that can be obtained from a URI."""
|
|
45
|
+
|
|
46
|
+
type: Literal["uri"] = Field(default="uri", description="The type of data source.")
|
|
47
|
+
uri: str = Field(
|
|
48
|
+
...,
|
|
49
|
+
description='The dataset can be obtained from a URI. E.g. "https://mywebsite.com/mydata.jsonl", "lsfs://mydata.jsonl", "data:csv;base64,{base64_content}"',
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@json_schema_type
|
|
54
|
+
class RowsDataSource(BaseModel):
|
|
55
|
+
"""A dataset stored in rows."""
|
|
56
|
+
|
|
57
|
+
type: Literal["rows"] = Field(default="rows", description="The type of data source.")
|
|
58
|
+
rows: list[dict[str, Any]] = Field(
|
|
59
|
+
...,
|
|
60
|
+
description='The dataset is stored in rows. E.g. [{"messages": [{"role": "user", "content": "Hello, world!"}, {"role": "assistant", "content": "Hello, world!"}]}]',
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
DataSource = Annotated[
|
|
65
|
+
URIDataSource | RowsDataSource,
|
|
66
|
+
Field(discriminator="type"),
|
|
67
|
+
]
|
|
68
|
+
register_schema(DataSource, name="DataSource")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class CommonDatasetFields(BaseModel):
|
|
72
|
+
"""Common fields for a dataset."""
|
|
73
|
+
|
|
74
|
+
purpose: DatasetPurpose = Field(..., description="Purpose of the dataset indicating its intended use")
|
|
75
|
+
source: DataSource = Field(..., description="Data source configuration for the dataset")
|
|
76
|
+
metadata: dict[str, Any] = Field(
|
|
77
|
+
default_factory=dict,
|
|
78
|
+
description="Any additional metadata for this dataset",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@json_schema_type
|
|
83
|
+
class Dataset(CommonDatasetFields, Resource):
|
|
84
|
+
"""Dataset resource for storing and accessing training or evaluation data."""
|
|
85
|
+
|
|
86
|
+
type: Literal[ResourceType.dataset] = Field(
|
|
87
|
+
default=ResourceType.dataset,
|
|
88
|
+
description="Type of resource, always 'dataset' for datasets",
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def dataset_id(self) -> str:
|
|
93
|
+
return self.identifier
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def provider_dataset_id(self) -> str | None:
|
|
97
|
+
return self.provider_resource_id
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@json_schema_type
|
|
101
|
+
class ListDatasetsResponse(BaseModel):
|
|
102
|
+
"""Response from listing datasets."""
|
|
103
|
+
|
|
104
|
+
data: list[Dataset] = Field(..., description="List of datasets")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# Request models for each endpoint
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@json_schema_type
|
|
111
|
+
class RegisterDatasetRequest(BaseModel):
|
|
112
|
+
"""Request model for registering a dataset."""
|
|
113
|
+
|
|
114
|
+
purpose: DatasetPurpose = Field(..., description="The purpose of the dataset.")
|
|
115
|
+
source: DataSource = Field(..., description="The data source of the dataset.")
|
|
116
|
+
metadata: dict[str, Any] | None = Field(
|
|
117
|
+
default=None,
|
|
118
|
+
description="The metadata for the dataset.",
|
|
119
|
+
)
|
|
120
|
+
dataset_id: str | None = Field(
|
|
121
|
+
default=None,
|
|
122
|
+
description="The ID of the dataset. If not provided, an ID will be generated.",
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@json_schema_type
|
|
127
|
+
class GetDatasetRequest(BaseModel):
|
|
128
|
+
"""Request model for getting a dataset by ID."""
|
|
129
|
+
|
|
130
|
+
dataset_id: str = Field(..., description="The ID of the dataset to get.")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@json_schema_type
|
|
134
|
+
class UnregisterDatasetRequest(BaseModel):
|
|
135
|
+
"""Request model for unregistering a dataset."""
|
|
136
|
+
|
|
137
|
+
dataset_id: str = Field(..., description="The ID of the dataset to unregister.")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
__all__ = [
|
|
141
|
+
"CommonDatasetFields",
|
|
142
|
+
"Dataset",
|
|
143
|
+
"DatasetPurpose",
|
|
144
|
+
"DatasetType",
|
|
145
|
+
"DataSource",
|
|
146
|
+
"RowsDataSource",
|
|
147
|
+
"URIDataSource",
|
|
148
|
+
"ListDatasetsResponse",
|
|
149
|
+
"RegisterDatasetRequest",
|
|
150
|
+
"GetDatasetRequest",
|
|
151
|
+
"UnregisterDatasetRequest",
|
|
152
|
+
]
|