@cobrowser/chatgpt 0.6.3 → 0.6.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.
@@ -0,0 +1,10 @@
1
+ export declare const MESSAGE_SENDER: {
2
+ visitor: string;
3
+ chatbot: string;
4
+ };
5
+ export declare const MESSAGE_TYPES: {
6
+ text: string;
7
+ button: string;
8
+ };
9
+ export declare const DEFAULT_CONTEXT = "You are a helpful assistant. Continue the conversation based on the previous conversations \n mentioned below. Keep your answers less than 200 characters. Any questions by the visitor out of the context\n should be politely declined. \n\n";
10
+ export declare const DESTINATION_LANGUAGE = "English";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DESTINATION_LANGUAGE = exports.DEFAULT_CONTEXT = exports.MESSAGE_TYPES = exports.MESSAGE_SENDER = void 0;
4
+ // Message sender in conversation history
5
+ exports.MESSAGE_SENDER = {
6
+ visitor: 'visitor',
7
+ chatbot: 'chatbot'
8
+ };
9
+ // Message types in conversation history
10
+ exports.MESSAGE_TYPES = {
11
+ text: 'text',
12
+ button: 'button'
13
+ };
14
+ // Default context for ChatGPT
15
+ exports.DEFAULT_CONTEXT = `You are a helpful assistant. Continue the conversation based on the previous conversations
16
+ mentioned below. Keep your answers less than 200 characters. Any questions by the visitor out of the context
17
+ should be politely declined. \n
18
+ `;
19
+ // Default destination language
20
+ exports.DESTINATION_LANGUAGE = 'English';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './services/TranslationService/TranslationService';
2
2
  export * from './services/CopilotService/CopilotService';
3
+ export * from './services/ChatService/ChatService';
package/dist/index.js CHANGED
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./services/TranslationService/TranslationService"), exports);
18
18
  __exportStar(require("./services/CopilotService/CopilotService"), exports);
19
+ __exportStar(require("./services/ChatService/ChatService"), exports);
@@ -0,0 +1,13 @@
1
+ interface ConversationHistory {
2
+ from: string;
3
+ to: string;
4
+ datetime: number;
5
+ body: MessageBody[];
6
+ }
7
+ interface MessageBody {
8
+ type: string;
9
+ text?: string;
10
+ id?: string;
11
+ label?: string;
12
+ }
13
+ export default ConversationHistory;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -10,7 +10,7 @@ declare class BaseService {
10
10
  * @param model the main chatGPT model to use
11
11
  * @param maxNumberOfChoices How many chat completion choices to generate for each request.
12
12
  */
13
- constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
13
+ constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number, apiKey?: string | undefined);
14
14
  /**
15
15
  * Filter the response from chatGpt API and only get the first choice
16
16
  * @param response
@@ -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', maxNumberOfChoices = 1, apiKey = process.env.OPEN_AI_API_KEY) {
16
16
  Object.defineProperty(this, "request", {
17
17
  enumerable: true,
18
18
  configurable: true,
@@ -41,7 +41,7 @@ class BaseService {
41
41
  baseURL: apiUrl || this.BASE_CHAT_COMPLETION_URL,
42
42
  headers: {
43
43
  'Content-Type': 'application/json',
44
- Authorization: `Bearer ${process.env.OPEN_AI_API_KEY} `,
44
+ Authorization: `Bearer ${apiKey} `,
45
45
  },
46
46
  });
47
47
  this.chatGptModel = model;
@@ -0,0 +1,8 @@
1
+ import BaseService from '../BaseService/BaseService';
2
+ import ConversationHistory from 'src/models/ConversationHistory';
3
+ export declare class ChatService extends BaseService {
4
+ #private;
5
+ model: string | undefined;
6
+ constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number, apiKey?: string);
7
+ ask(context: string | undefined, conversationHistory: ConversationHistory[]): Promise<string | undefined>;
8
+ }
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ var _ChatService_instances, _ChatService_formatConversationHistory;
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.ChatService = void 0;
22
+ const ChatGptMessage_1 = require("../../models/ChatGptMessage");
23
+ const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
24
+ const logger_1 = __importDefault(require("../logger"));
25
+ const constants_1 = require("../../constants");
26
+ class ChatService extends BaseService_1.default {
27
+ constructor(apiUrl, model, maxNumberOfChoices, apiKey) {
28
+ super(apiUrl, model, maxNumberOfChoices, apiKey);
29
+ _ChatService_instances.add(this);
30
+ Object.defineProperty(this, "model", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ this.model = model;
37
+ }
38
+ ask(context, conversationHistory) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if (!(conversationHistory === null || conversationHistory === void 0 ? void 0 : conversationHistory.length)) {
41
+ logger_1.default.error(':: ChatService.ask :: Conversation history must be provided');
42
+ return Promise.reject(new Error('Conversation history must be provided'));
43
+ }
44
+ const requestBody = {
45
+ model: this.model,
46
+ messages: __classPrivateFieldGet(this, _ChatService_instances, "m", _ChatService_formatConversationHistory).call(this, context, conversationHistory),
47
+ };
48
+ logger_1.default.info(`:: ChatService.ask :: ${JSON.stringify(requestBody)}`);
49
+ try {
50
+ const response = yield this.request.post('', requestBody);
51
+ return this.getResponseFirstChoice(response);
52
+ }
53
+ catch (e) {
54
+ this.handleErrors(e);
55
+ return Promise.reject(e);
56
+ }
57
+ });
58
+ }
59
+ }
60
+ exports.ChatService = ChatService;
61
+ _ChatService_instances = new WeakSet(), _ChatService_formatConversationHistory = function _ChatService_formatConversationHistory(context, conversationHistory) {
62
+ const formatted = [
63
+ {
64
+ role: ChatGptMessage_1.ChatGptRole.SYSTEM,
65
+ content: context || constants_1.DEFAULT_CONTEXT,
66
+ }
67
+ ];
68
+ if (conversationHistory.length) {
69
+ for (const message of conversationHistory) {
70
+ const messageBody = message.body;
71
+ for (const body of messageBody) {
72
+ if (body.type === constants_1.MESSAGE_TYPES.text && message.from === constants_1.MESSAGE_SENDER.visitor) {
73
+ formatted.push({
74
+ role: ChatGptMessage_1.ChatGptRole.USER,
75
+ content: body.text
76
+ });
77
+ }
78
+ else if (body.type === constants_1.MESSAGE_TYPES.text && message.from === constants_1.MESSAGE_SENDER.chatbot) {
79
+ formatted.push({
80
+ role: ChatGptMessage_1.ChatGptRole.ASSISTANT,
81
+ content: body.text
82
+ });
83
+ }
84
+ else if (body.type === constants_1.MESSAGE_TYPES.button && message.from === constants_1.MESSAGE_SENDER.chatbot) {
85
+ formatted.push({
86
+ role: ChatGptMessage_1.ChatGptRole.ASSISTANT,
87
+ content: body.label
88
+ });
89
+ }
90
+ }
91
+ }
92
+ }
93
+ return formatted;
94
+ };
@@ -2,7 +2,7 @@ import BaseService from '../BaseService/BaseService';
2
2
  import ChatGptMessage from '../../models/ChatGptMessage';
3
3
  import Item from '../../models/Item';
4
4
  export declare class CopilotService extends BaseService {
5
- constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
5
+ constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number, apiKey?: string);
6
6
  /**
7
7
  * a method to be used to suggest some quick replies for the agent based on the conversation
8
8
  * will be added as a single string in the format
@@ -17,8 +17,8 @@ const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
17
17
  const logger_1 = __importDefault(require("../logger"));
18
18
  const ChatGptMessage_1 = require("../../models/ChatGptMessage");
19
19
  class CopilotService extends BaseService_1.default {
20
- constructor(apiUrl, model, maxNumberOfChoices) {
21
- super(apiUrl, model, maxNumberOfChoices);
20
+ constructor(apiUrl, model, maxNumberOfChoices, apiKey) {
21
+ super(apiUrl, model, maxNumberOfChoices, apiKey);
22
22
  }
23
23
  /**
24
24
  * a method to be used to suggest some quick replies for the agent based on the conversation
@@ -1,7 +1,6 @@
1
1
  import BaseService from '../BaseService/BaseService';
2
2
  export declare class TranslationService extends BaseService {
3
- DESTINATION_LANGUAGE: string;
4
- constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
3
+ constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number, apiKey?: string);
5
4
  /**
6
5
  * request the chatGpt Api to translate a specific test to a desired language
7
6
  * @param text the text to translate
@@ -16,15 +16,10 @@ exports.TranslationService = void 0;
16
16
  const ChatGptMessage_1 = require("../../models/ChatGptMessage");
17
17
  const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
18
18
  const logger_1 = __importDefault(require("../logger"));
19
+ const constants_1 = require("../../constants");
19
20
  class TranslationService extends BaseService_1.default {
20
- constructor(apiUrl, model, maxNumberOfChoices) {
21
- super(apiUrl, model, maxNumberOfChoices);
22
- Object.defineProperty(this, "DESTINATION_LANGUAGE", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: 'English'
27
- });
21
+ constructor(apiUrl, model, maxNumberOfChoices, apiKey) {
22
+ super(apiUrl, model, maxNumberOfChoices, apiKey);
28
23
  }
29
24
  /**
30
25
  * request the chatGpt Api to translate a specific test to a desired language
@@ -32,7 +27,7 @@ class TranslationService extends BaseService_1.default {
32
27
  * @param toLanguage the desired language to translate to ( English is the default )
33
28
  * @param fromLanguage if we want to specify the language of the text, we can use this ( optional)
34
29
  */
35
- translate(text, toLanguage = this.DESTINATION_LANGUAGE, fromLanguage) {
30
+ translate(text, toLanguage = constants_1.DESTINATION_LANGUAGE, fromLanguage) {
36
31
  return __awaiter(this, void 0, void 0, function* () {
37
32
  if (!text) {
38
33
  logger_1.default.error('translation text should be provided');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "chatgpt services to connect our projects with chatgpt api",
5
5
  "keywords": [
6
6
  "chatgpt",
@@ -38,5 +38,5 @@
38
38
  "bugs": {
39
39
  "url": "https://bitbucket.org/cobrowser/cb_utils/issues"
40
40
  },
41
- "gitHead": "776fc3a9a455791a81880f83df04eb178aff5760"
41
+ "gitHead": "9a912216d362a55aff403525d19c21ff9c31b513"
42
42
  }