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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# subactor-subllm (the vision transport) requires Python 3.11+.
python-version: ["3.11", "3.12"]

steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# NLP2CMD

[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![PyPI Version](https://img.shields.io/pypi/v/nlp2cmd.svg)](https://pypi.org/project/nlp2cmd/)
[![1543+ Tests](https://img.shields.io/badge/tests-1543%2B-brightgreen.svg)](https://github.com/wronai/nlp2cmd)
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3451,7 +3451,7 @@ nlp2cmd/

## Requirements

- Python >= >=3.10
- Python >= 3.11
- pyyaml >=6.0- pydantic >=2.0- rich >=13.0- click >=8.0- httpx >=0.25.0- jinja2 >=3.0- jsonschema >=4.0- python-dotenv >=1.0- watchdog >=3.0- numpy >=1.24.0- psutil >=5.9.0- rapidfuzz >=3.0

## Contributing
Expand Down Expand Up @@ -3488,4 +3488,4 @@ pytest
| `CONTRIBUTING.md` | Contribution guidelines | [View](./CONTRIBUTING.md) |
| `examples` | Usage examples and code samples | [View](./examples) |

<!-- code2docs:end -->
<!-- code2docs:end -->
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies = [
"psutil>=5.9.0",
"rapidfuzz>=3.0",
"nlp2cmd-intent>=0.1.2",
"subactor-subllm @ git+https://github.com/subactor/subllm.git@505d89f505e90c7b40d98cafdc9104cc61072460",
"subactor-subllm @ git+https://github.com/subactor/subllm.git@40726bb9399eb5cf9dfc038c1c948b7d2fbdca3b",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -253,7 +253,7 @@ ignore = [
"tests/*" = ["S101"]

[tool.mypy]
python_version = "3.10"
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
Expand Down
42 changes: 23 additions & 19 deletions src/nlp2cmd/llm/openrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from typing import Any, Optional

try:
from subllm import available_routes, complete as subllm_complete
from subllm import complete as subllm_complete
except ImportError:
available_routes = None
subllm_complete = None

_DEBUG = os.environ.get("NLP2CMD_DEBUG", "").lower() in ("1", "true", "yes")
Expand Down Expand Up @@ -95,15 +94,8 @@ def __init__(

@property
def is_configured(self) -> bool:
"""Check whether the central NLP2CMD route has a usable credential."""
if available_routes is None:
return False
credentials = {"OPENROUTER_API_KEY": self.api_key} if self.api_key else None
return bool(
available_routes(
"autogrammar-nlp2cmd", "generate", credentials=credentials
)
)
"""Check whether the client can submit a request through SubLLM."""
return subllm_complete is not None and bool(self.api_key)

def _headers(self) -> dict[str, str]:
return {
Expand Down Expand Up @@ -190,6 +182,8 @@ async def vision(
Returns:
LLMResponse
"""
if subllm_complete is None:
return LLMResponse(success=False, error="subactor-subllm is not available")
if not self.api_key:
return LLMResponse(success=False, error="OPENROUTER_API_KEY not set")

Expand All @@ -207,14 +201,24 @@ async def vision(
],
})

body: dict[str, Any] = {
"model": model or self.MODELS["vision"],
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
}

return await self._request(body)
credentials = {"openrouter": self.api_key}
try:
response = await asyncio.to_thread(
subllm_complete,
"autogrammar-nlp2cmd",
"vision",
messages,
timeout_seconds=self.timeout,
credentials=credentials,
)
return LLMResponse(
content=response.content,
model=response.model,
usage=dict(response.usage),
finish_reason=response.finish_reason,
)
except Exception as exc:
return LLMResponse(success=False, error=f"SubLLM vision request failed: {exc}")

async def plan_actions(
self,
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_subllm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,26 @@ def fake_complete(application, function, messages, **kwargs):
assert response.model == "glm-5.3"
assert captured["application"] == "autogrammar-nlp2cmd"
assert captured["function"] == "generate"


def test_vision_uses_central_subllm(monkeypatch):
captured = {}

def fake_complete(application, function, messages, **kwargs):
captured.update(application=application, function=function, messages=messages, kwargs=kwargs)
return type(
"Response",
(),
{"content": "cat", "model": "z-ai/glm-4.5v", "usage": {}, "finish_reason": "stop"},
)()

monkeypatch.setattr(openrouter, "subllm_complete", fake_complete)
response = asyncio.run(
openrouter.OpenRouterClient(api_key="test-key").vision("aaa", "what is this?")
)

assert response.content == "cat"
assert captured["application"] == "autogrammar-nlp2cmd"
assert captured["function"] == "vision"
assert captured["messages"][0]["content"][0]["type"] == "image_url"
assert captured["kwargs"]["credentials"] == {"openrouter": "test-key"}
Loading