@cobrowser/chatgpt 0.7.5 → 0.7.6

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.
@@ -2,5 +2,6 @@ import ChatGptUsageTokens from './ChatGptUsageTokens';
2
2
  interface ChatGptResponse {
3
3
  firstChoice?: string;
4
4
  usageTokens?: ChatGptUsageTokens;
5
+ threadId?: string;
5
6
  }
6
7
  export default ChatGptResponse;
@@ -4,7 +4,8 @@ import ChatGptMessage from '../../models/ChatGptMessage';
4
4
  import Item from '../../models/Item';
5
5
  export declare class CopilotService extends BaseService {
6
6
  assistantId: string | undefined;
7
- constructor(apiKey?: string, model?: string, assistantId?: string, apiUrl?: string, maxNumberOfChoices?: number);
7
+ threadId: string | undefined;
8
+ constructor(apiKey?: string, model?: string, assistantId?: string, threadId?: string, apiUrl?: string, maxNumberOfChoices?: number);
8
9
  /**
9
10
  * a method to be used to suggest some quick replies for the agent based on the conversation
10
11
  * will be added as a single string in the format
@@ -29,5 +30,5 @@ export declare class CopilotService extends BaseService {
29
30
  *
30
31
  * @param message
31
32
  */
32
- getAssistantReply(message: string): Promise<string | undefined>;
33
+ getAssistantReply(message: string): Promise<ChatGptResponse | undefined>;
33
34
  }
@@ -18,7 +18,7 @@ const logger_1 = __importDefault(require("../logger"));
18
18
  const ChatGptMessage_1 = require("../../models/ChatGptMessage");
19
19
  const openai_1 = __importDefault(require("openai"));
20
20
  class CopilotService extends BaseService_1.default {
21
- constructor(apiKey, model, assistantId, apiUrl, maxNumberOfChoices) {
21
+ constructor(apiKey, model, assistantId, threadId, apiUrl, maxNumberOfChoices) {
22
22
  super(apiKey, model, apiUrl, maxNumberOfChoices);
23
23
  Object.defineProperty(this, "assistantId", {
24
24
  enumerable: true,
@@ -26,7 +26,14 @@ class CopilotService extends BaseService_1.default {
26
26
  writable: true,
27
27
  value: void 0
28
28
  });
29
+ Object.defineProperty(this, "threadId", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
29
35
  this.assistantId = assistantId;
36
+ this.threadId = threadId;
30
37
  }
31
38
  /**
32
39
  * a method to be used to suggest some quick replies for the agent based on the conversation
@@ -56,10 +63,7 @@ class CopilotService extends BaseService_1.default {
56
63
  logger_1.default.error('last customer message not found');
57
64
  return Promise.reject(new Error('last customer message not found'));
58
65
  }
59
- const assistantReply = yield this.getAssistantReply(lastCustomerMessage);
60
- return {
61
- firstChoice: assistantReply
62
- };
66
+ return yield this.getAssistantReply(lastCustomerMessage);
63
67
  }
64
68
  // main prompt used to instruct chatGPT to suggest some quick replies
65
69
  const mainPrompt = `Propose some quick replies for the agent to be used on the following conversation
@@ -126,10 +130,12 @@ class CopilotService extends BaseService_1.default {
126
130
  var _a;
127
131
  return __awaiter(this, void 0, void 0, function* () {
128
132
  const openai = new openai_1.default({ apiKey: this.openaiApiKey });
129
- let reply = undefined;
130
- const thread = yield openai.beta.threads.create();
131
- yield openai.beta.threads.messages.create(thread.id, { role: "user", content: message });
132
- let run = yield openai.beta.threads.runs.create(thread.id, { assistant_id: (_a = this.assistantId) !== null && _a !== void 0 ? _a : '' });
133
+ if (!this.threadId) {
134
+ const thread = yield openai.beta.threads.create();
135
+ yield openai.beta.threads.messages.create(thread.id, { role: "user", content: message });
136
+ this.threadId = thread.id;
137
+ }
138
+ let run = yield openai.beta.threads.runs.create(this.threadId, { assistant_id: (_a = this.assistantId) !== null && _a !== void 0 ? _a : '' });
133
139
  while (['queued', 'in_progress', 'cancelling'].includes(run.status)) {
134
140
  yield new Promise(resolve => setTimeout(resolve, 500));
135
141
  run = yield openai.beta.threads.runs.retrieve(run.thread_id, run.id);
@@ -137,12 +143,17 @@ class CopilotService extends BaseService_1.default {
137
143
  if (run.status === 'completed') {
138
144
  const { data } = yield openai.beta.threads.messages.list(run.thread_id);
139
145
  const messages = data.flatMap((message) => message === null || message === void 0 ? void 0 : message.content.map((content) => ((content.type === 'text' && message.role === 'assistant') ? content.text : '')));
140
- const assistantMessage = messages.find(message => typeof message === 'object' && 'value' in message && typeof message.value === 'string');
141
- if (assistantMessage) {
142
- reply = assistantMessage.value;
146
+ const assistantMessages = messages.filter(message => typeof message === 'object' && 'value' in message && typeof message.value === 'string');
147
+ if (assistantMessages.length) {
148
+ let answers = assistantMessages.map(message => message.value).join('\n');
149
+ return {
150
+ firstChoice: answers,
151
+ threadId: run.thread_id,
152
+ usageTokens: run.usage,
153
+ };
143
154
  }
144
155
  }
145
- return reply;
156
+ return undefined;
146
157
  });
147
158
  }
148
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.7.5",
3
+ "version": "0.7.6",
4
4
  "description": "chatgpt services to connect our projects with chatgpt api",
5
5
  "keywords": [
6
6
  "chatgpt",
@@ -39,5 +39,5 @@
39
39
  "bugs": {
40
40
  "url": "https://bitbucket.org/cobrowser/cb_utils/issues"
41
41
  },
42
- "gitHead": "5a4bff51323eed4e0c17a865e7783d8dfc0323e1"
42
+ "gitHead": "13f2b86caf4381d983749289840a7a53fddfc066"
43
43
  }