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+ """
17from typing import Any , List , Optional , Dict , Union
28from typing_extensions import TypedDict , NotRequired , Literal
39
@@ -7,16 +13,19 @@ class EmbeddingUsage(TypedDict):
713 total_tokens : int
814
915
10- class EmbeddingData (TypedDict ):
16+ class Embedding (TypedDict ):
1117 index : int
1218 object : str
1319 embedding : List [float ]
1420
1521
16- class Embedding (TypedDict ):
22+ EmbeddingData = Embedding
23+
24+
25+ class CreateEmbeddingResponse (TypedDict ):
1726 object : Literal ["list" ]
1827 model : str
19- data : List [EmbeddingData ]
28+ data : List [Embedding ]
2029 usage : EmbeddingUsage
2130
2231
@@ -31,7 +40,7 @@ class CompletionChoice(TypedDict):
3140 text : str
3241 index : int
3342 logprobs : Optional [CompletionLogprobs ]
34- finish_reason : Optional [str ]
43+ finish_reason : Optional [Literal [ "stop" , "length" ] ]
3544
3645
3746class CompletionUsage (TypedDict ):
@@ -40,15 +49,18 @@ class CompletionUsage(TypedDict):
4049 total_tokens : int
4150
4251
43- class CompletionChunk (TypedDict ):
52+ class CreateCompletionStreamResponse (TypedDict ):
4453 id : str
4554 object : Literal ["text_completion" ]
4655 created : int
4756 model : str
4857 choices : List [CompletionChoice ]
4958
5059
51- class Completion (TypedDict ):
60+ CompletionChunk = CreateCompletionStreamResponse
61+
62+
63+ class CreateCompletionResponse (TypedDict ):
5264 id : str
5365 object : Literal ["text_completion" ]
5466 created : int
@@ -57,29 +69,43 @@ class Completion(TypedDict):
5769 usage : CompletionUsage
5870
5971
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 ]
6383 user : NotRequired [str ]
84+ function_call : NotRequired [ChatCompletionFunctionCall ]
85+
6486
87+ ChatCompletionMessage = ChatCompletionResponseMessage
6588
66- class ChatCompletionFunction (TypedDict ):
89+
90+ class ChatCompletionResponseFunction (TypedDict ):
6791 name : str
6892 description : NotRequired [str ]
6993 parameters : Dict [str , Any ] # TODO: make this more specific
7094
7195
72- class ChatCompletionFunctionCall (TypedDict ):
73- name : str
96+ ChatCompletionFunction = ChatCompletionResponseFunction
7497
7598
76- class ChatCompletionChoice (TypedDict ):
99+ class ChatCompletionResponseChoice (TypedDict ):
77100 index : int
78101 message : ChatCompletionMessage
79102 finish_reason : Optional [str ]
80103
81104
82- class ChatCompletion (TypedDict ):
105+ ChatCompletionChoice = ChatCompletionResponseChoice
106+
107+
108+ class CreateChatCompletionResponse (TypedDict ):
83109 id : str
84110 object : Literal ["chat.completion" ]
85111 created : int
@@ -88,24 +114,59 @@ class ChatCompletion(TypedDict):
88114 usage : CompletionUsage
89115
90116
91- class ChatCompletionChunkDeltaEmpty (TypedDict ):
117+ ChatCompletion = CreateChatCompletionResponse
118+
119+
120+ class ChatCompletionStreamResponseDeltaEmpty (TypedDict ):
92121 pass
93122
94123
95- class ChatCompletionChunkDelta (TypedDict ):
124+ ChatCompletionChunkDeltaEmpty = ChatCompletionStreamResponseDeltaEmpty
125+
126+
127+ class ChatCompletionStreamResponseDelta (TypedDict ):
96128 role : NotRequired [Literal ["assistant" ]]
97129 content : NotRequired [str ]
130+ function_call : NotRequired [ChatCompletionFunctionCall ]
131+
132+
133+ ChatCompletionChunkDelta = ChatCompletionStreamResponseDelta
98134
99135
100- class ChatCompletionChunkChoice (TypedDict ):
136+ class ChatCompletionStreamResponseChoice (TypedDict ):
101137 index : int
102138 delta : Union [ChatCompletionChunkDelta , ChatCompletionChunkDeltaEmpty ]
103- finish_reason : Optional [str ]
139+ finish_reason : Optional [Literal ["stop" , "length" , "function_call" ]]
140+
141+
142+ ChatCompletionChunkChoice = ChatCompletionStreamResponseChoice
104143
105144
106- class ChatCompletionChunk (TypedDict ):
145+ class ChatCompletionStreamResponse (TypedDict ):
107146 id : str
108147 model : str
109148 object : Literal ["chat.completion.chunk" ]
110149 created : int
111150 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