( input: HallucinationValidationInput )
| 229 | * Validate user input against knowledge base using RAG + LLM scoring |
| 230 | */ |
| 231 | export async function validateHallucination( |
| 232 | input: HallucinationValidationInput |
| 233 | ): Promise<HallucinationValidationResult> { |
| 234 | const { |
| 235 | userInput, |
| 236 | knowledgeBaseId, |
| 237 | threshold, |
| 238 | topK, |
| 239 | model, |
| 240 | apiKey, |
| 241 | providerCredentials, |
| 242 | workflowId, |
| 243 | workspaceId, |
| 244 | authHeaders, |
| 245 | requestId, |
| 246 | } = input |
| 247 | |
| 248 | try { |
| 249 | if (!userInput || userInput.trim().length === 0) { |
| 250 | return { |
| 251 | passed: false, |
| 252 | error: 'User input is required', |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!knowledgeBaseId) { |
| 257 | return { |
| 258 | passed: false, |
| 259 | error: 'Knowledge base ID is required', |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Step 1: Query knowledge base with RAG |
| 264 | const ragContext = await queryKnowledgeBase( |
| 265 | knowledgeBaseId, |
| 266 | userInput, |
| 267 | topK, |
| 268 | requestId, |
| 269 | workflowId, |
| 270 | authHeaders |
| 271 | ) |
| 272 | |
| 273 | if (ragContext.length === 0) { |
| 274 | return { |
| 275 | passed: false, |
| 276 | error: 'No relevant context found in knowledge base', |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Step 2: Use LLM to score confidence |
| 281 | const { score, reasoning, cost } = await scoreHallucinationWithLLM( |
| 282 | userInput, |
| 283 | ragContext, |
| 284 | model, |
| 285 | apiKey, |
| 286 | providerCredentials, |
| 287 | workspaceId, |
| 288 | requestId |
no test coverage detected