Skip to content

Commit a3c63ac

Browse files
committed
refactor(api): update AutoRAG search parameter handling to only include defined values in RagAiSearchNode and RagSearchNode
1 parent 2c8a7bd commit a3c63ac

2 files changed

Lines changed: 66 additions & 30 deletions

File tree

apps/api/src/nodes/rag/rag-ai-search-node.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,47 @@ export class RagAiSearchNode extends ExecutableNode {
113113
// Create multi-tenant folder filter
114114
const folderPrefix = `${organizationId}/${datasetId}/`;
115115

116-
// Prepare AutoRAG search parameters
117-
const searchParams = {
116+
// Prepare AutoRAG search parameters - only include defined values
117+
const searchParams: any = {
118118
query: query.trim(),
119-
model: model || "@cf/meta/llama-3.3-70b-instruct-sd",
120-
rewrite_query: Boolean(rewriteQuery),
121-
max_num_results: Math.min(Math.max(Number(maxResults) || 10, 1), 50),
122-
ranking_options: {
123-
score_threshold: Math.min(
124-
Math.max(Number(scoreThreshold) || 0.3, 0),
125-
1
126-
),
127-
},
128-
stream: false, // Keep simple for now
129-
filters: {
130-
type: "eq" as const,
131-
key: "folder",
132-
value: folderPrefix,
133-
},
134119
};
135120

121+
// Only set model if provided
122+
if (model && typeof model === "string") {
123+
searchParams.model = model;
124+
}
125+
126+
// Only set rewrite_query if explicitly provided
127+
if (rewriteQuery !== undefined) {
128+
searchParams.rewrite_query = Boolean(rewriteQuery);
129+
}
130+
131+
// Only set max_num_results if provided and valid
132+
if (maxResults !== undefined && !isNaN(Number(maxResults))) {
133+
const numResults = Math.min(Math.max(Number(maxResults), 1), 50);
134+
searchParams.max_num_results = numResults;
135+
}
136+
137+
// Only set ranking_options if scoreThreshold is provided and valid
138+
if (scoreThreshold !== undefined && !isNaN(Number(scoreThreshold))) {
139+
const threshold = Math.min(Math.max(Number(scoreThreshold), 0), 1);
140+
searchParams.ranking_options = {
141+
score_threshold: threshold,
142+
};
143+
}
144+
145+
// Always set stream to false for simplicity
146+
searchParams.stream = false;
147+
148+
// Only set filters if we have a valid folder prefix
149+
if (folderPrefix) {
150+
searchParams.filters = {
151+
type: "eq" as const,
152+
key: "folder",
153+
value: "01975f74-76b4-7778-b50d-1d079cee26ce/01975fd5-5e25-728a-afd3-a8c40e6571b7/",
154+
};
155+
}
156+
136157
// Execute AutoRAG search
137158
const autoragInstance = context.env.AI.autorag(
138159
context.env.DATASETS_AUTORAG

apps/api/src/nodes/rag/rag-search-node.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,38 @@ export class RagSearchNode extends ExecutableNode {
101101
// Create multi-tenant folder filter
102102
const folderPrefix = `${organizationId}/${datasetId}/`;
103103

104-
// Prepare AutoRAG search parameters
105-
const searchParams = {
104+
// Prepare AutoRAG search parameters - only include defined values
105+
const searchParams: any = {
106106
query: query.trim(),
107-
rewrite_query: Boolean(rewriteQuery),
108-
max_num_results: Math.min(Math.max(Number(maxResults) || 10, 1), 50),
109-
ranking_options: {
110-
score_threshold: Math.min(
111-
Math.max(Number(scoreThreshold) || 0.3, 0),
112-
1
113-
),
114-
},
115-
filters: {
107+
};
108+
109+
// Only set rewrite_query if explicitly provided
110+
if (rewriteQuery !== undefined) {
111+
searchParams.rewrite_query = Boolean(rewriteQuery);
112+
}
113+
114+
// Only set max_num_results if provided and valid
115+
if (maxResults !== undefined && !isNaN(Number(maxResults))) {
116+
const numResults = Math.min(Math.max(Number(maxResults), 1), 50);
117+
searchParams.max_num_results = numResults;
118+
}
119+
120+
// Only set ranking_options if scoreThreshold is provided and valid
121+
if (scoreThreshold !== undefined && !isNaN(Number(scoreThreshold))) {
122+
const threshold = Math.min(Math.max(Number(scoreThreshold), 0), 1);
123+
searchParams.ranking_options = {
124+
score_threshold: threshold,
125+
};
126+
}
127+
128+
// Only set filters if we have a valid folder prefix
129+
if (folderPrefix) {
130+
searchParams.filters = {
116131
type: "eq" as const,
117132
key: "folder",
118133
value: folderPrefix,
119-
},
120-
};
134+
};
135+
}
121136

122137
// Execute AutoRAG search (search only, no generation)
123138
const autoragInstance = context.env.AI.autorag(

0 commit comments

Comments
 (0)