Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/config/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::utils::count_tokens;

use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand All @@ -26,14 +25,6 @@ pub enum MessageRole {
User,
}

pub fn within_max_tokens_limit(messages: &[Message], max_tokens: usize) -> Result<()> {
let tokens = num_tokens_from_messages(messages);
if tokens >= max_tokens {
bail!("Exceed max tokens limit")
}
Ok(())
}

pub fn num_tokens_from_messages(messages: &[Message]) -> usize {
let mut num_tokens = 0;
for message in messages.iter() {
Expand Down
17 changes: 15 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ mod conversation;
mod message;
mod role;

use self::conversation::Conversation;
use self::message::Message;
use self::role::Role;
use self::{conversation::Conversation, message::within_max_tokens_limit};

use crate::config::message::num_tokens_from_messages;
use crate::utils::now;

use anyhow::{anyhow, bail, Context, Result};
Expand Down Expand Up @@ -292,7 +293,10 @@ impl Config {
let message = Message::new(content);
vec![message]
};
within_max_tokens_limit(&messages, self.model.1)?;
let tokens = num_tokens_from_messages(&messages);
if tokens >= self.model.1 {
bail!("Exceed max tokens limit")
}

Ok(messages)
}
Expand Down Expand Up @@ -434,6 +438,15 @@ impl Config {
(self.highlight, self.light_theme)
}

pub fn maybe_print_send_tokens(&self, input: &str) {
if self.dry_run {
if let Ok(messages) = self.build_messages(input) {
let tokens = num_tokens_from_messages(&messages);
println!(">>> The following message consumes {tokens} tokens.")
}
}
}

fn open_message_file(&self) -> Result<File> {
let path = Config::messages_file()?;
ensure_parent_exists(&path)?;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn start_directive(
if !stdout().is_terminal() {
config.write().highlight = false;
}
config.read().maybe_print_send_tokens(input);
let output = if no_stream {
let (highlight, light_theme) = config.read().get_render_options();
let output = client.send_message(input)?;
Expand Down
1 change: 1 addition & 0 deletions src/repl/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl ReplCmdHandler {
self.reply.borrow_mut().clear();
return Ok(());
}
self.config.read().maybe_print_send_tokens(&input);
let wg = WaitGroup::new();
let ret = render_stream(
&input,
Expand Down