@adminforth/completion-adapter-open-ai-chat-gpt 1.0.1
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/Changelog.md +5 -0
- package/dist/index.js +44 -0
- package/dist/types.js +1 -0
- package/index.ts +44 -0
- package/package.json +16 -0
- package/tsconfig.json +12 -0
- package/types.ts +24 -0
package/Changelog.md
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export default class CompletionAdapterOpenAIChatGPT {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.complete = (content_1, ...args_1) => __awaiter(this, [content_1, ...args_1], void 0, function* (content, stop = ["."], maxTokens = 50) {
|
|
13
|
+
var _a;
|
|
14
|
+
const resp = yield fetch("https://api.openai.com/v1/chat/completions", {
|
|
15
|
+
method: "POST",
|
|
16
|
+
headers: {
|
|
17
|
+
"Content-Type": "application/json",
|
|
18
|
+
Authorization: `Bearer ${this.options.openAiApiKey}`,
|
|
19
|
+
},
|
|
20
|
+
body: JSON.stringify({
|
|
21
|
+
model: this.options.model || "gpt-4o-mini",
|
|
22
|
+
messages: [
|
|
23
|
+
{
|
|
24
|
+
role: "user",
|
|
25
|
+
content, //param
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
temperature: ((_a = this.options.expert) === null || _a === void 0 ? void 0 : _a.temperature) || 0.7,
|
|
29
|
+
max_tokens: maxTokens,
|
|
30
|
+
stop, //param
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
const data = yield resp.json();
|
|
34
|
+
return {
|
|
35
|
+
content: data.choices[0].message.content,
|
|
36
|
+
finishReason: data.choices[0].finish_reason,
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
this.options = options;
|
|
40
|
+
if (!options.openAiApiKey) {
|
|
41
|
+
throw new Error("openAiApiKey is required");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { da } from "@faker-js/faker";
|
|
2
|
+
import type { AdapterOptions } from "./types.js";
|
|
3
|
+
import type { CompletionAdapter } from "adminforth";
|
|
4
|
+
|
|
5
|
+
export default class CompletionAdapterOpenAIChatGPT
|
|
6
|
+
implements CompletionAdapter
|
|
7
|
+
{
|
|
8
|
+
options: AdapterOptions;
|
|
9
|
+
|
|
10
|
+
constructor(options: AdapterOptions) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
if (!options.openAiApiKey) {
|
|
13
|
+
throw new Error("openAiApiKey is required");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
complete = async (content: string, stop = ["."], maxTokens = 50) => {
|
|
18
|
+
const resp = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": "application/json",
|
|
22
|
+
Authorization: `Bearer ${this.options.openAiApiKey}`,
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({
|
|
25
|
+
model: this.options.model || "gpt-4o-mini",
|
|
26
|
+
messages: [
|
|
27
|
+
{
|
|
28
|
+
role: "user",
|
|
29
|
+
content, //param
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
temperature: this.options.expert?.temperature || 0.7,
|
|
33
|
+
max_tokens: maxTokens,
|
|
34
|
+
stop, //param
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
const data = await resp.json();
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
content: data.choices[0].message.content,
|
|
41
|
+
finishReason: data.choices[0].finish_reason,
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adminforth/completion-adapter-open-ai-chat-gpt",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc && npm version patch",
|
|
9
|
+
"rollout": "npm run build && npm publish --access public",
|
|
10
|
+
"prepare": "npm link adminforth"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"description": ""
|
|
16
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include*/
|
|
4
|
+
"module": "node16", /* Specify what module code is generated. */
|
|
5
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
6
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. */
|
|
7
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
8
|
+
"strict": false, /* Enable all strict type-checking options. */
|
|
9
|
+
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
|
|
10
|
+
},
|
|
11
|
+
"exclude": ["node_modules", "dist", "custom"], /* Exclude files from compilation. */
|
|
12
|
+
}
|
package/types.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface AdapterOptions {
|
|
2
|
+
/**
|
|
3
|
+
* OpenAI API key. Go to https://platform.openai.com/, go to Dashboard -> API keys -> Create new secret key
|
|
4
|
+
* Paste value in your .env file OPENAI_API_KEY=your_key
|
|
5
|
+
* Set openAiApiKey: process.env.OPENAI_API_KEY to access it
|
|
6
|
+
*/
|
|
7
|
+
openAiApiKey: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Model name. Go to https://platform.openai.com/docs/models, select model and copy name.
|
|
11
|
+
* Default is `gpt-4o-mini`. Use e.g. more expensive `gpt-4o` for more powerful model.
|
|
12
|
+
*/
|
|
13
|
+
model?: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Expert settings
|
|
17
|
+
*/
|
|
18
|
+
expert?: {
|
|
19
|
+
/**
|
|
20
|
+
* Temperature (0-1). Lower is more deterministic, higher is more unpredicted creative. Default is 0.7.
|
|
21
|
+
*/
|
|
22
|
+
temperature?: number;
|
|
23
|
+
};
|
|
24
|
+
}
|