doomiaichat 6.0.5 → 6.1.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
@@ -16,7 +16,7 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
16
16
  * 获得文字的向量
17
17
  * @param text
18
18
  */
19
- getTextEmbedding(text: string, axiosOption?: any): Promise<EmbeddingResult>;
19
+ getTextEmbedding(text: string | string[], callOption?: any): Promise<EmbeddingResult>;
20
20
  /**
21
21
  * 非流式聊天请求
22
22
  * @param _chatText
package/dist/azureai.js CHANGED
@@ -23,7 +23,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  * 微软AZure OpenAI
24
24
  */
25
25
  const openaibase_1 = __importDefault(require("./openaibase"));
26
- const declare_1 = require("./declare");
27
26
  const openai_1 = require("@azure/openai");
28
27
  class AzureAI extends openaibase_1.default {
29
28
  constructor(apiKey, azureOption, apiOption = {}) {
@@ -46,26 +45,36 @@ class AzureAI extends openaibase_1.default {
46
45
  * 获得文字的向量
47
46
  * @param text
48
47
  */
49
- getTextEmbedding(text, axiosOption = {}) {
48
+ getTextEmbedding(text, callOption = {}) {
50
49
  return __awaiter(this, void 0, void 0, function* () {
51
50
  if (!text)
52
51
  return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
53
- if (!axiosOption.headers)
54
- axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
55
- else {
56
- axiosOption.headers['api-key'] = this.apiKey;
57
- axiosOption.headers['Content-Type'] = 'application/json';
58
- }
52
+ if (!this.aiApi)
53
+ this.aiApi = this.createOpenAI(this.apiKey);
59
54
  try {
60
- let param = Object.assign(Object.assign({}, axiosOption), { method: "post", data: {
61
- input: text
62
- }, url: this.EmbeddingUrl });
63
- const response = yield (0, declare_1.request)(param);
64
- if (response.successed && response.data) {
65
- return { successed: true, embedding: response.data.data[0].embedding };
66
- }
67
- return Object.assign({ successed: false }, response.data);
55
+ const result = yield this.aiApi.getEmbeddings(this.embeddingmodel || 'openai-embedding-ada-002', typeof text === 'string' ? [text] : text, callOption);
56
+ return { successed: true, embedding: result.data };
68
57
  }
58
+ // if (!axiosOption.headers)
59
+ // axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
60
+ // else {
61
+ // axiosOption.headers['api-key'] = this.apiKey;
62
+ // axiosOption.headers['Content-Type'] = 'application/json';
63
+ // }
64
+ // try {
65
+ // let param = {
66
+ // ...axiosOption,
67
+ // method: "post",
68
+ // data: {
69
+ // input: text
70
+ // },
71
+ // url: this.EmbeddingUrl
72
+ // };
73
+ // const response = await request(param)
74
+ // if (response.successed && response.data) {
75
+ // return { successed: true, embedding: response.data.data[0].embedding };
76
+ // }
77
+ // return { successed: false, ...response.data };
69
78
  catch (error) {
70
79
  return { successed: false, error };
71
80
  }
@@ -160,7 +169,7 @@ class AzureAI extends openaibase_1.default {
160
169
  currentIndex = toolCalls[0].index;
161
170
  has_tool_calls = 1;
162
171
  // 检查index是否发生变化
163
- console.log('currentIndex,previous_index', currentIndex, previous_index);
172
+ //console.log('currentIndex,previous_index', currentIndex, previous_index)
164
173
  if (currentIndex !== previous_index) {
165
174
  tool_calls.push({
166
175
  id: toolCalls[0].id,
package/dist/declare.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { EmbeddingItem } from '@azure/openai';
1
2
  export interface ApiResult {
2
3
  /**
3
4
  * return the result of api called
@@ -58,7 +59,7 @@ export interface AzureOpenAIPatameters {
58
59
  * 调用OpenAI Api的向量约定
59
60
  */
60
61
  export interface EmbeddingResult extends ApiResult {
61
- 'embedding'?: number[];
62
+ 'embedding'?: EmbeddingItem[];
62
63
  }
63
64
  /**
64
65
  * 远程请求的返回
package/dist/openai.d.ts CHANGED
@@ -13,7 +13,7 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
13
13
  * 获得文字的向量
14
14
  * @param text
15
15
  */
16
- getTextEmbedding(text: string, axiosOption: any): Promise<EmbeddingResult>;
16
+ getTextEmbedding(text: string | string[], axiosOption: any): Promise<EmbeddingResult>;
17
17
  /**
18
18
  * 向OpenAI发送一个聊天请求
19
19
  * @param {*} chatText
package/dist/openai.js CHANGED
@@ -45,15 +45,13 @@ class OpenAIGpt extends openaibase_1.default {
45
45
  this.aiApi = this.createOpenAI(this.apiKey);
46
46
  }
47
47
  try {
48
- //const response: any = await this.aiApi.createEmbedding({
49
48
  const response = yield this.aiApi.embeddings.create({
50
49
  model: this.embeddingmodel,
51
50
  input: text,
52
51
  }, axiosOption);
53
- return { successed: true, embedding: response.data.data[0].embedding };
52
+ return { successed: true, embedding: response.data.data }; //[0].embedding };
54
53
  }
55
54
  catch (error) {
56
- // console.log('result is error ', error)
57
55
  return { successed: false, error };
58
56
  }
59
57
  });
@@ -84,7 +82,8 @@ class OpenAIGpt extends openaibase_1.default {
84
82
  // tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
85
83
  // tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
86
84
  }, axiosOption);
87
- return { successed: true, message: response.data.choices, usage: response.data.usage };
85
+ // console.log('response.data', response)
86
+ return { successed: true, message: response.choices, usage: response.usage };
88
87
  }
89
88
  catch (error) {
90
89
  console.log('result is error ', error);
@@ -124,16 +123,39 @@ class OpenAIGpt extends openaibase_1.default {
124
123
  stream: true
125
124
  }, axiosOption);
126
125
  let replytext = [];
126
+ let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls = []; // 使用数组来存储工具调用
127
127
  try {
128
- // let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls: any[] = [];// 使用数组来存储工具调用
129
128
  for (var _d = true, response_1 = __asyncValues(response), response_1_1; response_1_1 = yield response_1.next(), _a = response_1_1.done, !_a;) {
130
129
  _c = response_1_1.value;
131
130
  _d = false;
132
131
  try {
133
132
  const chunk = _c;
134
- const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content } = choice.delta;
135
- replytext.push(content);
136
- let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
133
+ const [choice] = chunk.choices, { finish_reason: finishreason, index, usage } = choice, { content, tool_calls: toolCalls } = choice.delta;
134
+ if (toolCalls && toolCalls.length) {
135
+ currentIndex = toolCalls[0].index;
136
+ has_tool_calls = 1;
137
+ // 检查index是否发生变化
138
+ //console.log('currentIndex,previous_index', currentIndex, previous_index)
139
+ if (currentIndex !== previous_index) {
140
+ tool_calls.push({
141
+ id: toolCalls[0].id,
142
+ type: 'function',
143
+ function: {
144
+ name: toolCalls[0].function.name,
145
+ arguments: toolCalls[0].function.arguments
146
+ }
147
+ });
148
+ // 更新previousIndex以供下次比较
149
+ previous_index = currentIndex;
150
+ }
151
+ else {
152
+ tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments;
153
+ }
154
+ }
155
+ else {
156
+ replytext.push(content);
157
+ }
158
+ let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage, has_tool_calls: has_tool_calls, tool_calls: tool_calls };
137
159
  if (attach)
138
160
  output = Object.assign({}, output, attach);
139
161
  this.emit(finishreason ? 'chatdone' : 'chattext', output);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "6.0.5",
3
+ "version": "6.1.0",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/azureai.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * 微软AZure OpenAI
3
3
  */
4
4
  import OpenAIBase from "./openaibase"
5
- import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters, request } from "./declare";
5
+ import { AzureOpenAIPatameters, ChatReponse, EmbeddingResult, OpenAIApiParameters} from "./declare";
6
6
  import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
7
7
  export default class AzureAI extends OpenAIBase<OpenAIClient> {
8
8
 
@@ -29,30 +29,35 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
29
29
  * 获得文字的向量
30
30
  * @param text
31
31
  */
32
- override async getTextEmbedding(text: string, axiosOption: any = {}): Promise<EmbeddingResult> {
32
+ override async getTextEmbedding(text: string|string[], callOption: any = {}): Promise<EmbeddingResult> {
33
33
  if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
34
- if (!axiosOption.headers)
35
- axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
36
- else {
37
- axiosOption.headers['api-key'] = this.apiKey;
38
- axiosOption.headers['Content-Type'] = 'application/json';
34
+ if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey);
35
+ try{
36
+ const result = await this.aiApi.getEmbeddings(this.embeddingmodel || 'openai-embedding-ada-002', typeof text === 'string' ? [text] : text, callOption)
37
+ return { successed: true, embedding:result.data };
39
38
  }
39
+ // if (!axiosOption.headers)
40
+ // axiosOption.headers = { 'api-key': this.apiKey, 'Content-Type': 'application/json' };
41
+ // else {
42
+ // axiosOption.headers['api-key'] = this.apiKey;
43
+ // axiosOption.headers['Content-Type'] = 'application/json';
44
+ // }
40
45
 
41
- try {
42
- let param = {
43
- ...axiosOption,
44
- method: "post",
45
- data: {
46
- input: text
47
- },
48
- url: this.EmbeddingUrl
49
- };
50
- const response = await request(param)
51
- if (response.successed && response.data) {
52
- return { successed: true, embedding: response.data.data[0].embedding };
53
- }
54
- return { successed: false, ...response.data };
55
- } catch (error) {
46
+ // try {
47
+ // let param = {
48
+ // ...axiosOption,
49
+ // method: "post",
50
+ // data: {
51
+ // input: text
52
+ // },
53
+ // url: this.EmbeddingUrl
54
+ // };
55
+ // const response = await request(param)
56
+ // if (response.successed && response.data) {
57
+ // return { successed: true, embedding: response.data.data[0].embedding };
58
+ // }
59
+ // return { successed: false, ...response.data };
60
+ catch (error) {
56
61
  return { successed: false, error };
57
62
  }
58
63
  }
@@ -142,7 +147,7 @@ export default class AzureAI extends OpenAIBase<OpenAIClient> {
142
147
  currentIndex = toolCalls[0].index;
143
148
  has_tool_calls = 1;
144
149
  // 检查index是否发生变化
145
- console.log('currentIndex,previous_index', currentIndex, previous_index)
150
+ //console.log('currentIndex,previous_index', currentIndex, previous_index)
146
151
  if (currentIndex !== previous_index) {
147
152
  tool_calls.push({
148
153
  id: toolCalls[0].id,
package/src/declare.ts CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ import { EmbeddingItem } from '@azure/openai';
2
3
  import axios from 'axios';
3
4
  export interface ApiResult {
4
5
  /**
@@ -63,7 +64,7 @@ export interface AzureOpenAIPatameters {
63
64
  * 调用OpenAI Api的向量约定
64
65
  */
65
66
  export interface EmbeddingResult extends ApiResult {
66
- 'embedding'?: number[],
67
+ 'embedding'?: EmbeddingItem[],
67
68
  }
68
69
  /**
69
70
  * 远程请求的返回
package/src/openai.ts CHANGED
@@ -18,20 +18,18 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
18
18
  * 获得文字的向量
19
19
  * @param text
20
20
  */
21
- override async getTextEmbedding(text: string, axiosOption: any): Promise<EmbeddingResult> {
21
+ override async getTextEmbedding(text: string|string[], axiosOption: any): Promise<EmbeddingResult> {
22
22
  if (!text) return { successed: false, error: { errcode: 2, errmsg: 'content required' } };
23
23
  if (!this.aiApi) {
24
24
  this.aiApi = this.createOpenAI(this.apiKey);
25
25
  }
26
26
  try {
27
- //const response: any = await this.aiApi.createEmbedding({
28
27
  const response: any = await this.aiApi.embeddings.create({
29
28
  model: this.embeddingmodel,
30
29
  input: text,
31
30
  }, axiosOption);
32
- return { successed: true, embedding: response.data.data[0].embedding };
31
+ return { successed: true, embedding: response.data.data};//[0].embedding };
33
32
  } catch (error) {
34
- // console.log('result is error ', error)
35
33
  return { successed: false, error };
36
34
  }
37
35
  }
@@ -60,7 +58,8 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
60
58
  // tools: (callChatOption?.enableToolCall === 1 && callChatOption?.tools) ? callChatOption.tools : undefined,
61
59
  // tool_choice: callChatOption?.enableToolCall === 1 ? 'auto' : undefined,
62
60
  }, axiosOption);
63
- return { successed: true, message: response.data.choices, usage: response.data.usage };
61
+ // console.log('response.data', response)
62
+ return { successed: true, message: response.choices, usage: response.usage };
64
63
  } catch (error) {
65
64
  console.log('result is error ', error)
66
65
  return { successed: false, error };
@@ -97,13 +96,34 @@ export default class OpenAIGpt extends OpenAIBase<OpenAI> {
97
96
  stream:true
98
97
  }, axiosOption);
99
98
  let replytext: string[] = [];
100
- // let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls: any[] = [];// 使用数组来存储工具调用
99
+ let has_tool_calls = 0, currentIndex, previous_index = -1, tool_calls: any[] = [];// 使用数组来存储工具调用
101
100
  for await (const chunk of response) {
102
101
  const [choice] = chunk.choices,
103
102
  { finish_reason:finishreason, index, usage } = choice,
104
- { content} = choice.delta;
105
- replytext.push(content);
106
- let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
103
+ { content, tool_calls:toolCalls } = choice.delta;
104
+ if (toolCalls && toolCalls.length) {
105
+ currentIndex = toolCalls[0].index;
106
+ has_tool_calls = 1;
107
+ // 检查index是否发生变化
108
+ //console.log('currentIndex,previous_index', currentIndex, previous_index)
109
+ if (currentIndex !== previous_index) {
110
+ tool_calls.push({
111
+ id: toolCalls[0].id,
112
+ type: 'function',
113
+ function: {
114
+ name: toolCalls[0].function.name,
115
+ arguments: toolCalls[0].function.arguments
116
+ }
117
+ });
118
+ // 更新previousIndex以供下次比较
119
+ previous_index = currentIndex;
120
+ } else {
121
+ tool_calls[previous_index].function.arguments += toolCalls[0].function.arguments
122
+ }
123
+ } else {
124
+ replytext.push(content);
125
+ }
126
+ let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage, has_tool_calls: has_tool_calls, tool_calls: tool_calls };
107
127
  if (attach) output = Object.assign({}, output, attach);
108
128
  this.emit(finishreason ? 'chatdone' : 'chattext', output)
109
129
  }