@adminforth/completion-adapter-open-ai-chat-gpt 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +6 -1
- package/index.ts +12 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -31,13 +31,18 @@ export default class CompletionAdapterOpenAIChatGPT {
|
|
|
31
31
|
}),
|
|
32
32
|
});
|
|
33
33
|
const data = yield resp.json();
|
|
34
|
+
if (data.error) {
|
|
35
|
+
return { error: data.error.message };
|
|
36
|
+
}
|
|
34
37
|
return {
|
|
35
38
|
content: data.choices[0].message.content,
|
|
36
39
|
finishReason: data.choices[0].finish_reason,
|
|
37
40
|
};
|
|
38
41
|
});
|
|
39
42
|
this.options = options;
|
|
40
|
-
|
|
43
|
+
}
|
|
44
|
+
validate() {
|
|
45
|
+
if (!this.options.openAiApiKey) {
|
|
41
46
|
throw new Error("openAiApiKey is required");
|
|
42
47
|
}
|
|
43
48
|
}
|
package/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { da } from "@faker-js/faker";
|
|
2
1
|
import type { AdapterOptions } from "./types.js";
|
|
3
2
|
import type { CompletionAdapter } from "adminforth";
|
|
4
3
|
|
|
@@ -9,12 +8,19 @@ export default class CompletionAdapterOpenAIChatGPT
|
|
|
9
8
|
|
|
10
9
|
constructor(options: AdapterOptions) {
|
|
11
10
|
this.options = options;
|
|
12
|
-
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
validate() {
|
|
14
|
+
if (!this.options.openAiApiKey) {
|
|
13
15
|
throw new Error("openAiApiKey is required");
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
complete = async (content: string, stop = ["."], maxTokens = 50)
|
|
19
|
+
complete = async (content: string, stop = ["."], maxTokens = 50): Promise<{
|
|
20
|
+
content?: string;
|
|
21
|
+
finishReason?: string;
|
|
22
|
+
error?: string;
|
|
23
|
+
}> => {
|
|
18
24
|
const resp = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
19
25
|
method: "POST",
|
|
20
26
|
headers: {
|
|
@@ -35,7 +41,9 @@ export default class CompletionAdapterOpenAIChatGPT
|
|
|
35
41
|
}),
|
|
36
42
|
});
|
|
37
43
|
const data = await resp.json();
|
|
38
|
-
|
|
44
|
+
if (data.error) {
|
|
45
|
+
return { error: data.error.message };
|
|
46
|
+
}
|
|
39
47
|
return {
|
|
40
48
|
content: data.choices[0].message.content,
|
|
41
49
|
finishReason: data.choices[0].finish_reason,
|