mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix: align FlowSpeech integration with provider contracts
This commit is contained in:
parent
d84f5aa51d
commit
337b6791db
9 changed files with 194 additions and 43 deletions
|
|
@ -673,6 +673,7 @@ nebius_embedding_models: Set = set()
|
|||
aiml_models: Set = set()
|
||||
deepgram_models: Set = set()
|
||||
elevenlabs_models: Set = set()
|
||||
flowspeech_models: Final[set[str]] = set() # mutable-ok: populated when the shared model catalog loads
|
||||
dashscope_models: Set = set()
|
||||
qwencloud_models: Set = set()
|
||||
qwen_ai_platform_models: Set = set()
|
||||
|
|
@ -949,6 +950,8 @@ def _populate_provider_model_sets(model_cost_map: Mapping[str, Mapping[str, obje
|
|||
deepgram_models.add(key)
|
||||
elif value.get("litellm_provider") == "elevenlabs":
|
||||
elevenlabs_models.add(key)
|
||||
elif value.get("litellm_provider") == "flowspeech":
|
||||
flowspeech_models.add(key)
|
||||
elif value.get("litellm_provider") == "heroku":
|
||||
heroku_models.add(key)
|
||||
elif value.get("litellm_provider") == "dashscope":
|
||||
|
|
@ -1131,6 +1134,7 @@ model_list = list(
|
|||
| nscale_models
|
||||
| deepgram_models
|
||||
| elevenlabs_models
|
||||
| flowspeech_models
|
||||
| dashscope_models
|
||||
| qwencloud_models
|
||||
| qwen_ai_platform_models
|
||||
|
|
@ -1247,6 +1251,7 @@ def _build_models_by_provider() -> dict:
|
|||
"featherless_ai": featherless_ai_models,
|
||||
"deepgram": deepgram_models,
|
||||
"elevenlabs": elevenlabs_models,
|
||||
"flowspeech": flowspeech_models,
|
||||
"heroku": heroku_models,
|
||||
"dashscope": dashscope_models,
|
||||
"qwencloud": qwencloud_models,
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ from typing import Final, TypeAlias
|
|||
|
||||
import httpx
|
||||
|
||||
from litellm.llms.custom_httpx.http_handler import (
|
||||
get_async_httpx_client, # pyright: ignore[reportUnknownVariableType] # unused factory params are untyped upstream
|
||||
)
|
||||
from litellm.types.llms.openai import HttpxBinaryResponseContent
|
||||
from litellm.types.utils import LlmProviders
|
||||
|
||||
from .transformation import (
|
||||
FlowSpeechErrorResponse,
|
||||
|
|
@ -339,10 +343,10 @@ class FlowSpeechTTS:
|
|||
flow.close()
|
||||
|
||||
async def arun(self, client: httpx.AsyncClient | None = None) -> HttpxBinaryResponseContent:
|
||||
if client is None:
|
||||
async with httpx.AsyncClient(trust_env=False) as owned_client:
|
||||
return await self._arun_with_deadline(owned_client)
|
||||
return await self._arun_with_deadline(client)
|
||||
selected_client: Final = (
|
||||
client if client is not None else get_async_httpx_client(LlmProviders.FLOWSPEECH).client
|
||||
)
|
||||
return await self._arun_with_deadline(selected_client)
|
||||
|
||||
async def _arun_with_deadline(self, client: httpx.AsyncClient) -> HttpxBinaryResponseContent:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -231,6 +231,10 @@ class FlowSpeechTextToSpeechConfig(BaseTextToSpeechConfig):
|
|||
def get_complete_url(self, model: str, api_base: str | None, litellm_params: Mapping[str, object]) -> str:
|
||||
base: Final = api_base if api_base is not None else (get_secret_str("FLOWSPEECH_API_BASE") or self.TTS_BASE_URL)
|
||||
url: Final = validate_url(base)
|
||||
if url.query or url.path.rstrip("/") not in ("", self.TTS_ENDPOINT_PATH):
|
||||
raise ValueError("FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint")
|
||||
return str(url.copy_with(path=self.TTS_ENDPOINT_PATH))
|
||||
if url.query:
|
||||
raise ValueError("FlowSpeech api_base must not contain a query")
|
||||
path: Final = url.path.rstrip("/")
|
||||
if path.endswith("/api/ai/text-to-speech"):
|
||||
raise ValueError("FlowSpeech api_base must not use the legacy text-to-speech endpoint")
|
||||
endpoint: Final = path if path.endswith(self.TTS_ENDPOINT_PATH) else path + self.TTS_ENDPOINT_PATH
|
||||
return str(url.copy_with(path=endpoint))
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ if TYPE_CHECKING:
|
|||
import dotenv
|
||||
import httpx
|
||||
import openai
|
||||
from pydantic import BaseModel, TypeAdapter, ValidationError
|
||||
from pydantic import BaseModel, StrictStr, TypeAdapter, ValidationError
|
||||
from typing_extensions import assert_never, overload
|
||||
|
||||
import litellm
|
||||
|
|
@ -8892,21 +8892,27 @@ def speech(
|
|||
if api_key is not None:
|
||||
litellm_params_dict["api_key"] = api_key
|
||||
|
||||
flowspeech_params: Final = TypeAdapter(Mapping[str, object]).validate_python(litellm_params_dict)
|
||||
flowspeech_headers: Final = flowspeech_config.validate_environment(
|
||||
extra_headers or headers or MappingProxyType({}), model, litellm_params_dict.get("api_key"), api_base
|
||||
TypeAdapter(Mapping[StrictStr, StrictStr]).validate_python(
|
||||
extra_headers or headers or MappingProxyType({})
|
||||
),
|
||||
model,
|
||||
TypeAdapter[str | None](str | None).validate_python(flowspeech_params.get("api_key"), strict=True),
|
||||
api_base,
|
||||
)
|
||||
flowspeech_request: Final = flowspeech_config.transform_text_to_speech_request(
|
||||
model=model,
|
||||
input=input,
|
||||
voice=voice if isinstance(voice, str) else None,
|
||||
optional_params=optional_params,
|
||||
litellm_params=litellm_params_dict,
|
||||
optional_params=TypeAdapter(Mapping[str, object]).validate_python(optional_params),
|
||||
litellm_params=flowspeech_params,
|
||||
headers=flowspeech_headers,
|
||||
)
|
||||
flowspeech_url: Final = flowspeech_config.get_complete_url(model, api_base, litellm_params_dict)
|
||||
flowspeech_url: Final = flowspeech_config.get_complete_url(model, api_base, flowspeech_params)
|
||||
|
||||
def flowspeech_post_call(job: FlowSpeechJob) -> None:
|
||||
logging_obj.post_call(
|
||||
logging_obj.post_call( # pyright: ignore[reportUnknownMemberType] # upstream logging callback has untyped parameters
|
||||
input=input,
|
||||
api_key="",
|
||||
original_response=job.model_dump_json(include={"id", "requestId", "status", "usage"}),
|
||||
|
|
@ -8920,27 +8926,30 @@ def speech(
|
|||
**MappingProxyType(
|
||||
{key: value for key, value in flowspeech_headers.items() if key.lower() != "idempotency-key"}
|
||||
),
|
||||
**flowspeech_request["headers"],
|
||||
**TypeAdapter(Mapping[str, str]).validate_python(flowspeech_request.get("headers"), strict=True),
|
||||
}
|
||||
),
|
||||
body=flowspeech_request["dict_body"],
|
||||
timeout=timeout,
|
||||
body=TypeAdapter(Mapping[str, object]).validate_python(flowspeech_request.get("dict_body")),
|
||||
timeout=http_timeout,
|
||||
max_retries=max_retries,
|
||||
on_completed=flowspeech_post_call,
|
||||
)
|
||||
logging_obj.pre_call(
|
||||
logging_obj.pre_call( # pyright: ignore[reportUnknownMemberType] # upstream logging callback has untyped parameters
|
||||
input=input,
|
||||
api_key="",
|
||||
additional_args=MappingProxyType({"api_base": flowspeech_url, "request_id": flowspeech_job.request_id}),
|
||||
)
|
||||
flowspeech_client: Final = client.client if isinstance(client, (HTTPHandler, AsyncHTTPHandler)) else client
|
||||
if flowspeech_client is not None and not isinstance(
|
||||
flowspeech_client, httpx.AsyncClient if aspeech else httpx.Client
|
||||
):
|
||||
raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call")
|
||||
response = ( # rebind-ok: provider branch sets dispatch result
|
||||
flowspeech_job.arun(flowspeech_client) if aspeech else flowspeech_job.run(flowspeech_client)
|
||||
flowspeech_client: Final = TypeAdapter(object).validate_python(
|
||||
client.client if isinstance(client, (HTTPHandler, AsyncHTTPHandler)) else client
|
||||
)
|
||||
if aspeech:
|
||||
if flowspeech_client is not None and not isinstance(flowspeech_client, httpx.AsyncClient):
|
||||
raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call")
|
||||
response = flowspeech_job.arun(flowspeech_client) # rebind-ok: provider branch sets dispatch result
|
||||
else:
|
||||
if flowspeech_client is not None and not isinstance(flowspeech_client, httpx.Client):
|
||||
raise ValueError("FlowSpeech client must match the synchronous or asynchronous speech call")
|
||||
response = flowspeech_job.run(flowspeech_client) # rebind-ok: provider branch sets dispatch result
|
||||
elif custom_llm_provider == "vertex_ai" or custom_llm_provider == "vertex_ai_beta":
|
||||
from litellm.llms.vertex_ai.text_to_speech.transformation import (
|
||||
VertexAILyriaTextToSpeechConfig,
|
||||
|
|
|
|||
|
|
@ -13378,22 +13378,26 @@ async def audio_speech(
|
|||
response_format=requested_format if isinstance(requested_format, str) else None,
|
||||
)
|
||||
|
||||
if isinstance(response, HttpxBinaryResponseContent) and "x-flowspeech-job-id" in response.response.headers:
|
||||
media_type = "audio/wav"
|
||||
custom_headers.update(
|
||||
MappingProxyType(
|
||||
{
|
||||
key: response.response.headers[key]
|
||||
for key in ("x-flowspeech-job-id", "x-flowspeech-request-id")
|
||||
if key in response.response.headers
|
||||
}
|
||||
)
|
||||
flowspeech_raw_response: Final = _UNTYPED_VALUE.validate_python(
|
||||
getattr(_UNTYPED_VALUE.validate_python(response), "response", None)
|
||||
)
|
||||
flowspeech_recovery_headers: Final[Mapping[str, str]] = (
|
||||
MappingProxyType(
|
||||
{
|
||||
key: flowspeech_raw_response.headers[key]
|
||||
for key in ("x-flowspeech-job-id", "x-flowspeech-request-id")
|
||||
if key in flowspeech_raw_response.headers
|
||||
}
|
||||
)
|
||||
if isinstance(flowspeech_raw_response, httpx.Response)
|
||||
and "x-flowspeech-job-id" in flowspeech_raw_response.headers
|
||||
else MappingProxyType({})
|
||||
)
|
||||
|
||||
return StreamingResponse(
|
||||
_audio_speech_chunk_generator(response),
|
||||
media_type=media_type,
|
||||
headers=custom_headers,
|
||||
headers={**TypeAdapter(Mapping[str, str]).validate_python(custom_headers), **flowspeech_recovery_headers},
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -1561,6 +1561,34 @@
|
|||
],
|
||||
"default_model_placeholder": "gpt-3.5-turbo"
|
||||
},
|
||||
{
|
||||
"provider": "FlowSpeech",
|
||||
"provider_display_name": "FlowSpeech",
|
||||
"litellm_provider": "flowspeech",
|
||||
"credential_fields": [
|
||||
{
|
||||
"key": "api_key",
|
||||
"label": "API Key",
|
||||
"placeholder": null,
|
||||
"tooltip": null,
|
||||
"required": true,
|
||||
"field_type": "password",
|
||||
"options": null,
|
||||
"default_value": null
|
||||
},
|
||||
{
|
||||
"key": "api_base",
|
||||
"label": "API Base",
|
||||
"placeholder": "https://flowspeech.io",
|
||||
"tooltip": "HTTPS origin, trusted gateway prefix, or complete /api/v1/tts/jobs endpoint",
|
||||
"required": false,
|
||||
"field_type": "text",
|
||||
"options": null,
|
||||
"default_value": null
|
||||
}
|
||||
],
|
||||
"default_model_placeholder": "flowspeech/flowspeech-tts"
|
||||
},
|
||||
{
|
||||
"provider": "FRIENDLIAI",
|
||||
"provider_display_name": "Friendliai",
|
||||
|
|
|
|||
|
|
@ -9,8 +9,10 @@ from email.utils import format_datetime
|
|||
import httpx
|
||||
import pytest
|
||||
|
||||
from litellm.llms.custom_httpx.http_handler import get_async_httpx_client
|
||||
from litellm.llms.flowspeech.text_to_speech.handler import FlowSpeechTTS
|
||||
from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException
|
||||
from litellm.types.utils import LlmProviders
|
||||
|
||||
REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39"
|
||||
JOB_ID = "11111111-2222-4333-8444-555555555555"
|
||||
|
|
@ -504,7 +506,10 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful):
|
|||
return client
|
||||
|
||||
if owned:
|
||||
monkeypatch.setattr(httpx, "AsyncClient" if asynchronous else "Client", create_client)
|
||||
if asynchronous:
|
||||
get_async_httpx_client(LlmProviders.FLOWSPEECH).client = client
|
||||
else:
|
||||
monkeypatch.setattr(httpx, "Client", create_client)
|
||||
tts = handler(Clock())
|
||||
|
||||
async def call():
|
||||
|
|
@ -520,7 +525,7 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful):
|
|||
with pytest.raises(FlowSpeechException, match="FlowSpeech job failed"):
|
||||
await call()
|
||||
assert methods == (["POST", "GET"] if successful else ["POST"])
|
||||
assert client.is_closed is owned
|
||||
assert client.is_closed is (owned and not asynchronous)
|
||||
finally:
|
||||
if asynchronous:
|
||||
await client.aclose()
|
||||
|
|
@ -528,6 +533,34 @@ async def test_client_lifetime(monkeypatch, asynchronous, owned, successful):
|
|||
client.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_async_client_is_reused_without_download_credentials():
|
||||
requests = []
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
if request.method == "POST":
|
||||
return httpx.Response(202, json=job())
|
||||
assert "authorization" not in request.headers
|
||||
assert "cookie" not in request.headers
|
||||
assert "x-client-secret" not in request.headers
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
cached = get_async_httpx_client(LlmProviders.FLOWSPEECH)
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(respond),
|
||||
headers={"Authorization": "discard", "X-Client-Secret": "discard"},
|
||||
cookies={"session": "discard"},
|
||||
) as client:
|
||||
cached.client = client
|
||||
for _ in range(2):
|
||||
result = await handler(Clock()).arun()
|
||||
assert result.content == wav_bytes()
|
||||
assert not client.is_closed
|
||||
assert get_async_httpx_client(LlmProviders.FLOWSPEECH).client is client
|
||||
assert [request.method for request in requests] == ["POST", "GET", "POST", "GET"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_total_deadline_interrupts_network():
|
||||
async def respond(request):
|
||||
|
|
|
|||
|
|
@ -169,11 +169,11 @@ def test_complete_url(base):
|
|||
("http://example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"),
|
||||
("https://u:p@example.com", "FlowSpeech requires an HTTPS URL without credentials or fragment"),
|
||||
("file:///tmp/test", "FlowSpeech requires an HTTPS URL without credentials or fragment"),
|
||||
("https://example.com/?key=x", "FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint"),
|
||||
("https://example.com/?key=x", "FlowSpeech api_base must not contain a query"),
|
||||
("https://example.com/#x", "FlowSpeech requires an HTTPS URL without credentials or fragment"),
|
||||
(
|
||||
"https://example.com/api/ai/text-to-speech",
|
||||
"FlowSpeech api_base must be an origin or the /api/v1/tts/jobs endpoint",
|
||||
"FlowSpeech api_base must not use the legacy text-to-speech endpoint",
|
||||
),
|
||||
("", "FlowSpeech requires an HTTPS URL without credentials or fragment"),
|
||||
],
|
||||
|
|
@ -183,6 +183,19 @@ def test_rejects_invalid_base(base, message):
|
|||
FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"base,expected",
|
||||
[
|
||||
("https://gateway.example/p", "https://gateway.example/p/api/v1/tts/jobs"),
|
||||
("https://gateway.example/p/", "https://gateway.example/p/api/v1/tts/jobs"),
|
||||
("https://gateway.example/p/api/v1/tts/jobs/", "https://gateway.example/p/api/v1/tts/jobs"),
|
||||
("https://api/v1/", "https://api/v1/api/v1/tts/jobs"),
|
||||
],
|
||||
)
|
||||
def test_gateway_base_preserves_prefix_without_duplicating_endpoint(base, expected):
|
||||
assert FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", base, {}) == expected
|
||||
|
||||
|
||||
def test_registers_provider():
|
||||
from litellm.utils import ProviderConfigManager
|
||||
|
||||
|
|
@ -191,10 +204,14 @@ def test_registers_provider():
|
|||
assert model == "flowspeech-tts"
|
||||
assert provider == "flowspeech"
|
||||
assert isinstance(config, FlowSpeechTextToSpeechConfig)
|
||||
assert "flowspeech/flowspeech-tts" in litellm.model_list
|
||||
assert litellm.models_by_provider[provider] == litellm.flowspeech_models
|
||||
assert "flowspeech/flowspeech-tts" in litellm.models_by_provider[provider]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("multi", [False, True])
|
||||
def test_speech_dispatch(multi):
|
||||
@pytest.mark.parametrize("timeout_args", [{}, {"timeout": None}, {"timeout": 60}])
|
||||
def test_speech_dispatch(multi, timeout_args):
|
||||
requests = []
|
||||
logs = []
|
||||
segments = [{"speaker": "A", "text": "Hi", "voiceName": "Kore"}]
|
||||
|
|
@ -229,9 +246,9 @@ def test_speech_dispatch(multi):
|
|||
api_key="test-key",
|
||||
response_format="wav",
|
||||
request_id=REQUEST_ID,
|
||||
timeout=60,
|
||||
client=HTTPHandler(client=http_client),
|
||||
logger_fn=log_event,
|
||||
**timeout_args,
|
||||
**({"segments": segments} if multi else {"voice": "Aoede"}),
|
||||
)
|
||||
assert result.content == wav_bytes()
|
||||
|
|
@ -245,7 +262,8 @@ def test_speech_dispatch(multi):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_aspeech_dispatch():
|
||||
@pytest.mark.parametrize("timeout_args", [{}, {"timeout": None}, {"timeout": 60}])
|
||||
async def test_aspeech_dispatch(timeout_args):
|
||||
requests = []
|
||||
logs = []
|
||||
|
||||
|
|
@ -268,8 +286,8 @@ async def test_aspeech_dispatch():
|
|||
input="Hello",
|
||||
api_key="test-key",
|
||||
client=client,
|
||||
timeout=60,
|
||||
logger_fn=log_event,
|
||||
**timeout_args,
|
||||
)
|
||||
assert result.content == wav_bytes()
|
||||
assert len(requests) == 2
|
||||
|
|
@ -277,3 +295,31 @@ async def test_aspeech_dispatch():
|
|||
assert json.loads(logs[0][1])["requestId"] == requests[0].headers["idempotency-key"]
|
||||
assert logs[0][2]["job_id"] == JOB_ID
|
||||
assert "test-key" not in repr(logs) and "audio.example.com" not in repr(logs)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
async def test_dispatch_rejects_mismatched_client_before_sending(asynchronous):
|
||||
requests = []
|
||||
|
||||
def respond(request):
|
||||
requests.append(request)
|
||||
return httpx.Response(200, json=success())
|
||||
|
||||
client_type = httpx.Client if asynchronous else httpx.AsyncClient
|
||||
client = client_type(transport=httpx.MockTransport(respond))
|
||||
try:
|
||||
kwargs = {"model": "flowspeech/flowspeech-tts", "input": "Hello", "api_key": "test-key", "client": client}
|
||||
message = "client must match the synchronous or asynchronous speech call"
|
||||
if asynchronous:
|
||||
with pytest.raises(litellm.APIConnectionError, match=message):
|
||||
await litellm.aspeech(**kwargs)
|
||||
else:
|
||||
with pytest.raises(ValueError, match=message):
|
||||
litellm.speech(**kwargs)
|
||||
assert requests == []
|
||||
finally:
|
||||
if isinstance(client, httpx.AsyncClient):
|
||||
await client.aclose()
|
||||
else:
|
||||
client.close()
|
||||
|
|
|
|||
|
|
@ -560,6 +560,24 @@ def test_every_backend_provider_is_listed_in_add_model_or_frozen_as_unlisted():
|
|||
)
|
||||
|
||||
|
||||
def test_flowspeech_provider_fields_support_configured_credentials():
|
||||
app_instance = FastAPI()
|
||||
app_instance.include_router(router)
|
||||
test_client = TestClient(app_instance)
|
||||
|
||||
response = test_client.get("/public/providers/fields")
|
||||
assert response.status_code == 200
|
||||
entry = next(p for p in response.json() if p["litellm_provider"] == "flowspeech")
|
||||
fields = {field["key"]: field for field in entry["credential_fields"]}
|
||||
assert entry["provider_display_name"] == "FlowSpeech"
|
||||
assert entry["default_model_placeholder"] in litellm.models_by_provider["flowspeech"]
|
||||
assert fields["api_key"]["required"] is True
|
||||
assert fields["api_key"]["field_type"] == "password"
|
||||
assert fields["api_base"]["required"] is False
|
||||
assert fields["api_base"]["field_type"] == "text"
|
||||
assert fields["api_base"]["placeholder"] == "https://flowspeech.io"
|
||||
|
||||
|
||||
def test_google_ai_studio_provider_fields_expose_api_base():
|
||||
"""The Google AI Studio (gemini) credential form must let admins set a custom
|
||||
api_base so they can point at a Gemini-compatible gateway (e.g. a self-hosted
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue