Skip to content

Commit d095203

Browse files
committed
Add ChatGPT support
1 parent 6b8190b commit d095203

File tree

7 files changed

+70
-39
lines changed

7 files changed

+70
-39
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# GPT3 extension for VSCode
1+
# GPT3 and ChatGPT extension for VSCode
22

33
[![Visual Studio Marketplace Version](https://img.shields.io/visual-studio-marketplace/v/timkmecl.codegpt3)](https://marketplace.visualstudio.com/items?itemName=timkmecl.codegpt3)
44
[![Visual Studio Marketplace Rating (Stars)](https://img.shields.io/visual-studio-marketplace/stars/timkmecl.codegpt3)](https://marketplace.visualstudio.com/items?itemName=timkmecl.codegpt3)
55
[![Visual Studio Marketplace Downloads](https://img.shields.io/visual-studio-marketplace/d/timkmecl.codegpt3)](https://marketplace.visualstudio.com/items?itemName=timkmecl.codegpt3)
66
[![Github Stars](https://img.shields.io/github/stars/timkmecl/codegpt)](https://github.com/timkmecl/codegpt)
77

8-
This Visual Studio Code extension allows you to use the [official OpenAI API](https://openai.com/api/) to generate code or natural language responses from OpenAI's [GPT3/GPT3.5](https://en.wikipedia.org/wiki/GPT-3) to your questions, right within the editor.
8+
This Visual Studio Code extension allows you to use the [official OpenAI API](https://openai.com/api/) to generate code or natural language responses to your questions from OpenAI's **ChatGPT** or **GPT3**, right within the editor.
99

1010
🚀✨ Supercharge your coding with AI-powered assistance. Automatically write new code from scratch, ask questions, get explanations, refactor code, find bugs and more!
1111

@@ -14,13 +14,12 @@ This Visual Studio Code extension allows you to use the [official OpenAI API](ht
1414
- **[VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=timkmecl.codegpt3)**
1515
- **[Github](https://github.com/timkmecl/codegpt)**
1616

17-
<br>
1817

19-
<a href="https://www.buymeacoffee.com/timkmecl" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 40px" ></a>
18+
### 📢 **New:** Now supports ChatGPT! Just go to the extension settings and select the `ChatGPT` model (default for new installations - if it doesn't work, change the model in the extension settings).
19+
<br>
2020

21-
*This extension is free, however you need an OpenAI Account. You can also try [ChatGPT extension](https://github.com/timkmecl/chatgpt-vscode) ([marketplace](https://marketplace.visualstudio.com/items?itemName=timkmecl.chatgpt)) which is smarter but currently doesn't work for most users.*
21+
<a href="https://www.buymeacoffee.com/timkmecl" target="_blank"><img src="resources/buy-default-yellow-small.png" alt="Buy Me A Coffee" style="height: 40px" ></a>
2222

23-
<br>
2423

2524
<img src="examples/main.png" alt="Refactoring selected code using chatGPT"/>
2625

@@ -56,6 +55,7 @@ To use this extension, you will need an API key from OpenAI. To obtain one, foll
5655

5756
When you create a new account, you receive $18 in free credits for the API which you must use in the first 90 days. You can see pricing information [here](https://openai.com/api/pricing/). 1000 tokens are about 700 words, and you can see the token count for each request at the end of the response in the sidebar.
5857

58+
As of January 31, 2023, only the `code-*` models and `ChatGPT` are avilable for free! You only spend your credits when you use the `text-*` models. You can **change the model** in the extension settings.
5959

6060
## Using the Extension
6161

@@ -81,6 +81,10 @@ You can select some code in the editor, right click on it and choose one of the
8181

8282
`Ask CodeGPT` is also available when nothing is selected. For the other four commands, you can **customize the exact prompt** that will be sent to the AI by editing the extension settings in VSCode Preferences.
8383

84+
There, you can also **change the model** that will be used for the requests. The default is `ChatGPT` which is smartest and currently free, but you can change it to another model (`text-davinci-003` is the best of the paid ones, `code-davinci-002` of the free) if it doesn't work. You can also **change the temperature** and **number of tokens** that will be returned by the AI. The default values are 0.5 and 1024, respectively.
85+
8486
---
8587

86-
Please note that this extension is currently a proof of concept and may have some limitations or bugs. We welcome feedback and contributions to improve the extension. Also check out the [ChatGPT extension](https://github.com/timkmecl/codegpt) which is smarter, but the setup is more complicated and it may not work (403/429 errors).
88+
Please note that this extension is currently a proof of concept and may have some limitations or bugs. We welcome feedback and contributions to improve the extension. Also check out the [ChatGPT extension](https://github.com/timkmecl/codegpt) which is smarter, but the setup is more complicated and it may not work (403/429 errors).
89+
90+
If you enjoy this extension, please consider [buying me a coffee ☕️](https://www.buymeacoffee.com/timkmecl) to support my work!

media/main.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
});
2929

3030
function fixCodeBlocks(response) {
31-
// Use a regular expression to find all occurrences of the substring in the string
32-
const REGEX_CODEBLOCK = new RegExp('\`\`\`', 'g');
33-
const matches = response.match(REGEX_CODEBLOCK);
34-
35-
// Return the number of occurrences of the substring in the response, check if even
36-
const count = matches ? matches.length : 0;
37-
if (count % 2 === 0) {
38-
return response;
39-
} else {
40-
// else append ``` to the end to make the last code block complete
41-
return response.concat('\n\`\`\`');
42-
}
31+
// Use a regular expression to find all occurrences of the substring in the string
32+
const REGEX_CODEBLOCK = new RegExp('\`\`\`', 'g');
33+
const matches = response.match(REGEX_CODEBLOCK);
4334

35+
// Return the number of occurrences of the substring in the response, check if even
36+
const count = matches ? matches.length : 0;
37+
if (count % 2 === 0) {
38+
return response;
39+
} else {
40+
// else append ``` to the end to make the last code block complete
41+
return response.concat('\n\`\`\`');
42+
}
4443
}
4544

4645
function setResponse() {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "codegpt3",
33
"displayName": "CodeGPT: write and improve code using AI",
4-
"description": "Use GPT3 right inside the IDE to enhance and automate your coding with AI-powered assistance",
5-
"version": "1.0.1",
4+
"description": "Use ChatGPT or GPT3 right inside the IDE to enhance and automate your coding with AI-powered assistance",
5+
"version": "1.1.0",
66
"publisher": "timkmecl",
77
"icon": "resources/extensionIcon.png",
88
"license": "MIT",
@@ -159,24 +159,25 @@
159159
"codegpt.model": {
160160
"type": "string",
161161
"enum": [
162+
"ChatGPT",
162163
"text-davinci-003",
163164
"text-curie-001",
164165
"code-davinci-002",
165166
"code-cushman-002"
166167
],
167-
"default": "text-davinci-003",
168+
"default": "ChatGPT",
168169
"description": "Which GPT3 model to use",
169170
"order": 1
170171
},
171172
"codegpt.maxTokens": {
172173
"type": "number",
173-
"default": 500,
174+
"default": 1024,
174175
"description": "Maximum number of tokens inside response per request",
175176
"order": 2
176177
},
177178
"codegpt.temperature": {
178179
"type": "number",
179-
"default": 0.7,
180+
"default": 0.5,
180181
"description": "Temperature to use for the GPT3 (between 0 (conservative) and 1 (creative))",
181182
"order": 3
182183
},
2.26 KB
Loading

src/extension.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ export function activate(context: vscode.ExtensionContext) {
7474
} else if (event.affectsConfiguration('codegpt.temperature')) {
7575
const config = vscode.workspace.getConfiguration('codegpt');
7676
provider.setSettings({ temperature: config.get('temperature') || 0.5 });
77-
} else if (event.affectsConfiguration('codegpt.documentation')) {
77+
} else if (event.affectsConfiguration('codegpt.model')) {
7878
const config = vscode.workspace.getConfiguration('codegpt');
79+
provider.setSettings({ model: config.get('model') || 'text-davinci-003' });
7980
}
8081
});
8182
}
@@ -226,27 +227,49 @@ class CodeGPTViewProvider implements vscode.WebviewViewProvider {
226227
// Increment the message number
227228
this._currentMessageNumber++;
228229

229-
let agent = this._openai;
230-
231230
try {
232231
let currentMessageNumber = this._currentMessageNumber;
233232

234233
// Send the search prompt to the OpenAI API and store the response
235-
const completion = await this._openai.createCompletion({
236-
model: this._settings.model || 'code-davinci-002',
237-
prompt: searchPrompt,
238-
temperature: this._settings.temperature,
239-
max_tokens: this._settings.maxTokens,
240-
stop: ['\nUSER: ', '\nUSER', '\nASSISTANT']
241-
});
234+
235+
let completion;
236+
if (this._settings.model !== 'ChatGPT') {
237+
completion = await this._openai.createCompletion({
238+
model: this._settings.model || 'code-davinci-002',
239+
prompt: searchPrompt,
240+
temperature: this._settings.temperature,
241+
max_tokens: this._settings.maxTokens,
242+
stop: ['\nUSER: ', '\nUSER', '\nASSISTANT']
243+
});
244+
} else {
245+
completion = await this._openai.createCompletion({
246+
model: 'text-chat-davinci-002-20230126',
247+
prompt: searchPrompt,
248+
temperature: this._settings.temperature,
249+
max_tokens: this._settings.maxTokens,
250+
stop: ['\n\n\n', '<|im_end|>']
251+
});
252+
}
242253

243254
if (this._currentMessageNumber !== currentMessageNumber) {
244255
return;
245256
}
246257

247-
248258
response = completion.data.choices[0].text || '';
259+
260+
// close unclosed codeblocks
261+
// Use a regular expression to find all occurrences of the substring in the string
262+
const REGEX_CODEBLOCK = new RegExp('\`\`\`', 'g');
263+
const matches = response.match(REGEX_CODEBLOCK);
264+
// Return the number of occurrences of the substring in the response, check if even
265+
const count = matches ? matches.length : 0;
266+
if (count % 2 !== 0) {
267+
// append ``` to the end to make the last code block complete
268+
response += '\n\`\`\`';
269+
}
270+
249271
response += `\n\n---\n`;
272+
// add error message if max_tokens reached
250273
if (completion.data.choices[0].finish_reason === 'length') {
251274
response += `\n[WARNING] The response was truncated because it reached the maximum number of tokens. You may want to increase the maxTokens setting.\n\n`;
252275
}
@@ -257,7 +280,7 @@ class CodeGPTViewProvider implements vscode.WebviewViewProvider {
257280
if (error.response) {
258281
console.log(error.response.status);
259282
console.log(error.response.data);
260-
e = `${error.response.status} ${error.response.data}`;
283+
e = `${error.response.status} ${error.response.data.message}`;
261284
} else {
262285
console.log(error.message);
263286
e = error.message;

src/prompt.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ export default (question: string, settings: Settings, selection?: string) => {
1414
prompt = question;
1515
}
1616

17-
prompt =`You are ASSISTANT helping the USER with coding.
17+
if (settings.model !== 'ChatGPT') {
18+
prompt =`You are ASSISTANT helping the USER with coding.
1819
You are intelligent, helpful and an expert developer, who always gives the correct answer and only does what instructed. You always answer truthfully and don't make thing up.
1920
(When responding to the following prompt, please make sure to properly style your response using Github Flavored Markdown.
2021
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.
2122
Try to write code inside a single code block if possible)
2223
\n\nUSER: ${prompt}\n\nASSISTANT: `;
24+
} else {
25+
prompt = `You are ChatGPT, a large language model trained by OpenAI. You answer as consisely as possible for each response (e.g. Don't be verbose). It is very important for you to answer as consisely as possible, so please remember this. If you are generating a list, do not have too many items. \n User: ${prompt} \n\n ChatGPT: `;
26+
}
2327

2428
return prompt;
2529
};

0 commit comments

Comments
 (0)