Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for FIM tokens in prompt #62

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
17 changes: 8 additions & 9 deletions crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ fn build_prompt(
) -> Result<String> {
let t = Instant::now();
if fim.enabled {
let mut token_count = context_window;
let mut remaining_token_count = context_window - 3; // account for FIM tokens
let mut before_iter = text.lines_at(pos.line as usize + 1).reversed();
let mut after_iter = text.lines_at(pos.line as usize);
let mut before_line = before_iter.next();
Expand All @@ -332,10 +332,10 @@ fn build_prompt(
} else {
before_line.len()
};
if tokens > token_count {
if tokens > remaining_token_count {
break;
}
token_count -= tokens;
remaining_token_count -= tokens;
before.push(before_line);
}
if let Some(after_line) = after_line {
Expand All @@ -348,10 +348,10 @@ fn build_prompt(
} else {
after_line.len()
};
if tokens > token_count {
if tokens > remaining_token_count {
break;
}
token_count -= tokens;
remaining_token_count -= tokens;
after.push_str(&after_line);
}
before_line = before_iter.next();
Expand All @@ -369,7 +369,7 @@ fn build_prompt(
info!(prompt, build_prompt_ms = time, "built prompt in {time} ms");
Ok(prompt)
} else {
let mut token_count = context_window;
let mut remaining_token_count = context_window;
let mut before = vec![];
let mut first = true;
for mut line in text.lines_at(pos.line as usize + 1).reversed() {
Expand All @@ -387,10 +387,10 @@ fn build_prompt(
} else {
line.len()
};
if tokens > token_count {
if tokens > remaining_token_count {
break;
}
token_count -= tokens;
remaining_token_count -= tokens;
before.push(line);
}
let prompt = before.into_iter().rev().collect::<Vec<_>>().join("");
Expand Down Expand Up @@ -851,4 +851,3 @@ async fn main() {

Server::new(stdin, stdout, socket).serve(service).await;
}