@cobrowser/chatgpt 0.4.1 → 0.5.2

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/README.md CHANGED
@@ -13,6 +13,29 @@ the API key will be used to make authorization with the openAI APIs
13
13
 
14
14
  we will have a list of services to use for different purposes
15
15
 
16
+ ### CoPilotService
17
+ a service that can be used to make products recommendation and quick replies suggestion
18
+
19
+ **Usage**
20
+
21
+ to suggest some quick replies for a conversation
22
+
23
+ ```
24
+ const { CopilotService } = require('@cobrowser/chatgpt');
25
+ const copilotService = new CopilotService();
26
+ const conversation = `
27
+ Customer: Hello
28
+ Agent: Hello
29
+ Customer: Need some help
30
+ `;
31
+
32
+ /**
33
+ * return a promise that will resolve a string ( a list of suggestions ) or an undefined or an error
34
+ */
35
+ copilotService.suggest(conversation);
36
+
37
+ ```
38
+
16
39
  ### Translation Service
17
40
  can be used to translate a text from one language to another
18
41
 
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './services/TranslationService/TranslationService';
2
+ export * from './services/CopilotService/CopilotService';
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -10,4 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ const CopilotService_1 = require("./services/CopilotService/CopilotService");
13
18
  __exportStar(require("./services/TranslationService/TranslationService"), exports);
19
+ __exportStar(require("./services/CopilotService/CopilotService"), exports);
20
+ const copilotService = new CopilotService_1.CopilotService();
21
+ const conversation = `
22
+ Customer: Hello
23
+ Agent: Hello
24
+ Customer: Need some help
25
+ `;
26
+ /**
27
+ * return a promise that will resolve a string ( a list of suggestions ) or an undefined or an error
28
+ */
29
+ copilotService.suggest(conversation);
@@ -0,0 +1,10 @@
1
+ interface Item {
2
+ id?: string;
3
+ name?: string;
4
+ description?: string;
5
+ link?: string;
6
+ price: number;
7
+ category?: string;
8
+ rating?: string;
9
+ }
10
+ export default Item;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,25 @@
1
+ import BaseService from '../BaseService/BaseService';
2
+ import ChatGptMessage from '../../models/ChatGptMessage';
3
+ import Item from '../../models/Item';
4
+ export declare class CopilotService extends BaseService {
5
+ constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
6
+ /**
7
+ * a method to be used to suggest some quick replies for the agent based on the conversation
8
+ * will be added as a single string in the format
9
+ * ```
10
+ * customer: hi
11
+ * agent: hi
12
+ * customer: I need some help
13
+ * ```
14
+ *
15
+ * this will make it easy to ask chatGpt for quick replies
16
+ * @param conversation
17
+ */
18
+ suggest(conversation: string): Promise<string | undefined>;
19
+ /**
20
+ * this will request chatGPT to recommend product or item based on the whole conversation
21
+ * @param conversation the whole chat conversation
22
+ * @param items the items to recommend from
23
+ */
24
+ recommend(conversation: ChatGptMessage[], items: Item[]): Promise<string | undefined>;
25
+ }
@@ -0,0 +1,98 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CopilotService = void 0;
16
+ const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
17
+ const logger_1 = __importDefault(require("../logger"));
18
+ const ChatGptMessage_1 = require("../../models/ChatGptMessage");
19
+ class CopilotService extends BaseService_1.default {
20
+ constructor(apiUrl, model, maxNumberOfChoices) {
21
+ super(apiUrl, model, maxNumberOfChoices);
22
+ }
23
+ /**
24
+ * a method to be used to suggest some quick replies for the agent based on the conversation
25
+ * will be added as a single string in the format
26
+ * ```
27
+ * customer: hi
28
+ * agent: hi
29
+ * customer: I need some help
30
+ * ```
31
+ *
32
+ * this will make it easy to ask chatGpt for quick replies
33
+ * @param conversation
34
+ */
35
+ suggest(conversation) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ if (!(conversation === null || conversation === void 0 ? void 0 : conversation.length)) {
38
+ logger_1.default.error('conversation must be provided');
39
+ return Promise.reject(new Error('conversation must be provided'));
40
+ }
41
+ try {
42
+ // main prompt used to instruct chatGPT to suggest some quick replies
43
+ const mainPrompt = `Propose some quick replies for the agent to be used on the following conversation
44
+ display your response as unordered list:-\n${conversation}`;
45
+ // main prompt will be the whole conversation as string
46
+ const mainPromptMessage = {
47
+ role: ChatGptMessage_1.ChatGptRole.USER,
48
+ content: mainPrompt,
49
+ };
50
+ // the body we need to send to the request
51
+ const requestBody = {
52
+ model: this.chatGptModel,
53
+ messages: [mainPromptMessage],
54
+ };
55
+ const response = yield this.request.post('', requestBody);
56
+ return this.getResponseFirstChoice(response);
57
+ }
58
+ catch (e) {
59
+ this.handleErrors(e);
60
+ return Promise.reject(e);
61
+ }
62
+ });
63
+ }
64
+ /**
65
+ * this will request chatGPT to recommend product or item based on the whole conversation
66
+ * @param conversation the whole chat conversation
67
+ * @param items the items to recommend from
68
+ */
69
+ recommend(conversation, items) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ if (!(items === null || items === void 0 ? void 0 : items.length) || !(conversation === null || conversation === void 0 ? void 0 : conversation.length)) {
72
+ logger_1.default.error('conversation or items not found');
73
+ return Promise.reject(new Error('conversation and items must be provided'));
74
+ }
75
+ try {
76
+ // main prompt used to instruct chatGPT to recommend products
77
+ const mainPrompt = `we have this list of Items: ${JSON.stringify(items)}, what to recommend for you`;
78
+ // main prompt will be the first message of the whole conversation
79
+ const firstMessage = {
80
+ role: ChatGptMessage_1.ChatGptRole.ASSISTANT,
81
+ content: mainPrompt,
82
+ };
83
+ // the body we need to send to the request
84
+ const requestBody = {
85
+ model: this.chatGptModel,
86
+ messages: [firstMessage, ...conversation],
87
+ };
88
+ const response = yield this.request.post('', requestBody);
89
+ return this.getResponseFirstChoice(response);
90
+ }
91
+ catch (e) {
92
+ this.handleErrors(e);
93
+ return Promise.reject(e);
94
+ }
95
+ });
96
+ }
97
+ }
98
+ exports.CopilotService = CopilotService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.4.1",
3
+ "version": "0.5.2",
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": "77659f6a9a47d7199bc70985aad19ac8e7616355"
41
+ "gitHead": "42de31ffab1af1627e6c16a19b19b5861d477088"
42
42
  }