@@ -62,7 +62,7 @@ use num_traits::{
62
62
pub use redis:: { RedisWrite , ToRedisArgs } ;
63
63
use serde:: { Deserialize , Serialize } ;
64
64
use storage:: TapestryChest ;
65
- use tracing:: { debug, error, instrument} ;
65
+ use tracing:: { debug, error, instrument, trace } ;
66
66
67
67
pub mod architecture;
68
68
pub mod storage;
@@ -313,6 +313,8 @@ impl<T: Config> TapestryFragment<T> {
313
313
) )
314
314
} ) ?;
315
315
316
+ trace ! ( "Pushing message: {:?}, new token count: {}" , msg, new_token_count) ;
317
+
316
318
self . context_tokens = new_token_count;
317
319
self . context_messages . push ( msg) ;
318
320
Ok ( ( ) )
@@ -331,7 +333,8 @@ impl<T: Config> TapestryFragment<T> {
331
333
. iter ( )
332
334
. fold ( PromptModelTokens :: < T > :: default ( ) , |acc, x| acc. saturating_add ( x) ) ;
333
335
334
- // Check the total token count before proceeding
336
+ trace ! ( "Extending messages with token sum: {}" , sum) ;
337
+
335
338
let new_token_count = self . context_tokens . checked_add ( & sum) . ok_or_else ( || {
336
339
LoomError :: from ( WeaveError :: BadConfig (
337
340
"Number of tokens exceeds max tokens for model" . to_string ( ) ,
@@ -341,7 +344,7 @@ impl<T: Config> TapestryFragment<T> {
341
344
// Update the token count and messages only if all checks pass
342
345
self . context_tokens = new_token_count;
343
346
for m in msgs {
344
- self . context_messages . push ( m) ; // Push the message directly without cloning
347
+ self . context_messages . push ( m) ;
345
348
}
346
349
347
350
Ok ( ( ) )
@@ -383,7 +386,8 @@ pub trait Loom<T: Config> {
383
386
Self :: build_context_message ( SYSTEM_ROLE . into ( ) , instructions, None ) ;
384
387
let instructions_req_msg: PromptModelRequest < T > = instructions_ctx_msg. clone ( ) . into ( ) ;
385
388
386
- // Get current tapestry fragment to work with
389
+ trace ! ( "Fetching current tapestry fragment for ID: {:?}" , tapestry_id) ;
390
+
387
391
let current_tapestry_fragment = T :: Chest :: get_tapestry_fragment ( tapestry_id. clone ( ) , None )
388
392
. await ?
389
393
. unwrap_or_default ( ) ;
@@ -393,8 +397,7 @@ pub trait Loom<T: Config> {
393
397
394
398
// Request messages which will be sent as a whole to the LLM
395
399
let mut req_msgs = VecPromptMsgsDeque :: < T , T :: PromptModel > :: with_capacity (
396
- current_tapestry_fragment. context_messages . len ( ) + 1 , /* +1 for the instruction
397
- * message to add */
400
+ current_tapestry_fragment. context_messages . len ( ) + 1 ,
398
401
) ;
399
402
400
403
// Add instructions as the first message
@@ -416,6 +419,12 @@ pub trait Loom<T: Config> {
416
419
// or we are continuing the current tapestry fragment.
417
420
let msgs_tokens = Self :: count_tokens_in_messages ( msgs. iter ( ) ) ;
418
421
422
+ trace ! (
423
+ "Total tokens after adding new messages: {:?}, maximum allowed: {:?}" ,
424
+ req_msgs. tokens. saturating_add( & msgs_tokens) ,
425
+ max_prompt_tokens_limit
426
+ ) ;
427
+
419
428
// Check if the total number of tokens in the tapestry fragment exceeds the maximum number
420
429
// of tokens allowed after adding the new messages and the minimum response length.
421
430
let does_exceeding_max_token_limit = max_prompt_tokens_limit <=
@@ -425,12 +434,13 @@ pub trait Loom<T: Config> {
425
434
426
435
let ( mut tapestry_fragment_to_persist, was_summary_generated) =
427
436
if does_exceeding_max_token_limit {
437
+ trace ! ( "Generating summary as the token limit exceeded" ) ;
438
+
428
439
// Summary generation should not exceed the maximum token limit of the prompt model
429
440
// since it will be added to the tapestry fragment
430
441
let summary_max_tokens: PromptModelTokens < T > =
431
442
prompt_llm_config. model . max_context_length ( ) - max_prompt_tokens_limit;
432
443
433
- // Generate summary
434
444
let summary = Self :: generate_summary (
435
445
summary_llm_config,
436
446
& current_tapestry_fragment,
@@ -464,11 +474,14 @@ pub trait Loom<T: Config> {
464
474
// Tokens available for LLM response which would not exceed maximum token limit
465
475
let max_completion_tokens = max_prompt_tokens_limit. saturating_sub ( & req_msgs. tokens ) ;
466
476
477
+ trace ! ( "Max completion tokens available: {:?}" , max_completion_tokens) ;
478
+
467
479
if max_completion_tokens. is_zero ( ) {
468
480
return Err ( LoomError :: from ( WeaveError :: MaxCompletionTokensIsZero ) . into ( ) ) ;
469
481
}
470
482
471
- // Execute prompt to LLM
483
+ trace ! ( "Prompting LLM with request messages" ) ;
484
+
472
485
let response = prompt_llm_config
473
486
. model
474
487
. prompt (
@@ -521,6 +534,12 @@ pub trait Loom<T: Config> {
521
534
tapestry_fragment : & TapestryFragment < T > ,
522
535
summary_max_tokens : SummaryModelTokens < T > ,
523
536
) -> Result < String > {
537
+ trace ! (
538
+ "Generating summary with max tokens: {:?}, for tapestry fragment: {:?}" ,
539
+ summary_max_tokens,
540
+ tapestry_fragment
541
+ ) ;
542
+
524
543
let mut summary_generation_prompt = VecPromptMsgsDeque :: < T , T :: SummaryModel > :: new ( ) ;
525
544
526
545
summary_generation_prompt. extend (
@@ -546,6 +565,8 @@ pub trait Loom<T: Config> {
546
565
547
566
let summary_response_content = res. into ( ) ;
548
567
568
+ trace ! ( "Generated summary: {:?}" , summary_response_content) ;
569
+
549
570
Ok ( summary_response_content. unwrap_or_default ( ) )
550
571
}
551
572
@@ -555,6 +576,8 @@ pub trait Loom<T: Config> {
555
576
content : String ,
556
577
account_id : Option < String > ,
557
578
) -> ContextMessage < T > {
579
+ trace ! ( "Building context message for role: {:?}, content: {}" , role, content) ;
580
+
558
581
ContextMessage {
559
582
role,
560
583
content,
0 commit comments