diff --git a/frontend/src/lib/components/Editor.svelte b/frontend/src/lib/components/Editor.svelte index 552315a28d..d24ed3e724 100644 --- a/frontend/src/lib/components/Editor.svelte +++ b/frontend/src/lib/components/Editor.svelte @@ -463,7 +463,7 @@ async provideInlineCompletionItems(model, position, context, token) { abortController?.abort() const textUntilPosition = model.getText( - new vscode.Range(1, 1, position.line, position.character) + new vscode.Range(0, 0, position.line, position.character) ) let items: vscode.InlineCompletionItem[] = [] diff --git a/frontend/src/lib/components/copilot/completion.ts b/frontend/src/lib/components/copilot/completion.ts index 8eccd591fb..e8a669d5e1 100644 --- a/frontend/src/lib/components/copilot/completion.ts +++ b/frontend/src/lib/components/copilot/completion.ts @@ -2,24 +2,15 @@ import type { ChatCompletionMessageParam } from 'openai/resources/chat' import { getNonStreamingCompletion } from './lib' import { codeCompletionLoading } from '$lib/stores' -const systemPrompt = - 'You are a code completion assistant, return the code that should go inside the tags. If you add a line break, take into account the indentation of the code. You can also not return anything if you think the code is already complete.' -const prompt = `complete the following code: -\`\`\`{language} -{before}{after} -\`\`\`` +const systemPrompt = `You are a code completion assistant, return the code that should go instead of the . -const exampleUser = prompt - .replace('{language}', 'python') - .replace( - '{before}', - `def main(number): - # print the number and divide it by 4 - ` - ) - .replace('{after}', `\n\n return number`) -const exampleAssistant = `print(number) - number = number / 4` +Only return the completion tokens. Wrap the completion tokens in a code block (\`\`\`lang\n[completion_tokens]\n\`\`\`). + +Return \`None\` if you think the code is already complete.` +const prompt = `\`\`\`{language} +{before}{after} +\`\`\` +` export async function editorCodeCompletion( before: string, @@ -33,14 +24,6 @@ export async function editorCodeCompletion( role: 'system', content: systemPrompt }, - { - role: 'user', - content: exampleUser - }, - { - role: 'assistant', - content: exampleAssistant - }, { role: 'user', content: prompt @@ -51,8 +34,17 @@ export async function editorCodeCompletion( ] try { - const result = await getNonStreamingCompletion(messages, abortController, 'gpt-3.5-turbo') - const completion = result.match(/(.*)<\/completion>/s)?.[1] || '' + const result = await getNonStreamingCompletion(messages, abortController, 'gpt-3.5-turbo-1106') + + const match = result.match(/```[a-zA-Z]+\n([\s\S]*?)\n```/) + + let completion = match?.[1] || '' + if (completion) { + const previousIndent = before.match(/(\n|^)([ \t]*)$/)?.[2] + if (previousIndent) { + completion = completion.replace(/\n/g, `\n${previousIndent}`) + } + } return completion } catch (err) { diff --git a/frontend/src/lib/components/copilot/lib.ts b/frontend/src/lib/components/copilot/lib.ts index 85c7b365e6..78a21c9664 100644 --- a/frontend/src/lib/components/copilot/lib.ts +++ b/frontend/src/lib/components/copilot/lib.ts @@ -16,8 +16,8 @@ export const SUPPORTED_LANGUAGES = new Set(Object.keys(GEN_CONFIG.prompts)) const openaiConfig: ChatCompletionCreateParamsStreaming = { temperature: 0, - max_tokens: 2048, - model: 'gpt-4-1106-preview', + max_tokens: 4096, + model: 'gpt-4-0125-preview', seed: 42, stream: true, messages: []