Skip to content

C++ RAG integration and function calling#7

Merged
drzo merged 1 commit into
mainfrom
copilot/implement-cpp-features
May 8, 2026
Merged

C++ RAG integration and function calling#7
drzo merged 1 commit into
mainfrom
copilot/implement-cpp-features

Conversation

Copilot AI commented May 8, 2026

Copy link
Copy Markdown

Implements the two remaining near-term features in the C++ cognitive kernel: retrieval-augmented generation and LLM tool/function calling.

RAG (cpp/src/rag/)

  • Self-contained vector store — no external embedding model required
  • 1024-dim FNV-1a hash-projection feature space with L2-normalised term-frequency vectors and cosine similarity retrieval
  • Configurable chunk size + overlap; file ingestion via rag_add_file()
  • rag_build_context() returns a prompt-ready context block for direct injection before llm_chat_completion()
rag_store_t store = rag_create(NULL);
rag_add_file(store, "docs/manual.txt");
char* ctx = rag_build_context(store, "How do I configure X?", 4);
// prepend ctx to system prompt
free(ctx);
rag_destroy(store);

Function calling (cpp/src/function/)

  • Tool registry with C-callback and popen-based shell-command backends
  • Injects tool descriptions into the system prompt automatically
  • Protocol: model emits <tool_call>{"name":"…","args":{…}}</tool_call>; dispatcher parses, invokes, and returns <tool_result>…</tool_result> for re-injection
function_registry_t reg = function_registry_create();
function_register_shell(reg, "calc", "Evaluate math", "echo $(({args}))");
char* tools_prompt = function_get_tools_prompt(reg);  // inject into system msg

char result[4096];
function_dispatch(reg, model_output, result, sizeof(result));
// inject result as next user message

Tests

12 new ctest cases (5 RAG, 7 function); total suite goes from 15 → 27 passing.

- cpp/include/aichat/rag.h: public API for RAG store
  (chunk ingestion, cosine-similarity retrieval, context builder)
- cpp/src/rag/rag.cpp: implementation using hash-projection feature
  vectors (FNV-1a, 1024-dim), L2-normalised TF, cosine similarity
- cpp/include/aichat/function.h: public API for tool/function calling
  (registry, system-prompt injection, tool-call dispatch)
- cpp/src/function/function.cpp: implementation with C-callback and
  shell-command (popen) backends; tool_call/tool_result XML protocol
- cpp/tests/test_rag.cpp: 5 RAG tests
- cpp/tests/test_function.cpp: 7 function-calling tests
- cpp/CMakeLists.txt: add new source files
- cpp/tests/CMakeLists.txt: register 12 new test cases
- cpp/include/aichat.h: include new headers
- CPP_IMPLEMENTATION_SUMMARY.md: mark RAG and function calling complete

All 27 ctest cases pass (was 15, now 27).

Agent-Logs-Url: https://github.com/9cog/aichat/sessions/36c9c42d-3853-438e-8742-ed9d5ba29821

Co-authored-by: drzo <15202748+drzo@users.noreply.github.com>
Copilot AI assigned Copilot and drzo May 8, 2026
@drzo drzo marked this pull request as ready for review May 8, 2026 06:41
@drzo drzo merged commit 87ffefe into main May 8, 2026
3 of 6 checks passed
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.

2 participants