Skip to content

Commit 9a8dc5a

Browse files
committed
fix(server): adjust search tool response to not exceed context window size + do not include penalties
1 parent cab7649 commit 9a8dc5a

File tree

7 files changed

+130
-47
lines changed

7 files changed

+130
-47
lines changed

clients/agentic-rag-cli/index.ts

Lines changed: 111 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import fs from 'fs';
44
import path from 'path';
5-
import { TrieveSDK, Topic, ChunkMetadata } from 'trieve-ts-sdk';
5+
import {
6+
TrieveSDK,
7+
Topic,
8+
ChunkMetadata,
9+
UpdateDatasetReqPayload,
10+
} from 'trieve-ts-sdk';
611
import { program } from 'commander';
712
import chalk from 'chalk';
813
import inquirer from 'inquirer';
@@ -108,6 +113,10 @@ function getConfigOrEnv(key: string, envVar: string): string | undefined {
108113
function ensureTrieveConfig() {
109114
const apiKey = getConfigOrEnv('TRIEVE_API_KEY', 'TRIEVE_API_KEY');
110115
const datasetId = getConfigOrEnv('TRIEVE_DATASET_ID', 'TRIEVE_DATASET_ID');
116+
const organizationId = getConfigOrEnv(
117+
'TRIEVE_ORGANIZATION_ID',
118+
'TRIEVE_ORGANIZATION_ID',
119+
);
111120

112121
if (!apiKey) {
113122
console.error(
@@ -125,7 +134,15 @@ function ensureTrieveConfig() {
125134
console.log(chalk.cyan('trieve-cli configure'));
126135
process.exit(1);
127136
}
128-
return { apiKey, datasetId };
137+
if (!organizationId) {
138+
console.error(
139+
chalk.red('Error: TRIEVE_ORGANIZATION_ID is not set in env or config.'),
140+
);
141+
console.log(chalk.yellow('Run the following command to set it:'));
142+
console.log(chalk.cyan('trieve-cli configure'));
143+
process.exit(1);
144+
}
145+
return { apiKey, datasetId, organizationId };
129146
}
130147

131148
const uploadFile = async (
@@ -217,10 +234,11 @@ async function checkFileUploadStatus(
217234
groupTrackingId: string,
218235
): Promise<boolean> {
219236
try {
220-
const { apiKey, datasetId } = ensureTrieveConfig();
237+
const { apiKey, datasetId, organizationId } = ensureTrieveConfig();
221238
const trieveClient: TrieveSDK = new TrieveSDK({
222239
apiKey,
223240
datasetId,
241+
organizationId,
224242
});
225243

226244
const response = await trieveClient.getChunksGroupByTrackingId({
@@ -388,7 +406,6 @@ async function askQuestion(question: string): Promise<void> {
388406
'default-user-' + Math.random().toString(36).substring(2, 15);
389407

390408
const topicData = await trieveClient.createTopic({
391-
first_user_message: question,
392409
name: topicName,
393410
owner_id: ownerId,
394411
});
@@ -411,7 +428,7 @@ async function askQuestion(question: string): Promise<void> {
411428
let fullResponse = '';
412429
let parsedChunks: ChunkMetadata[] = [];
413430
let isCollapsed = true;
414-
let isChunkSection = true; // Initially assume we're receiving chunks
431+
let isChunkSection = false; // Initially assume we're receiving chunks
415432
let actualAnswer = '';
416433

417434
// Set up keyboard interaction for collapsible chunks
@@ -425,12 +442,22 @@ async function askQuestion(question: string): Promise<void> {
425442
key: { name: string; ctrl?: boolean; sequence?: string },
426443
) => {
427444
// Handle both key.name and raw sequence for better compatibility
428-
if (key.name === 'j' || key.sequence === 'j') {
445+
if (str === 'j' || key.name === 'j' || key.sequence === 'j') {
429446
isCollapsed = !isCollapsed;
430447
// Clear console and redisplay with updated collapse state
431448
console.clear();
432449

433450
if (parsedChunks.length > 0) {
451+
// Show the answer first (if available)
452+
if (actualAnswer) {
453+
console.log(actualAnswer);
454+
}
455+
456+
// Add a separator between answer and chunks
457+
console.log(
458+
chalk.dim('─'.repeat(40) + ' References ' + '─'.repeat(40)),
459+
);
460+
434461
if (isCollapsed) {
435462
console.log(
436463
chalk.cyan(
@@ -440,12 +467,7 @@ async function askQuestion(question: string): Promise<void> {
440467
} else {
441468
console.log(formatChunksCollapsible(parsedChunks));
442469
}
443-
444-
// Add a separator between chunks and answer
445-
console.log(chalk.dim('─'.repeat(40) + ' Answer ' + '─'.repeat(40)));
446-
}
447-
448-
if (actualAnswer) {
470+
} else if (actualAnswer) {
449471
console.log(actualAnswer);
450472
}
451473
} else if (key.name === 'c' && key.ctrl) {
@@ -475,21 +497,8 @@ async function askQuestion(question: string): Promise<void> {
475497
if (chunksJson) {
476498
parsedChunks = JSON.parse(chunksJson);
477499

478-
// Only show a collapsed summary initially
479-
if (isCollapsed) {
480-
console.log(
481-
chalk.cyan(
482-
`📚 Found ${parsedChunks.length} reference chunks (press 'j' to expand)`,
483-
),
484-
);
485-
} else {
486-
console.log(formatChunksCollapsible(parsedChunks));
487-
}
488-
489-
// Add a separator between chunks and answer
490-
console.log(
491-
chalk.dim('─'.repeat(40) + ' Answer ' + '─'.repeat(40)),
492-
);
500+
// Don't display chunks immediately, we'll show them after the answer
501+
// Just save them for later display
493502
}
494503
} catch (e) {
495504
console.error(chalk.red('❌ Error parsing chunks:'), e);
@@ -519,11 +528,18 @@ async function askQuestion(question: string): Promise<void> {
519528
console.log(chalk.green('✅ Response complete'));
520529

521530
if (parsedChunks.length > 0) {
531+
// Add a separator between answer and chunks at the end
532+
console.log(chalk.dim('─'.repeat(40) + ' References ' + '─'.repeat(40)));
522533
console.log(
523534
chalk.blue(
524535
`📚 ${parsedChunks.length} reference chunks used (press 'j' to ${isCollapsed ? 'expand' : 'collapse'})`,
525536
),
526537
);
538+
539+
// If not collapsed, show the chunks again
540+
if (!isCollapsed) {
541+
console.log(formatChunksCollapsible(parsedChunks));
542+
}
527543
}
528544

529545
console.log(
@@ -601,6 +617,66 @@ program
601617
}
602618
});
603619

620+
// -t "Always use the search tool here when the user asks a question about some information you're supposed to have or some files."
621+
// -q "Write this as a maximum 5 word query with the keywords you think would be useful"
622+
623+
program
624+
.command('update-tool-config')
625+
.description('Update the tool configuration for a dataset')
626+
.requiredOption(
627+
'-t, --tool-description <toolDescription>',
628+
'Description that tells the LLM when it should use the search tool to retrieve information from your dataset',
629+
"Always use the search tool here when the user asks a question about some information you're supposed to have or some files.",
630+
)
631+
.requiredOption(
632+
'-q, --query-description <queryDescription>',
633+
'Description of how the LLM should write its search queries to retrieve relevant information from your dataset',
634+
'Write this as a maximum 5 word query with the keywords you think would be useful',
635+
)
636+
.action(async (options) => {
637+
try {
638+
console.log(chalk.blue('🔧 Updating tool configuration...'));
639+
640+
const { apiKey, datasetId, organizationId } = ensureTrieveConfig();
641+
642+
// Initialize SDK with apiKey and datasetId from config
643+
const trieveClient: TrieveSDK = new TrieveSDK({
644+
apiKey,
645+
datasetId,
646+
organizationId,
647+
});
648+
649+
const updatePayload: UpdateDatasetReqPayload = {
650+
dataset_id: datasetId,
651+
server_configuration: {
652+
TOOL_CONFIGURATION: {
653+
query_tool_options: {
654+
tool_description: options.toolDescription,
655+
query_parameter_description: options.queryDescription,
656+
price_filter_description:
657+
'The price or page range filter to use for the search',
658+
659+
max_price_option_description:
660+
'The maximum price or page to filter by',
661+
662+
min_price_option_description:
663+
'The minimum price or page to filter by',
664+
},
665+
},
666+
},
667+
};
668+
669+
await trieveClient.updateDataset(updatePayload);
670+
671+
console.log(chalk.green('✅ Tool configuration updated successfully!'));
672+
} catch (error) {
673+
console.error(
674+
chalk.red('❌ Failed to update tool configuration:'),
675+
error instanceof Error ? error.message : error,
676+
);
677+
}
678+
});
679+
604680
program
605681
.command('configure')
606682
.description('Set up or update your Trieve CLI configuration')
@@ -618,6 +694,12 @@ program
618694
message: 'Enter your TRIEVE_DATASET_ID:',
619695
default: (config.get('TRIEVE_DATASET_ID') as string) || '',
620696
},
697+
{
698+
type: 'input',
699+
name: 'TRIEVE_ORGANIZATION_ID',
700+
message: 'Enter your TRIEVE_ORGANIZATION_ID:',
701+
default: (config.get('TRIEVE_ORGANIZATION_ID') as string) || '',
702+
},
621703
{
622704
type: 'input',
623705
name: 'userId',
@@ -627,6 +709,7 @@ program
627709
]);
628710
config.set('TRIEVE_API_KEY', answers.TRIEVE_API_KEY);
629711
config.set('TRIEVE_DATASET_ID', answers.TRIEVE_DATASET_ID);
712+
config.set('TRIEVE_ORGANIZATION_ID', answers.TRIEVE_ORGANIZATION_ID);
630713
config.set('userId', answers.userId);
631714
console.log(chalk.green('✅ Configuration saved!'));
632715
});
@@ -685,6 +768,7 @@ ${chalk.yellow('Examples:')}
685768
$ ${chalk.green('trieve-cli check-upload-status --tracking-id <tracking-id>')}
686769
$ ${chalk.green('trieve-cli ask "What is the capital of France?"')}
687770
$ ${chalk.green('trieve-cli ask')}
771+
$ ${chalk.green('trieve-cli update-tool-config -t "Use this tool to search for information about our products and services" -q "Write specific search queries to find relevant information from our knowledge base"')}
688772
`,
689773
);
690774

clients/agentic-rag-cli/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/agentic-rag-cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
"commander": "^14.0.0",
3131
"conf": "^14.0.0",
3232
"inquirer": "^12.6.3",
33-
"trieve-ts-sdk": "^0.0.119"
33+
"trieve-ts-sdk": "^0.0.120"
3434
}
3535
}

clients/ts-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"files": [
1818
"dist"
1919
],
20-
"version": "0.0.120",
20+
"version": "0.0.121",
2121
"license": "MIT",
2222
"scripts": {
2323
"lint": "eslint 'src/**/*.ts'",

server/src/bin/file-worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ async fn upload_file(
607607
split_avg: None,
608608
convert_html_to_text: None,
609609
image_urls: None,
610-
num_value: None,
610+
num_value: Some(page.page_num as f64),
611611
fulltext_boost: None,
612612
semantic_boost: None,
613613
high_priority: None,

server/src/data/models.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,9 +2902,9 @@ impl From<DatasetConfigurationDTO> for DatasetConfiguration {
29022902
query_tool_options: Some(QueryToolOptions {
29032903
tool_description: Some("Search for relevant information in the knowledge base. You can use this tool multiple times if the information you need is not found in the first search.".to_string()),
29042904
query_parameter_description: Some("The search query to find relevant information".to_string()),
2905-
price_filter_description: Some("The price filter to use for the search".to_string()),
2906-
max_price_option_description: Some("The maximum price to filter by".to_string()),
2907-
min_price_option_description: Some("The minimum price to filter by".to_string()),
2905+
price_filter_description: Some("The price or page range filter to use for the search".to_string()),
2906+
max_price_option_description: Some("The maximum price or page to filter by".to_string()),
2907+
min_price_option_description: Some("The minimum price or page to filter by".to_string()),
29082908
}),
29092909
}),
29102910
}

server/src/operators/message_operator.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ pub async fn get_rag_chunks_query(
449449
messages: gen_inference_msgs,
450450
stream: Some(false),
451451
temperature: dataset_config.TEMPERATURE.map(|temp| temp as f32),
452-
frequency_penalty: Some(dataset_config.FREQUENCY_PENALTY.unwrap_or(0.8) as f32),
453-
presence_penalty: Some(dataset_config.PRESENCE_PENALTY.unwrap_or(0.8) as f32),
452+
frequency_penalty: dataset_config.FREQUENCY_PENALTY.map(|pen| pen as f32),
453+
presence_penalty: dataset_config.PRESENCE_PENALTY.map(|pen| pen as f32),
454454
stop: dataset_config.STOP_TOKENS.clone().map(StopToken::Array),
455455
top_p: None,
456456
n: None,
@@ -1794,7 +1794,6 @@ async fn handle_search_tool_call(
17941794
"id": chunk_metadata.id,
17951795
"content": convert_html_to_text(&chunk_metadata.chunk_html.unwrap_or_default()),
17961796
"tag_set": chunk_metadata.tag_set.clone(),
1797-
"metadata": chunk_metadata.metadata.clone(),
17981797
"num_value": chunk_metadata.num_value,
17991798
"score": score_chunk.score,
18001799
"link": chunk_metadata.link.unwrap_or_default()
@@ -1804,7 +1803,7 @@ async fn handle_search_tool_call(
18041803

18051804
Ok((
18061805
results,
1807-
serde_json::to_string(&formatted_results).unwrap_or_default(),
1806+
serde_json::to_string_pretty(&formatted_results).unwrap_or_default(),
18081807
clickhouse_search_event,
18091808
))
18101809
}
@@ -2748,8 +2747,8 @@ pub async fn get_topic_string(
27482747
top_p: None,
27492748
n: None,
27502749
stop: None,
2751-
frequency_penalty: Some(dataset_config.FREQUENCY_PENALTY.unwrap_or(0.8) as f32),
2752-
presence_penalty: Some(dataset_config.PRESENCE_PENALTY.unwrap_or(0.8) as f32),
2750+
frequency_penalty: None,
2751+
presence_penalty: None,
27532752
logit_bias: None,
27542753
user: None,
27552754
response_format: None,
@@ -3008,8 +3007,8 @@ pub async fn suggested_followp_questions(
30083007
n: None,
30093008
stop: None,
30103009
max_completion_tokens: None,
3011-
frequency_penalty: Some(dataset_config.FREQUENCY_PENALTY.unwrap_or(0.8) as f32),
3012-
presence_penalty: Some(dataset_config.PRESENCE_PENALTY.unwrap_or(0.8) as f32),
3010+
frequency_penalty: None,
3011+
presence_penalty: None,
30133012
logit_bias: None,
30143013
user: None,
30153014
response_format: None,
@@ -3249,8 +3248,8 @@ pub async fn suggested_new_queries(
32493248
n: None,
32503249
stop: None,
32513250
max_completion_tokens: None,
3252-
frequency_penalty: Some(dataset_config.clone().FREQUENCY_PENALTY.unwrap_or(0.8) as f32),
3253-
presence_penalty: Some(dataset_config.clone().PRESENCE_PENALTY.unwrap_or(0.8) as f32),
3251+
frequency_penalty: None,
3252+
presence_penalty: None,
32543253
logit_bias: None,
32553254
user: None,
32563255
response_format: None,

0 commit comments

Comments
 (0)