Skip to content

Commit 1ab5640

Browse files
authored
Merge pull request #869 from MODSetter/dev
feat: Enhance LLM configuration and routing with model profile attach…
2 parents 5571e8a + eec4db4 commit 1ab5640

File tree

2 files changed

+310
-7
lines changed

2 files changed

+310
-7
lines changed

surfsense_backend/app/agents/new_chat/llm_config.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import yaml
1717
from langchain_litellm import ChatLiteLLM
18+
from litellm import get_model_info
1819
from sqlalchemy import select
1920
from sqlalchemy.ext.asyncio import AsyncSession
2021

@@ -62,6 +63,22 @@
6263
}
6364

6465

66+
def _attach_model_profile(llm: ChatLiteLLM, model_string: str) -> None:
67+
"""Attach a ``profile`` dict to ChatLiteLLM with model context metadata."""
68+
try:
69+
info = get_model_info(model_string)
70+
max_input_tokens = info.get("max_input_tokens")
71+
if isinstance(max_input_tokens, int) and max_input_tokens > 0:
72+
llm.profile = {
73+
"max_input_tokens": max_input_tokens,
74+
"max_input_tokens_upper": max_input_tokens,
75+
"token_count_model": model_string,
76+
"token_count_models": [model_string],
77+
}
78+
except Exception:
79+
return
80+
81+
6582
@dataclass
6683
class AgentConfig:
6784
"""
@@ -366,7 +383,9 @@ def create_chat_litellm_from_config(llm_config: dict) -> ChatLiteLLM | None:
366383
if llm_config.get("litellm_params"):
367384
litellm_kwargs.update(llm_config["litellm_params"])
368385

369-
return ChatLiteLLM(**litellm_kwargs)
386+
llm = ChatLiteLLM(**litellm_kwargs)
387+
_attach_model_profile(llm, model_string)
388+
return llm
370389

371390

372391
def create_chat_litellm_from_agent_config(
@@ -419,4 +438,6 @@ def create_chat_litellm_from_agent_config(
419438
if agent_config.litellm_params:
420439
litellm_kwargs.update(agent_config.litellm_params)
421440

422-
return ChatLiteLLM(**litellm_kwargs)
441+
llm = ChatLiteLLM(**litellm_kwargs)
442+
_attach_model_profile(llm, model_string)
443+
return llm

0 commit comments

Comments
 (0)