Skip to content

Fix terraphim/terraphim-clients#77: include JSON instructions in RLM prompt#5

Merged
AlexMikhalev merged 4 commits into
mainfrom
task/77-grep-model-answer-json
Jul 14, 2026
Merged

Fix terraphim/terraphim-clients#77: include JSON instructions in RLM prompt#5
AlexMikhalev merged 4 commits into
mainfrom
task/77-grep-model-answer-json

Conversation

@AlexMikhalev

Copy link
Copy Markdown
Contributor

Summary

Fixes terraphim/terraphim-clients#77 by including AnswerSignature::instructions() in the RLM prompt and adding a tolerant JSON extractor that handles markdown-wrapped or prose-embedded LLM output.

Root cause

AnswerSignature expects a JSON object but the prompt never told the model to return JSON. Models that emit plain text (e.g. deepseek/deepseek-v4-flash, meta-llama/llama-3.2-*) had their answers silently dropped.

Changes

  • crates/terraphim_grep/src/lib.rs: append AnswerSignature::instructions() to the user message when include_answer is true.
  • crates/terraphim_grep/src/signatures.rs: add extract_json() to strip markdown fences and locate top-level JSON objects/arrays inside prose.
  • crates/terraphim_grep/src/openrouter_client.rs: new long-timeout (120s) OpenRouter client so slow providers do not hit the shared 10s API timeout.
  • crates/terraphim_grep/src/main.rs: prefer the long-timeout OpenRouter client when OPENROUTER_API_KEY is set.

Verification

Model Before After
deepseek/deepseek-v4-flash answer: null answer: PRESENT, confidence 1.0
amazon/nova-micro-v1 answer: PRESENT answer: PRESENT, confidence 0.95
meta-llama/llama-3.2-3b-instruct answer: null answer: null (model still ignores JSON instructions; acceptable)

All terraphim_grep tests pass (cargo test -p terraphim_grep --features "code-search openrouter").

Trade-off note

Benchmark analysis on terraphim-ai branch task/3098-doc-grep-openrouter-rlm shows gpt-oss-20b is too large and slow for this use case; recommended models remain amazon/nova-micro-v1, mistralai/mistral-nemo, and deepseek/deepseek-v4-flash.

Refs terraphim/terraphim-clients#77

- P2-1: fall back to role_from_env when direct OpenRouter client build fails;
  document precedence between env-var client and role configuration.
- P2-2: add maintainability and safety notes to openrouter_client.rs explaining
  intentional duplication and summarize() hard-error contract.
- P2-3: warn when the free-tier default model is used, noting data-handling
  considerations for repository content excerpts.

Refs terraphim/terraphim-clients#77
- P3-1: map transient OpenRouter errors (network, rate limit, API, decode)
  to ServiceError::Common(CommonError::Network) instead of Config, so
  upstream handling can distinguish transient conditions from user
  misconfiguration.
- P3-2: make reqwest an optional dependency tied to the llm feature, avoiding
  unconditional build cost when llm is disabled.
- P3-3: add tests for prompt assembly (AnswerSignature instructions present
  in answer path, absent in list path), OpenRouterClient construction
  (input validation, base URL resolution, trait object), and extract_json
  edge cases (generic fence, array payload, unterminated fence, mixed
  language fences).

Refs terraphim/terraphim-clients#77
@AlexMikhalev
AlexMikhalev merged commit ea67448 into main Jul 14, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant