mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:48:36 +00:00
298 lines
9.3 KiB
Python
298 lines
9.3 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from livekit.agents.llm import ChatContext, FunctionCall, FunctionCallOutput, ImageContent
|
|
from livekit.plugins.sarvam.llm.client import (
|
|
LLM as SarvamLLM,
|
|
SARVAM_LLM_BASE_URL_V1,
|
|
SARVAM_LLM_BASE_URL_V2,
|
|
)
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _sent_kwargs(llm: SarvamLLM, stream: Any) -> dict[str, Any]:
|
|
"""The kwargs a chat sends: the call's, with the instance's over them."""
|
|
return {**(stream._extra_kwargs or {}), **llm._native.extra_kwargs}
|
|
|
|
|
|
def _chat_ctx(text: str = "hello") -> ChatContext:
|
|
ctx = ChatContext.empty()
|
|
ctx.add_message(role="user", content=text)
|
|
return ctx
|
|
|
|
|
|
def _chat_ctx_with_image() -> ChatContext:
|
|
ctx = ChatContext.empty()
|
|
ctx.add_message(
|
|
role="user",
|
|
content=[
|
|
"describe this image",
|
|
ImageContent(image="data:image/png;base64,iVBORw0KGgo="),
|
|
],
|
|
)
|
|
return ctx
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Constructor — model validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Auth and headers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Extra body filtering
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Image rejection on non-vision models
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_image_rejected_on_non_vision_model() -> None:
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b",
|
|
api_key="sk_test",
|
|
)
|
|
with pytest.raises(ValueError, match="Image input is not supported"):
|
|
llm.chat(chat_ctx=_chat_ctx_with_image())
|
|
|
|
|
|
def test_image_rejected_on_glm52() -> None:
|
|
llm = SarvamLLM(
|
|
model="glm5.2",
|
|
api_key="sk_test",
|
|
)
|
|
with pytest.raises(ValueError, match="Image input is not supported"):
|
|
llm.chat(chat_ctx=_chat_ctx_with_image())
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_image_accepted_on_vision_model() -> None:
|
|
llm = SarvamLLM(
|
|
model="gemma4",
|
|
api_key="sk_test",
|
|
)
|
|
# Should not raise
|
|
stream = llm.chat(chat_ctx=_chat_ctx_with_image())
|
|
assert stream is not None
|
|
await stream.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_vision_validation_skips_non_dict_messages() -> None:
|
|
"""Non-message items (function calls, tool outputs) should not cause errors."""
|
|
ctx = ChatContext.empty()
|
|
ctx.add_message(role="user", content="hello")
|
|
# Add a function call output (non-message item)
|
|
from livekit.agents.llm import FunctionCallOutput
|
|
|
|
ctx.insert(FunctionCallOutput(call_id="test_call", output="result", is_error=False))
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b",
|
|
api_key="sk_test",
|
|
)
|
|
# Should not raise
|
|
stream = llm.chat(chat_ctx=ctx)
|
|
await stream.aclose()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# tool_choice validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_tool_choice_without_tools_raises() -> None:
|
|
llm = SarvamLLM(api_key="sk_test")
|
|
with pytest.raises(ValueError, match="tool_choice requires a non-empty tools array"):
|
|
llm.chat(chat_ctx=_chat_ctx(), tool_choice="required")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tool_choice_with_tools_allowed() -> None:
|
|
llm = SarvamLLM(api_key="sk_test")
|
|
from livekit.agents.llm import function_tool
|
|
|
|
@function_tool
|
|
def get_weather(city: str) -> str:
|
|
return "sunny"
|
|
|
|
stream = llm.chat(
|
|
chat_ctx=_chat_ctx("weather?"),
|
|
tools=[get_weather],
|
|
tool_choice="auto",
|
|
)
|
|
assert stream is not None
|
|
await stream.aclose()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Orphan tool history
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _chat_ctx_with_tool_call() -> ChatContext:
|
|
ctx = ChatContext.empty()
|
|
ctx.add_message(role="user", content="weather?")
|
|
ctx.insert(
|
|
[
|
|
FunctionCall(call_id="call_1", name="get_weather", arguments='{"city": "Bengaluru"}'),
|
|
FunctionCallOutput(
|
|
call_id="call_1", name="get_weather", output="sunny", is_error=False
|
|
),
|
|
]
|
|
)
|
|
return ctx
|
|
|
|
|
|
async def _tool_messages(stream: Any) -> list[dict[str, Any]]:
|
|
messages, _ = await stream._chat_ctx.to_provider_format(format="openai")
|
|
return [m for m in messages if m.get("role") == "tool" or "tool_calls" in m]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tool_history_stripped_when_turn_has_no_tools() -> None:
|
|
"""Sarvam 400s on tool messages with no `tools` array, poisoning the session."""
|
|
llm = SarvamLLM(api_key="sk_test")
|
|
stream = llm.chat(chat_ctx=_chat_ctx_with_tool_call())
|
|
assert await _tool_messages(stream) == []
|
|
await stream.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tool_history_kept_when_turn_has_tools() -> None:
|
|
"""A tool reply still sees its own result — only unmatchable history is dropped."""
|
|
from livekit.agents.llm import function_tool
|
|
|
|
@function_tool
|
|
def get_weather(city: str) -> str:
|
|
return "sunny"
|
|
|
|
llm = SarvamLLM(api_key="sk_test")
|
|
stream = llm.chat(
|
|
chat_ctx=_chat_ctx_with_tool_call(),
|
|
tools=[get_weather],
|
|
tool_choice="none",
|
|
)
|
|
assert await _tool_messages(stream)
|
|
await stream.aclose()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Unsupported OpenAI fields stripped
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsupported_fields_stripped_from_stream() -> None:
|
|
llm = SarvamLLM(api_key="sk_test")
|
|
stream = llm.chat(chat_ctx=_chat_ctx())
|
|
for field in ("stream_options", "max_completion_tokens", "service_tier"):
|
|
assert field not in _sent_kwargs(llm, stream)
|
|
await stream.aclose()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# API key validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# sarvam-105b-conversations — endpoint routing and capability gating
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_conversations_resolves_to_v1_endpoint() -> None:
|
|
llm = SarvamLLM(model="sarvam-105b-conversations", api_key="sk_test")
|
|
assert llm._base_url.rstrip("/") == SARVAM_LLM_BASE_URL_V1.rstrip("/")
|
|
import asyncio
|
|
|
|
asyncio.get_event_loop().run_until_complete(llm.aclose())
|
|
|
|
|
|
def test_v2_models_resolve_to_v2_endpoint() -> None:
|
|
"""Non-conversations models resolve to /v2."""
|
|
for model in ["sarvam-105b", "gemma4", "glm5.2"]:
|
|
llm = SarvamLLM(model=model, api_key="sk_test")
|
|
assert llm._base_url.rstrip("/") == SARVAM_LLM_BASE_URL_V2.rstrip("/")
|
|
import asyncio
|
|
|
|
asyncio.get_event_loop().run_until_complete(llm.aclose())
|
|
|
|
|
|
def test_conversations_strips_reasoning_effort() -> None:
|
|
"""reasoning_effort is not supported on conversations — should not be set in opts."""
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b-conversations",
|
|
api_key="sk_test",
|
|
reasoning_effort="high",
|
|
)
|
|
assert "reasoning_effort" not in llm._native.extra_kwargs
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_conversations_strips_reasoning_effort_from_stream() -> None:
|
|
"""reasoning_effort should be absent from built stream params for conversations."""
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b-conversations",
|
|
api_key="sk_test",
|
|
)
|
|
stream = llm.chat(chat_ctx=_chat_ctx())
|
|
assert "reasoning_effort" not in _sent_kwargs(llm, stream)
|
|
await stream.aclose()
|
|
|
|
|
|
def test_conversations_strips_wiki_grounding() -> None:
|
|
"""wiki_grounding is not supported on conversations — should not appear in extra_body."""
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b-conversations",
|
|
api_key="sk_test",
|
|
wiki_grounding=True,
|
|
)
|
|
assert "wiki_grounding" not in llm._native.extra_kwargs.get("extra_body", {})
|
|
|
|
|
|
def test_conversations_rejects_image_input() -> None:
|
|
"""Conversations model does not support vision — image raises ValueError."""
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b-conversations",
|
|
api_key="sk_test",
|
|
)
|
|
with pytest.raises(ValueError, match="Image input is not supported"):
|
|
llm.chat(chat_ctx=_chat_ctx_with_image())
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_conversations_supports_tool_calling() -> None:
|
|
"""Conversations model supports tool calling — tools and tool_choice pass through."""
|
|
llm = SarvamLLM(
|
|
model="sarvam-105b-conversations",
|
|
api_key="sk_test",
|
|
)
|
|
from livekit.agents.llm import function_tool
|
|
|
|
@function_tool
|
|
def get_weather(city: str) -> str:
|
|
return "sunny"
|
|
|
|
stream = llm.chat(
|
|
chat_ctx=_chat_ctx("weather?"),
|
|
tools=[get_weather],
|
|
tool_choice="auto",
|
|
)
|
|
assert stream is not None
|
|
await stream.aclose()
|