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

XFEstudio/gpt4free

Check content-type in OpenaiAPI provider

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

代码差异

1 个文件 +6 -3
Modified g4f/Provider/needs_auth/OpenaiAPI.py +6 -3
@@ -8,7 +8,7 @@ from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin, RaiseErr
8 8 from ...typing import Union, Optional, AsyncResult, Messages, ImagesType
9 9 from ...requests import StreamSession, raise_for_status
10 10 from ...providers.response import FinishReason, ToolCalls, Usage
11 from ...errors import MissingAuthError
11 from ...errors import MissingAuthError, ResponseError
12 12 from ...image import to_data_uri
13 13 from ... import debug
14 14
@@ -108,7 +108,7 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin):
108 108 if api_endpoint is None:
109 109 api_endpoint = f"{api_base.rstrip('/')}/chat/completions"
110 110 async with session.post(api_endpoint, json=data) as response:
111 if not stream or response.headers.get("content-type") == "application/json":
111 if response.headers.get("content-type", None if stream else "application/json") == "application/json":
112 112 data = await response.json()
113 113 cls.raise_error(data)
114 114 await raise_for_status(response)
@@ -122,7 +122,7 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin):
122 122 if "finish_reason" in choice and choice["finish_reason"] is not None:
123 123 yield FinishReason(choice["finish_reason"])
124 124 return
125 else:
125 elif response.headers.get("content-type", "text/event-stream" if stream else None) == "text/event-stream":
126 126 await raise_for_status(response)
127 127 first = True
128 128 async for line in response.iter_lines():
@@ -145,6 +145,9 @@ class OpenaiAPI(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin):
145 145 if "finish_reason" in choice and choice["finish_reason"] is not None:
146 146 yield FinishReason(choice["finish_reason"])
147 147 break
148 else:
149 await raise_for_status(response)
150 raise ResponseError(f"Not supported content-type: {response.headers.get("content-type")}")
148 151
149 152 @classmethod
150 153 def get_headers(cls, stream: bool, api_key: str = None, headers: dict = None) -> dict: