doomiaichat 7.0.0 → 7.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/dist/aimp.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import GptBase from "./gptbase";
2
+ import { ChatReponse } from './declare';
3
+ export default class AIMiddlePlatform extends GptBase {
4
+ protected apikey: string;
5
+ protected agent: {
6
+ endpoint: string;
7
+ agentid: string;
8
+ };
9
+ /**
10
+ *
11
+ * @param apikey 调用AI中台 的key
12
+ * @param agent 智能体信息
13
+ */
14
+ constructor(apikey: string, agent: {
15
+ endpoint: string;
16
+ agentid: string;
17
+ });
18
+ /**
19
+ * 非流式传输聊天请求
20
+ * @param chatText
21
+ * @param callChatOption
22
+ * @param axiosOption
23
+ */
24
+ chatRequest(chatText: string, callChatOption: any, axiosOption?: any): Promise<ChatReponse>;
25
+ /**
26
+ * 流式传输聊天请求
27
+ * @param chatText
28
+ * @param callChatOption
29
+ * @param attach
30
+ * @param axiosOption
31
+ * @returns
32
+ */
33
+ chatRequestInStream(chatText: string | Array<any>, callChatOption: any, attach?: any, axiosOption?: any): Promise<any>;
34
+ }
package/dist/aimp.js ADDED
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ var __importDefault = (this && this.__importDefault) || function (mod) {
19
+ return (mod && mod.__esModule) ? mod : { "default": mod };
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ /**
23
+ * 接入AI平台的中间层
24
+ */
25
+ const axios_1 = __importDefault(require("axios"));
26
+ const declare_1 = require("./declare");
27
+ const gptbase_1 = __importDefault(require("./gptbase"));
28
+ const stream_1 = require("stream");
29
+ class AIMiddlePlatform extends gptbase_1.default {
30
+ /**
31
+ *
32
+ * @param apikey 调用AI中台 的key
33
+ * @param agent 智能体信息
34
+ */
35
+ constructor(apikey, agent) {
36
+ super();
37
+ this.apikey = apikey;
38
+ this.agent = agent;
39
+ }
40
+ /**
41
+ * 非流式传输聊天请求
42
+ * @param chatText
43
+ * @param callChatOption
44
+ * @param axiosOption
45
+ */
46
+ chatRequest(chatText, callChatOption, axiosOption = {}) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ if (!chatText)
49
+ this.emit('chaterror', { successed: false, error: 'no text in chat' });
50
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
51
+ const opts = Object.assign({ headers: {
52
+ 'Content-Type': 'application/json',
53
+ 'authorization': `Bearer ${this.apikey}`
54
+ }, method: 'post', url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`, data: {
55
+ question: chatText,
56
+ session_id: callChatOption.session_id,
57
+ stream: false
58
+ } }, axiosOption);
59
+ const response = yield (0, declare_1.request)(opts);
60
+ if (!response.successed)
61
+ return { successed: false, error: 'failed' };
62
+ return { successed: true, message: response.data.answer };
63
+ });
64
+ }
65
+ /**
66
+ * 流式传输聊天请求
67
+ * @param chatText
68
+ * @param callChatOption
69
+ * @param attach
70
+ * @param axiosOption
71
+ * @returns
72
+ */
73
+ chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
74
+ var _a, e_1, _b, _c;
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ if (!chatText)
77
+ this.emit('chaterror', { successed: false, error: 'no text in chat' });
78
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
79
+ let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
80
+ try {
81
+ const opts = Object.assign({ headers: {
82
+ 'Content-Type': 'application/json',
83
+ 'authorization': `Bearer ${this.apikey}`
84
+ }, method: 'post', url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`, data: {
85
+ question: chatText,
86
+ session_id: callChatOption.session_id,
87
+ stream: true,
88
+ optional: callChatOption.optional,
89
+ }, responseType: 'stream' }, axiosOption);
90
+ // console.log('opts', opts)
91
+ const response = yield (0, axios_1.default)(opts);
92
+ const readableStream = stream_1.Readable.from(response.data);
93
+ let index = 0, session_id;
94
+ try {
95
+ for (var _d = true, readableStream_1 = __asyncValues(readableStream), readableStream_1_1; readableStream_1_1 = yield readableStream_1.next(), _a = readableStream_1_1.done, !_a;) {
96
+ _c = readableStream_1_1.value;
97
+ _d = false;
98
+ try {
99
+ const chunk = _c;
100
+ const answerData = JSON.parse(chunk.toString().replace('data:', ''));
101
+ const { answer, running_status } = answerData.data;
102
+ if (running_status === true)
103
+ continue;
104
+ if (!session_id)
105
+ session_id = answerData.data.session_id;
106
+ const finished = answerData.data === true;
107
+ let output = { successed: true, requestid, segment: answer, text: answer, finish_reason: finished ? 'stop' : null, index: index++, session_id };
108
+ if (attach)
109
+ output = Object.assign({}, output, attach);
110
+ this.emit(finished ? 'chatdone' : 'chattext', output);
111
+ }
112
+ finally {
113
+ _d = true;
114
+ }
115
+ }
116
+ }
117
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
118
+ finally {
119
+ try {
120
+ if (!_d && !_a && (_b = readableStream_1.return)) yield _b.call(readableStream_1);
121
+ }
122
+ finally { if (e_1) throw e_1.error; }
123
+ }
124
+ return { successed: true, requestid };
125
+ }
126
+ catch (error) {
127
+ this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
128
+ return { successed: false, requestid };
129
+ }
130
+ });
131
+ }
132
+ }
133
+ exports.default = AIMiddlePlatform;
package/dist/doubaoai.js CHANGED
@@ -37,10 +37,8 @@ class DouBaoAI extends gptbase_1.default {
37
37
  const callParams = this.assembleApiParams(chatText, false, callChatOption, axiosOption);
38
38
  try {
39
39
  const response = yield (0, declare_1.request)(callParams);
40
- if (response.successed && !response.data.code) {
40
+ if (response.successed && !response.data.code)
41
41
  return { successed: true, message: response.data.choices, usage: response.data.usage };
42
- // return { successed: true, ...response.data };
43
- }
44
42
  return Object.assign({ successed: false }, response.data);
45
43
  }
46
44
  catch (error) {
@@ -7,6 +7,7 @@ export declare const GptProviderEnum: {
7
7
  readonly OPENAI: "openai";
8
8
  readonly OPENAIPROXY: "openaiproxy";
9
9
  readonly MICROSOFT: "microsoft";
10
+ readonly AIMP: "aimp";
10
11
  readonly BAIDU: "baidu";
11
12
  readonly DOUBAO: "doubao";
12
13
  readonly GOOGLE: "google";
@@ -14,6 +14,7 @@ const azureai_1 = __importDefault(require("./azureai"));
14
14
  const stabilityai_1 = __importDefault(require("./stabilityai"));
15
15
  const stabilityplusai_1 = __importDefault(require("./stabilityplusai"));
16
16
  const baiduai_1 = __importDefault(require("./baiduai"));
17
+ const aimp_1 = __importDefault(require("./aimp"));
17
18
  const doubaoai_1 = __importDefault(require("./doubaoai"));
18
19
  /**
19
20
  * OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
@@ -22,6 +23,7 @@ exports.GptProviderEnum = {
22
23
  OPENAI: 'openai',
23
24
  OPENAIPROXY: 'openaiproxy',
24
25
  MICROSOFT: 'microsoft',
26
+ AIMP: 'aimp',
25
27
  BAIDU: 'baidu',
26
28
  DOUBAO: 'doubao',
27
29
  GOOGLE: 'google',
@@ -36,7 +38,7 @@ exports.GptProviderEnum = {
36
38
  * @returns
37
39
  */
38
40
  function createGpt(provider, apikey, setting) {
39
- let { model, maxtoken, temperature, serviceurl, endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
41
+ let { model, agentid, maxtoken, temperature, serviceurl, endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
40
42
  switch (provider) {
41
43
  case exports.GptProviderEnum.OPENAI:
42
44
  return new openai_1.default(apikey + '', { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
@@ -47,6 +49,8 @@ function createGpt(provider, apikey, setting) {
47
49
  case exports.GptProviderEnum.BAIDU:
48
50
  let cred = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
49
51
  return new baiduai_1.default(cred);
52
+ case exports.GptProviderEnum.AIMP:
53
+ return new aimp_1.default(apikey + '', { endpoint, agentid });
50
54
  case exports.GptProviderEnum.DOUBAO:
51
55
  return new doubaoai_1.default(apikey + '', { model, maxtoken, temperature, top_p, presence_penalty, frequency_penalty });
52
56
  case exports.GptProviderEnum.STABILITY:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "7.0.0",
3
+ "version": "7.0.1",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/aimp.ts ADDED
@@ -0,0 +1,100 @@
1
+ /**
2
+ * 接入AI平台的中间层
3
+ */
4
+ import axios from 'axios';
5
+ import { request } from "./declare";
6
+ import GptBase from "./gptbase"
7
+ import { Readable } from 'stream';
8
+ import { ChatReponse } from './declare';
9
+ export default class AIMiddlePlatform extends GptBase {
10
+
11
+ protected apikey: string;
12
+ protected agent: { endpoint: string, agentid: string };
13
+ /**
14
+ *
15
+ * @param apikey 调用AI中台 的key
16
+ * @param agent 智能体信息
17
+ */
18
+ constructor(apikey: string, agent: { endpoint: string, agentid: string }) {
19
+ super();
20
+ this.apikey = apikey;
21
+ this.agent = agent;
22
+ }
23
+
24
+ /**
25
+ * 非流式传输聊天请求
26
+ * @param chatText
27
+ * @param callChatOption
28
+ * @param axiosOption
29
+ */
30
+ public async chatRequest(chatText: string, callChatOption: any, axiosOption: any = {}): Promise<ChatReponse> {
31
+ if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' });
32
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
33
+ const opts: any = {
34
+ headers: {
35
+ 'Content-Type': 'application/json',
36
+ 'authorization': `Bearer ${this.apikey}`
37
+ },
38
+ method: 'post',
39
+ url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
40
+ data: {
41
+ question: chatText,
42
+ session_id: callChatOption.session_id,
43
+ stream: false
44
+ },
45
+ ...axiosOption
46
+ }
47
+ const response = await request(opts);
48
+ if (!response.successed) return { successed: false, error: 'failed' };
49
+ return { successed: true, message: response.data.answer };
50
+ }
51
+ /**
52
+ * 流式传输聊天请求
53
+ * @param chatText
54
+ * @param callChatOption
55
+ * @param attach
56
+ * @param axiosOption
57
+ * @returns
58
+ */
59
+ override async chatRequestInStream(chatText: string | Array<any>, callChatOption: any, attach?: any, axiosOption?: any): Promise<any> {
60
+ if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' });
61
+ axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
62
+ let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
63
+ try {
64
+ const opts: any = {
65
+ headers: {
66
+ 'Content-Type': 'application/json',
67
+ 'authorization': `Bearer ${this.apikey}`
68
+ },
69
+ method: 'post',
70
+ url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
71
+ data: {
72
+ question: chatText,
73
+ session_id: callChatOption.session_id,
74
+ stream: true,
75
+ optional: callChatOption.optional,
76
+ },
77
+ responseType: 'stream',
78
+ ...axiosOption
79
+ }
80
+ // console.log('opts', opts)
81
+ const response = await axios(opts);
82
+ const readableStream = Readable.from(response.data);
83
+ let index = 0, session_id;
84
+ for await (const chunk of readableStream) {
85
+ const answerData = JSON.parse(chunk.toString().replace('data:',''));
86
+ const { answer, running_status } = answerData.data;
87
+ if (running_status === true) continue;
88
+ if (!session_id) session_id = answerData.data.session_id;
89
+ const finished = answerData.data === true;
90
+ let output = { successed: true, requestid, segment: answer, text: answer, finish_reason: finished ? 'stop' : null, index: index++, session_id };
91
+ if (attach) output = Object.assign({}, output, attach);
92
+ this.emit(finished ? 'chatdone' : 'chattext', output)
93
+ }
94
+ return { successed: true, requestid }
95
+ } catch (error) {
96
+ this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
97
+ return { successed: false, requestid }
98
+ }
99
+ }
100
+ }
package/src/doubaoai.ts CHANGED
@@ -22,16 +22,12 @@ export default class DouBaoAI extends GptBase {
22
22
  const callParams = this.assembleApiParams(chatText, false, callChatOption, axiosOption);
23
23
  try {
24
24
  const response = await request(callParams)
25
- if (response.successed && !response.data.code) {
26
- return { successed: true, message: response.data.choices, usage: response.data.usage }
27
- // return { successed: true, ...response.data };
28
- }
25
+ if (response.successed && !response.data.code) return { successed: true, message: response.data.choices, usage: response.data.usage }
29
26
  return { successed: false, ...response.data };
30
27
  } catch (error) {
31
28
  console.log('result is error ', error)
32
29
  return { successed: false, error };
33
30
  }
34
-
35
31
  }
36
32
  /**
37
33
  * 组装最后的调用参数
@@ -8,6 +8,7 @@ import AzureAI from './azureai'
8
8
  import StabilityAI from './stabilityai'
9
9
  import StabilityPlusAI from './stabilityplusai'
10
10
  import BaiduWenXinAI, { ApiCredential } from './baiduai'
11
+ import AIMiddlePlatform from './aimp';
11
12
  import DouBaoAI from './doubaoai'
12
13
  import GptBase from './gptbase';
13
14
  /**
@@ -17,6 +18,7 @@ export const GptProviderEnum = {
17
18
  OPENAI: 'openai',
18
19
  OPENAIPROXY:'openaiproxy',
19
20
  MICROSOFT: 'microsoft',
21
+ AIMP: 'aimp', ///AI 中台业务
20
22
  BAIDU: 'baidu',
21
23
  DOUBAO:'doubao',
22
24
  GOOGLE:'google',
@@ -32,7 +34,7 @@ export type GptProviderEnum = typeof GptProviderEnum[keyof typeof GptProviderEnu
32
34
  * @returns
33
35
  */
34
36
  export function createGpt(provider: GptProviderEnum, apikey: string|ApiCredential, setting: any): GptBase | null {
35
- let { model, maxtoken, temperature, serviceurl,endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
37
+ let { model,agentid, maxtoken, temperature, serviceurl,endpoint, engine, version, embedding, top_p, presence_penalty, frequency_penalty } = setting || {};
36
38
  switch (provider) {
37
39
  case GptProviderEnum.OPENAI:
38
40
  return new OpenAIGpt(apikey + '', { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty });
@@ -41,8 +43,10 @@ export function createGpt(provider: GptProviderEnum, apikey: string|ApiCredentia
41
43
  case GptProviderEnum.MICROSOFT:
42
44
  return new AzureAI(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature, embedding, top_p, presence_penalty, frequency_penalty }, );
43
45
  case GptProviderEnum.BAIDU:
44
- let cred: ApiCredential = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey
46
+ let cred: ApiCredential = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
45
47
  return new BaiduWenXinAI(cred);
48
+ case GptProviderEnum.AIMP:
49
+ return new AIMiddlePlatform(apikey+'',{ endpoint,agentid });
46
50
  case GptProviderEnum.DOUBAO:
47
51
  return new DouBaoAI(apikey + '', { model, maxtoken, temperature, top_p, presence_penalty, frequency_penalty })
48
52
  case GptProviderEnum.STABILITY: