@cobrowser/chatgpt 0.6.3 → 0.6.4
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/models/ChatGptMessage.d.ts +1 -0
- package/dist/models/ChatGptUsageTokens.d.ts +6 -0
- package/dist/models/ChatGptUsageTokens.js +3 -0
- package/dist/models/CopilotSuggestion.d.ts +6 -0
- package/dist/models/CopilotSuggestion.js +2 -0
- package/dist/services/BaseService/BaseService.d.ts +6 -0
- package/dist/services/BaseService/BaseService.js +14 -1
- package/dist/services/CopilotService/CopilotService.d.ts +2 -1
- package/dist/services/CopilotService/CopilotService.js +4 -1
- package/dist/services/TranslationService/TranslationService.d.ts +2 -1
- package/dist/services/TranslationService/TranslationService.js +48 -3
- package/package.json +4 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
+
import ChatGptUsageTokens from '../../models/ChatGptUsageTokens';
|
|
2
3
|
declare class BaseService {
|
|
3
4
|
request: AxiosInstance;
|
|
4
5
|
readonly BASE_CHAT_COMPLETION_URL = "https://api.openai.com/v1/chat/completions";
|
|
@@ -16,6 +17,11 @@ declare class BaseService {
|
|
|
16
17
|
* @param response
|
|
17
18
|
*/
|
|
18
19
|
getResponseFirstChoice(response: AxiosResponse): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Filter the response from chatGpt API and only get the first choice
|
|
22
|
+
* @param response
|
|
23
|
+
*/
|
|
24
|
+
getResponseUsageTokens(response: AxiosResponse): ChatGptUsageTokens | undefined;
|
|
19
25
|
/**
|
|
20
26
|
* function to handle requests errors
|
|
21
27
|
* @param error
|
|
@@ -12,7 +12,7 @@ class BaseService {
|
|
|
12
12
|
* @param model the main chatGPT model to use
|
|
13
13
|
* @param maxNumberOfChoices How many chat completion choices to generate for each request.
|
|
14
14
|
*/
|
|
15
|
-
constructor(apiUrl, model = 'gpt-3.5-turbo', maxNumberOfChoices = 1) {
|
|
15
|
+
constructor(apiUrl, model = 'gpt-3.5-turbo-1106', maxNumberOfChoices = 1) {
|
|
16
16
|
Object.defineProperty(this, "request", {
|
|
17
17
|
enumerable: true,
|
|
18
18
|
configurable: true,
|
|
@@ -60,6 +60,19 @@ class BaseService {
|
|
|
60
60
|
}
|
|
61
61
|
return undefined;
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Filter the response from chatGpt API and only get the first choice
|
|
65
|
+
* @param response
|
|
66
|
+
*/
|
|
67
|
+
getResponseUsageTokens(response) {
|
|
68
|
+
var _a;
|
|
69
|
+
if ((_a = response.data) === null || _a === void 0 ? void 0 : _a.usage) {
|
|
70
|
+
const usage = response.data.usage;
|
|
71
|
+
logger_1.default.info(`ChatGpt response usage tokens: ${JSON.stringify(usage)}`);
|
|
72
|
+
return usage;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
63
76
|
/**
|
|
64
77
|
* function to handle requests errors
|
|
65
78
|
* @param error
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BaseService from '../BaseService/BaseService';
|
|
2
2
|
import ChatGptMessage from '../../models/ChatGptMessage';
|
|
3
|
+
import CopilotSuggestion from '../../models/CopilotSuggestion';
|
|
3
4
|
import Item from '../../models/Item';
|
|
4
5
|
export declare class CopilotService extends BaseService {
|
|
5
6
|
constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
|
|
@@ -15,7 +16,7 @@ export declare class CopilotService extends BaseService {
|
|
|
15
16
|
* this will make it easy to ask chatGpt for quick replies
|
|
16
17
|
* @param conversation
|
|
17
18
|
*/
|
|
18
|
-
suggest(conversation: string): Promise<
|
|
19
|
+
suggest(conversation: string): Promise<CopilotSuggestion | undefined>;
|
|
19
20
|
/**
|
|
20
21
|
* this will request chatGPT to recommend product or item based on the whole conversation
|
|
21
22
|
* @param conversation the whole chat conversation
|
|
@@ -53,7 +53,10 @@ class CopilotService extends BaseService_1.default {
|
|
|
53
53
|
messages: [mainPromptMessage],
|
|
54
54
|
};
|
|
55
55
|
const response = yield this.request.post('', requestBody);
|
|
56
|
-
return
|
|
56
|
+
return {
|
|
57
|
+
firstChoice: this.getResponseFirstChoice(response),
|
|
58
|
+
usageTokens: this.getResponseUsageTokens(response),
|
|
59
|
+
};
|
|
57
60
|
}
|
|
58
61
|
catch (e) {
|
|
59
62
|
this.handleErrors(e);
|
|
@@ -8,5 +8,6 @@ export declare class TranslationService extends BaseService {
|
|
|
8
8
|
* @param toLanguage the desired language to translate to ( English is the default )
|
|
9
9
|
* @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
|
|
10
10
|
*/
|
|
11
|
-
translate(text: string, toLanguage?: string, fromLanguage?: string): Promise<
|
|
11
|
+
translate(text: string, toLanguage?: string, fromLanguage?: string): Promise<any | undefined>;
|
|
12
|
+
findTextLanguage(text: string): Promise<any | undefined>;
|
|
12
13
|
}
|
|
@@ -44,14 +44,59 @@ class TranslationService extends BaseService_1.default {
|
|
|
44
44
|
// the body we need to send to the request
|
|
45
45
|
requestBody = {
|
|
46
46
|
model: this.chatGptModel,
|
|
47
|
-
|
|
47
|
+
response_format: { type: "json_object" },
|
|
48
|
+
messages: [
|
|
49
|
+
{
|
|
50
|
+
role: ChatGptMessage_1.ChatGptRole.SYSTEM,
|
|
51
|
+
content: "You are a translator that provides a response in JSON format only. The response should be a JSON object with a boolean called 'error' and 'data,' which is an array of objects. Each object in the array will contain a 'translation' property, holding the translation of the message, and a 'translated_message' property, which will contain the original message before being translated. Additionally, the 'translation' property will hold the translation of the 'translated_message' property.",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
48
54
|
role: ChatGptMessage_1.ChatGptRole.USER,
|
|
49
55
|
content: translateText,
|
|
50
|
-
}
|
|
56
|
+
}
|
|
57
|
+
],
|
|
51
58
|
};
|
|
52
59
|
try {
|
|
53
60
|
const response = yield this.request.post('', requestBody);
|
|
54
|
-
return
|
|
61
|
+
return {
|
|
62
|
+
firstChoice: this.getResponseFirstChoice(response),
|
|
63
|
+
usageTokens: this.getResponseUsageTokens(response),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
this.handleErrors(e);
|
|
68
|
+
return Promise.reject(e);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
findTextLanguage(text) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
if (!text) {
|
|
75
|
+
logger_1.default.error('translation text should be provided');
|
|
76
|
+
return Promise.reject(new Error('text must be provided'));
|
|
77
|
+
}
|
|
78
|
+
// instruction on what to do with the text
|
|
79
|
+
const translateText = `In which language is this text in '${text}'? The response should be {'languageCode': 'language_code_here', 'isError': 'true or false based on whether you did find the language code or not'}. Other responses or explanations are not allowed.`,
|
|
80
|
+
// the body we need to send to the request
|
|
81
|
+
requestBody = {
|
|
82
|
+
model: this.chatGptModel,
|
|
83
|
+
messages: [
|
|
84
|
+
{
|
|
85
|
+
role: ChatGptMessage_1.ChatGptRole.SYSTEM,
|
|
86
|
+
content: "You are a translator that provides a response in JSON format only—a JSON that contains 'languageCode' and 'isError' if you cannot detect the language.",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
role: ChatGptMessage_1.ChatGptRole.USER,
|
|
90
|
+
content: translateText,
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
const response = yield this.request.post('', requestBody);
|
|
96
|
+
return {
|
|
97
|
+
firstChoice: this.getResponseFirstChoice(response),
|
|
98
|
+
usageTokens: this.getResponseUsageTokens(response),
|
|
99
|
+
};
|
|
55
100
|
}
|
|
56
101
|
catch (e) {
|
|
57
102
|
this.handleErrors(e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobrowser/chatgpt",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "chatgpt services to connect our projects with chatgpt api",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chatgpt",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@cobrowser/logger": "^1.0.0",
|
|
25
|
-
"axios": "^1.
|
|
26
|
-
"axios-mock-adapter": "^1.
|
|
25
|
+
"axios": "^1.3.5",
|
|
26
|
+
"axios-mock-adapter": "^1.21.4"
|
|
27
27
|
},
|
|
28
28
|
"directories": {
|
|
29
29
|
"dist": "dist"
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"bugs": {
|
|
39
39
|
"url": "https://bitbucket.org/cobrowser/cb_utils/issues"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "0d2b7809e9ec1456985f4f7ecf30f43632fbf75b"
|
|
42
42
|
}
|