Skip to content

Commit 210dfdc

Browse files
committed
Add azure provider
1 parent 7965487 commit 210dfdc

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

g4f/Provider/needs_auth/Azure.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

g4f/Provider/needs_auth/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .Anthropic import Anthropic
2+
from .Azure import Azure
23
from .BingCreateImages import BingCreateImages
34
from .BlackboxPro import BlackboxPro
45
from .CablyAI import CablyAI

g4f/Provider/template/OpenaiTemplate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ async def create_async_generator(
135135
if "usage" in data:
136136
yield Usage(**data["usage"])
137137
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"]:
140140
yield choice["message"]["content"].strip()
141141
if "tool_calls" in choice["message"]:
142142
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:
144144
yield FinishReason(choice["finish_reason"])
145145
return
146146
elif content_type.startswith("text/event-stream"):
@@ -153,8 +153,8 @@ async def create_async_generator(
153153
if not model_returned and model:
154154
yield ProviderInfo(**cls.get_dict(), model=model)
155155
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"]:
158158
delta = choice["delta"]["content"]
159159
if first:
160160
delta = delta.lstrip()
@@ -163,7 +163,7 @@ async def create_async_generator(
163163
yield delta
164164
if "usage" in data and data["usage"]:
165165
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:
167167
yield FinishReason(choice["finish_reason"])
168168
break
169169
else:

g4f/providers/base_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ def raise_error(data: dict, status: int = None):
408408
raise ResponseError(data["error"]["message"])
409409
else:
410410
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)}")
413413

414414
class AuthFileMixin():
415415

g4f/tools/media.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..image import is_data_an_media, to_input_audio, is_valid_media, is_valid_audio, to_data_uri
1111
from .files import get_bucket_dir, read_bucket
1212

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]:
1414
if (as_base64 or as_path or url.startswith("/")):
1515
file = Path(get_bucket_dir(bucket_id, "thumbnail", name))
1616
if not file.exists():

0 commit comments

Comments
 (0)