Skip to content

Commit a5f16dc

Browse files
authored
Update llama_types.py
1 parent 683868b commit a5f16dc

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

llama_cpp/llama_types.py

+35-33
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
from typing import Any, Dict, List, Optional, Union
1111

12-
from typing_extensions import NotRequired, Literal, TypedDict
12+
from typing_extensions import Literal, NotRequired, TypedDict
1313

1414
# NOTE: Defining this correctly using annotations seems to break pydantic validation.
1515
# This is a workaround until we can figure out how to do this correctly
1616
# JsonType = Union[None, int, str, bool, List["JsonType"], Dict[str, "JsonType"]]
17-
JsonType = Union[None, int, str, bool, list[Any], dict[str, Any]]
17+
JsonType = Union[None, int, str, bool, List[Any], Dict[str, Any]]
1818

1919

2020
class EmbeddingUsage(TypedDict):
@@ -31,22 +31,22 @@ class Embedding(TypedDict):
3131
class CreateEmbeddingResponse(TypedDict):
3232
object: Literal["list"]
3333
model: str
34-
data: list[Embedding]
34+
data: List[Embedding]
3535
usage: EmbeddingUsage
3636

3737

3838
class CompletionLogprobs(TypedDict):
39-
text_offset: list[int]
40-
token_logprobs: list[float | None]
41-
tokens: list[str]
42-
top_logprobs: list[dict[str, float] | None]
39+
text_offset: List[int]
40+
token_logprobs: List[Optional[float]]
41+
tokens: List[str]
42+
top_logprobs: List[Optional[Dict[str, float]]]
4343

4444

4545
class CompletionChoice(TypedDict):
4646
text: str
4747
index: int
48-
logprobs: CompletionLogprobs | None
49-
finish_reason: Literal["stop", "length"] | None
48+
logprobs: Optional[CompletionLogprobs]
49+
finish_reason: Optional[Literal["stop", "length"]]
5050

5151

5252
class CompletionUsage(TypedDict):
@@ -60,7 +60,7 @@ class CreateCompletionResponse(TypedDict):
6060
object: Literal["text_completion"]
6161
created: int
6262
model: str
63-
choices: list[CompletionChoice]
63+
choices: List[CompletionChoice]
6464
usage: NotRequired[CompletionUsage]
6565

6666

@@ -70,7 +70,7 @@ class ChatCompletionResponseFunctionCall(TypedDict):
7070

7171

7272
class ChatCompletionResponseMessage(TypedDict):
73-
content: str | None
73+
content: Optional[str]
7474
tool_calls: NotRequired["ChatCompletionMessageToolCalls"]
7575
role: Literal["assistant", "function"] # NOTE: "function" may be incorrect here
7676
function_call: NotRequired[ChatCompletionResponseFunctionCall] # DEPRECATED
@@ -79,27 +79,27 @@ class ChatCompletionResponseMessage(TypedDict):
7979
class ChatCompletionFunction(TypedDict):
8080
name: str
8181
description: NotRequired[str]
82-
parameters: dict[str, JsonType] # TODO: make this more specific
82+
parameters: Dict[str, JsonType] # TODO: make this more specific
8383

8484

8585
class ChatCompletionResponseChoice(TypedDict):
8686
index: int
8787
message: "ChatCompletionResponseMessage"
88-
logprobs: CompletionLogprobs | None
89-
finish_reason: str | None
88+
logprobs: Optional[CompletionLogprobs]
89+
finish_reason: Optional[str]
9090

9191

9292
class CreateChatCompletionResponse(TypedDict):
9393
id: str
9494
object: Literal["chat.completion"]
9595
created: int
9696
model: str
97-
choices: list["ChatCompletionResponseChoice"]
97+
choices: List["ChatCompletionResponseChoice"]
9898
usage: CompletionUsage
9999

100100

101101
class ChatCompletionMessageToolCallChunkFunction(TypedDict):
102-
name: str | None
102+
name: Optional[str]
103103
arguments: str
104104

105105

@@ -120,33 +120,35 @@ class ChatCompletionStreamResponseDeltaFunctionCall(TypedDict):
120120

121121

122122
class ChatCompletionStreamResponseDelta(TypedDict):
123-
content: NotRequired[str | None]
123+
content: NotRequired[Optional[str]]
124124
function_call: NotRequired[
125-
ChatCompletionStreamResponseDeltaFunctionCall | None
125+
Optional[ChatCompletionStreamResponseDeltaFunctionCall]
126126
] # DEPRECATED
127-
tool_calls: NotRequired[list[ChatCompletionMessageToolCallChunk] | None]
128-
role: NotRequired[Literal["system", "user", "assistant", "tool"] | None]
127+
tool_calls: NotRequired[Optional[List[ChatCompletionMessageToolCallChunk]]]
128+
role: NotRequired[Optional[Literal["system", "user", "assistant", "tool"]]]
129129

130130

131131
class ChatCompletionStreamResponseChoice(TypedDict):
132132
index: int
133-
delta: ChatCompletionStreamResponseDelta | ChatCompletionStreamResponseDeltaEmpty
134-
finish_reason: Literal["stop", "length", "tool_calls", "function_call"] | None
135-
logprobs: NotRequired[CompletionLogprobs | None]
133+
delta: Union[
134+
ChatCompletionStreamResponseDelta, ChatCompletionStreamResponseDeltaEmpty,
135+
]
136+
finish_reason: Optional[Literal["stop", "length", "tool_calls", "function_call"]]
137+
logprobs: NotRequired[Optional[CompletionLogprobs]]
136138

137139

138140
class CreateChatCompletionStreamResponse(TypedDict):
139141
id: str
140142
model: str
141143
object: Literal["chat.completion.chunk"]
142144
created: int
143-
choices: list[ChatCompletionStreamResponseChoice]
145+
choices: List[ChatCompletionStreamResponseChoice]
144146

145147

146148
class ChatCompletionFunctions(TypedDict):
147149
name: str
148150
description: NotRequired[str]
149-
parameters: dict[str, JsonType] # TODO: make this more specific
151+
parameters: Dict[str, JsonType] # TODO: make this more specific
150152

151153

152154
class ChatCompletionFunctionCallOption(TypedDict):
@@ -172,7 +174,7 @@ class ChatCompletionRequestMessageContentPartImageImageUrl(TypedDict):
172174

173175
class ChatCompletionRequestMessageContentPartImage(TypedDict):
174176
type: Literal["image_url"]
175-
image_url: str | ChatCompletionRequestMessageContentPartImageImageUrl
177+
image_url: Union[str, ChatCompletionRequestMessageContentPartImageImageUrl]
176178

177179

178180
ChatCompletionRequestMessageContentPart = Union[
@@ -183,12 +185,12 @@ class ChatCompletionRequestMessageContentPartImage(TypedDict):
183185

184186
class ChatCompletionRequestSystemMessage(TypedDict):
185187
role: Literal["system"]
186-
content: str | None
188+
content: Optional[str]
187189

188190

189191
class ChatCompletionRequestUserMessage(TypedDict):
190192
role: Literal["user"]
191-
content: str | list[ChatCompletionRequestMessageContentPart] | None
193+
content: Optional[Union[str, List[ChatCompletionRequestMessageContentPart]]]
192194

193195

194196
class ChatCompletionMessageToolCallFunction(TypedDict):
@@ -202,7 +204,7 @@ class ChatCompletionMessageToolCall(TypedDict):
202204
function: ChatCompletionMessageToolCallFunction
203205

204206

205-
ChatCompletionMessageToolCalls = list[ChatCompletionMessageToolCall]
207+
ChatCompletionMessageToolCalls = List[ChatCompletionMessageToolCall]
206208

207209

208210
class ChatCompletionRequestAssistantMessageFunctionCall(TypedDict):
@@ -212,7 +214,7 @@ class ChatCompletionRequestAssistantMessageFunctionCall(TypedDict):
212214

213215
class ChatCompletionRequestAssistantMessage(TypedDict):
214216
role: Literal["assistant"]
215-
content: str | None
217+
content: Optional[str]
216218
tool_calls: NotRequired[ChatCompletionMessageToolCalls]
217219
function_call: NotRequired[
218220
ChatCompletionRequestAssistantMessageFunctionCall
@@ -221,13 +223,13 @@ class ChatCompletionRequestAssistantMessage(TypedDict):
221223

222224
class ChatCompletionRequestToolMessage(TypedDict):
223225
role: Literal["tool"]
224-
content: str | None
226+
content: Optional[str]
225227
tool_call_id: str
226228

227229

228230
class ChatCompletionRequestFunctionMessage(TypedDict):
229231
role: Literal["function"]
230-
content: str | None
232+
content: Optional[str]
231233
name: str
232234

233235

@@ -249,7 +251,7 @@ class ChatCompletionRequestFunctionCallOption(TypedDict):
249251
Literal["none", "auto"], ChatCompletionRequestFunctionCallOption,
250252
]
251253

252-
ChatCompletionFunctionParameters = dict[str, JsonType] # TODO: make this more specific
254+
ChatCompletionFunctionParameters = Dict[str, JsonType] # TODO: make this more specific
253255

254256

255257
class ChatCompletionToolFunction(TypedDict):

0 commit comments

Comments
 (0)