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: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
python -m pip install --upgrade setuptools wheel
pip install poetry
poetry config virtualenvs.create true
poetry install --with dev
poetry install --with dev --all-extras
- name: Install Pandoc # apt version seems too old
uses: r-lib/actions/setup-pandoc@v2
- name: Linting Checks
Expand Down
10 changes: 10 additions & 0 deletions in2lambda/llm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""OpenRouter-backed LLM helpers used by ``in2lambda wizard``.

Everything here is part of the optional ``llm`` extra
(``pip install in2lambda[llm]``); importing this package without the extra is
fine, but calling into it raises a clear error.
"""

from in2lambda.llm.client import DEFAULT_MODEL, get_client, resolve_model

__all__ = ["DEFAULT_MODEL", "get_client", "resolve_model"]
76 changes: 76 additions & 0 deletions in2lambda/llm/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Build an OpenAI-compatible client pointed at OpenRouter.

Configuration is read from the environment (a local ``.env`` file is loaded if
one is present):

* ``OPENROUTER_API_KEY`` - required; create one at https://openrouter.ai/keys.
* ``IN2LAMBDA_MODEL`` - optional; the model slug to use when none is passed
on the command line. Defaults to :data:`DEFAULT_MODEL`.
"""

import os
from typing import Optional

try:
from dotenv import load_dotenv
except ImportError: # pragma: no cover - provided by the `llm` extra
load_dotenv = None

try:
import openai
except ImportError: # pragma: no cover - provided by the `llm` extra
openai = None

OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"

# A cheap, widely-available default. Override per run with --model or the
# IN2LAMBDA_MODEL environment variable; any OpenRouter model slug works.
DEFAULT_MODEL = "openai/gpt-4o-mini"

_INSTALL_HINT = (
"in2lambda's LLM features need the 'llm' extra: pip install 'in2lambda[llm]'"
)


def resolve_model(cli_value: Optional[str] = None) -> str:
"""Return the model slug to use.

Precedence: an explicit CLI value, then ``$IN2LAMBDA_MODEL``, then
:data:`DEFAULT_MODEL`.

Args:
cli_value: The value passed via ``--model``, if any.

Returns:
An OpenRouter model slug.

Examples:
>>> from in2lambda.llm.client import resolve_model
>>> resolve_model("anthropic/claude-3.5-haiku")
'anthropic/claude-3.5-haiku'
"""
return cli_value or os.getenv("IN2LAMBDA_MODEL") or DEFAULT_MODEL


def get_client() -> "openai.OpenAI":
"""Return an OpenAI client configured to talk to OpenRouter.

Raises:
RuntimeError: if the ``llm`` extra is not installed, or
``OPENROUTER_API_KEY`` is not set.
"""
if openai is None:
raise RuntimeError(_INSTALL_HINT)

if load_dotenv is not None:
load_dotenv()

api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
raise RuntimeError(
"OPENROUTER_API_KEY is not set. Create a key at "
"https://openrouter.ai/keys and put it in your environment or a "
".env file."
)

return openai.OpenAI(base_url=OPENROUTER_BASE_URL, api_key=api_key)
Loading
Loading