返回提交历史
Added
g4f/Provider/needs_auth/Azure.py
+36
-0
Modified
g4f/Provider/needs_auth/__init__.py
+1
-0
Modified
g4f/Provider/template/OpenaiTemplate.py
+6
-6
Modified
g4f/providers/base_provider.py
+2
-2
Modified
g4f/tools/media.py
+1
-1
XFEstudio/gpt4free
Add azure provider
210dfdc5
代码差异
5 个文件
+46
-9
@@ -0,0 +1,36 @@
1
from __future__ import annotations
2
3
import os
4
5
from ...typing import Messages, AsyncResult
6
from ..template import OpenaiTemplate
7
8
class Azure(OpenaiTemplate):
9
working = True
10
needs_auth = True
11
12
@classmethod
13
async def create_async_generator(
14
cls,
15
model: str,
16
messages: Messages,
17
api_key: str = None,
18
api_endpoint: str = None,
19
**kwargs
20
) -> AsyncResult:
21
if not model:
22
model = os.environ.get("AZURE_DEFAULT_MODEL", cls.default_model)
23
if not api_key:
24
raise ValueError("API key is required for Azure provider")
25
if not api_endpoint:
26
api_endpoint = os.environ.get("AZURE_API_ENDPOINT")
27
if not api_endpoint:
28
raise ValueError("API endpoint is required for Azure provider")
29
async for chunk in super().create_async_generator(
30
model=model,
31
messages=messages,
32
api_key=api_key,
33
api_endpoint=api_endpoint,
34
**kwargs
35
):
36
yield chunk
@@ -1,4 +1,5 @@
1
1
from .Anthropic import Anthropic
2
from .Azure import Azure
2
3
from .BingCreateImages import BingCreateImages
3
4
from .BlackboxPro import BlackboxPro
4
5
from .CablyAI import CablyAI
@@ -135,12 +135,12 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
135
135
if "usage" in data:
136
136
yield Usage(**data["usage"])
137
137
if "choices" in data:
138
choice = data["choices"][0]
139
if "content" in choice["message"] and choice["message"]["content"]:
138
choice = next(iter(data["choices"]), None)
139
if choice and "content" in choice["message"] and choice["message"]["content"]:
140
140
yield choice["message"]["content"].strip()
141
141
if "tool_calls" in choice["message"]:
142
142
yield ToolCalls(choice["message"]["tool_calls"])
143
if "finish_reason" in choice and choice["finish_reason"] is not None:
143
if choice and "finish_reason" in choice and choice["finish_reason"] is not None:
144
144
yield FinishReason(choice["finish_reason"])
145
145
return
146
146
elif content_type.startswith("text/event-stream"):
@@ -153,8 +153,8 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
153
153
if not model_returned and model:
154
154
yield ProviderInfo(**cls.get_dict(), model=model)
155
155
model_returned = True
156
choice = data["choices"][0]
157
if "content" in choice["delta"] and choice["delta"]["content"]:
156
choice = next(iter(data["choices"]), None)
157
if choice and "content" in choice["delta"] and choice["delta"]["content"]:
158
158
delta = choice["delta"]["content"]
159
159
if first:
160
160
delta = delta.lstrip()
@@ -163,7 +163,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
163
163
yield delta
164
164
if "usage" in data and data["usage"]:
165
165
yield Usage(**data["usage"])
166
if "finish_reason" in choice and choice["finish_reason"] is not None:
166
if choice and "finish_reason" in choice and choice["finish_reason"] is not None:
167
167
yield FinishReason(choice["finish_reason"])
168
168
break
169
169
else:
@@ -408,8 +408,8 @@ class RaiseErrorMixin():
408
408
raise ResponseError(data["error"]["message"])
409
409
else:
410
410
raise ResponseError(data["error"])
411
elif ("choices" not in data or not data["choices"]) and "data" not in data:
412
raise ResponseError(f"Invalid response: {json.dumps(data)}")
411
#elif ("choices" not in data or not data["choices"]) and "data" not in data:
412
# raise ResponseError(f"Invalid response: {json.dumps(data)}")
413
413
414
414
class AuthFileMixin():
415
415
@@ -10,7 +10,7 @@ from ..typing import Messages
10
10
from ..image import is_data_an_media, to_input_audio, is_valid_media, is_valid_audio, to_data_uri
11
11
from .files import get_bucket_dir, read_bucket
12
12
13
def render_media(bucket_id: str, name: str, url: str, as_path: bool = False, as_base64: bool = False) -> Union[str, Path]:
13
def render_media(bucket_id: str, name: str, url: str, as_path: bool = False, as_base64: bool = False, **kwargs) -> Union[str, Path]:
14
14
if (as_base64 or as_path or url.startswith("/")):
15
15
file = Path(get_bucket_dir(bucket_id, "thumbnail", name))
16
16
if not file.exists():