Repository navigation
Closed
Labels
Description
Overview
Extend the NLP engine (packages/nlp-engine/) with a new chat module that provides GGUF chat model loading and streaming inference alongside the existing embedding service. This is the core LLM capability that the local agent loop consumes.
The PoC (nodespace-nlp-experiment, April 2026) validated dual-model loading, streaming inference, and tool-call parsing on Metal. This issue formalizes those findings into production code.
Acceptance Criteria
-
ChatInferenceEnginetrait fully implemented - Streaming works: tokens arrive via callback as they are generated
- Tool-call parsing:
[TOOL_CALLS]search_nodes[ARGS]{"query":"test"}correctly parsed intoToolCallRaw - Tool-call parsing handles multiple tool calls in one response
- Tool-call parsing handles malformed JSON gracefully (returns error, does not panic)
- Tool-call parsing handles partial
[TOOL_CALLS]sentinel split across token boundaries - Token counting matches model tokenizer output
- Dual-model loading: embedding + chat models coexist on Metal without crash
- GPU scheduling: concurrent requests are serialized (no Metal errors)
- Unit tests for tool-call parser with corpus of example outputs
- Integration test: load 3B model, send prompt, receive streamed response
- Code passes
bun run quality:fix
Technical Specifications
New Module
packages/nlp-engine/src/chat.rs(orchat/module)
Chat Inference Implementation
- Load GGUF chat models via llama-cpp-2
LlamaModel/LlamaContext - Streaming:
sampler.sample()->token_to_piece()->ctx.decode()loop yields tokens individually - Chat templates:
model.chat_template(None)+model.apply_chat_template()(embedded in GGUF metadata) - Temperature: 0.1 default (validated in PoC for reliable tool-call output)
- Context window: 8192 tokens
- Implements
ChatInferenceEnginetrait from Shared types, traits, and interface contracts for agent subsystems #998
Tool-Call Parser
Mistral raw GGUF models use a non-standard format:
[TOOL_CALLS]function_name[ARGS]{"param": "value"}
Multiple calls:
[TOOL_CALLS]search_nodes[ARGS]{"query": "embeddings"}[TOOL_CALLS]search_nodes[ARGS]{"query": "vector search"}
This is NOT the JSON array format from Mistral's API. Parser must handle streaming tokens accumulating into these patterns.
GPU Scheduling
- Simple mutex or semaphore — one inference at a time
- Embedding takes priority if both are queued (embeddings are fast, chat is interruptible)
- Both models share Metal via same
LlamaBackend(PoC validated) - 3B: combined GPU 2.3 GB (19%), embedding latency unaffected (0.7ms)
- 8B: combined GPU 5.2 GB (43%), embedding latency ~118ms (acceptable with 30s debounce)
API Notes (from PoC)
token_to_strdeprecated in v0.1.141+ (usetoken_to_piece)LlamaBackendinitializes once per process- Dual
LlamaModelinstances coexist on same backend sharing Metal
Reference Files
packages/nlp-engine/src/embedding.rs— existing embedding service (same crate)packages/nlp-engine/src/config.rs— configuration patternspackages/nlp-engine/Cargo.toml— existing llama-cpp-2 dependency
Exports
LlamaChatEnginestruct implementingChatInferenceEngine
Dependencies
Related Issues
- ACP Integration and Local LLM Capability #997 (parent epic)
Activity
malibio commentedon Apr 3, 2026
Implementation Guide
Start Here
Read
packages/nlp-engine/src/embedding.rs(lines 200-550) — this is the existing inference pattern you'll mirror for chat. Key structures:LlamaState(line 208) — holdsLlamaModel+ persistentLlamaContextget_or_create_context()(line 240) — lazy context creation with batch size managementgenerate_embedding_llama()(line 518) — the core inference loop usingAddBos::Always,LlamaBatch, context decodeRead
packages/nlp-engine/src/lib.rs— public API surface. Your chat module will add parallel exports here.Read
packages/nlp-engine/src/config.rs—EmbeddingConfigandresolve_model_path(). You'll create aChatConfigfollowing the same pattern.Read
packages/nlp-engine/Cargo.toml— llama-cpp-2 is>=0.1.132with Metal auto-enabled on macOS.Key Differences: Chat vs Embedding Inference
LlamaSamplerwith temperature 0.1AddBos::Alwaysmodel.apply_chat_template()Streaming Loop Pattern (from PoC)
The core generation loop follows this pattern:
Tool-Call Parser
Mistral raw GGUF outputs
[TOOL_CALLS]fn_name[ARGS]{json}, NOT the JSON array format from Mistral's API. Your parser must:[TOOL_CALLS]sentinel (may be split across token boundaries)[TOOL_CALLS]and[ARGS][ARGS]and the next[TOOL_CALLS]or end of outputGPU Coexistence
The PoC validated that embedding model (nomic-embed-text, ~270MB) and chat model coexist on the same
LlamaBackendsharing Metal. Both use the global singleton atembedding.rsline 38 (static LLAMA_BACKEND). Your chat model registers with the sameLLAMA_STATESvec for cleanup.Testing
malibio commentedon Apr 3, 2026
Baseline: Frontend tests fail with pre-existing TSConfckParseError (svelte-kit/tsconfig.json not found in worktree). Rust nlp-engine tests will be the relevant baseline for this PR.
feat: NLP chat inference engine with streaming and tool-call parsing (c…
feat: NLP chat inference engine with streaming and tool-call parsing (c…
feat: NLP chat inference engine with streaming and tool-call parsing (c…
Pin noderef.css print colors to theme-invariant tokens (closes #2593) (…