@@ -39,7 +39,7 @@ class Annotated:
39
39
from g4f .client import AsyncClient , ChatCompletion , ImagesResponse , convert_to_provider
40
40
from g4f .providers .response import BaseConversation
41
41
from g4f .client .helper import filter_none
42
- from g4f .image import is_accepted_format , images_dir
42
+ from g4f .image import is_accepted_format , is_data_uri_an_image , images_dir
43
43
from g4f .typing import Messages
44
44
from g4f .errors import ProviderNotFoundError , ModelNotFoundError , MissingAuthError
45
45
from g4f .cookies import read_cookie_files , get_cookies_dir
@@ -93,6 +93,8 @@ class ChatCompletionsConfig(BaseModel):
93
93
model : str = Field (default = "" )
94
94
provider : Optional [str ] = None
95
95
stream : bool = False
96
+ image : Optional [str ] = None
97
+ image_name : Optional [str ] = None
96
98
temperature : Optional [float ] = None
97
99
max_tokens : Optional [int ] = None
98
100
stop : Union [list [str ], str , None ] = None
@@ -263,6 +265,7 @@ async def model_info(model_name: str) -> ModelResponseModel:
263
265
HTTP_200_OK : {"model" : ChatCompletion },
264
266
HTTP_401_UNAUTHORIZED : {"model" : ErrorResponseModel },
265
267
HTTP_404_NOT_FOUND : {"model" : ErrorResponseModel },
268
+ HTTP_422_UNPROCESSABLE_ENTITY : {"model" : ErrorResponseModel },
266
269
HTTP_500_INTERNAL_SERVER_ERROR : {"model" : ErrorResponseModel },
267
270
})
268
271
async def chat_completions (
@@ -284,14 +287,20 @@ async def chat_completions(
284
287
if config .provider in self .conversations [config .conversation_id ]:
285
288
conversation = self .conversations [config .conversation_id ][config .provider ]
286
289
290
+ if config .image is not None :
291
+ try :
292
+ is_data_uri_an_image (config .image )
293
+ except ValueError as e :
294
+ return ErrorResponse .from_message (f"The image you send must be a data URI. Example: data:image/webp;base64,..." , status_code = HTTP_422_UNPROCESSABLE_ENTITY )
295
+
287
296
# Create the completion response
288
297
response = self .client .chat .completions .create (
289
298
** filter_none (
290
299
** {
291
300
"model" : AppConfig .model ,
292
301
"provider" : AppConfig .provider ,
293
302
"proxy" : AppConfig .proxy ,
294
- ** config .dict (exclude_none = True ),
303
+ ** config .model_dump (exclude_none = True ),
295
304
** {
296
305
"conversation_id" : None ,
297
306
"return_conversation" : return_conversation ,
0 commit comments