1
+ """Types and request signatrues for OpenAI compatibility
2
+
3
+ Based on the OpenAI OpenAPI specification:
4
+ https://github.com/openai/openai-openapi/blob/master/openapi.yaml
5
+
6
+ """
1
7
from typing import Any , List , Optional , Dict , Union
2
8
from typing_extensions import TypedDict , NotRequired , Literal
3
9
@@ -7,16 +13,19 @@ class EmbeddingUsage(TypedDict):
7
13
total_tokens : int
8
14
9
15
10
- class EmbeddingData (TypedDict ):
16
+ class Embedding (TypedDict ):
11
17
index : int
12
18
object : str
13
19
embedding : List [float ]
14
20
15
21
16
- class Embedding (TypedDict ):
22
+ EmbeddingData = Embedding
23
+
24
+
25
+ class CreateEmbeddingResponse (TypedDict ):
17
26
object : Literal ["list" ]
18
27
model : str
19
- data : List [EmbeddingData ]
28
+ data : List [Embedding ]
20
29
usage : EmbeddingUsage
21
30
22
31
@@ -31,7 +40,7 @@ class CompletionChoice(TypedDict):
31
40
text : str
32
41
index : int
33
42
logprobs : Optional [CompletionLogprobs ]
34
- finish_reason : Optional [str ]
43
+ finish_reason : Optional [Literal [ "stop" , "length" ] ]
35
44
36
45
37
46
class CompletionUsage (TypedDict ):
@@ -40,15 +49,18 @@ class CompletionUsage(TypedDict):
40
49
total_tokens : int
41
50
42
51
43
- class CompletionChunk (TypedDict ):
52
+ class CreateCompletionStreamResponse (TypedDict ):
44
53
id : str
45
54
object : Literal ["text_completion" ]
46
55
created : int
47
56
model : str
48
57
choices : List [CompletionChoice ]
49
58
50
59
51
- class Completion (TypedDict ):
60
+ CompletionChunk = CreateCompletionStreamResponse
61
+
62
+
63
+ class CreateCompletionResponse (TypedDict ):
52
64
id : str
53
65
object : Literal ["text_completion" ]
54
66
created : int
@@ -57,29 +69,43 @@ class Completion(TypedDict):
57
69
usage : CompletionUsage
58
70
59
71
60
- class ChatCompletionMessage (TypedDict ):
61
- role : Literal ["assistant" , "user" , "system" ]
62
- content : str
72
+ Completion = CreateCompletionResponse
73
+
74
+
75
+ class ChatCompletionFunctionCall (TypedDict ):
76
+ name : str
77
+ arguments : str
78
+
79
+
80
+ class ChatCompletionResponseMessage (TypedDict ):
81
+ role : Literal ["assistant" , "user" , "system" , "function" ]
82
+ content : Optional [str ]
63
83
user : NotRequired [str ]
84
+ function_call : NotRequired [ChatCompletionFunctionCall ]
85
+
64
86
87
+ ChatCompletionMessage = ChatCompletionResponseMessage
65
88
66
- class ChatCompletionFunction (TypedDict ):
89
+
90
+ class ChatCompletionResponseFunction (TypedDict ):
67
91
name : str
68
92
description : NotRequired [str ]
69
93
parameters : Dict [str , Any ] # TODO: make this more specific
70
94
71
95
72
- class ChatCompletionFunctionCall (TypedDict ):
73
- name : str
96
+ ChatCompletionFunction = ChatCompletionResponseFunction
74
97
75
98
76
- class ChatCompletionChoice (TypedDict ):
99
+ class ChatCompletionResponseChoice (TypedDict ):
77
100
index : int
78
101
message : ChatCompletionMessage
79
102
finish_reason : Optional [str ]
80
103
81
104
82
- class ChatCompletion (TypedDict ):
105
+ ChatCompletionChoice = ChatCompletionResponseChoice
106
+
107
+
108
+ class CreateChatCompletionResponse (TypedDict ):
83
109
id : str
84
110
object : Literal ["chat.completion" ]
85
111
created : int
@@ -88,24 +114,59 @@ class ChatCompletion(TypedDict):
88
114
usage : CompletionUsage
89
115
90
116
91
- class ChatCompletionChunkDeltaEmpty (TypedDict ):
117
+ ChatCompletion = CreateChatCompletionResponse
118
+
119
+
120
+ class ChatCompletionStreamResponseDeltaEmpty (TypedDict ):
92
121
pass
93
122
94
123
95
- class ChatCompletionChunkDelta (TypedDict ):
124
+ ChatCompletionChunkDeltaEmpty = ChatCompletionStreamResponseDeltaEmpty
125
+
126
+
127
+ class ChatCompletionStreamResponseDelta (TypedDict ):
96
128
role : NotRequired [Literal ["assistant" ]]
97
129
content : NotRequired [str ]
130
+ function_call : NotRequired [ChatCompletionFunctionCall ]
131
+
132
+
133
+ ChatCompletionChunkDelta = ChatCompletionStreamResponseDelta
98
134
99
135
100
- class ChatCompletionChunkChoice (TypedDict ):
136
+ class ChatCompletionStreamResponseChoice (TypedDict ):
101
137
index : int
102
138
delta : Union [ChatCompletionChunkDelta , ChatCompletionChunkDeltaEmpty ]
103
- finish_reason : Optional [str ]
139
+ finish_reason : Optional [Literal ["stop" , "length" , "function_call" ]]
140
+
141
+
142
+ ChatCompletionChunkChoice = ChatCompletionStreamResponseChoice
104
143
105
144
106
- class ChatCompletionChunk (TypedDict ):
145
+ class ChatCompletionStreamResponse (TypedDict ):
107
146
id : str
108
147
model : str
109
148
object : Literal ["chat.completion.chunk" ]
110
149
created : int
111
150
choices : List [ChatCompletionChunkChoice ]
151
+
152
+
153
+ ChatCompletionChunk = ChatCompletionStreamResponse
154
+
155
+ JsonType = Union [None , int , str , bool , List ["JsonType" ], Dict [str , "JsonType" ]]
156
+
157
+
158
+ class ChatCompletionFunctions (TypedDict ):
159
+ name : str
160
+ description : NotRequired [str ]
161
+ parameters : Dict [str , JsonType ] # TODO: make this more specific
162
+
163
+
164
+ class ChatCompletionFunctionCallOption (TypedDict ):
165
+ name : str
166
+
167
+
168
+ class ChatCompletionRequestMessage (TypedDict ):
169
+ role : Literal ["assistant" , "user" , "system" , "function" ]
170
+ content : Optional [str ]
171
+ name : NotRequired [str ]
172
+ funcion_call : NotRequired [ChatCompletionFunctionCall ]
0 commit comments