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
2 changes: 2 additions & 0 deletions livekit-agents/livekit/agents/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def __init__(
self._event_ch = aio.Chan[ChatChunk]()
self._event_aiter, monitor_aiter = aio.itertools.tee(self._event_ch, 2)
self._current_attempt_has_error = False
self._attempt_number = 1
self._metrics_task = asyncio.create_task(
self._metrics_monitor_task(monitor_aiter), name="LLM._metrics_task"
)
Expand All @@ -193,6 +194,7 @@ async def _main_task(self) -> None:
self._llm_request_span.set_attribute(trace_types.ATTR_GEN_AI_REQUEST_MODEL, self._llm.model)

for i in range(self._conn_options.max_retry + 1):
self._attempt_number = i + 1
try:
with tracer.start_as_current_span("llm_request_run") as attempt_span:
attempt_span.set_attribute(trace_types.ATTR_RETRY_COUNT, i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import json
import os
from dataclasses import dataclass
from typing import Any, cast
from typing import Any, Callable, cast

from google.auth._default_async import default_async
from google.genai import Client, types
Expand Down Expand Up @@ -77,7 +77,7 @@ class _LLMOptions:
thinking_config: NotGivenOr[types.ThinkingConfigOrDict]
retrieval_config: NotGivenOr[types.RetrievalConfigOrDict]
automatic_function_calling_config: NotGivenOr[types.AutomaticFunctionCallingConfigOrDict]
http_options: NotGivenOr[types.HttpOptions]
http_options: NotGivenOr[types.HttpOptions | Callable[[int], types.HttpOptions]]
seed: NotGivenOr[int]
safety_settings: NotGivenOr[list[types.SafetySettingOrDict]]

Expand Down Expand Up @@ -113,7 +113,9 @@ def __init__(
automatic_function_calling_config: NotGivenOr[
types.AutomaticFunctionCallingConfigOrDict
] = NOT_GIVEN,
http_options: NotGivenOr[types.HttpOptions] = NOT_GIVEN,
http_options: NotGivenOr[
types.HttpOptions | Callable[[int], types.HttpOptions]
] = NOT_GIVEN,
seed: NotGivenOr[int] = NOT_GIVEN,
safety_settings: NotGivenOr[list[types.SafetySettingOrDict]] = NOT_GIVEN,
) -> None:
Expand Down Expand Up @@ -143,7 +145,7 @@ def __init__(
thinking_config (ThinkingConfigOrDict, optional): The thinking configuration for response generation. Defaults to None.
retrieval_config (RetrievalConfigOrDict, optional): The retrieval configuration for response generation. Defaults to None.
automatic_function_calling_config (AutomaticFunctionCallingConfigOrDict, optional): The automatic function calling configuration for response generation. Defaults to None.
http_options (HttpOptions, optional): The HTTP options to use for the session.
http_options (HttpOptions | Callable[[int], HttpOptions], optional): The HTTP options to use for requests. Can be either a static HttpOptions object, or a callable that takes the attempt number (1-indexed, where 1 is the first attempt) and returns HttpOptions, allowing different options per retry attempt.
seed (int, optional): Random seed for reproducible generation. Defaults to None.
safety_settings (list[SafetySettingOrDict], optional): Safety settings for content filtering. Defaults to None.
""" # noqa: E501
Expand Down Expand Up @@ -426,9 +428,13 @@ async def _run(self) -> None:
tools_config = create_tools_config(tool_context, _only_single_type=True)
if tools_config:
self._extra_kwargs["tools"] = tools_config
http_options = self._llm._opts.http_options or types.HttpOptions(
timeout=int(self._conn_options.timeout * 1000)
)
opts_http_options = self._llm._opts.http_options
if is_given(opts_http_options) and callable(opts_http_options):
http_options = opts_http_options(self._attempt_number)
elif is_given(opts_http_options):
http_options = opts_http_options
else:
http_options = types.HttpOptions(timeout=int(self._conn_options.timeout * 1000))
if not http_options.headers:
http_options.headers = {}
http_options.headers["x-goog-api-client"] = f"livekit-agents/{__version__}"
Expand Down
Loading