| 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 List, Optional, Protocol |
| 8 | |
| 9 | from llama_models.schema_utils import json_schema_type, webmethod |
| 10 | |
| 11 | from pydantic import BaseModel, Field |
| 12 | |
| 13 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 14 | from llama_stack.apis.inference import * # noqa: F403 |
| 15 | |
| 16 | |
| 17 | @json_schema_type |
| 18 | class BatchCompletionRequest(BaseModel): |
| 19 | model: str |
| 20 | content_batch: List[InterleavedTextMedia] |
| 21 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 22 | logprobs: Optional[LogProbConfig] = None |
| 23 | |
| 24 | |
| 25 | @json_schema_type |
| 26 | class BatchCompletionResponse(BaseModel): |
| 27 | completion_message_batch: List[CompletionMessage] |
| 28 | |
| 29 | |
| 30 | @json_schema_type |
| 31 | class BatchChatCompletionRequest(BaseModel): |
| 32 | model: str |
| 33 | messages_batch: List[List[Message]] |
| 34 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 35 | |
| 36 | # zero-shot tool definitions as input to the model |
| 37 | tools: Optional[List[ToolDefinition]] = Field(default_factory=list) |
| 38 | tool_choice: Optional[ToolChoice] = Field(default=ToolChoice.auto) |
| 39 | tool_prompt_format: Optional[ToolPromptFormat] = Field( |
| 40 | default=ToolPromptFormat.json |
| 41 | ) |
| 42 | logprobs: Optional[LogProbConfig] = None |
| 43 | |
| 44 | |
| 45 | @json_schema_type |
| 46 | class BatchChatCompletionResponse(BaseModel): |
| 47 | completion_message_batch: List[CompletionMessage] |
| 48 | |
| 49 | |
| 50 | class BatchInference(Protocol): |
| 51 | @webmethod(route="/batch_inference/completion") |
| 52 | async def batch_completion( |
| 53 | self, |
| 54 | model: str, |
| 55 | content_batch: List[InterleavedTextMedia], |
| 56 | sampling_params: Optional[SamplingParams] = SamplingParams(), |
| 57 | logprobs: Optional[LogProbConfig] = None, |
| 58 | ) -> BatchCompletionResponse: ... |
| 59 | |
| 60 | @webmethod(route="/batch_inference/chat_completion") |
| 61 | async def batch_chat_completion( |
| 62 | self, |
| 63 | model: str, |
| 64 | messages_batch: List[List[Message]], |
| 65 | sampling_params: Optional[SamplingParams] = SamplingParams(), |
| 66 | # zero-shot tool definitions as input to the model |
| 67 | tools: Optional[List[ToolDefinition]] = list, |
| 68 | tool_choice: Optional[ToolChoice] = ToolChoice.auto, |
| 69 | tool_prompt_format: Optional[ToolPromptFormat] = ToolPromptFormat.json, |
| 70 | logprobs: Optional[LogProbConfig] = None, |
| 71 | ) -> BatchChatCompletionResponse: ... |