@cobrowser/chatgpt 0.7.4 → 0.7.5
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.
|
@@ -3,6 +3,7 @@ import ChatGptUsageTokens from '../../models/ChatGptUsageTokens';
|
|
|
3
3
|
declare class BaseService {
|
|
4
4
|
request: AxiosInstance;
|
|
5
5
|
readonly BASE_CHAT_COMPLETION_URL = "https://api.openai.com/v1/chat/completions";
|
|
6
|
+
openaiApiKey: string | undefined;
|
|
6
7
|
chatGptModel: string;
|
|
7
8
|
maxNumberOfChatCompletionResult: number;
|
|
8
9
|
/**
|
|
@@ -27,6 +27,12 @@ class BaseService {
|
|
|
27
27
|
writable: true,
|
|
28
28
|
value: 'https://api.openai.com/v1/chat/completions'
|
|
29
29
|
});
|
|
30
|
+
Object.defineProperty(this, "openaiApiKey", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
30
36
|
Object.defineProperty(this, "chatGptModel", {
|
|
31
37
|
enumerable: true,
|
|
32
38
|
configurable: true,
|
|
@@ -46,6 +52,7 @@ class BaseService {
|
|
|
46
52
|
Authorization: `Bearer ${apiKey} `,
|
|
47
53
|
},
|
|
48
54
|
});
|
|
55
|
+
this.openaiApiKey = apiKey;
|
|
49
56
|
this.chatGptModel = model;
|
|
50
57
|
this.maxNumberOfChatCompletionResult = maxNumberOfChoices;
|
|
51
58
|
}
|
|
@@ -3,7 +3,8 @@ import BaseService from '../BaseService/BaseService';
|
|
|
3
3
|
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
8
|
/**
|
|
8
9
|
* a method to be used to suggest some quick replies for the agent based on the conversation
|
|
9
10
|
* will be added as a single string in the format
|
|
@@ -23,4 +24,10 @@ export declare class CopilotService extends BaseService {
|
|
|
23
24
|
* @param items the items to recommend from
|
|
24
25
|
*/
|
|
25
26
|
recommend(conversation: ChatGptMessage[], items: Item[]): Promise<string | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Get the reply of the assistant if the assistant_id is available
|
|
29
|
+
*
|
|
30
|
+
* @param message
|
|
31
|
+
*/
|
|
32
|
+
getAssistantReply(message: string): Promise<string | undefined>;
|
|
26
33
|
}
|
|
@@ -16,9 +16,17 @@ exports.CopilotService = void 0;
|
|
|
16
16
|
const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
|
|
17
17
|
const logger_1 = __importDefault(require("../logger"));
|
|
18
18
|
const ChatGptMessage_1 = require("../../models/ChatGptMessage");
|
|
19
|
+
const openai_1 = __importDefault(require("openai"));
|
|
19
20
|
class CopilotService extends BaseService_1.default {
|
|
20
|
-
constructor(apiKey, model, apiUrl, maxNumberOfChoices) {
|
|
21
|
+
constructor(apiKey, model, assistantId, apiUrl, maxNumberOfChoices) {
|
|
21
22
|
super(apiKey, model, apiUrl, maxNumberOfChoices);
|
|
23
|
+
Object.defineProperty(this, "assistantId", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
this.assistantId = assistantId;
|
|
22
30
|
}
|
|
23
31
|
/**
|
|
24
32
|
* a method to be used to suggest some quick replies for the agent based on the conversation
|
|
@@ -39,6 +47,20 @@ class CopilotService extends BaseService_1.default {
|
|
|
39
47
|
return Promise.reject(new Error('conversation must be provided'));
|
|
40
48
|
}
|
|
41
49
|
try {
|
|
50
|
+
if (this.assistantId) {
|
|
51
|
+
// Here we get the last customer message as we dont need the whole conversation
|
|
52
|
+
// as we are doing with the other approach
|
|
53
|
+
const conversationArray = conversation.split('\n');
|
|
54
|
+
const lastCustomerMessage = conversationArray.reverse().find(e => e.includes('customer:'));
|
|
55
|
+
if (!lastCustomerMessage) {
|
|
56
|
+
logger_1.default.error('last customer message not found');
|
|
57
|
+
return Promise.reject(new Error('last customer message not found'));
|
|
58
|
+
}
|
|
59
|
+
const assistantReply = yield this.getAssistantReply(lastCustomerMessage);
|
|
60
|
+
return {
|
|
61
|
+
firstChoice: assistantReply
|
|
62
|
+
};
|
|
63
|
+
}
|
|
42
64
|
// main prompt used to instruct chatGPT to suggest some quick replies
|
|
43
65
|
const mainPrompt = `Propose some quick replies for the agent to be used on the following conversation
|
|
44
66
|
display your response as unordered list:-\n${conversation}`,
|
|
@@ -95,5 +117,33 @@ class CopilotService extends BaseService_1.default {
|
|
|
95
117
|
}
|
|
96
118
|
});
|
|
97
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the reply of the assistant if the assistant_id is available
|
|
122
|
+
*
|
|
123
|
+
* @param message
|
|
124
|
+
*/
|
|
125
|
+
getAssistantReply(message) {
|
|
126
|
+
var _a;
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
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
|
+
while (['queued', 'in_progress', 'cancelling'].includes(run.status)) {
|
|
134
|
+
yield new Promise(resolve => setTimeout(resolve, 500));
|
|
135
|
+
run = yield openai.beta.threads.runs.retrieve(run.thread_id, run.id);
|
|
136
|
+
}
|
|
137
|
+
if (run.status === 'completed') {
|
|
138
|
+
const { data } = yield openai.beta.threads.messages.list(run.thread_id);
|
|
139
|
+
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;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return reply;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
98
148
|
}
|
|
99
149
|
exports.CopilotService = CopilotService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobrowser/chatgpt",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.5",
|
|
4
4
|
"description": "chatgpt services to connect our projects with chatgpt api",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chatgpt",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@cobrowser/logger": "^1.0.0",
|
|
25
25
|
"axios": "^1.6.1",
|
|
26
|
-
"axios-mock-adapter": "^1.22.0"
|
|
26
|
+
"axios-mock-adapter": "^1.22.0",
|
|
27
|
+
"openai": "^4.32.0"
|
|
27
28
|
},
|
|
28
29
|
"directories": {
|
|
29
30
|
"dist": "dist"
|
|
@@ -38,5 +39,5 @@
|
|
|
38
39
|
"bugs": {
|
|
39
40
|
"url": "https://bitbucket.org/cobrowser/cb_utils/issues"
|
|
40
41
|
},
|
|
41
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "5a4bff51323eed4e0c17a865e7783d8dfc0323e1"
|
|
42
43
|
}
|