|
| 1 | +"""Nous Research (Nous Portal) provider for fast-agent. |
| 2 | +
|
| 3 | +Mirrors the Hermes Nous Portal provider in /hermes-agent/plugins/model-providers/nous/. |
| 4 | +Key differences from raw OpenAI: |
| 5 | + - Nous Portal product-attribution tags injected into ``extra_body`` on every call |
| 6 | + - Base URL defaults to ``https://inference.nousresearch.com/v1`` |
| 7 | + - Auth via NOUS_API_KEY env var or ``nous.api_key`` in fast-agent config |
| 8 | +
|
| 9 | +Usage:: |
| 10 | +
|
| 11 | + fast-agent --model nous.hermes-3-405b |
| 12 | + # or rely on default_model from config: |
| 13 | + # default_model: nous.hermes-3-405b |
| 14 | +""" |
| 15 | + |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +from openai.types.chat import ChatCompletionMessageParam |
| 19 | + |
| 20 | +from fast_agent.llm.provider.openai.llm_openai_compatible import OpenAICompatibleLLM |
| 21 | +from fast_agent.llm.provider_types import Provider |
| 22 | +from fast_agent.types import RequestParams |
| 23 | + |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | +# Constants |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +NOUS_BASE_URL = "https://inference.nousresearch.com/v1" |
| 29 | +NOUS_DEFAULT_MODEL = "hermes-3-405b" |
| 30 | + |
| 31 | + |
| 32 | +# --------------------------------------------------------------------------- |
| 33 | +# NousLLM |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | + |
| 36 | + |
| 37 | +class NousLLM(OpenAICompatibleLLM): |
| 38 | + """Nous Portal provider — OpenAI-compatible calls with Portal attribution tags. |
| 39 | +
|
| 40 | + The base ``OpenAICompatibleLLM`` handles the standard OpenAI protocol. |
| 41 | + Nous adds two things: |
| 42 | + 1. ``extra_body["tags"]`` — product=fast-agent + client version tags for portal attribution |
| 43 | + 2. Nous-specific base URL (``https://inference.nousresearch.com/v1``) |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, **kwargs) -> None: |
| 47 | + kwargs.pop("provider", None) |
| 48 | + super().__init__(provider=Provider.NOUS, **kwargs) |
| 49 | + |
| 50 | + # ------------------------------------------------------------------ |
| 51 | + # Base URL |
| 52 | + # ------------------------------------------------------------------ |
| 53 | + |
| 54 | + def _provider_base_url(self) -> str | None: |
| 55 | + """Return Nous Portal base URL, honouring an explicit config override.""" |
| 56 | + if self.context.config and self.context.config.nous: |
| 57 | + override = self.context.config.nous.base_url |
| 58 | + if override: |
| 59 | + return override |
| 60 | + return NOUS_BASE_URL |
| 61 | + |
| 62 | + # ------------------------------------------------------------------ |
| 63 | + # Default model (when no model is specified) |
| 64 | + # ------------------------------------------------------------------ |
| 65 | + |
| 66 | + def _initialize_default_params(self, kwargs: dict) -> RequestParams: |
| 67 | + """Initialize Nous default parameters when no model is explicitly set.""" |
| 68 | + return self._initialize_default_params_with_model_fallback( |
| 69 | + kwargs, NOUS_DEFAULT_MODEL |
| 70 | + ) |
| 71 | + |
| 72 | + # ------------------------------------------------------------------ |
| 73 | + # Portal attribution tags injected on every request |
| 74 | + # ------------------------------------------------------------------ |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def _nous_portal_tags() -> list[str]: |
| 78 | + """Return fast-agent product-attribution tags for Nous Portal requests. |
| 79 | +
|
| 80 | + Shape: ``["product=fast-agent", "client=fast-agent-client-v{__version__}"]``. |
| 81 | +
|
| 82 | + The version tag allows Nous to bucketed usage by agent toolkit release. |
| 83 | + """ |
| 84 | + try: |
| 85 | + from fast_agent import __version__ as _ver |
| 86 | + |
| 87 | + client_tag = f"client=fast-agent-client-v{_ver}" |
| 88 | + except Exception: |
| 89 | + client_tag = "client=fast-agent-client-vunknown" |
| 90 | + return ["product=fast-agent", client_tag] |
| 91 | + |
| 92 | + def _prepare_api_request( |
| 93 | + self, |
| 94 | + messages: list[ChatCompletionMessageParam], |
| 95 | + tools: list[Any] | None, |
| 96 | + request_params: RequestParams, |
| 97 | + ) -> dict[str, Any]: |
| 98 | + """Build keyword arguments for the OpenAI-compatible chat completion. |
| 99 | +
|
| 100 | + Calls ``super()._prepare_api_request()`` for the standard path, then |
| 101 | + adds ``tags`` to ``extra_body`` for Nous Portal product attribution. |
| 102 | + """ |
| 103 | + arguments: dict[str, Any] = super()._prepare_api_request( |
| 104 | + messages, tools, request_params |
| 105 | + ) |
| 106 | + |
| 107 | + # Nous Portal attribution tags — idempotent merge |
| 108 | + tags = self._nous_portal_tags() |
| 109 | + existing = arguments.get("extra_body") |
| 110 | + if isinstance(existing, dict): |
| 111 | + existing.setdefault("tags", []).extend(tags) |
| 112 | + else: |
| 113 | + arguments["extra_body"] = {"tags": tags} |
| 114 | + |
| 115 | + return arguments |
0 commit comments