doomiaichat 2.5.0 → 2.7.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.
package/dist/azureai.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AzureOpenAIPatameters, ChatReponse, OpenAIApiParameters } from "./declare";
1
+ import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters } from "./declare";
2
2
  import OpenAIGpt from "./openai";
3
3
  export default class AzureAI extends OpenAIGpt {
4
4
  protected readonly azureSetting: AzureOpenAIPatameters;
@@ -7,6 +7,12 @@ export default class AzureAI extends OpenAIGpt {
7
7
  * ZAure OpenAI 最新的URL地址
8
8
  */
9
9
  get BaseUrl(): string;
10
+ get EmbeddingUrl(): string;
11
+ /**
12
+ * 获得文字的向量
13
+ * @param text
14
+ */
15
+ getTextEmbedding(text: string, axiosOption?: any): Promise<EmbeddingResult>;
10
16
  /**
11
17
  * 请求GPT接口
12
18
  */
package/dist/azureai.js CHANGED
@@ -29,6 +29,38 @@ class AzureAI extends openai_1.default {
29
29
  get BaseUrl() {
30
30
  return `${this.azureSetting.endpoint}/openai/deployments/${this.azureSetting.engine}/chat/completions?api-version=${this.azureSetting.version || '2023-03-15-preview'}`;
31
31
  }
32
+ get EmbeddingUrl() {
33
+ return `${this.azureSetting.endpoint}/openai/deployments/${this.azureSetting.embedding || 'openai-embedding-ada-002'}/embeddings?api-version=2022-12-01`;
34
+ }
35
+ /**
36
+ * 获得文字的向量
37
+ * @param text
38
+ */
39
+ getTextEmbedding(text, axiosOption = {}) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ if (!text)
42
+ return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
43
+ if (!axiosOption.headers)
44
+ axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
45
+ else {
46
+ axiosOption.headers['api-key'] = this.apiKey;
47
+ axiosOption.headers['Content-Type'] = 'application/json';
48
+ }
49
+ try {
50
+ let param = Object.assign(Object.assign({}, axiosOption), { method: "post", data: {
51
+ input: text
52
+ }, url: this.EmbeddingUrl });
53
+ const response = yield (0, declare_1.request)(param);
54
+ if (response.data) {
55
+ return { successed: true, embedding: response.data.data[0].embedding };
56
+ }
57
+ return Object.assign({ successed: false }, response.data);
58
+ }
59
+ catch (error) {
60
+ return { successed: false, error };
61
+ }
62
+ });
63
+ }
32
64
  /**
33
65
  * 请求GPT接口
34
66
  */
@@ -96,6 +128,9 @@ class AzureAI extends openai_1.default {
96
128
  ///回答的内容非JSON格式,自己来提取算了
97
129
  console.log('自己组装', value);
98
130
  let sentences = value.split(/",|\n/g); ///用换行或",来割分文本内容
131
+ sentences = sentences.map((str) => {
132
+ return str.replace(/(\[|"|\]|\{|\})/g, '');
133
+ });
99
134
  // let matched = value.match(/\d+分/g), score = 0;
100
135
  // if (matched && matched.length) {
101
136
  // score = Number(matched[0].replace('分', ''));
package/dist/baiduai.d.ts CHANGED
@@ -14,6 +14,11 @@ export default class BaiduWenXinAI extends GptBase {
14
14
  * 获取调用Api的Token
15
15
  */
16
16
  getAccessToken(): Promise<AccessTokenResult>;
17
+ /**
18
+ * 获得文字的向量
19
+ * @param text
20
+ */
21
+ getTextEmbedding(_text: string, _axiosOption: any): Promise<any>;
17
22
  /**
18
23
  * 请求GPT接口
19
24
  */
package/dist/baiduai.js CHANGED
@@ -53,6 +53,15 @@ class BaiduWenXinAI extends gptbase_1.default {
53
53
  return { successed: false, error: tokenData.data.msg };
54
54
  });
55
55
  }
56
+ /**
57
+ * 获得文字的向量
58
+ * @param text
59
+ */
60
+ getTextEmbedding(_text, _axiosOption) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ return null;
63
+ });
64
+ }
56
65
  /**
57
66
  * 请求GPT接口
58
67
  */
package/dist/declare.d.ts CHANGED
@@ -57,6 +57,7 @@ export interface OpenAIApiParameters {
57
57
  export interface AzureOpenAIPatameters {
58
58
  'endpoint': string;
59
59
  'engine': string;
60
+ 'embedding'?: string;
60
61
  'version'?: string;
61
62
  }
62
63
  /**
@@ -97,6 +98,12 @@ export interface CommentResult extends ApiResult {
97
98
  'score'?: number;
98
99
  'comment'?: string;
99
100
  }
101
+ /**
102
+ * 调用OpenAI Api的向量约定
103
+ */
104
+ export interface EmbeddingResult extends ApiResult {
105
+ 'embedding'?: number[];
106
+ }
100
107
  /**
101
108
  * 调用OpenAI Api的参数约定
102
109
  */
package/dist/gptbase.d.ts CHANGED
@@ -6,6 +6,11 @@ export default abstract class GptBase extends EventEmitter {
6
6
  * 构造函数
7
7
  */
8
8
  constructor();
9
+ /**
10
+ * 获得文字的向量
11
+ * @param text
12
+ */
13
+ abstract getTextEmbedding(text: string, axiosOption: any): Promise<any>;
9
14
  /**
10
15
  * 自由聊天模式
11
16
  * @param chatText
package/dist/openai.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { OpenAIApi, ChatCompletionRequestMessage } from "openai";
2
2
  import GptBase from "./gptbase";
3
- import { OpenAIApiParameters, ChatReponse, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem, CommentResult } from './declare';
3
+ import { OpenAIApiParameters, ChatReponse, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem, CommentResult, EmbeddingResult } from './declare';
4
4
  export default class OpenAIGpt extends GptBase {
5
5
  protected readonly apiKey: string;
6
6
  private aiApi;
@@ -17,6 +17,11 @@ export default class OpenAIGpt extends GptBase {
17
17
  * 初始化OpenAI 的聊天对象Api
18
18
  */
19
19
  createOpenAI(apiKey: string): OpenAIApi;
20
+ /**
21
+ * 获得文字的向量
22
+ * @param text
23
+ */
24
+ getTextEmbedding(text: string, axiosOption: any): Promise<EmbeddingResult>;
20
25
  /**
21
26
  * 向OpenAI发送一个聊天请求
22
27
  * @param {*} chatText
package/dist/openai.js CHANGED
@@ -44,6 +44,31 @@ class OpenAIGpt extends gptbase_1.default {
44
44
  createOpenAI(apiKey) {
45
45
  return new openai_1.OpenAIApi(new openai_1.Configuration({ apiKey }));
46
46
  }
47
+ /**
48
+ * 获得文字的向量
49
+ * @param text
50
+ */
51
+ getTextEmbedding(text, axiosOption) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ if (!text)
54
+ return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
55
+ if (!this.aiApi) {
56
+ this.aiApi = this.createOpenAI(this.apiKey);
57
+ }
58
+ // console.log('message', message)
59
+ try {
60
+ const response = yield this.aiApi.createEmbedding({
61
+ model: 'text-embedding-ada-002',
62
+ input: text,
63
+ }, axiosOption);
64
+ return { successed: true, embedding: response.data.data[0].embedding };
65
+ }
66
+ catch (error) {
67
+ console.log('result is error ', error);
68
+ return { successed: false, error };
69
+ }
70
+ });
71
+ }
47
72
  /**
48
73
  * 向OpenAI发送一个聊天请求
49
74
  * @param {*} chatText
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/azureai.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AzureOpenAIPatameters, ChatReponse, OpenAIApiParameters, request } from "./declare";
1
+ import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters, request } from "./declare";
2
2
  import OpenAIGpt from "./openai"
3
3
  import { ChatCompletionRequestMessage } from "openai";
4
4
  export default class AzureAI extends OpenAIGpt {
@@ -17,6 +17,42 @@ export default class AzureAI extends OpenAIGpt {
17
17
  get BaseUrl():string{
18
18
  return `${this.azureSetting.endpoint}/openai/deployments/${this.azureSetting.engine}/chat/completions?api-version=${this.azureSetting.version ||'2023-03-15-preview'}`
19
19
  }
20
+
21
+ get EmbeddingUrl(): string {
22
+ return `${this.azureSetting.endpoint}/openai/deployments/${this.azureSetting.embedding ||'openai-embedding-ada-002'}/embeddings?api-version=2022-12-01`
23
+ }
24
+
25
+ /**
26
+ * 获得文字的向量
27
+ * @param text
28
+ */
29
+ override async getTextEmbedding(text: string,axiosOption: any = {}): Promise<EmbeddingResult> {
30
+ if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
31
+ if (!axiosOption.headers)
32
+ axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
33
+ else {
34
+ axiosOption.headers['api-key'] = this.apiKey;
35
+ axiosOption.headers['Content-Type'] = 'application/json';
36
+ }
37
+
38
+ try {
39
+ let param = {
40
+ ...axiosOption,
41
+ method: "post",
42
+ data: {
43
+ input: text
44
+ },
45
+ url: this.EmbeddingUrl
46
+ };
47
+ const response = await request(param)
48
+ if (response.data) {
49
+ return { successed: true, embedding: response.data.data[0].embedding };
50
+ }
51
+ return { successed: false, ...response.data };
52
+ } catch (error) {
53
+ return { successed: false, error };
54
+ }
55
+ }
20
56
  /**
21
57
  * 请求GPT接口
22
58
  */
@@ -85,6 +121,9 @@ export default class AzureAI extends OpenAIGpt {
85
121
 
86
122
  console.log('自己组装', value);
87
123
  let sentences = value.split(/",|\n/g) ///用换行或",来割分文本内容
124
+ sentences = sentences.map((str: string)=>{
125
+ return str.replace(/(\[|"|\]|\{|\})/g, '')
126
+ })
88
127
  // let matched = value.match(/\d+分/g), score = 0;
89
128
  // if (matched && matched.length) {
90
129
  // score = Number(matched[0].replace('分', ''));
package/src/baiduai.ts CHANGED
@@ -38,6 +38,13 @@ export default class BaiduWenXinAI extends GptBase {
38
38
  }
39
39
  return { successed: false, error: tokenData.data.msg }
40
40
  }
41
+ /**
42
+ * 获得文字的向量
43
+ * @param text
44
+ */
45
+ async getTextEmbedding(_text: string, _axiosOption: any): Promise<any> {
46
+ return null;
47
+ }
41
48
  /**
42
49
  * 请求GPT接口
43
50
  */
package/src/declare.ts CHANGED
@@ -61,7 +61,8 @@ export interface OpenAIApiParameters {
61
61
  */
62
62
  export interface AzureOpenAIPatameters{
63
63
  'endpoint':string; ///端点
64
- 'engine':string; ///部署的项目名称
64
+ 'engine':string; ///GPT部署的项目名称
65
+ 'embedding'?: string; ///向量引擎项目名称
65
66
  'version'?:string; ///Api 版本
66
67
  }
67
68
 
@@ -105,6 +106,13 @@ export interface CommentResult extends ApiResult {
105
106
  'score'?:number,
106
107
  'comment'?: string; ///评价内容
107
108
  }
109
+
110
+ /**
111
+ * 调用OpenAI Api的向量约定
112
+ */
113
+ export interface EmbeddingResult extends ApiResult {
114
+ 'embedding'?: number[],
115
+ }
108
116
  /**
109
117
  * 调用OpenAI Api的参数约定
110
118
  */
package/src/gptbase.ts CHANGED
@@ -8,6 +8,11 @@ export default abstract class GptBase extends EventEmitter {
8
8
  constructor() {
9
9
  super();
10
10
  }
11
+ /**
12
+ * 获得文字的向量
13
+ * @param text
14
+ */
15
+ abstract getTextEmbedding(text: string, axiosOption: any):Promise<any>;
11
16
  /**
12
17
  * 自由聊天模式
13
18
  * @param chatText
package/src/openai.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "openai"
2
2
  // import { EventEmitter } from "events";
3
3
  import GptBase from "./gptbase"
4
- import { OpenAIApiParameters, ChatReponse, OutlineSummaryItem, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem, CommentResult } from './declare'
4
+ import { OpenAIApiParameters, ChatReponse, OutlineSummaryItem, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem, CommentResult, EmbeddingResult } from './declare'
5
5
  const SECTION_LENGTH = 1600; ///每2400个字符分成一组
6
6
  const MESSAGE_LENGTH = 1; ///每次送8句话给openai 进行解析,送多了,会报错
7
7
  //请将答案放在最后,标记为答案:()
@@ -38,6 +38,28 @@ export default class OpenAIGpt extends GptBase {
38
38
  createOpenAI(apiKey: string): OpenAIApi {
39
39
  return new OpenAIApi(new Configuration({ apiKey }))
40
40
  }
41
+
42
+ /**
43
+ * 获得文字的向量
44
+ * @param text
45
+ */
46
+ async getTextEmbedding(text: string, axiosOption: any): Promise<EmbeddingResult>{
47
+ if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
48
+ if (!this.aiApi) {
49
+ this.aiApi = this.createOpenAI(this.apiKey);
50
+ }
51
+ // console.log('message', message)
52
+ try {
53
+ const response:any = await this.aiApi.createEmbedding({
54
+ model:'text-embedding-ada-002',
55
+ input: text,
56
+ }, axiosOption);
57
+ return { successed: true, embedding: response.data.data[0].embedding };
58
+ } catch (error) {
59
+ console.log('result is error ', error)
60
+ return { successed: false, error };
61
+ }
62
+ }
41
63
  /**
42
64
  * 向OpenAI发送一个聊天请求
43
65
  * @param {*} chatText