Case 4. RAG for internal documentation with LLPhant

A controlled internal documentation QA pipeline in PHP with LLPhant

In this case we move from pure PHP RAG to LLPhant components for embeddings and vector storage while preserving strict control over retrieval, context building, and hallucination prevention.

 
<?php

include 'code-en.php';

$apiKey = (string)config('OPENAI_API_KEY');

$documents = [
    
makeDocument('The SLA for service X is 99.9% per month.'),
    
makeDocument('API key rotation is performed every 90 days.'),
    
makeDocument('Disaster recovery process is documented in section 6.3 of the payments runbook.'),
    
makeDocument('Security policy requires mandatory MFA for all admin access.'),
];
$data runRagWithLlphant(
    
$apiKey,
    
$documents,
    
query'How often are API keys rotated?',
    
topK2
);

$documentsRows documentsForOutputRows($data['documents']);
$retrievedRows documentsForOutputRows($data['relevant_documents']);


echo 
'Case goal:' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
'Build an internal documentation QA system with LLPhant while keeping engineering control over retrieval and context.' PHP_EOL;
echo 
PHP_EOL;

echo 
'Business scenario:' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
'- Internal docs: regulations, service README files, SLA and security policies.' PHP_EOL;
echo 
'- Employees ask questions in natural language.' PHP_EOL;
echo 
'- The model must not invent facts.' PHP_EOL;
echo 
'- If information is absent in docs, the system should say so explicitly.' PHP_EOL;
echo 
PHP_EOL;

echo 
'Architecture:' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
"Query -> Embedding -> Similarity search -> Top-K docs -> Controlled prompt -> LLM -> Answer" PHP_EOL;
echo 
PHP_EOL;

echo 
'Stage 1. Document indexing (LLPhant embeddings + MemoryVectorStore):' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
array_to_matrix($documentsRowswithHeaderstruewithPaddingtruewithCommasfalseshowRowNumberstrue);
echo 
PHP_EOL PHP_EOL;

echo 
'Stage 2. Retrieval (Top-K=' $data['top_k'] . '):' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
'Query: ' $data['query'] . PHP_EOL;
echo 
array_to_matrix($retrievedRowswithHeaderstruewithPaddingtruewithCommasfalseshowRowNumberstrue);
echo 
PHP_EOL PHP_EOL;

echo 
'Stage 3. Controlled context building:' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
$data['context_prompt'] . PHP_EOL PHP_EOL;

echo 
'Stage 4. Answer generation:' PHP_EOL;
echo 
'-----------' PHP_EOL;
echo 
'Answer: ' trim((string)$data['answer']) . PHP_EOL;
Result: Memory: 0.006 Mb Time running: < 0.001 sec.
Case goal:
-----------
Build an internal documentation QA system with LLPhant while keeping engineering control over retrieval and context.

Business scenario:
-----------
- Internal docs: regulations, service README files, SLA and security policies.
- Employees ask questions in natural language.
- The model must not invent facts.
- If information is absent in docs, the system should say so explicitly.

Architecture:
-----------
Query -> Embedding -> Similarity search -> Top-K docs -> Controlled prompt -> LLM -> Answer

Stage 1. Document indexing (LLPhant embeddings + MemoryVectorStore):
-----------
#: [id content                                                                        ]
1: [1  The SLA for service X is 99.9% per month.                                      ]
2: [2  API key rotation is performed every 90 days.                                   ]
3: [3  Disaster recovery process is documented in section 6.3 of the payments runbook.]
4: [4  Security policy requires mandatory MFA for all admin access.                   ]

Stage 2. Retrieval (Top-K=2):
-----------
Query: How often are API keys rotated?
#: [id content                                     ]
1: [1  API key rotation is performed every 90 days.]
2: [2  The SLA for service X is 99.9% per month.   ]

Stage 3. Controlled context building:
-----------
You answer strictly based on the context below. If the answer is missing, say that information is insufficient.

Context:
- API key rotation is performed every 90 days.
- The SLA for service X is 99.9% per month.

Question: How often are API keys rotated?

Stage 4. Answer generation:
-----------
Answer: API keys are rotated every 90 days.
Conclusion: LLPhant reduces infrastructure noise, but preserves transparency and control over RAG mechanics.