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
代码差异
@@ -23,6 +23,10 @@ class MarkItDown(AsyncGeneratorProvider, ProviderModelMixin):
media: MediaListType = None,
**kwargs
) -> AsyncResult:
if media is None:
raise ValueError("MarkItDown requires media to be provided.")
if not has_markitdown:
raise ImportError("MarkItDown is not installed. Please install it with `pip install markitdown`.")
md = MaItDo()
for file, filename in media:
text = None
@@ -301,21 +301,25 @@ class Api:
})
return ErrorResponse.from_message("The model does not exist.", HTTP_404_NOT_FOUND)
@self.app.post("/v1/chat/completions", responses={
responses = {
HTTP_200_OK: {"model": ChatCompletion},
HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},
HTTP_404_NOT_FOUND: {"model": ErrorResponseModel},
HTTP_422_UNPROCESSABLE_ENTITY: {"model": ErrorResponseModel},
HTTP_500_INTERNAL_SERVER_ERROR: {"model": ErrorResponseModel},
})
}
@self.app.post("/v1/chat/completions", responses=responses)
async def chat_completions(
config: ChatCompletionsConfig,
credentials: Annotated[HTTPAuthorizationCredentials, Depends(Api.security)] = None,
provider: str = None
provider: str = None,
conversation_id: str = None,
):
try:
if config.provider is None:
config.provider = AppConfig.provider if provider is None else provider
if config.conversation_id is None:
config.conversation_id = conversation_id
if credentials is not None and credentials.credentials != "secret":
config.api_key = credentials.credentials
@@ -395,13 +399,7 @@ class Api:
logger.exception(e)
return ErrorResponse.from_exception(e, config, HTTP_500_INTERNAL_SERVER_ERROR)
@self.app.post("/api/{provider}/chat/completions", responses={
HTTP_200_OK: {"model": ChatCompletion},
HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},
HTTP_404_NOT_FOUND: {"model": ErrorResponseModel},
HTTP_422_UNPROCESSABLE_ENTITY: {"model": ErrorResponseModel},
HTTP_500_INTERNAL_SERVER_ERROR: {"model": ErrorResponseModel},
})
@self.app.post("/api/{provider}/chat/completions", responses=responses)
async def provider_chat_completions(
provider: str,
config: ChatCompletionsConfig,
@@ -409,6 +407,15 @@ class Api:
):
return await chat_completions(config, credentials, provider)
@self.app.post("/api/{provider}/{conversation_id}/chat/completions", responses=responses)
async def provider_chat_completions(
provider: str,
conversation_id: str,
config: ChatCompletionsConfig,
credentials: Annotated[HTTPAuthorizationCredentials, Depends(Api.security)] = None,
):
return await chat_completions(config, credentials, provider, conversation_id)
responses = {
HTTP_200_OK: {"model": ImagesResponse},
HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},