XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

feat: Improve handling of media and API response structures

- Added validation for `media` parameter in `MarkItDown` class to raise `ValueError` if `media` is not provided. - Included a check for `has_markitdown` in `MarkItDown` class to raise `ImportError` if `markitdown` is not installed. - Refactored API response structure by defining `responses` as a reusable dictionary in `g4f/api/__init__.py`. - Updated `/v1/chat/completions` endpoint to include a new parameter `conversation_id` in the `chat_completions` function. - Enhanced logic in `chat_completions` to set `conversation_id` from configuration or function input. - Added a new endpoint `/api/{provider}/{conversation_id}/chat/completions` in `g4f/api/__init__.py` to handle chat completions with `conversation_id`. - Replaced duplicate response dictionary definitions with the reusable `responses` dictionary in the `/api/{provider}/chat/completions` and `/v1/chat/completions` endpoints.

da650670
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +21 -10
Modified g4f/Provider/audio/MarkItDown.py +4 -0
@@ -23,6 +23,10 @@ class MarkItDown(AsyncGeneratorProvider, ProviderModelMixin):
23 23 media: MediaListType = None,
24 24 **kwargs
25 25 ) -> AsyncResult:
26 if media is None:
27 raise ValueError("MarkItDown requires media to be provided.")
28 if not has_markitdown:
29 raise ImportError("MarkItDown is not installed. Please install it with `pip install markitdown`.")
26 30 md = MaItDo()
27 31 for file, filename in media:
28 32 text = None
Modified g4f/api/__init__.py +17 -10
@@ -301,21 +301,25 @@ class Api:
301 301 })
302 302 return ErrorResponse.from_message("The model does not exist.", HTTP_404_NOT_FOUND)
303 303
304 @self.app.post("/v1/chat/completions", responses={
304 responses = {
305 305 HTTP_200_OK: {"model": ChatCompletion},
306 306 HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},
307 307 HTTP_404_NOT_FOUND: {"model": ErrorResponseModel},
308 308 HTTP_422_UNPROCESSABLE_ENTITY: {"model": ErrorResponseModel},
309 309 HTTP_500_INTERNAL_SERVER_ERROR: {"model": ErrorResponseModel},
310 })
310 }
311 @self.app.post("/v1/chat/completions", responses=responses)
311 312 async def chat_completions(
312 313 config: ChatCompletionsConfig,
313 314 credentials: Annotated[HTTPAuthorizationCredentials, Depends(Api.security)] = None,
314 provider: str = None
315 provider: str = None,
316 conversation_id: str = None,
315 317 ):
316 318 try:
317 319 if config.provider is None:
318 320 config.provider = AppConfig.provider if provider is None else provider
321 if config.conversation_id is None:
322 config.conversation_id = conversation_id
319 323 if credentials is not None and credentials.credentials != "secret":
320 324 config.api_key = credentials.credentials
321 325
@@ -395,13 +399,7 @@ class Api:
395 399 logger.exception(e)
396 400 return ErrorResponse.from_exception(e, config, HTTP_500_INTERNAL_SERVER_ERROR)
397 401
398 @self.app.post("/api/{provider}/chat/completions", responses={
399 HTTP_200_OK: {"model": ChatCompletion},
400 HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},
401 HTTP_404_NOT_FOUND: {"model": ErrorResponseModel},
402 HTTP_422_UNPROCESSABLE_ENTITY: {"model": ErrorResponseModel},
403 HTTP_500_INTERNAL_SERVER_ERROR: {"model": ErrorResponseModel},
404 })
402 @self.app.post("/api/{provider}/chat/completions", responses=responses)
405 403 async def provider_chat_completions(
406 404 provider: str,
407 405 config: ChatCompletionsConfig,
@@ -409,6 +407,15 @@ class Api:
409 407 ):
410 408 return await chat_completions(config, credentials, provider)
411 409
410 @self.app.post("/api/{provider}/{conversation_id}/chat/completions", responses=responses)
411 async def provider_chat_completions(
412 provider: str,
413 conversation_id: str,
414 config: ChatCompletionsConfig,
415 credentials: Annotated[HTTPAuthorizationCredentials, Depends(Api.security)] = None,
416 ):
417 return await chat_completions(config, credentials, provider, conversation_id)
418
412 419 responses = {
413 420 HTTP_200_OK: {"model": ImagesResponse},
414 421 HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},