Skip to content

Commit

Permalink
fix tapestry instance increment, upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
snowmead committed Apr 14, 2024
1 parent b682d38 commit ec25185
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
publish = true
name = "llm-weaver"
version = "0.1.92"
version = "0.1.93"
edition = "2021"
description = "Manage long conversations with any LLM"
readme = "README.md"
Expand Down
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ pub trait Config: Debug + Sized + Clone + Default + Send + Sync + 'static {
///
/// Defaults to `85%`
const TOKEN_THRESHOLD_PERCENTILE: BoundedU8<0, 100> = BoundedU8::new(85).unwrap();
/// Ensures that the maximum completion tokens is at least the minimum response length.
///
/// If the maximum completion tokens is less than the minimum response length, a summary
/// will be generated and a new tapestry fragment will be created.
const MINIMUM_RESPONSE_LENGTH: u64;

/// The LLM to use for generating responses to prompts.
type PromptModel: Llm<Self>;
Expand Down Expand Up @@ -396,15 +401,17 @@ pub trait Loom<T: Config> {
);
req_msgs.append(&mut ctx_msgs);

// New messages are not added here yet since we first calculate if the new messages would
// New messages are not added here yet since we first calculate if the new `msgs` would
// have the tapestry fragment exceed the maximum token limit and require a summary
// generation resulting in a new tapestry fragment.
//
// Either we are starting a new tapestry fragment with the instruction and summary messages
// or we are continuing the current tapestry fragment.
let msgs_tokens = Self::count_tokens_in_messages(msgs.iter());
let does_exceeding_max_token_limit = max_prompt_tokens_limit <=
current_tapestry_fragment.context_tokens.saturating_add(&msgs_tokens);
req_msgs.tokens.saturating_add(&msgs_tokens).saturating_add(
&PromptModelTokens::<T>::from_u64(T::MINIMUM_RESPONSE_LENGTH).unwrap(),
);

let (mut tapestry_fragment_to_persist, was_summary_generated) =
if does_exceeding_max_token_limit {
Expand Down Expand Up @@ -445,9 +452,7 @@ pub trait Loom<T: Config> {
req_msgs.extend(msgs.iter().map(|m| m.clone().into()).collect::<Vec<_>>());

// Tokens available for LLM response which would not exceed maximum token limit
let max_completion_tokens = max_prompt_tokens_limit
.saturating_sub(&tapestry_fragment_to_persist.context_tokens)
.saturating_sub(&req_msgs.tokens);
let max_completion_tokens = max_prompt_tokens_limit.saturating_sub(&req_msgs.tokens);

// Execute prompt to LLM
let response = prompt_llm_config
Expand Down Expand Up @@ -510,6 +515,8 @@ pub trait Loom<T: Config> {
.ctx_msgs_to_prompt_requests(tapestry_fragment.context_messages.as_slice()),
);

println!("prompting summary model");

let res = summary_model_config
.model
.prompt(
Expand Down
2 changes: 2 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ impl TapestryId for TestTapestryId {
pub struct TestApp;
impl Config for TestApp {
const TOKEN_THRESHOLD_PERCENTILE: BoundedU8<0, 100> = BoundedU8::new(70).unwrap();
const MINIMUM_RESPONSE_LENGTH: u64 = 300;

type PromptModel = TestLlm;
type SummaryModel = TestLlm;
type Chest = TestChest;
Expand Down
17 changes: 8 additions & 9 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,16 @@ impl<T: Config> TapestryChestHandler<T> for TapestryChest {
let mut con = client.get_connection()?;
let base_key = &tapestry_id.base_key();

let tapestry_instance = verify_and_get_instance(&mut con, base_key, None).await?;
let mut tapestry_instance =
verify_and_get_instance(&mut con, base_key, None).await?.unwrap_or(0);

redis::transaction(&mut con, &[base_key], |con, pipe| {
let mut tapestry_instance = match tapestry_instance {
Some(instance) => instance,
None => {
pipe.hset(base_key, INSTANCE_COUNT, 1).ignore();
debug!("Saved \"instance_count\" member to {} key", base_key);
// If the tapestry does not exist (i.e. instance is at 0), then set it to 1
if tapestry_instance == 0 {
pipe.hset(base_key, INSTANCE_COUNT, 1).ignore();
debug!("Saved \"instance_count\" member to {} key", base_key);

1
},
tapestry_instance = 1
};

if increment {
Expand Down Expand Up @@ -149,7 +148,7 @@ impl<T: Config> TapestryChestHandler<T> for TapestryChest {
LoomError::from(StorageError::Redis(e))
})?;

Ok(tapestry_instance.unwrap_or(1))
Ok(tapestry_instance)
}

async fn save_tapestry_metadata<
Expand Down

0 comments on commit ec25185

Please sign in to comment.