Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

refactor(backend): use shared get_openai_client in tally extraction (#13027)

Zamil Majdydf9d4c933f32
diff --git a/autogpt_platform/backend/backend/data/tally.py b/autogpt_platform/backend/backend/data/tally.py
index e28418f01d..2fdf090d42 100644
--- a/autogpt_platform/backend/backend/data/tally.py
+++ b/autogpt_platform/backend/backend/data/tally.py
@@ -6,15 +6,13 @@ import logging
from datetime import datetime, timezone
from typing import Optional
-from openai import AsyncOpenAI
-
from backend.data.redis_client import get_redis_async
from backend.data.understanding import (
BusinessUnderstandingInput,
get_business_understanding,
upsert_business_understanding,
)
-from backend.util.clients import OPENROUTER_BASE_URL
+from backend.util.clients import get_openai_client
from backend.util.request import Requests
from backend.util.settings import Settings
@@ -354,9 +352,11 @@ async def extract_business_understanding(
Raises on timeout or unparseable response so the caller can handle it.
"""
- settings = Settings()
- api_key = settings.secrets.open_router_api_key
- client = AsyncOpenAI(api_key=api_key, base_url=OPENROUTER_BASE_URL)
+ client = get_openai_client(prefer_openrouter=True)
+ if client is None:
+ raise RuntimeError(
+ "open_router_api_key not set, cannot extract business understanding"
+ )
try:
response = await asyncio.wait_for(
diff --git a/autogpt_platform/backend/backend/data/tally_test.py b/autogpt_platform/backend/backend/data/tally_test.py
index 7444af8237..9bdeb16b0c 100644
--- a/autogpt_platform/backend/backend/data/tally_test.py
+++ b/autogpt_platform/backend/backend/data/tally_test.py
@@ -425,7 +425,7 @@ async def test_extract_business_understanding_themed_prompts():
mock_client = AsyncMock()
mock_client.chat.completions.create.return_value = mock_response
- with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
+ with patch("backend.data.tally.get_openai_client", return_value=mock_client):
result = await extract_business_understanding("Q: Name?\nA: Alice")
assert result.user_name == "Alice"
@@ -455,7 +455,7 @@ async def test_extract_themed_prompts_filters_long_and_unknown_keys():
mock_client = AsyncMock()
mock_client.chat.completions.create.return_value = mock_response
- with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
+ with patch("backend.data.tally.get_openai_client", return_value=mock_client):
result = await extract_business_understanding("Q: Name?\nA: Alice")
assert result.suggested_prompts is not None
@@ -480,7 +480,7 @@ async def test_extract_business_understanding_filters_nulls():
mock_client = AsyncMock()
mock_client.chat.completions.create.return_value = mock_response
- with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
+ with patch("backend.data.tally.get_openai_client", return_value=mock_client):
result = await extract_business_understanding("Q: Name?\nA: Alice")
assert result.user_name == "Alice"
@@ -500,7 +500,7 @@ async def test_extract_business_understanding_invalid_json():
mock_client.chat.completions.create.return_value = mock_response
with (
- patch("backend.data.tally.AsyncOpenAI", return_value=mock_client),
+ patch("backend.data.tally.get_openai_client", return_value=mock_client),
pytest.raises(json.JSONDecodeError),
):
await extract_business_understanding("Q: Name?\nA: Alice")
@@ -513,13 +513,26 @@ async def test_extract_business_understanding_timeout():
mock_client.chat.completions.create.side_effect = asyncio.TimeoutError()
with (
- patch("backend.data.tally.AsyncOpenAI", return_value=mock_client),
+ patch("backend.data.tally.get_openai_client", return_value=mock_client),
patch("backend.data.tally._LLM_TIMEOUT", 0.001),
pytest.raises(asyncio.TimeoutError),
):
await extract_business_understanding("Q: Name?\nA: Alice")
+async def test_extract_business_understanding_missing_openrouter_key():
+ """When OpenRouter is not configured, raise a clear RuntimeError."""
+ with (
+ patch(
+ "backend.data.tally.get_openai_client", return_value=None
+ ) as mock_get_client,
+ pytest.raises(RuntimeError, match="open_router_api_key not set"),
+ ):
+ await extract_business_understanding("Q: Name?\nA: Alice")
+ mock_get_client.assert_called_once_with(prefer_openrouter=True)
+
+
# ── _refresh_cache ───────────────────────────────────────────────────────────