|
| 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 Literal, Protocol, runtime_checkable |
| 8 | + |
| 9 | +from pydantic import BaseModel, Field |
| 10 | + |
| 11 | +from llama_stack.schema_utils import json_schema_type, webmethod |
| 12 | + |
| 13 | +try: |
| 14 | + from openai.types import Batch as BatchObject |
| 15 | +except ImportError as e: |
| 16 | + raise ImportError("OpenAI package is required for batches API. Please install it with: pip install openai") from e |
| 17 | + |
| 18 | + |
| 19 | +@json_schema_type |
| 20 | +class ListBatchesResponse(BaseModel): |
| 21 | + """Response containing a list of batch objects.""" |
| 22 | + |
| 23 | + object: Literal["list"] = "list" |
| 24 | + data: list[BatchObject] = Field(..., description="List of batch objects") |
| 25 | + first_id: str | None = Field(default=None, description="ID of the first batch in the list") |
| 26 | + last_id: str | None = Field(default=None, description="ID of the last batch in the list") |
| 27 | + has_more: bool = Field(default=False, description="Whether there are more batches available") |
| 28 | + |
| 29 | + |
| 30 | +@runtime_checkable |
| 31 | +class Batches(Protocol): |
| 32 | + """Protocol for batch processing API operations. |
| 33 | +
|
| 34 | + The Batches API enables efficient processing of multiple requests in a single operation, |
| 35 | + particularly useful for processing large datasets, batch evaluation workflows, and |
| 36 | + cost-effective inference at scale. |
| 37 | +
|
| 38 | + Note: This API is currently under active development and may undergo changes. |
| 39 | + """ |
| 40 | + |
| 41 | + @webmethod(route="/openai/v1/batches", method="POST") |
| 42 | + async def create_batch( |
| 43 | + self, |
| 44 | + input_file_id: str, |
| 45 | + endpoint: str, |
| 46 | + completion_window: Literal["24h"], |
| 47 | + metadata: dict[str, str] | None = None, |
| 48 | + ) -> BatchObject: |
| 49 | + """Create a new batch for processing multiple API requests. |
| 50 | +
|
| 51 | + :param input_file_id: The ID of an uploaded file containing requests for the batch. |
| 52 | + :param endpoint: The endpoint to be used for all requests in the batch. |
| 53 | + :param completion_window: The time window within which the batch should be processed. |
| 54 | + :param metadata: Optional metadata for the batch. |
| 55 | + :returns: The created batch object. |
| 56 | + """ |
| 57 | + ... |
| 58 | + |
| 59 | + @webmethod(route="/openai/v1/batches/{batch_id}", method="GET") |
| 60 | + async def retrieve_batch(self, batch_id: str) -> BatchObject: |
| 61 | + """Retrieve information about a specific batch. |
| 62 | +
|
| 63 | + :param batch_id: The ID of the batch to retrieve. |
| 64 | + :returns: The batch object. |
| 65 | + """ |
| 66 | + ... |
| 67 | + |
| 68 | + @webmethod(route="/openai/v1/batches/{batch_id}/cancel", method="POST") |
| 69 | + async def cancel_batch(self, batch_id: str) -> BatchObject: |
| 70 | + """Cancel a batch that is in progress. |
| 71 | +
|
| 72 | + :param batch_id: The ID of the batch to cancel. |
| 73 | + :returns: The updated batch object. |
| 74 | + """ |
| 75 | + ... |
| 76 | + |
| 77 | + @webmethod(route="/openai/v1/batches", method="GET") |
| 78 | + async def list_batches( |
| 79 | + self, |
| 80 | + after: str | None = None, |
| 81 | + limit: int = 20, |
| 82 | + ) -> ListBatchesResponse: |
| 83 | + """List all batches for the current user. |
| 84 | +
|
| 85 | + :param after: A cursor for pagination; returns batches after this batch ID. |
| 86 | + :param limit: Number of batches to return (default 20, max 100). |
| 87 | + :returns: A list of batch objects. |
| 88 | + """ |
| 89 | + ... |
0 commit comments