History

Ensure the inline assistant works with gpt-3.5

Antonio Scandurra committed 3 years ago

Commit fdbf4681 parent 27c90f11 file · +118 −19Browse files

Unified · Split

crates/ai/src/assistant.rs+118−19
@@ -344,7 +344,7 @@ impl AssistantPanel { · 343 unmodified lines
344344 let assist_id = inline_assistant.read(cx).id;
345345 match event {
346346 InlineAssistantEvent::Confirmed { prompt } => {
347− self.generate(assist_id, prompt, cx);
347+ self.confirm_inline_assist(assist_id, prompt, cx);
348348 }
349349 InlineAssistantEvent::Canceled => {
350350 self.complete_inline_assist(assist_id, true, cx);
@@ -398,7 +398,12 @@ impl AssistantPanel { · 47 unmodified lines
398398 }
399399 }
400400
401− fn generate(&mut self, inline_assist_id: usize, user_prompt: &str, cx: &mut ViewContext<Self>) {
401+ fn confirm_inline_assist(
402+ &mut self,
403+ inline_assist_id: usize,
404+ user_prompt: &str,
405+ cx: &mut ViewContext<Self>,
406+ ) {
402407 let api_key = if let Some(api_key) = self.api_key.borrow().clone() {
403408 api_key
404409 } else {
@@ -473,26 +478,51 @@ impl AssistantPanel { · 68 unmodified lines
473478 .language_at(range.start)
474479 .map(|language| language.name());
475480 let language_name = language_name.as_deref().unwrap_or("");
481+ let model = settings::get::<AssistantSettings>(cx)
482+ .default_open_ai_model
483+ .clone();
476484
477485 let mut prompt = String::new();
478486 writeln!(prompt, "You're an expert {language_name} engineer.").unwrap();
479− writeln!(
480− prompt,
481− "You're currently working inside an editor on this code:"
482− )
483− .unwrap();
484487 match pending_assist.kind {
485488 InlineAssistKind::Refactor => {
489+ writeln!(
490+ prompt,
491+ "You're currently working inside an editor on this code:"
492+ )
493+ .unwrap();
494+ writeln!(prompt, "```{language_name}").unwrap();
495+ for chunk in snapshot.text_for_range(Anchor::min()..Anchor::max()) {
496+ write!(prompt, "{chunk}").unwrap();
497+ }
498+ writeln!(prompt, "```").unwrap();
499+
500+ writeln!(
501+ prompt,
502+ "In particular, the user has selected the following code:"
503+ )
504+ .unwrap();
486505 writeln!(prompt, "```{language_name}").unwrap();
487506 writeln!(prompt, "{normalized_selected_text}").unwrap();
488507 writeln!(prompt, "```").unwrap();
508+ writeln!(prompt).unwrap();
489509 writeln!(
490510 prompt,
491− "Modify the code given the user prompt: {user_prompt}"
511+ "Modify the selected code given the user prompt: {user_prompt}"
512+ )
513+ .unwrap();
514+ writeln!(
515+ prompt,
516+ "You MUST reply only with the edited selected code, not the entire file."
492517 )
493518 .unwrap();
494519 }
495520 InlineAssistKind::Insert => {
521+ writeln!(
522+ prompt,
523+ "You're currently working inside an editor on this code:"
524+ )
525+ .unwrap();
496526 writeln!(prompt, "```{language_name}").unwrap();
497527 for chunk in snapshot.text_for_range(Anchor::min()..range.start) {
498528 write!(prompt, "{chunk}").unwrap();
@@ -517,11 +547,11 @@ impl AssistantPanel { · 18 unmodified lines
517547 }
518548 }
519549 writeln!(prompt, "Your answer MUST always be valid {language_name}.").unwrap();
520− writeln!(prompt, "DO NOT wrap your response in Markdown blocks.").unwrap();
550+ writeln!(prompt, "Always wrap your response in a Markdown codeblock.").unwrap();
521551 writeln!(prompt, "Never make remarks, always output code.").unwrap();
522552
523553 let request = OpenAIRequest {
524− model: "gpt-4".into(),
554+ model: model.full_name().into(),
525555 messages: vec![RequestMessage {
526556 role: Role::User,
527557 content: prompt,
@@ -563,23 +593,92 @@ impl AssistantPanel { · 35 unmodified lines
563593 indentation_text = "";
564594 };
565595
566− let mut new_text = indentation_text
567− .repeat(indentation_len.saturating_sub(selection_start.column) as usize);
596+ let mut inside_first_line = true;
597+ let mut starts_with_fenced_code_block = None;
598+ let mut has_pending_newline = false;
599+ let mut new_text = String::new();
600+
568601 while let Some(message) = messages.next().await {
569602 let mut message = message?;
570− if let Some(choice) = message.choices.pop() {
603+ if let Some(mut choice) = message.choices.pop() {
604+ if has_pending_newline {
605+ has_pending_newline = false;
606+ choice
607+ .delta
608+ .content
609+ .get_or_insert(String::new())
610+ .insert(0, '\n');
611+ }
612+
613+ // Buffer a trailing codeblock fence. Note that we don't stop
614+ // right away because this may be an inner fence that we need
615+ // to insert into the editor.
616+ if starts_with_fenced_code_block.is_some()
617+ && choice.delta.content.as_deref() == Some("\n```")
618+ {
619+ new_text.push_str("\n```");
620+ continue;
621+ }
622+
623+ // If this was the last completion and we started with a codeblock
624+ // fence and we ended with another codeblock fence, then we can
625+ // stop right away. Otherwise, whatever text we buffered will be
626+ // processed normally.
627+ if choice.finish_reason.is_some()
628+ && starts_with_fenced_code_block.unwrap_or(false)
629+ && new_text == "\n```"
630+ {
631+ break;
632+ }
633+
571634 if let Some(text) = choice.delta.content {
635+ // Never push a newline if there's nothing after it. This is
636+ // useful to detect if the newline was pushed because of a
637+ // trailing codeblock fence.
638+ let text = if let Some(prefix) = text.strip_suffix('\n') {
639+ has_pending_newline = true;
640+ prefix
641+ } else {
642+ text.as_str()
643+ };
644+
645+ if text.is_empty() {
646+ continue;
647+ }
648+
572649 let mut lines = text.split('\n');
573− if let Some(first_line) = lines.next() {
574− new_text.push_str(&first_line);
650+ if let Some(line) = lines.next() {
651+ if starts_with_fenced_code_block.is_none() {
652+ starts_with_fenced_code_block =
653+ Some(line.starts_with("```"));
654+ }
655+
656+ // Avoid pushing the first line if it's the start of a fenced code block.
657+ if !inside_first_line || !starts_with_fenced_code_block.unwrap()
658+ {
659+ new_text.push_str(&line);
660+ }
575661 }
576662
577663 for line in lines {
578− new_text.push('\n');
579− new_text.push_str(
580− &indentation_text.repeat(indentation_len as usize),
581− );
664+ if inside_first_line && starts_with_fenced_code_block.unwrap() {
665+ // If we were inside the first line and that line was the
666+ // start of a fenced code block, we just need to push the
667+ // leading indentation of the original selection.
668+ new_text.push_str(&indentation_text.repeat(
669+ indentation_len.saturating_sub(selection_start.column)
670+ as usize,
671+ ));
672+ } else {
673+ // Otherwise, we need to push a newline and the base indentation.
674+ new_text.push('\n');
675+ new_text.push_str(
676+ &indentation_text.repeat(indentation_len as usize),
677+ );
678+ }
679+
582680 new_text.push_str(line);
681+ inside_first_line = false;
583682 }
584683 }
585684 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t