MCPcopy Index your code
hub / github.com/simstudioai/sim / queryKnowledgeBase

Function queryKnowledgeBase

apps/sim/lib/guardrails/validate_hallucination.ts:50–96  ·  view source on GitHub ↗

* Query knowledge base to get relevant context chunks using the search API

(
  knowledgeBaseId: string,
  query: string,
  topK: number,
  requestId: string,
  workflowId?: string,
  authHeaders?: { cookie?: string; authorization?: string }
)

Source from the content-addressed store, hash-verified

48 * Query knowledge base to get relevant context chunks using the search API
49 */
50async function queryKnowledgeBase(
51 knowledgeBaseId: string,
52 query: string,
53 topK: number,
54 requestId: string,
55 workflowId?: string,
56 authHeaders?: { cookie?: string; authorization?: string }
57): Promise<string[]> {
58 try {
59 // Call the knowledge base search API directly
60 const searchUrl = `${getInternalApiBaseUrl()}/api/knowledge/search`
61
62 const response = await fetch(searchUrl, {
63 method: 'POST',
64 headers: {
65 'Content-Type': 'application/json',
66 ...(authHeaders?.cookie ? { Cookie: authHeaders.cookie } : {}),
67 ...(authHeaders?.authorization ? { Authorization: authHeaders.authorization } : {}),
68 },
69 body: JSON.stringify({
70 knowledgeBaseIds: [knowledgeBaseId],
71 query,
72 topK,
73 workflowId,
74 }),
75 })
76
77 if (!response.ok) {
78 logger.error(`[${requestId}] Knowledge base query failed`, {
79 status: response.status,
80 })
81 return []
82 }
83
84 const result = await response.json()
85 const results = result.data?.results || []
86
87 const chunks = results.map((r: any) => r.content || '').filter((c: string) => c.length > 0)
88
89 return chunks
90 } catch (error: any) {
91 logger.error(`[${requestId}] Error querying knowledge base`, {
92 error: error.message,
93 })
94 return []
95 }
96}
97
98/**
99 * Use an LLM to score confidence based on RAG context

Callers 1

validateHallucinationFunction · 0.85

Calls 2

getInternalApiBaseUrlFunction · 0.90
errorMethod · 0.80

Tested by

no test coverage detected