Skip to content

Commit a6c5059

Browse files
committed
Allow code generation with local models that require no API key
In local mode the OpenAI-compatible endpoint (Ollama, LM Studio, ...) ignores auth, but two things blocked submitting a query without a key: the Generate Code button was gated on a non-empty api_token, so it never appeared, and the client refuses to construct an OpenAI client with an empty key. Show the button whenever a key is not required (local mode) or one is present (cloud), and fall back to a placeholder key in local mode that a real key typed into the field overrides. Cloud providers (OpenAI, Anthropic, Gemini, NIM) still require a real key, so an empty key there still surfaces the "Set API Key" prompt as before.
1 parent 95e4d0d commit a6c5059

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/vtk_prompt/state/config_state.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@
1111

1212

1313
def get_api_key(app: Any) -> str | None:
14-
"""Get API key from state (requires manual input in UI)."""
14+
"""Get API key from state.
15+
16+
Cloud providers need a real key. Local models hit an OpenAI-compatible
17+
endpoint (Ollama, LM Studio, ...) that ignores auth, but the OpenAI client
18+
still refuses to construct with an empty key, so in local mode we fall back
19+
to a placeholder. A real key typed into the field overrides it.
20+
"""
1521
api_token = getattr(app.state, "api_token", "")
16-
return api_token.strip() if api_token and api_token.strip() else None
22+
if api_token and api_token.strip():
23+
return api_token.strip()
24+
if not app.state.use_cloud_models:
25+
return "sk-no-key-required"
26+
return None
1727

1828

1929
def get_base_url(app: Any) -> str | None:

src/vtk_prompt/ui/layout/content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def build_content(layout: Any, app: Any) -> None:
116116
loading=("trame__busy", False),
117117
click=app.ctrl.generate_code,
118118
classes="my-2",
119-
v_show="api_token.trim()",
119+
v_show="!use_cloud_models || api_token.trim()",
120120
)
121121
vuetify.VBtn(
122122
"Set API Key",

0 commit comments

Comments
 (0)