1
1
"""Types and request signatures for OpenAI compatibility
2
2
3
+ NOTE: These types may change to match the OpenAI OpenAPI specification.
4
+
3
5
Based on the OpenAI OpenAPI specification:
4
6
https://github.com/openai/openai-openapi/blob/master/openapi.yaml
5
7
@@ -19,9 +21,6 @@ class Embedding(TypedDict):
19
21
embedding : List [float ]
20
22
21
23
22
- EmbeddingData = Embedding
23
-
24
-
25
24
class CreateEmbeddingResponse (TypedDict ):
26
25
object : Literal ["list" ]
27
26
model : str
@@ -57,9 +56,6 @@ class CreateCompletionStreamResponse(TypedDict):
57
56
choices : List [CompletionChoice ]
58
57
59
58
60
- CompletionChunk = CreateCompletionStreamResponse
61
-
62
-
63
59
class CreateCompletionResponse (TypedDict ):
64
60
id : str
65
61
object : Literal ["text_completion" ]
@@ -69,9 +65,6 @@ class CreateCompletionResponse(TypedDict):
69
65
usage : CompletionUsage
70
66
71
67
72
- Completion = CreateCompletionResponse
73
-
74
-
75
68
class ChatCompletionFunctionCall (TypedDict ):
76
69
name : str
77
70
arguments : str
@@ -100,73 +93,58 @@ class ChatCompletionResponseMessage(TypedDict):
100
93
function_call : NotRequired [ChatCompletionFunctionCall ]
101
94
102
95
103
- ChatCompletionMessage = ChatCompletionResponseMessage
104
-
105
-
106
96
class ChatCompletionResponseFunction (TypedDict ):
107
97
name : str
108
98
description : NotRequired [str ]
109
99
parameters : Dict [str , Any ] # TODO: make this more specific
110
100
111
101
112
- ChatCompletionFunction = ChatCompletionResponseFunction
113
-
114
-
115
102
class ChatCompletionResponseChoice (TypedDict ):
116
103
index : int
117
- message : ChatCompletionMessage
104
+ message : " ChatCompletionMessage"
118
105
finish_reason : Optional [str ]
119
106
120
107
121
- ChatCompletionChoice = ChatCompletionResponseChoice
122
-
123
-
124
108
class CreateChatCompletionResponse (TypedDict ):
125
109
id : str
126
110
object : Literal ["chat.completion" ]
127
111
created : int
128
112
model : str
129
- choices : List [ChatCompletionChoice ]
113
+ choices : List [" ChatCompletionChoice" ]
130
114
usage : CompletionUsage
131
115
132
116
133
- ChatCompletion = CreateChatCompletionResponse
117
+ class ChatCompletionMessageToolCallChunk (TypedDict ):
118
+ index : int
119
+ id : NotRequired [str ]
120
+ type : Literal ["function" ]
121
+ function : ChatCompletionFunctionCall
134
122
135
123
136
124
class ChatCompletionStreamResponseDeltaEmpty (TypedDict ):
137
125
pass
138
126
139
127
140
- ChatCompletionChunkDeltaEmpty = ChatCompletionStreamResponseDeltaEmpty
141
-
142
-
143
128
class ChatCompletionStreamResponseDelta (TypedDict ):
144
- role : NotRequired [Literal ["assistant" ]]
145
129
content : NotRequired [str ]
146
130
function_call : NotRequired [ChatCompletionFunctionCall ]
147
-
148
-
149
- ChatCompletionChunkDelta = ChatCompletionStreamResponseDelta
131
+ tool_calls : NotRequired [List [ChatCompletionMessageToolCallChunk ]]
132
+ role : NotRequired [Literal ["system" , "user" , "assistant" , "tool" ]]
150
133
151
134
152
135
class ChatCompletionStreamResponseChoice (TypedDict ):
153
136
index : int
154
- delta : Union [ChatCompletionChunkDelta , ChatCompletionChunkDeltaEmpty ]
137
+ delta : Union [" ChatCompletionChunkDelta" , " ChatCompletionChunkDeltaEmpty" ]
155
138
finish_reason : Optional [Literal ["stop" , "length" , "function_call" ]]
156
139
157
140
158
- ChatCompletionChunkChoice = ChatCompletionStreamResponseChoice
159
-
160
-
161
141
class ChatCompletionStreamResponse (TypedDict ):
162
142
id : str
163
143
model : str
164
144
object : Literal ["chat.completion.chunk" ]
165
145
created : int
166
- choices : List [ChatCompletionChunkChoice ]
167
-
146
+ choices : List ["ChatCompletionChunkChoice" ]
168
147
169
- ChatCompletionChunk = ChatCompletionStreamResponse
170
148
171
149
JsonType = Union [None , int , str , bool , List ["JsonType" ], Dict [str , "JsonType" ]]
172
150
@@ -181,8 +159,90 @@ class ChatCompletionFunctionCallOption(TypedDict):
181
159
name : str
182
160
183
161
184
- class ChatCompletionRequestMessage (TypedDict ):
185
- role : Literal ["assistant" , "user" , "system" , "function" ]
162
+ class ChatCompletionRequestMessageContentPartText (TypedDict ):
163
+ type : Literal ["text" ]
164
+ text : str
165
+
166
+
167
+ class ChatCompletionRequestMessageContentPartImageImageUrl (TypedDict ):
168
+ url : str
169
+ detail : NotRequired [Literal ["auto" , "low" , "high" ]]
170
+
171
+
172
+ class ChatCompletionRequestMessageContentPartImage (TypedDict ):
173
+ type : Literal ["image_url" ]
174
+ image_url : ChatCompletionRequestMessageContentPartImageImageUrl
175
+
176
+
177
+ ChatCompletionRequestMessageContentPart = Union [
178
+ ChatCompletionRequestMessageContentPartText ,
179
+ ChatCompletionRequestMessageContentPartImage ,
180
+ ]
181
+
182
+
183
+ class ChatCompletionRequestSystemMessage (TypedDict ):
184
+ role : Literal ["system" ]
186
185
content : Optional [str ]
187
- name : NotRequired [str ]
188
- function_call : NotRequired [ChatCompletionFunctionCall ]
186
+
187
+
188
+ class ChatCompletionRequestUserMessage (TypedDict ):
189
+ role : Literal ["user" ]
190
+ content : Optional [Union [str , List [ChatCompletionRequestMessageContentPart ]]]
191
+
192
+
193
+ class ChatCompletionMessageToolCallFunction (TypedDict ):
194
+ name : str
195
+ arguments : str
196
+
197
+
198
+ class ChatCompletionMessageToolCall (TypedDict ):
199
+ id : str
200
+ type : Literal ["function" ]
201
+ function : ChatCompletionMessageToolCallFunction
202
+
203
+
204
+ ChatCompletionMessageToolCalls = List [ChatCompletionMessageToolCall ]
205
+
206
+
207
+ class ChatCompletionRequestAssistantMessage (TypedDict ):
208
+ role : Literal ["assistant" ]
209
+ content : Optional [str ]
210
+ tool_calls : NotRequired [ChatCompletionMessageToolCalls ]
211
+ function_call : NotRequired [ChatCompletionFunctionCall ] # DEPRECATED
212
+
213
+
214
+ class ChatCompletionRequestToolMessage (TypedDict ):
215
+ role : Literal ["tool" ]
216
+ content : Optional [str ]
217
+ tool_call_id : str
218
+
219
+
220
+ class ChatCompletionRequestFunctionMessage (TypedDict ):
221
+ role : Literal ["function" ]
222
+ content : Optional [str ]
223
+ name : str
224
+
225
+
226
+ ChatCompletionRequestMessage = Union [
227
+ ChatCompletionRequestSystemMessage ,
228
+ ChatCompletionRequestUserMessage ,
229
+ ChatCompletionRequestAssistantMessage ,
230
+ ChatCompletionRequestUserMessage ,
231
+ ChatCompletionRequestToolMessage ,
232
+ ChatCompletionRequestFunctionMessage ,
233
+ ]
234
+
235
+ # NOTE: The following type names are not part of the OpenAI OpenAPI specification
236
+ # and will be removed in a future major release.
237
+
238
+ EmbeddingData = Embedding
239
+ CompletionChunk = CreateCompletionStreamResponse
240
+ Completion = CreateCompletionResponse
241
+ ChatCompletionMessage = ChatCompletionResponseMessage
242
+ ChatCompletionChoice = ChatCompletionResponseChoice
243
+ ChatCompletion = CreateChatCompletionResponse
244
+ ChatCompletionChunkDeltaEmpty = ChatCompletionStreamResponseDeltaEmpty
245
+ ChatCompletionChunkChoice = ChatCompletionStreamResponseChoice
246
+ ChatCompletionChunkDelta = ChatCompletionStreamResponseDelta
247
+ ChatCompletionChunk = ChatCompletionStreamResponse
248
+ ChatCompletionFunction = ChatCompletionResponseFunction
0 commit comments