Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions astrbot/core/provider/sources/whisper_api_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import httpx
from openai import NOT_GIVEN, AsyncOpenAI

from astrbot.core.utils.media_utils import MediaResolver
from astrbot.core.utils.network_utils import create_proxy_client

from ..entities import ProviderType
from ..provider import STTProvider
Expand All @@ -20,11 +22,24 @@ def __init__(
) -> None:
super().__init__(provider_config, provider_settings)
self.chosen_api_key = provider_config.get("api_key", "")
proxy = provider_config.get("proxy", "")
httpx_module = httpx
try:
# The OpenAI SDK can bundle its own compatible httpx module.
from openai import _base_client as openai_base_client

httpx_module = getattr(openai_base_client, "httpx", httpx)
except ImportError:
pass
http_client = create_proxy_client(
"OpenAI Whisper", proxy, httpx_module=httpx_module
)

self.client = AsyncOpenAI(
api_key=self.chosen_api_key,
base_url=provider_config.get("api_base"),
timeout=provider_config.get("timeout", NOT_GIVEN),
http_client=http_client,
)

self.set_model(provider_config["model"])
Expand Down
32 changes: 31 additions & 1 deletion tests/test_whisper_api_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock
from unittest.mock import ANY, AsyncMock, MagicMock, patch

import pytest

Expand Down Expand Up @@ -28,6 +28,36 @@ def _make_provider() -> ProviderOpenAIWhisperAPI:
return provider


def test_init_passes_the_provider_proxy_to_the_http_client():
proxy = "http://127.0.0.1:7890"
http_client = MagicMock()

with (
patch(
"astrbot.core.provider.sources.whisper_api_source.create_proxy_client",
return_value=http_client,
) as create_proxy_client,
patch(
"astrbot.core.provider.sources.whisper_api_source.AsyncOpenAI"
) as async_openai,
):
ProviderOpenAIWhisperAPI(
provider_config={
"id": "test-whisper-api",
"type": "openai_whisper_api",
"model": "whisper-1",
"api_key": "test-key",
"proxy": proxy,
},
provider_settings={},
)

create_proxy_client.assert_called_once_with(
"OpenAI Whisper", proxy, httpx_module=ANY
)
assert async_openai.call_args.kwargs["http_client"] is http_client


@pytest.mark.asyncio
async def test_get_text_converts_opus_files_to_wav_before_transcription(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand Down
Loading