1
1
import base64
2
2
import json
3
3
import time
4
+ from collections .abc import AsyncGenerator
4
5
from pathlib import Path
5
- from typing import Any , AsyncGenerator , Dict , List , Literal , Optional , Union
6
+ from typing import Any , Literal , Optional , Union
6
7
7
8
import httpx
8
9
from loguru import logger
@@ -111,7 +112,7 @@ def model(self) -> Optional[str]:
111
112
return self ._model
112
113
113
114
@property
114
- def info (self ) -> Dict [str , Any ]:
115
+ def info (self ) -> dict [str , Any ]:
115
116
"""
116
117
:return: The information about the backend.
117
118
"""
@@ -157,7 +158,7 @@ async def prepare_multiprocessing(self):
157
158
await self ._async_client .aclose ()
158
159
self ._async_client = None
159
160
160
- async def available_models (self ) -> List [str ]:
161
+ async def available_models (self ) -> list [str ]:
161
162
"""
162
163
Get the available models for the target server using the OpenAI models endpoint:
163
164
/v1/models
@@ -176,7 +177,7 @@ async def available_models(self) -> List[str]:
176
177
177
178
async def text_completions ( # type: ignore[override]
178
179
self ,
179
- prompt : Union [str , List [str ]],
180
+ prompt : Union [str , list [str ]],
180
181
request_id : Optional [str ] = None ,
181
182
prompt_token_count : Optional [int ] = None ,
182
183
output_token_count : Optional [int ] = None ,
@@ -232,7 +233,7 @@ async def chat_completions( # type: ignore[override]
232
233
self ,
233
234
content : Union [
234
235
str ,
235
- List [Union [str , Dict [str , Union [str , Dict [str , str ]]], Path , Image .Image ]],
236
+ list [Union [str , dict [str , Union [str , dict [str , str ]]], Path , Image .Image ]],
236
237
Any ,
237
238
],
238
239
request_id : Optional [str ] = None ,
@@ -318,7 +319,7 @@ def _get_async_client(self) -> httpx.AsyncClient:
318
319
319
320
return client
320
321
321
- def _headers (self ) -> Dict [str , str ]:
322
+ def _headers (self ) -> dict [str , str ]:
322
323
headers = {
323
324
"Content-Type" : "application/json" ,
324
325
}
@@ -335,8 +336,8 @@ def _headers(self) -> Dict[str, str]:
335
336
return headers
336
337
337
338
def _completions_payload (
338
- self , orig_kwargs : Optional [Dict ], max_output_tokens : Optional [int ], ** kwargs
339
- ) -> Dict :
339
+ self , orig_kwargs : Optional [dict ], max_output_tokens : Optional [int ], ** kwargs
340
+ ) -> dict :
340
341
payload = orig_kwargs or {}
341
342
payload .update (kwargs )
342
343
payload ["model" ] = self .model
@@ -366,10 +367,10 @@ def _completions_payload(
366
367
def _create_chat_messages (
367
368
content : Union [
368
369
str ,
369
- List [Union [str , Dict [str , Union [str , Dict [str , str ]]], Path , Image .Image ]],
370
+ list [Union [str , dict [str , Union [str , dict [str , str ]]], Path , Image .Image ]],
370
371
Any ,
371
372
],
372
- ) -> List [ Dict ]:
373
+ ) -> list [ dict ]:
373
374
if isinstance (content , str ):
374
375
return [
375
376
{
@@ -382,7 +383,7 @@ def _create_chat_messages(
382
383
resolved_content = []
383
384
384
385
for item in content :
385
- if isinstance (item , Dict ):
386
+ if isinstance (item , dict ):
386
387
resolved_content .append (item )
387
388
elif isinstance (item , str ):
388
389
resolved_content .append ({"type" : "text" , "text" : item })
@@ -430,8 +431,8 @@ async def _iterative_completions_request(
430
431
request_id : Optional [str ],
431
432
request_prompt_tokens : Optional [int ],
432
433
request_output_tokens : Optional [int ],
433
- headers : Dict ,
434
- payload : Dict ,
434
+ headers : dict ,
435
+ payload : dict ,
435
436
) -> AsyncGenerator [Union [StreamingTextResponse , ResponseSummary ], None ]:
436
437
if type_ == "text_completions" :
437
438
target = f"{ self .target } { TEXT_COMPLETIONS_PATH } "
@@ -551,7 +552,7 @@ async def _iterative_completions_request(
551
552
552
553
@staticmethod
553
554
def _extract_completions_delta_content (
554
- type_ : Literal ["text_completions" , "chat_completions" ], data : Dict
555
+ type_ : Literal ["text_completions" , "chat_completions" ], data : dict
555
556
) -> Optional [str ]:
556
557
if "choices" not in data or not data ["choices" ]:
557
558
return None
@@ -566,8 +567,8 @@ def _extract_completions_delta_content(
566
567
567
568
@staticmethod
568
569
def _extract_completions_usage (
569
- data : Dict ,
570
- ) -> Optional [Dict [Literal ["prompt" , "output" ], int ]]:
570
+ data : dict ,
571
+ ) -> Optional [dict [Literal ["prompt" , "output" ], int ]]:
571
572
if "usage" not in data or not data ["usage" ]:
572
573
return None
573
574
0 commit comments