@cobrowser/chatgpt 0.7.5 → 0.7.7

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,23 +130,40 @@ 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
+ let run;
134
+ if (!this.threadId) {
135
+ const thread = yield openai.beta.threads.create();
136
+ this.threadId = thread.id;
137
+ }
138
+ const lastThreadMessasges = yield openai.beta.threads.messages.list(this.threadId, { order: "desc", limit: 2 });
139
+ const isRepeatedMessage = lastThreadMessasges.data.find(msg => msg.content[0].type === 'text' && msg.content[0].text.value === message);
140
+ // making sure that we're not saving repeated messages
141
+ if (isRepeatedMessage) {
142
+ const runs = yield openai.beta.threads.runs.list(this.threadId, { order: "desc", limit: 1 });
143
+ run = runs.data[0];
144
+ }
145
+ else {
146
+ yield openai.beta.threads.messages.create(this.threadId, { role: "user", content: message });
147
+ run = yield openai.beta.threads.runs.create(this.threadId, { assistant_id: (_a = this.assistantId) !== null && _a !== void 0 ? _a : '' });
148
+ }
133
149
  while (['queued', 'in_progress', 'cancelling'].includes(run.status)) {
134
150
  yield new Promise(resolve => setTimeout(resolve, 500));
135
151
  run = yield openai.beta.threads.runs.retrieve(run.thread_id, run.id);
136
152
  }
137
153
  if (run.status === 'completed') {
138
- const { data } = yield openai.beta.threads.messages.list(run.thread_id);
154
+ const { data } = yield openai.beta.threads.messages.list(run.thread_id, { order: 'desc', limit: 1 });
139
155
  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;
156
+ const assistantMessages = messages.filter(message => typeof message === 'object' && 'value' in message && typeof message.value === 'string');
157
+ if (assistantMessages.length) {
158
+ let answers = assistantMessages.map(message => message.value).join('\n');
159
+ return {
160
+ firstChoice: answers,
161
+ threadId: run.thread_id,
162
+ usageTokens: run.usage,
163
+ };
143
164
  }
144
165
  }
145
- return reply;
166
+ return undefined;
146
167
  });
147
168
  }
148
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.7.5",
3
+ "version": "0.7.7",
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": "2f235befbe0be80629be10c8a34d197ca189f512"
43
43
  }