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

XFEstudio/gpt4free

Add api prefix to provider api endpoints Add api_key to get models call

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

代码差异

1 个文件 +12 -6
Modified g4f/api/__init__.py +12 -6
@@ -169,7 +169,7 @@ class Api:
169 169 except HTTPException:
170 170 user_g4f_api_key = None
171 171 path = request.url.path
172 if path.startswith("/v1") or (AppConfig.demo and path == '/backend-api/v2/upload_cookies'):
172 if path.startswith("/v1") or path.startswith("/api/") or (AppConfig.demo and path == '/backend-api/v2/upload_cookies'):
173 173 if user_g4f_api_key is None:
174 174 return ErrorResponse.from_message("G4F API key required", HTTP_401_UNAUTHORIZED)
175 175 if not secrets.compare_digest(AppConfig.g4f_api_key, user_g4f_api_key):
@@ -245,13 +245,19 @@ class Api:
245 245 ]
246 246 }
247 247
248 @self.app.get("/{provider}/models", responses={
248 @self.app.get("/api/{provider}/models", responses={
249 249 HTTP_200_OK: {"model": List[ModelResponseModel]},
250 250 })
251 async def models(provider: str):
251 async def models(provider: str, credentials: Annotated[HTTPAuthorizationCredentials, Depends(Api.security)] = None):
252 252 if provider not in ProviderUtils.convert:
253 253 return ErrorResponse.from_message("The provider does not exist.", 404)
254 254 provider: ProviderType = ProviderUtils.convert[provider]
255 if not hasattr(provider, "get_models"):
256 models = []
257 elif credentials is not None:
258 models = provider.get_models(api_key=credentials.credentials)
259 else:
260 models = provider.get_models()
255 261 return {
256 262 "object": "list",
257 263 "data": [{
@@ -260,8 +266,8 @@ class Api:
260 266 "created": 0,
261 267 "owned_by": getattr(provider, "label", provider.__name__),
262 268 "image": model in getattr(provider, "image_models", []),
263 "image": model in getattr(provider, "vision_models", []),
264 } for model in provider.get_models() if hasattr(provider, "get_models")]
269 "vision": model in getattr(provider, "vision_models", []),
270 } for model in models]
265 271 }
266 272
267 273 @self.app.get("/v1/models/{model_name}", responses={
@@ -371,7 +377,7 @@ class Api:
371 377 logger.exception(e)
372 378 return ErrorResponse.from_exception(e, config, HTTP_500_INTERNAL_SERVER_ERROR)
373 379
374 @self.app.post("/{provider}/chat/completions", responses={
380 @self.app.post("/api/{provider}/chat/completions", responses={
375 381 HTTP_200_OK: {"model": ChatCompletion},
376 382 HTTP_401_UNAUTHORIZED: {"model": ErrorResponseModel},
377 383 HTTP_404_NOT_FOUND: {"model": ErrorResponseModel},