doomiaichat 1.0.0

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.
@@ -0,0 +1,20 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from "events";
3
+ import { AxiosRequestConfig } from "axios";
4
+ export default class AIChat extends EventEmitter {
5
+ private readonly chatRobot;
6
+ private readonly chatModel;
7
+ private readonly maxtoken;
8
+ private readonly temperature;
9
+ /**
10
+ *
11
+ * @param apiKey 调用OpenAI 的key
12
+ * @param apiOption
13
+ */
14
+ constructor(apiKey: string, apiOption?: any);
15
+ /**
16
+ * 向OpenAI发送一个聊天请求
17
+ * @param {*} chatText
18
+ */
19
+ chatRequest(chatText: string | any, axiosOption: AxiosRequestConfig): Promise<any>;
20
+ }
package/dist/index.js ADDED
@@ -0,0 +1,50 @@
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
+ import { Configuration, OpenAIApi } from "openai";
11
+ import { EventEmitter } from "events";
12
+ export default class AIChat extends EventEmitter {
13
+ /**
14
+ *
15
+ * @param apiKey 调用OpenAI 的key
16
+ * @param apiOption
17
+ */
18
+ constructor(apiKey, apiOption = {}) {
19
+ super();
20
+ this.chatRobot = new OpenAIApi(new Configuration({ apiKey }));
21
+ this.chatModel = apiOption.model || 'gpt-3.5-turbo';
22
+ this.maxtoken = apiOption.maxtoken || 1024;
23
+ this.temperature = apiOption.temperature || 0.9;
24
+ }
25
+ /**
26
+ * 向OpenAI发送一个聊天请求
27
+ * @param {*} chatText
28
+ */
29
+ chatRequest(chatText, axiosOption) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ if (!chatText)
32
+ return { successed: false, errcode: 2, errmsg: '缺失聊天的内容' };
33
+ if (!this.chatRobot)
34
+ return { successed: false, errcode: 1, errmsg: '聊天机器人无效' };
35
+ try {
36
+ const response = yield this.chatRobot.createChatCompletion({
37
+ model: this.chatModel,
38
+ messages: chatText,
39
+ temperature: this.temperature,
40
+ max_tokens: this.maxtoken
41
+ // n: this.replyCounts
42
+ }, axiosOption);
43
+ return { successed: true, message: response.data.choices };
44
+ }
45
+ catch (error) {
46
+ return { successed: false, error };
47
+ }
48
+ });
49
+ }
50
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "doomiaichat",
3
+ "version": "1.0.0",
4
+ "description": "Doomisoft OpenAI",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc"
8
+ },
9
+ "keywords": [
10
+ "ChatGpt"
11
+ ],
12
+ "author": "Stephen.Shen",
13
+ "license": "ISC",
14
+ "devDependencies": {
15
+ "@types/node": "^18.15.0",
16
+ "typescript": "^4.9.5"
17
+ },
18
+ "dependencies": {
19
+ "openai": "^3.2.1"
20
+ }
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { Configuration, OpenAIApi, CreateChatCompletionResponse } from "openai"
2
+ import { EventEmitter } from "events";
3
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
4
+ export default class AIChat extends EventEmitter {
5
+ private readonly chatRobot: OpenAIApi;
6
+ private readonly chatModel: string;
7
+ private readonly maxtoken: number;
8
+ private readonly temperature: number;
9
+ /**
10
+ *
11
+ * @param apiKey 调用OpenAI 的key
12
+ * @param apiOption
13
+ */
14
+ constructor(apiKey: string, apiOption: any = {}) {
15
+ super();
16
+ this.chatRobot = new OpenAIApi(new Configuration({ apiKey }))
17
+ this.chatModel = apiOption.model || 'gpt-3.5-turbo';
18
+ this.maxtoken = apiOption.maxtoken || 1024;
19
+ this.temperature = apiOption.temperature || 0.9;
20
+ }
21
+
22
+ /**
23
+ * 向OpenAI发送一个聊天请求
24
+ * @param {*} chatText
25
+ */
26
+ public async chatRequest(chatText: string | any, axiosOption: AxiosRequestConfig):Promise<any> {
27
+ if (!chatText) return { successed: false, errcode: 2, errmsg: '缺失聊天的内容' }
28
+ if (!this.chatRobot) return { successed: false, errcode: 1, errmsg: '聊天机器人无效' }
29
+ try{
30
+ const response: AxiosResponse<CreateChatCompletionResponse, any>
31
+ = await this.chatRobot.createChatCompletion({
32
+ model: this.chatModel,
33
+ messages: chatText,
34
+ temperature: this.temperature,
35
+ max_tokens: this.maxtoken
36
+ // n: this.replyCounts
37
+ }, axiosOption);
38
+ return { successed: true, message: response.data.choices }
39
+ }catch(error){
40
+ return { successed: false,error }
41
+ }
42
+
43
+ }
44
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "files": [
3
+ "src/index.ts"
4
+ ],
5
+ "compilerOptions": {
6
+ "target": "es2015",
7
+ "module": "es2015",
8
+ "declaration": true,
9
+ "outDir": "./dist",
10
+ "noEmit": false,
11
+ "strict": true,
12
+ "noImplicitAny": true,
13
+ "strictNullChecks": true,
14
+ "strictFunctionTypes": true,
15
+ "strictBindCallApply": true,
16
+ "strictPropertyInitialization": true,
17
+ "noImplicitThis": true,
18
+ "alwaysStrict": true,
19
+ "noUnusedLocals": true,
20
+ "noUnusedParameters": true,
21
+ "noImplicitReturns": true,
22
+ "noFallthroughCasesInSwitch": true,
23
+ "noUncheckedIndexedAccess": true,
24
+ "noImplicitOverride": true,
25
+ "moduleResolution":"node",
26
+ "noPropertyAccessFromIndexSignature": true,
27
+ "esModuleInterop": true,
28
+ "forceConsistentCasingInFileNames": true,
29
+ "skipLibCheck": true
30
+ }
31
+ }