add support for chat models

This commit is contained in:
celeste 2023-03-20 07:14:12 -07:00
parent d8ea2243b2
commit 0589820878
3 changed files with 31 additions and 10 deletions

31
main.ts
View file

@ -49,6 +49,15 @@ const DEFAULT_SETTINGS: LoomSettings = {
showExport: false,
};
const CHAT_MODELS = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-4",
"gpt-4-0314",
"gpt-4-32k",
"gpt-4-32k-0314",
];
type Color = "red" | "orange" | "yellow" | "green" | "blue" | "purple" | null;
interface Node {
@ -847,15 +856,27 @@ export default class LoomPlugin extends Plugin {
// complete, or visually display an error and return if that fails
let completions;
try {
completions = (
await this.openai.createCompletion({
if (CHAT_MODELS.contains(this.settings.model)) {
completions = (await this.openai.createChatCompletion({
model: this.settings.model,
prompt,
messages: [
{ role: "assistant", content: prompt },
],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
})
).data.choices.map((choice) => choice.text);
})).data.choices.map((choice) => choice.message?.content);
} else {
completions = (
await this.openai.createCompletion({
model: this.settings.model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
})
).data.choices.map((choice) => choice.text);
}
} catch (e) {
if (e.response.status === 401)
new Notice(

8
package-lock.json generated
View file

@ -12,7 +12,7 @@
"@types/lodash": "^4.14.191",
"gpt3-tokenizer": "^1.1.5",
"lodash": "^4.17.21",
"openai": "^3.1.0",
"openai": "^3.2.0",
"untildify": "^4.0.0",
"uuid": "^9.0.0"
},
@ -1451,9 +1451,9 @@
}
},
"node_modules/openai": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/openai/-/openai-3.1.0.tgz",
"integrity": "sha512-v5kKFH5o+8ld+t0arudj833Mgm3GcgBnbyN9946bj6u7bvel4Yg6YFz2A4HLIYDzmMjIo0s6vSG9x73kOwvdCg==",
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz",
"integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==",
"dependencies": {
"axios": "^0.26.0",
"form-data": "^4.0.0"

View file

@ -27,7 +27,7 @@
"@types/lodash": "^4.14.191",
"gpt3-tokenizer": "^1.1.5",
"lodash": "^4.17.21",
"openai": "^3.1.0",
"openai": "^3.2.0",
"untildify": "^4.0.0",
"uuid": "^9.0.0"
}