| 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 Enum |
| 8 | |
| 9 | from typing import List, Literal, Optional, Protocol, Union |
| 10 | |
| 11 | from llama_models.schema_utils import json_schema_type, webmethod |
| 12 | |
| 13 | from pydantic import BaseModel, Field |
| 14 | from typing_extensions import Annotated |
| 15 | |
| 16 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 17 | |
| 18 | |
| 19 | class LogProbConfig(BaseModel): |
| 20 | top_k: Optional[int] = 0 |
| 21 | |
| 22 | |
| 23 | @json_schema_type |
| 24 | class QuantizationType(Enum): |
| 25 | bf16 = "bf16" |
| 26 | fp8 = "fp8" |
| 27 | |
| 28 | |
| 29 | @json_schema_type |
| 30 | class Fp8QuantizationConfig(BaseModel): |
| 31 | type: Literal[QuantizationType.fp8.value] = QuantizationType.fp8.value |
| 32 | |
| 33 | |
| 34 | @json_schema_type |
| 35 | class Bf16QuantizationConfig(BaseModel): |
| 36 | type: Literal[QuantizationType.bf16.value] = QuantizationType.bf16.value |
| 37 | |
| 38 | |
| 39 | QuantizationConfig = Annotated[ |
| 40 | Union[Bf16QuantizationConfig, Fp8QuantizationConfig], |
| 41 | Field(discriminator="type"), |
| 42 | ] |
| 43 | |
| 44 | |
| 45 | @json_schema_type |
| 46 | class ChatCompletionResponseEventType(Enum): |
| 47 | start = "start" |
| 48 | complete = "complete" |
| 49 | progress = "progress" |
| 50 | |
| 51 | |
| 52 | @json_schema_type |
| 53 | class ToolCallParseStatus(Enum): |
| 54 | started = "started" |
| 55 | in_progress = "in_progress" |
| 56 | failure = "failure" |
| 57 | success = "success" |
| 58 | |
| 59 | |
| 60 | @json_schema_type |
| 61 | class ToolCallDelta(BaseModel): |
| 62 | content: Union[str, ToolCall] |
| 63 | parse_status: ToolCallParseStatus |
| 64 | |
| 65 | |
| 66 | @json_schema_type |
| 67 | class ChatCompletionResponseEvent(BaseModel): |
| 68 | """Chat completion response event.""" |
| 69 | |
| 70 | event_type: ChatCompletionResponseEventType |
| 71 | delta: Union[str, ToolCallDelta] |
| 72 | logprobs: Optional[List[TokenLogProbs]] = None |
| 73 | stop_reason: Optional[StopReason] = None |
| 74 | |
| 75 | |
| 76 | @json_schema_type |
| 77 | class CompletionRequest(BaseModel): |
| 78 | model: str |
| 79 | content: InterleavedTextMedia |
| 80 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 81 | |
| 82 | stream: Optional[bool] = False |
| 83 | logprobs: Optional[LogProbConfig] = None |
| 84 | |
| 85 | |
| 86 | @json_schema_type |
| 87 | class CompletionResponse(BaseModel): |
| 88 | """Completion response.""" |
| 89 | |
| 90 | completion_message: CompletionMessage |
| 91 | logprobs: Optional[List[TokenLogProbs]] = None |
| 92 | |
| 93 | |
| 94 | @json_schema_type |
| 95 | class CompletionResponseStreamChunk(BaseModel): |
| 96 | """streamed completion response.""" |
| 97 | |
| 98 | delta: str |
| 99 | stop_reason: Optional[StopReason] = None |
| 100 | logprobs: Optional[List[TokenLogProbs]] = None |
| 101 | |
| 102 | |
| 103 | @json_schema_type |
| 104 | class BatchCompletionRequest(BaseModel): |
| 105 | model: str |
| 106 | content_batch: List[InterleavedTextMedia] |
| 107 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 108 | logprobs: Optional[LogProbConfig] = None |
| 109 | |
| 110 | |
| 111 | @json_schema_type |
| 112 | class BatchCompletionResponse(BaseModel): |
| 113 | """Batch completion response.""" |
| 114 | |
| 115 | completion_message_batch: List[CompletionMessage] |
| 116 | |
| 117 | |
| 118 | @json_schema_type |
| 119 | class ChatCompletionRequest(BaseModel): |
| 120 | model: str |
| 121 | messages: List[Message] |
| 122 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 123 | |
| 124 | # zero-shot tool definitions as input to the model |
| 125 | tools: Optional[List[ToolDefinition]] = Field(default_factory=list) |
| 126 | tool_choice: Optional[ToolChoice] = Field(default=ToolChoice.auto) |
| 127 | tool_prompt_format: Optional[ToolPromptFormat] = Field( |
| 128 | default=ToolPromptFormat.json |
| 129 | ) |
| 130 | |
| 131 | stream: Optional[bool] = False |
| 132 | logprobs: Optional[LogProbConfig] = None |
| 133 | |
| 134 | |
| 135 | @json_schema_type |
| 136 | class ChatCompletionResponseStreamChunk(BaseModel): |
| 137 | """SSE-stream of these events.""" |
| 138 | |
| 139 | event: ChatCompletionResponseEvent |
| 140 | |
| 141 | |
| 142 | @json_schema_type |
| 143 | class ChatCompletionResponse(BaseModel): |
| 144 | """Chat completion response.""" |
| 145 | |
| 146 | completion_message: CompletionMessage |
| 147 | logprobs: Optional[List[TokenLogProbs]] = None |
| 148 | |
| 149 | |
| 150 | @json_schema_type |
| 151 | class BatchChatCompletionRequest(BaseModel): |
| 152 | model: str |
| 153 | messages_batch: List[List[Message]] |
| 154 | sampling_params: Optional[SamplingParams] = SamplingParams() |
| 155 | |
| 156 | # zero-shot tool definitions as input to the model |
| 157 | tools: Optional[List[ToolDefinition]] = Field(default_factory=list) |
| 158 | tool_choice: Optional[ToolChoice] = Field(default=ToolChoice.auto) |
| 159 | tool_prompt_format: Optional[ToolPromptFormat] = Field( |
| 160 | default=ToolPromptFormat.json |
| 161 | ) |
| 162 | logprobs: Optional[LogProbConfig] = None |
| 163 | |
| 164 | |
| 165 | @json_schema_type |
| 166 | class BatchChatCompletionResponse(BaseModel): |
| 167 | completion_message_batch: List[CompletionMessage] |
| 168 | |
| 169 | |
| 170 | @json_schema_type |
| 171 | class EmbeddingsResponse(BaseModel): |
| 172 | embeddings: List[List[float]] |
| 173 | |
| 174 | |
| 175 | class Inference(Protocol): |
| 176 | @webmethod(route="/inference/completion") |
| 177 | async def completion( |
| 178 | self, |
| 179 | model: str, |
| 180 | content: InterleavedTextMedia, |
| 181 | sampling_params: Optional[SamplingParams] = SamplingParams(), |
| 182 | stream: Optional[bool] = False, |
| 183 | logprobs: Optional[LogProbConfig] = None, |
| 184 | ) -> Union[CompletionResponse, CompletionResponseStreamChunk]: ... |
| 185 | |
| 186 | @webmethod(route="/inference/chat_completion") |
| 187 | async def chat_completion( |
| 188 | self, |
| 189 | model: str, |
| 190 | messages: List[Message], |
| 191 | sampling_params: Optional[SamplingParams] = SamplingParams(), |
| 192 | # zero-shot tool definitions as input to the model |
| 193 | tools: Optional[List[ToolDefinition]] = None, |
| 194 | tool_choice: Optional[ToolChoice] = ToolChoice.auto, |
| 195 | tool_prompt_format: Optional[ToolPromptFormat] = ToolPromptFormat.json, |
| 196 | stream: Optional[bool] = False, |
| 197 | logprobs: Optional[LogProbConfig] = None, |
| 198 | ) -> Union[ChatCompletionResponse, ChatCompletionResponseStreamChunk]: ... |
| 199 | |
| 200 | @webmethod(route="/inference/embeddings") |
| 201 | async def embeddings( |
| 202 | self, |
| 203 | model: str, |
| 204 | contents: List[InterleavedTextMedia], |
| 205 | ) -> EmbeddingsResponse: ... |