Skip to content

Commit

Permalink
Account for FIM tokens in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
HennerM committed Jan 23, 2024
1 parent 585ea3a commit 50e52e1
Showing 1 changed file with 8 additions and 9 deletions.
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;
}

0 comments on commit 50e52e1

Please sign in to comment.