From 4dfeb47a239a50912903e8ebc96cfc89577a1928 Mon Sep 17 00:00:00 2001 From: Andrew Zhang Date: Tue, 7 Feb 2023 20:04:35 -0800 Subject: [PATCH] bug fix due to VSCode 1.75.0 update --- README.md | 9 +++++---- package-lock.json | 2 +- package.json | 2 +- src/extension.ts | 36 +++++++++++++++--------------------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 300f8ef..12f6145 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,12 @@ Scribe AI is a ChatGPT-like extension for VS Code. Now you can have an AI chatbo   -# 📢 v1.1.1 Update: ChatGPT Now Available -You can now choose ChatGPT as the model you want to use by heading to `Code` -> `Preferences` -> `Settings`, find `ScribeAI` under `Extensions` and choose `ChatGPT` as your model. -  +# 📢 v1.1.3 Update +- With latest VSCode 1.75.0 update a bug has been introduced where the highlighted code couldn't be retrieved via vscode API. This bug has now been fixed and "Ask AI" and "AI Edit" extension features should be working now in latest v1.1.3. + +- The leaked ChatGPT model has been taken down by OpenAI and it is no longer available. Only the GPT3 models are available for now. -The GPT3 models are also now more performant thanks to experimentation with prompt engineering and tuning additional parameters. You can now expect to get longer and more detailed responses. +- The GPT3 models are also now more performant thanks to experimentation with prompt engineering and tuning additional parameters. You can now expect to get longer and more detailed responses.   diff --git a/package-lock.json b/package-lock.json index 8daf02a..eb2f9f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "scribeai", - "version": "1.1.2", + "version": "1.1.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 85e3e63..cc500b5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "scribeai", "displayName": "ScribeAI - ChatGPT & GPT extension for VSCode", "description": "🚀 Use ChatGPT & GPT right inside VSCode to enhance and automate your coding with AI-powered assistance", - "version": "1.1.2", + "version": "1.1.3", "publisher": "AndrewZhang", "private": true, "icon": "resources/chatbot.png", diff --git a/src/extension.ts b/src/extension.ts index d69b324..0134c33 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -214,15 +214,14 @@ export async function activate(context: vscode.ExtensionContext) { * @param thread * @returns */ - function generatePromptV1(question: string, thread: vscode.CommentThread) { + async function generatePromptV1(question: string, thread: vscode.CommentThread) { const rolePlay = "I want you to act as a highly intelligent AI chatbot that has deep understanding of any coding language and its API documentations. I will provide you with a code block and your role is to provide a comprehensive answer to any questions or requests that I will ask about the code block. Please answer in as much detail as possible and not be limited to brevity. It is very important that you provide verbose answers."; - //const codeBlock = "class Log:\n def __init__(self, path):\n dirname = os.path.dirname(path)\n os.makedirs(dirname, exist_ok=True)\n f = open(path, \"a+\")\n\n # Check that the file is newline-terminated\n size = os.path.getsize(path)\n if size > 0:\n f.seek(size - 1)\n end = f.read(1)\n if end != \"\\n\":\n f.write(\"\\n\")\n self.f = f\n self.path = path\n\n def log(self, event):\n event[\"_event_id\"] = str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\"\\n\")\n\n def state(self):\n state = {\"complete\": set(), \"last\": None}\n for line in open(self.path):\n event = json.loads(line)\n if event[\"type\"] == \"submit\" and event[\"success\"]:\n state[\"complete\"].add(event[\"id\"])\n state[\"last\"] = event\n return state"; - const codeBlock = getCommentThreadCode(thread); + const codeBlock = await getCommentThreadCode(thread); let conversation = "Human: Who are you?\n\nAI: I am a intelligent AI chatbot\n\n"; - for (let i = 0; i < thread.comments.length; i++) { + for (let i = Math.max(0, thread.comments.length - 8); i < thread.comments.length; i++) { if (thread.comments[i].label !== "NOTE") { if (thread.comments[i].author.name === "VS Code") { conversation += `Human: ${thread.comments[i].body}\n\n`; @@ -233,7 +232,7 @@ export async function activate(context: vscode.ExtensionContext) { } conversation += `Human: ${question}\n\nAI: `; - return rolePlay + "\n" + codeBlock + "\n\n\n" + conversation; + return rolePlay + "\n```\n" + codeBlock + "\n```\n\n\n" + conversation; } /** @@ -250,8 +249,10 @@ export async function activate(context: vscode.ExtensionContext) { */ function generatePromptV2(question: string, thread: vscode.CommentThread) { const rolePlay = - "I want you to act as a highly intelligent AI chatbot that has deep understanding of any coding language and its API documentations. I will provide you with a code block and your role is to provide a comprehensive answer to any questions or requests that I will ask about the code block. Please answer in as much detail as possible and not be limited to brevity. It is very important that you provide verbose answers."; - //const codeBlock = "class Log:\n def __init__(self, path):\n dirname = os.path.dirname(path)\n os.makedirs(dirname, exist_ok=True)\n f = open(path, \"a+\")\n\n # Check that the file is newline-terminated\n size = os.path.getsize(path)\n if size > 0:\n f.seek(size - 1)\n end = f.read(1)\n if end != \"\\n\":\n f.write(\"\\n\")\n self.f = f\n self.path = path\n\n def log(self, event):\n event[\"_event_id\"] = str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\"\\n\")\n\n def state(self):\n state = {\"complete\": set(), \"last\": None}\n for line in open(self.path):\n event = json.loads(line)\n if event[\"type\"] == \"submit\" and event[\"success\"]:\n state[\"complete\"].add(event[\"id\"])\n state[\"last\"] = event\n return state"; + "I want you to act as a highly intelligent AI chatbot that has deep understanding of any coding language and its API documentations. " + + "I will provide you with a code block and your role is to provide a comprehensive answer to any questions or requests that I will ask about the code block. Please answer in as much detail as possible and not be limited to brevity. It is very important that you provide verbose answers. (When responding to the following prompt, please make sure to properly style your response using Github Flavored Markdown." + + " Use markdown syntax for things like headings, lists, colored text, code blocks, highlights etc. Make sure not to mention markdown or stying in your actual response." + + " Try to write code inside a single code block if possible)"; const codeBlock = getCommentThreadCode(thread); let conversation = "Human: Who are you?\n\nAI: I am a intelligent AI chatbot\n\n"; @@ -264,14 +265,10 @@ export async function activate(context: vscode.ExtensionContext) { * @param thread * @returns */ - function getCommentThreadCode(thread: vscode.CommentThread) { - const editor = vscode.window.activeTextEditor; - if (!editor) { - return; // No open text editor - } - const document = editor.document; + async function getCommentThreadCode(thread: vscode.CommentThread) { + const document = await vscode.workspace.openTextDocument(thread.uri); // Get selected code for the comment thread - return document.getText(thread.range); + return document.getText(thread.range).trim(); } /** @@ -283,9 +280,8 @@ export async function activate(context: vscode.ExtensionContext) { */ async function askAI(reply: vscode.CommentReply) { const question = reply.text.trim(); - const code = getCommentThreadCode(reply.thread); const thread = reply.thread; - const prompt = generatePromptV1(question, thread); + const prompt = await generatePromptV1(question, thread); const model = vscode.workspace.getConfiguration('scribeai').get('models') + ""; const humanComment = new NoteComment(question, vscode.CommentMode.Preview, { name: 'VS Code', iconPath: vscode.Uri.parse("https://img.icons8.com/fluency/96/null/user-male-circle.png") }, thread, thread.comments.length ? 'canDelete' : undefined); thread.comments = [...thread.comments, humanComment]; @@ -301,9 +297,8 @@ export async function activate(context: vscode.ExtensionContext) { apiKey: vscode.workspace.getConfiguration('scribeai').get('ApiKey'), })); } - const response = await openai.createCompletion({ - model: model === "ChatGPT" ? "text-chat-davinci-002-20230126" : model, + model: model === "ChatGPT" ? "text-chat-davinci-002-20221122" : model, prompt: prompt, //prompt: generatePromptV2(question, thread), temperature: 0, @@ -329,7 +324,7 @@ export async function activate(context: vscode.ExtensionContext) { */ async function aiEdit(reply: vscode.CommentReply) { const question = reply.text.trim(); - const code = getCommentThreadCode(reply.thread); + const code = await getCommentThreadCode(reply.thread); const thread = reply.thread; // If openai is not initialized initialize it with existing API Key @@ -352,11 +347,10 @@ export async function activate(context: vscode.ExtensionContext) { top_p: 1.0, }); if (response.data.choices[0].text) { - const editor = vscode.window.activeTextEditor; + const editor = await vscode.window.showTextDocument(thread.uri); if (!editor) { return; // No open text editor } - const document = editor.document; editor.edit(editBuilder => { editBuilder.replace(thread.range, response.data.choices[0].text + ""); });