Skip to content

Commit 53aa9fc

Browse files
committed
add tracing
1 parent 1cf04ab commit 53aa9fc

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
publish = true
33
name = "llm-weaver"
4-
version = "0.1.96"
4+
version = "0.1.97"
55
edition = "2021"
66
description = "Manage long conversations with any LLM"
77
readme = "README.md"

src/lib.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use num_traits::{
6262
pub use redis::{RedisWrite, ToRedisArgs};
6363
use serde::{Deserialize, Serialize};
6464
use storage::TapestryChest;
65-
use tracing::{debug, error, instrument};
65+
use tracing::{debug, error, instrument, trace};
6666

6767
pub mod architecture;
6868
pub mod storage;
@@ -313,6 +313,8 @@ impl<T: Config> TapestryFragment<T> {
313313
))
314314
})?;
315315

316+
trace!("Pushing message: {:?}, new token count: {}", msg, new_token_count);
317+
316318
self.context_tokens = new_token_count;
317319
self.context_messages.push(msg);
318320
Ok(())
@@ -331,7 +333,8 @@ impl<T: Config> TapestryFragment<T> {
331333
.iter()
332334
.fold(PromptModelTokens::<T>::default(), |acc, x| acc.saturating_add(x));
333335

334-
// Check the total token count before proceeding
336+
trace!("Extending messages with token sum: {}", sum);
337+
335338
let new_token_count = self.context_tokens.checked_add(&sum).ok_or_else(|| {
336339
LoomError::from(WeaveError::BadConfig(
337340
"Number of tokens exceeds max tokens for model".to_string(),
@@ -341,7 +344,7 @@ impl<T: Config> TapestryFragment<T> {
341344
// Update the token count and messages only if all checks pass
342345
self.context_tokens = new_token_count;
343346
for m in msgs {
344-
self.context_messages.push(m); // Push the message directly without cloning
347+
self.context_messages.push(m);
345348
}
346349

347350
Ok(())
@@ -383,7 +386,8 @@ pub trait Loom<T: Config> {
383386
Self::build_context_message(SYSTEM_ROLE.into(), instructions, None);
384387
let instructions_req_msg: PromptModelRequest<T> = instructions_ctx_msg.clone().into();
385388

386-
// Get current tapestry fragment to work with
389+
trace!("Fetching current tapestry fragment for ID: {:?}", tapestry_id);
390+
387391
let current_tapestry_fragment = T::Chest::get_tapestry_fragment(tapestry_id.clone(), None)
388392
.await?
389393
.unwrap_or_default();
@@ -393,8 +397,7 @@ pub trait Loom<T: Config> {
393397

394398
// Request messages which will be sent as a whole to the LLM
395399
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,
398401
);
399402

400403
// Add instructions as the first message
@@ -416,6 +419,12 @@ pub trait Loom<T: Config> {
416419
// or we are continuing the current tapestry fragment.
417420
let msgs_tokens = Self::count_tokens_in_messages(msgs.iter());
418421

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+
419428
// Check if the total number of tokens in the tapestry fragment exceeds the maximum number
420429
// of tokens allowed after adding the new messages and the minimum response length.
421430
let does_exceeding_max_token_limit = max_prompt_tokens_limit <=
@@ -425,12 +434,13 @@ pub trait Loom<T: Config> {
425434

426435
let (mut tapestry_fragment_to_persist, was_summary_generated) =
427436
if does_exceeding_max_token_limit {
437+
trace!("Generating summary as the token limit exceeded");
438+
428439
// Summary generation should not exceed the maximum token limit of the prompt model
429440
// since it will be added to the tapestry fragment
430441
let summary_max_tokens: PromptModelTokens<T> =
431442
prompt_llm_config.model.max_context_length() - max_prompt_tokens_limit;
432443

433-
// Generate summary
434444
let summary = Self::generate_summary(
435445
summary_llm_config,
436446
&current_tapestry_fragment,
@@ -464,11 +474,14 @@ pub trait Loom<T: Config> {
464474
// Tokens available for LLM response which would not exceed maximum token limit
465475
let max_completion_tokens = max_prompt_tokens_limit.saturating_sub(&req_msgs.tokens);
466476

477+
trace!("Max completion tokens available: {:?}", max_completion_tokens);
478+
467479
if max_completion_tokens.is_zero() {
468480
return Err(LoomError::from(WeaveError::MaxCompletionTokensIsZero).into());
469481
}
470482

471-
// Execute prompt to LLM
483+
trace!("Prompting LLM with request messages");
484+
472485
let response = prompt_llm_config
473486
.model
474487
.prompt(
@@ -521,6 +534,12 @@ pub trait Loom<T: Config> {
521534
tapestry_fragment: &TapestryFragment<T>,
522535
summary_max_tokens: SummaryModelTokens<T>,
523536
) -> Result<String> {
537+
trace!(
538+
"Generating summary with max tokens: {:?}, for tapestry fragment: {:?}",
539+
summary_max_tokens,
540+
tapestry_fragment
541+
);
542+
524543
let mut summary_generation_prompt = VecPromptMsgsDeque::<T, T::SummaryModel>::new();
525544

526545
summary_generation_prompt.extend(
@@ -546,6 +565,8 @@ pub trait Loom<T: Config> {
546565

547566
let summary_response_content = res.into();
548567

568+
trace!("Generated summary: {:?}", summary_response_content);
569+
549570
Ok(summary_response_content.unwrap_or_default())
550571
}
551572

@@ -555,6 +576,8 @@ pub trait Loom<T: Config> {
555576
content: String,
556577
account_id: Option<String>,
557578
) -> ContextMessage<T> {
579+
trace!("Building context message for role: {:?}, content: {}", role, content);
580+
558581
ContextMessage {
559582
role,
560583
content,

0 commit comments

Comments
 (0)