Skip to content

Commit

Permalink
Correct symbols range handling in completion scratchpad (#452)
Browse files Browse the repository at this point in the history
* Refactor code completion logic for improved token handling and logging

* Disable DEBUG mode in code_completion_replace.rs for production readiness
  • Loading branch information
JegernOUTT authored Dec 3, 2024
1 parent 8a23874 commit 55e6af1
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/scratchpads/code_completion_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::ast::ast_db::doc_defs;
use crate::ast::ast_structs::AstDefinition;
use crate::scratchpads::completon_rag::retrieve_ast_based_extra_context;

const DEBUG: bool = true;
const DEBUG: bool = false;
const SYSTEM_PROMPT: &str = r#"You are given a code file, <BLOCK_OF_CODE> from that file and an extra context from other files.
An unfinished line in the <BLOCK_OF_CODE> is marked with the <CURSOR>.
Your task is to complete the code after the <CURSOR> by rewriting the <BLOCK_OF_CODE> using the provided context and make the <REWRITTEN_BLOCK_OF_CODE>.
Expand Down Expand Up @@ -199,14 +199,15 @@ async fn prepare_subblock(
}

if let Some(symbol) = get_cursor_symbol_from_doc(ast_service.clone(), cpath, cursor_pos).await {
for idx in symbol.full_line1().saturating_sub(1)..symbol.full_line2() {
let min_rows_to_include = 2;
for idx in symbol.full_line1().saturating_sub(1)..symbol.full_line2() + 1 {
let line = file_text.line(idx).to_string();
tokens_used += tokenizer.count_tokens(&line).unwrap_or(0) as usize;
if idx < cursor_pos.line as usize {
subblock.before_lines.push(line);
} else if idx > cursor_pos.line as usize {
subblock.after_lines_extra.push(line.clone());
if tokens_used <= max_tokens {
if tokens_used <= max_tokens || subblock.after_lines.len() < min_rows_to_include {
subblock.after_lines.push(line);
}
}
Expand Down Expand Up @@ -401,7 +402,7 @@ fn process_n_choices(
.enumerate()
.map(|(i, x)| {
if DEBUG {
info!("unprocessed {i} response_n_choice\n{:?}", x);
info!("unprocessed {i} response_n_choice\n{}", x);
}
if finish_reasons[i] == FinishReason::Stop && !x.contains("```") {
return json!({
Expand Down Expand Up @@ -1030,7 +1031,6 @@ impl ScratchpadAbstract for CodeCompletionReplacePassthroughScratchpad {
info!(" -- /post completion {}ms-- ", completion_ms);

if DEBUG {
info!("chat prompt\n{}", prompt);
info!(
"chat re-encode whole prompt again gives {} tokens",
self.t.count_tokens(prompt.as_str())?
Expand Down

0 comments on commit 55e6af1

Please sign in to comment.