@cobrowser/chatgpt 0.5.3 → 0.5.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,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);
@@ -8,5 +8,5 @@ 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<string | undefined>;
11
+ translate(text: string, toLanguage?: string, fromLanguage?: string): Promise<any | undefined>;
12
12
  }
@@ -51,7 +51,10 @@ class TranslationService extends BaseService_1.default {
51
51
  };
52
52
  try {
53
53
  const response = yield this.request.post('', requestBody);
54
- return this.getResponseFirstChoice(response);
54
+ return {
55
+ firstChoice: this.getResponseFirstChoice(response),
56
+ usageTokens: this.getResponseUsageTokens(response),
57
+ };
55
58
  }
56
59
  catch (e) {
57
60
  this.handleErrors(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.5.3",
3
+ "version": "0.5.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": "e68ebdaa78a548e653392a939a508d3e55e47494"
41
+ "gitHead": "919a4a49029fcd6c8d4c100bd55ed436dee5835c"
42
42
  }