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 20, 2024
1 parent 585ea3a commit e531c61
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 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 @@ -592,6 +592,9 @@ impl Backend {
async fn get_completions(&self, params: CompletionParams) -> Result<CompletionResult> {
let request_id = Uuid::new_v4();
let span = info_span!("completion_request", %request_id);
self.client
.log_message(MessageType::INFO, format!("get_completions"))
.await;
async move {
let document_map = self.document_map.read().await;

Expand Down Expand Up @@ -734,10 +737,14 @@ impl LanguageServer for Backend {

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.to_string();
self.client
.log_message(MessageType::INFO, format!("{uri} changed"))
.await;
// ignore the output scheme
if uri.starts_with("output:") {
return;
}
let mut document_map = self.document_map.write().await;
self.client
.log_message(MessageType::INFO, format!("{uri} changed"))
.await;
let doc = document_map.get_mut(&uri);
if let Some(doc) = doc {
for change in &params.content_changes {
Expand Down Expand Up @@ -851,4 +858,3 @@ async fn main() {

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

0 comments on commit e531c61

Please sign in to comment.