mirror of
https://github.com/usestrix/strix.git
synced 2026-08-16 09:26:39 +02:00
feat(llm): change OpenRouter LLM request headers (#760)
* feat(llm): attribute OpenRouter usage to Strix app Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com> * scope OpenRouter category header to OpenRouter models; add OR_APP_CATEGORIES override Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com> * clear stale OpenRouter category header when switching providers Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com> * hardcode OpenRouter attribution headers; drop env overrides and docs section Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com> * drop attribution comments Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com> --------- Co-authored-by: Ahmed Allam <ahmed39652003@gmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
Ahmed Allam
parent
993fd41f32
commit
5304baa424
@@ -65,6 +65,7 @@ def configure_sdk_model_defaults(settings: Settings) -> None:
|
|||||||
llm = settings.llm
|
llm = settings.llm
|
||||||
set_tracing_disabled(True)
|
set_tracing_disabled(True)
|
||||||
_configure_litellm_compatibility()
|
_configure_litellm_compatibility()
|
||||||
|
_configure_openrouter_attribution(llm.model)
|
||||||
if llm.api_key:
|
if llm.api_key:
|
||||||
set_default_openai_key(llm.api_key, use_for_tracing=False)
|
set_default_openai_key(llm.api_key, use_for_tracing=False)
|
||||||
_configure_litellm_default("api_key", llm.api_key)
|
_configure_litellm_default("api_key", llm.api_key)
|
||||||
@@ -111,6 +112,29 @@ def _configure_litellm_compatibility() -> None:
|
|||||||
_register_litellm_cost_callback()
|
_register_litellm_cost_callback()
|
||||||
|
|
||||||
|
|
||||||
|
_OPENROUTER_ATTRIBUTION_HEADERS = {
|
||||||
|
"HTTP-Referer": "https://strix.ai",
|
||||||
|
"X-Title": "Strix",
|
||||||
|
"X-OpenRouter-Categories": "cli-agent",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _configure_openrouter_attribution(model_name: str | None) -> None:
|
||||||
|
import litellm
|
||||||
|
|
||||||
|
current: object = litellm.headers
|
||||||
|
existing: dict[str, str] = current if isinstance(current, dict) else {}
|
||||||
|
if not model_name or "openrouter/" not in model_name.strip().lower():
|
||||||
|
if any(key in existing for key in _OPENROUTER_ATTRIBUTION_HEADERS):
|
||||||
|
remaining = {
|
||||||
|
k: v for k, v in existing.items() if k not in _OPENROUTER_ATTRIBUTION_HEADERS
|
||||||
|
}
|
||||||
|
litellm.headers = remaining or None # type: ignore[assignment]
|
||||||
|
return
|
||||||
|
|
||||||
|
litellm.headers = {**existing, **_OPENROUTER_ATTRIBUTION_HEADERS} # type: ignore[assignment]
|
||||||
|
|
||||||
|
|
||||||
def _register_litellm_cost_callback() -> None:
|
def _register_litellm_cost_callback() -> None:
|
||||||
import litellm
|
import litellm
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user