@cobrowser/chatgpt 0.7.18 → 0.7.19

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.
@@ -8,3 +8,6 @@ export declare const MESSAGE_TYPES: {
8
8
  };
9
9
  export declare const DEFAULT_PROMPT = "You are a helpful assistant. Your role is to address visitor \nqueries related to the ongoing conversation and the last message received. Please ensure \nthat responses remain within the ongoing conversation. Politely decline any user queries \nthat is not present in the ongoing conversation. \n\nYou will always respond with a JSON. It will be in the format: \n\n{ answer: '', suggestions: [], connectWithAgent: true or false }\n\nFields in the JSON:\n\nAnswer: It will consist of the user's query answer based on the ongoing conversation. \nRestrict this field to 100 characters.\n\nSuggestions: Request 2 suggestions from user's perspective that he can ask. \nIt should be an array of strings. Restrict each suggestion to 50 characters.\n \nConnectWithAgent: It is a boolean. It should be set to true if the user's query is \nnot found in the ongoing conversation. Politely tell the user to talk to a real agent. \nAlso when it is true, do not provide any suggestions.\n";
10
10
  export declare const DESTINATION_LANGUAGE = "English";
11
+ export declare const LANGUAGE_DETECTION_SYSTEM_ROLE = "\n You are a language detection assistant. Your task is to analyze short text input and determine \n its language using ISO language code. Respond only in JSON format with two key-value pairs: \n - 'languageCode': the detected language's ISO code (or undefined if detection fails).\n - 'isError': a boolean value (true if detection fails, false otherwise).\n";
12
+ export declare const WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = "\n You are a language detection assistant. Your task is to analyze the most frequently occurring words in the given text \n and determine the language based on those words. Respond only in JSON format with two key-value pairs: \n - 'languageCode': the detected language's ISO code (or undefined if detection fails).\n - 'isError': a boolean value (true if detection fails, false otherwise).\n";
13
+ export declare const TRANSLATION_SYSTEM_ROLE = "\n You are a translator that responds exclusively in JSON format. Your response must be a JSON object containing:\n - 'data': it is an array of objects. Each object in the array must contain:\n - 'translated_message': The original input message before translation.\n - 'translation: The translated version of 'translated_message', ensuring it is natural, culturally appropriate, and commonly used by native speakers.\n - 'isError': a boolean value (true if translation fails, false otherwise).\n\n Ensure that 'translation' accurately represents the meaning of 'translated_message' while considering cultural nuances, usual expressions, and natural phrasing in the target language. \n Your response must follow this structure strictly.\n";
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DESTINATION_LANGUAGE = exports.DEFAULT_PROMPT = exports.MESSAGE_TYPES = exports.MESSAGE_SENDER = void 0;
3
+ exports.TRANSLATION_SYSTEM_ROLE = exports.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = exports.LANGUAGE_DETECTION_SYSTEM_ROLE = exports.DESTINATION_LANGUAGE = exports.DEFAULT_PROMPT = exports.MESSAGE_TYPES = exports.MESSAGE_SENDER = void 0;
4
4
  // Message sender in conversation history
5
5
  exports.MESSAGE_SENDER = {
6
6
  visitor: 'visitor',
@@ -35,3 +35,28 @@ Also when it is true, do not provide any suggestions.
35
35
  `;
36
36
  // Default destination language
37
37
  exports.DESTINATION_LANGUAGE = 'English';
38
+ // Language detection system role
39
+ exports.LANGUAGE_DETECTION_SYSTEM_ROLE = `
40
+ You are a language detection assistant. Your task is to analyze short text input and determine
41
+ its language using ISO language code. Respond only in JSON format with two key-value pairs:
42
+ - 'languageCode': the detected language's ISO code (or undefined if detection fails).
43
+ - 'isError': a boolean value (true if detection fails, false otherwise).
44
+ `;
45
+ // Word frequency language detection system role
46
+ exports.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = `
47
+ You are a language detection assistant. Your task is to analyze the most frequently occurring words in the given text
48
+ and determine the language based on those words. Respond only in JSON format with two key-value pairs:
49
+ - 'languageCode': the detected language's ISO code (or undefined if detection fails).
50
+ - 'isError': a boolean value (true if detection fails, false otherwise).
51
+ `;
52
+ // Message translation system role
53
+ exports.TRANSLATION_SYSTEM_ROLE = `
54
+ You are a translator that responds exclusively in JSON format. Your response must be a JSON object containing:
55
+ - 'data': it is an array of objects. Each object in the array must contain:
56
+ - 'translated_message': The original input message before translation.
57
+ - 'translation: The translated version of 'translated_message', ensuring it is natural, culturally appropriate, and commonly used by native speakers.
58
+ - 'isError': a boolean value (true if translation fails, false otherwise).
59
+
60
+ Ensure that 'translation' accurately represents the meaning of 'translated_message' while considering cultural nuances, usual expressions, and natural phrasing in the target language.
61
+ Your response must follow this structure strictly.
62
+ `;
@@ -1,17 +1,25 @@
1
- import ChatGptResponse from '../../models/ChatGptResponse';
2
1
  import BaseService from '../BaseService/BaseService';
2
+ import ChatGptResponse from '../../models/ChatGptResponse';
3
3
  export declare class TranslationService extends BaseService {
4
- SYSTEM_ROLE_MESSAGE: string;
5
- FIND_TEXT_LANGUAGE_SYSTEM_ROLE: string;
6
4
  constructor(apiKey?: string, model?: string, apiUrl?: string, maxNumberOfChoices?: number);
7
5
  /**
8
- * request the chatGpt Api to translate a specific test to a desired language
9
- * @param text the text to translate
10
- * @param toLanguage the desired language to translate to ( English is the default )
11
- * @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
6
+ * Request the ChatGPT API to translate a specific test to a desired language
7
+ *
8
+ * @param text the text to translate
9
+ * @param toLanguage the desired language to translate to ( English is the default )
10
+ * @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
12
11
  */
13
12
  translate(text: string, toLanguage?: string, fromLanguage?: string): Promise<ChatGptResponse | undefined>;
13
+ /**
14
+ * Detects the ISO language code of a text
15
+ *
16
+ * @param text text to detect
17
+ */
14
18
  findTextLanguage(text: string): Promise<ChatGptResponse | undefined>;
15
- getFindTextLanguagePromptMessage(text: string): string;
19
+ /**
20
+ * Detects the ISO language code of a text based on frequency of words
21
+ *
22
+ * @param text text to detect
23
+ */
16
24
  findTextLanguageBasedOnFrequency(text: string): Promise<ChatGptResponse | undefined>;
17
25
  }
@@ -13,56 +13,37 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.TranslationService = void 0;
16
+ const constants_1 = require("../../constants");
16
17
  const ChatGptMessage_1 = require("../../models/ChatGptMessage");
17
- const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
18
18
  const logger_1 = __importDefault(require("../logger"));
19
- const constants_1 = require("../../constants");
19
+ const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
20
20
  class TranslationService extends BaseService_1.default {
21
21
  constructor(apiKey, model, apiUrl, maxNumberOfChoices) {
22
22
  super(apiKey, model, apiUrl, maxNumberOfChoices);
23
- Object.defineProperty(this, "SYSTEM_ROLE_MESSAGE", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: 'You are a translator that provides a response in JSON format only. '
28
- + 'The response should be a JSON object with a boolean called \'error\' and \'data,\''
29
- + ' which is an array of objects. Each object in the array will contain a \'translation\' property, '
30
- + 'holding the translation of the message, and a \'translated_message\' property, '
31
- + 'which will contain the original message before being translated.'
32
- + ' Additionally, the \'translation\' property will hold the translation of the \'translated_message\' property.'
33
- });
34
- Object.defineProperty(this, "FIND_TEXT_LANGUAGE_SYSTEM_ROLE", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: 'You are a translator that provides a response in JSON format only—a JSON that contains'
39
- + ' \'languageCode\' and \'isError\' if you cannot detect the language.'
40
- });
41
23
  }
42
24
  /**
43
- * request the chatGpt Api to translate a specific test to a desired language
44
- * @param text the text to translate
45
- * @param toLanguage the desired language to translate to ( English is the default )
46
- * @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
25
+ * Request the ChatGPT API to translate a specific test to a desired language
26
+ *
27
+ * @param text the text to translate
28
+ * @param toLanguage the desired language to translate to ( English is the default )
29
+ * @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
47
30
  */
48
31
  translate(text, toLanguage = constants_1.DESTINATION_LANGUAGE, fromLanguage) {
49
32
  return __awaiter(this, void 0, void 0, function* () {
50
33
  if (!text) {
51
- logger_1.default.error('translation text should be provided');
52
- return Promise.reject(new Error('test must be provided'));
34
+ logger_1.default.error('Translation text should be provided');
35
+ return Promise.reject(new Error('Text must be provided'));
53
36
  }
54
- const desiredLanguage = toLanguage,
55
- // instruction on what to do with the text
56
- translateText = `Translate the following ${fromLanguage} text to ${desiredLanguage}
57
- if it's not already in ${desiredLanguage}: ${text}`,
58
- // the body we need to send to the request
59
- requestBody = {
37
+ const translateText = `
38
+ Translate the following text from ${fromLanguage} to ${toLanguage} if it's not already in ${toLanguage}: ${text}.
39
+ `;
40
+ const requestBody = {
60
41
  model: this.chatGptModel,
61
42
  response_format: { type: 'json_object' },
62
43
  messages: [
63
44
  {
64
45
  role: ChatGptMessage_1.ChatGptRole.SYSTEM,
65
- content: this.SYSTEM_ROLE_MESSAGE,
46
+ content: constants_1.TRANSLATION_SYSTEM_ROLE,
66
47
  },
67
48
  {
68
49
  role: ChatGptMessage_1.ChatGptRole.USER,
@@ -83,22 +64,24 @@ class TranslationService extends BaseService_1.default {
83
64
  }
84
65
  });
85
66
  }
67
+ /**
68
+ * Detects the ISO language code of a text
69
+ *
70
+ * @param text text to detect
71
+ */
86
72
  findTextLanguage(text) {
87
73
  return __awaiter(this, void 0, void 0, function* () {
88
74
  if (!text) {
89
- logger_1.default.error('translation text should be provided');
90
- return Promise.reject(new Error('text must be provided'));
75
+ logger_1.default.error('Translation text should be provided');
76
+ return Promise.reject(new Error('Text must be provided'));
91
77
  }
92
- // instruction on what to do with the text
93
- const translateText = this.getFindTextLanguagePromptMessage(text),
94
- // the body we need to send to the request
95
- requestBody = {
78
+ const translateText = `Detect the ISO language code of this text: '${text}'`, requestBody = {
96
79
  model: this.chatGptModel,
97
80
  response_format: { type: 'json_object' },
98
81
  messages: [
99
82
  {
100
83
  role: ChatGptMessage_1.ChatGptRole.SYSTEM,
101
- content: this.FIND_TEXT_LANGUAGE_SYSTEM_ROLE,
84
+ content: constants_1.LANGUAGE_DETECTION_SYSTEM_ROLE,
102
85
  },
103
86
  {
104
87
  role: ChatGptMessage_1.ChatGptRole.USER,
@@ -119,28 +102,24 @@ class TranslationService extends BaseService_1.default {
119
102
  }
120
103
  });
121
104
  }
122
- getFindTextLanguagePromptMessage(text) {
123
- return `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'}.
124
- Other responses or explanations are not allowed.`;
125
- }
105
+ /**
106
+ * Detects the ISO language code of a text based on frequency of words
107
+ *
108
+ * @param text text to detect
109
+ */
126
110
  findTextLanguageBasedOnFrequency(text) {
127
111
  return __awaiter(this, void 0, void 0, function* () {
128
112
  if (!text) {
129
- logger_1.default.error('translation text should be provided');
130
- return Promise.reject(new Error('text must be provided'));
113
+ logger_1.default.error('Translation text should be provided');
114
+ return Promise.reject(new Error('Text must be provided'));
131
115
  }
132
- text = '```' + text + '```';
133
- // instruction on what to do with the text
134
- const translateText = `Tell me the language ISO code of the enclosed between triple backticks ${text}
135
- based on most words used. Give me the result in just one word, the ISO code of the language with most words.`,
136
- // the body we need to send to the request
137
- requestBody = {
116
+ const translateText = `Detect the ISO language code based on the most words used in this text: '${text}'`, requestBody = {
138
117
  model: this.chatGptModel,
139
118
  response_format: { type: 'json_object' },
140
119
  messages: [
141
120
  {
142
121
  role: ChatGptMessage_1.ChatGptRole.SYSTEM,
143
- content: this.FIND_TEXT_LANGUAGE_SYSTEM_ROLE,
122
+ content: constants_1.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE,
144
123
  },
145
124
  {
146
125
  role: ChatGptMessage_1.ChatGptRole.USER,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.7.18",
3
+ "version": "0.7.19",
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": "b899586229b32621acf33af6870b3664786ad970"
42
+ "gitHead": "92f181d648d5372db54258bf3518092251c855fd"
43
43
  }