@cobrowser/chatgpt 0.5.2 → 0.5.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/index.js CHANGED
@@ -14,16 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- const CopilotService_1 = require("./services/CopilotService/CopilotService");
18
17
  __exportStar(require("./services/TranslationService/TranslationService"), exports);
19
18
  __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,6 @@
1
+ interface ChatGptUsageTokens {
2
+ prompt_tokens: Number;
3
+ completion_tokens: Number;
4
+ total_tokens: Number;
5
+ }
6
+ export default ChatGptUsageTokens;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
@@ -0,0 +1,6 @@
1
+ import ChatGptUsageTokens from "./ChatGptUsageTokens";
2
+ interface CopilotSuggestion {
3
+ firstChoice?: string;
4
+ usageTokens?: ChatGptUsageTokens;
5
+ }
6
+ export default CopilotSuggestion;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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
@@ -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<string | undefined>;
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 this.getResponseFirstChoice(response);
56
+ return {
57
+ firstChoice: this.getResponseFirstChoice(response),
58
+ usageTokens: this.getResponseUsageTokens(response),
59
+ };
57
60
  }
58
61
  catch (e) {
59
62
  this.handleErrors(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
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": "42de31ffab1af1627e6c16a19b19b5861d477088"
41
+ "gitHead": "e1faace27b50c02cb93e2d4c124ba2a36cdf589a"
42
42
  }