Skip to content

feat: NLP chat inference with streaming and tool-call parsing - #1025

Merged
malibio merged 1 commit into
mainfrom
feature/issue-999-nlp-chat-engine
Apr 3, 2026
Merged

malibio merged 1 commit into
mainfrom
feature/issue-999-nlp-chat-engine

Conversation

@malibio

@malibio malibio commented Apr 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Implements ChatEngine for GGUF model chat inference via llama.cpp with streaming token generation
  • Adds StreamingToolCallParser for detecting Mistral-format [TOOL_CALLS] sentinels in streamed output
  • Includes GPU scheduling via tokio::sync::Mutex to prevent Metal command-buffer collisions
  • Fixes compilation errors: updated apply_chat_template call to use LlamaChatMessage + LlamaChatTemplate API (llama-cpp-2 v0.1.132), resolved borrow checker conflicts by splitting struct field borrows, and corrected a partial-sentinel test assertion

Test plan

  • cargo check -p nodespace-nlp-engine — zero errors, zero warnings
  • cargo test -p nodespace-nlp-engine — 41 unit tests + 11 integration tests all pass

Closes #999

@malibio
malibio merged commit 9a9d977 into main Apr 3, 2026
@malibio
malibio deleted the feature/issue-999-nlp-chat-engine branch April 3, 2026 22:55
@malibio

malibio commented Apr 3, 2026

Copy link
Copy Markdown
Collaborator Author

Post-Merge Code Review: PR #1025 (NLP chat inference engine with streaming and tool-call parsing)

Verdict: Has issues worth noting, but no blockers.

Findings

[Improvement] unsafe impl Send/Sync for ChatLlamaState -- same pattern as embedding service

File: packages/nlp-engine/src/chat/mod.rs (lines ~128-130)

The unsafe impl Send + Sync on ChatLlamaState follows the same pattern as the existing embedding service. The safety invariants are documented: the context is stored alongside the model, drop order is guaranteed, and access is serialized through Mutex. This is as safe as the existing pattern allows. If the embedding service pattern is ever revised, this should be updated in parallel.

[Improvement] uuid_v4_simple produces non-unique IDs under concurrent load

File: packages/nlp-engine/src/chat/mod.rs (lines ~632-639)

The function uses SystemTime::now().as_nanos() formatted as hex. Under the inference lock only one generation runs at a time, so within a single generation the IDs will be unique (nanosecond resolution). But if two separate ChatEngine instances generate simultaneously, IDs could collide. Since NodeSpace uses a single engine instance, this is fine in practice.

[Improvement] has_partial_sentinel_suffix has O(n*m) complexity

File: packages/nlp-engine/src/chat/parser.rs (around line 230)

The function checks every prefix length of both sentinels against the buffer suffix. For the current sentinel lengths (12 and 6 chars), this is ~18 iterations per token -- negligible. But if sentinels were ever longer or more numerous, this could become a hotspot. Current implementation is fine.

Notes

  • Excellent streaming parser design with proper partial-sentinel buffering -- handles token boundaries correctly.
  • Complete test coverage: 25+ parser tests including character-by-character stress test, nested JSON, and edge cases.
  • GPU scheduling with tokio::sync::Mutex correctly prevents Metal command-buffer collisions.
  • spawn_blocking correctly offloads the CPU-bound inference loop.
  • Context overflow detection prevents runaway generation.
  • Tool-call format ([TOOL_CALLS]...[ARGS]...) is well-documented and tested.
  • Chat template injection correctly places tool definitions in the system prompt.

No follow-up fixes required.

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.

NLP Engine: Chat inference with streaming and tool-call parsing

1 participant