Skip to content

Commit 645c8ed

Browse files
committed
feat(types): align with latest OpenAI API spec and fix type issues
- Expand `CompletionUsage` with `PromptTokensDetails` and `CompletionTokensDetails` for granular token tracking. - Add `usage` to `CreateChatCompletionStreamResponse` to support usage reporting in streaming mode. - Fix duplicate `object` field in `CreateCompletionResponse`. - Update `ChatCompletionRequestAssistantMessage` to accept `None` for `content` and introduce the new `refusal` field. - Clean up `ChatCompletionRequestMessage` Union by removing the duplicate user message type. - Broaden `ChatCompletionToolChoiceOption` to fully support `allowed_tools` and `custom` tool choice behaviors. Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent fbbf2dd commit 645c8ed

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

llama_cpp/llama_types.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,24 @@ class CompletionChoice(TypedDict):
4949
finish_reason: Optional[Literal["stop", "length", "content_filter"]]
5050

5151

52+
class PromptTokensDetails(TypedDict):
53+
cached_tokens: NotRequired[int]
54+
audio_tokens: NotRequired[int]
55+
56+
57+
class CompletionTokensDetails(TypedDict):
58+
reasoning_tokens: NotRequired[int]
59+
audio_tokens: NotRequired[int]
60+
accepted_prediction_tokens: NotRequired[int]
61+
rejected_prediction_tokens: NotRequired[int]
62+
63+
5264
class CompletionUsage(TypedDict):
5365
prompt_tokens: int
5466
completion_tokens: int
5567
total_tokens: int
68+
prompt_tokens_details: NotRequired[PromptTokensDetails]
69+
completion_tokens_details: NotRequired[CompletionTokensDetails]
5670

5771

5872
class CreateCompletionResponse(TypedDict):
@@ -61,7 +75,6 @@ class CreateCompletionResponse(TypedDict):
6175
created: int
6276
model: str
6377
choices: List[CompletionChoice]
64-
object: Optional[Literal["text_completion"]]
6578
usage: NotRequired[CompletionUsage]
6679

6780

@@ -198,6 +211,7 @@ class CreateChatCompletionStreamResponse(TypedDict):
198211
object: Literal["chat.completion.chunk"]
199212
created: int
200213
choices: List[ChatCompletionStreamResponseChoice]
214+
usage: NotRequired[CompletionUsage]
201215

202216

203217
class ChatCompletionFunctions(TypedDict):
@@ -307,7 +321,8 @@ class ChatCompletionRequestAssistantMessageFunctionCall(TypedDict):
307321

308322
class ChatCompletionRequestAssistantMessage(TypedDict):
309323
role: Literal["assistant"]
310-
content: NotRequired[str]
324+
content: NotRequired[Optional[str]]
325+
refusal: NotRequired[Optional[str]]
311326
tool_calls: NotRequired[ChatCompletionMessageToolCalls]
312327
function_call: NotRequired[
313328
ChatCompletionRequestAssistantMessageFunctionCall
@@ -331,7 +346,6 @@ class ChatCompletionRequestFunctionMessage(TypedDict):
331346
ChatCompletionRequestSystemMessage,
332347
ChatCompletionRequestUserMessage,
333348
ChatCompletionRequestAssistantMessage,
334-
ChatCompletionRequestUserMessage,
335349
ChatCompletionRequestToolMessage,
336350
ChatCompletionRequestFunctionMessage,
337351
]
@@ -359,6 +373,16 @@ class ChatCompletionTool(TypedDict):
359373
function: ChatCompletionToolFunction
360374

361375

376+
class ChatCompletionAllowedTools(TypedDict):
377+
mode: Literal["auto", "required"]
378+
tools: List[Dict[str, Any]]
379+
380+
381+
class ChatCompletionAllowedToolsChoice(TypedDict):
382+
type: Literal["allowed_tools"]
383+
allowed_tools: ChatCompletionAllowedTools
384+
385+
362386
class ChatCompletionNamedToolChoiceFunction(TypedDict):
363387
name: str
364388

@@ -368,8 +392,20 @@ class ChatCompletionNamedToolChoice(TypedDict):
368392
function: ChatCompletionNamedToolChoiceFunction
369393

370394

395+
class ChatCompletionNamedToolChoiceCustomObject(TypedDict):
396+
name: str
397+
398+
399+
class ChatCompletionNamedToolChoiceCustom(TypedDict):
400+
type: Literal["custom"]
401+
custom: ChatCompletionNamedToolChoiceCustomObject
402+
403+
371404
ChatCompletionToolChoiceOption = Union[
372-
Literal["none", "auto", "required"], ChatCompletionNamedToolChoice
405+
Literal["none", "auto", "required"],
406+
ChatCompletionAllowedToolsChoice,
407+
ChatCompletionNamedToolChoice,
408+
ChatCompletionNamedToolChoiceCustom
373409
]
374410

375411

0 commit comments

Comments
 (0)