@cobrowser/chatgpt 0.6.2 → 0.6.3

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.
@@ -5,7 +5,6 @@ export declare enum ChatGptRole {
5
5
  }
6
6
  export interface ChatGptRequestBody {
7
7
  model: string;
8
- response_format?: {};
9
8
  messages: ChatGptMessage[];
10
9
  }
11
10
  interface ChatGptMessage {
@@ -1,5 +1,4 @@
1
1
  import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
2
- import ChatGptUsageTokens from '../../models/ChatGptUsageTokens';
3
2
  declare class BaseService {
4
3
  request: AxiosInstance;
5
4
  readonly BASE_CHAT_COMPLETION_URL = "https://api.openai.com/v1/chat/completions";
@@ -17,11 +16,6 @@ declare class BaseService {
17
16
  * @param response
18
17
  */
19
18
  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;
25
19
  /**
26
20
  * function to handle requests errors
27
21
  * @param error
@@ -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-1106', maxNumberOfChoices = 1) {
15
+ constructor(apiUrl, model = 'gpt-3.5-turbo', maxNumberOfChoices = 1) {
16
16
  Object.defineProperty(this, "request", {
17
17
  enumerable: true,
18
18
  configurable: true,
@@ -60,19 +60,6 @@ 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
- }
76
63
  /**
77
64
  * function to handle requests errors
78
65
  * @param error
@@ -1,6 +1,5 @@
1
1
  import BaseService from '../BaseService/BaseService';
2
2
  import ChatGptMessage from '../../models/ChatGptMessage';
3
- import CopilotSuggestion from '../../models/CopilotSuggestion';
4
3
  import Item from '../../models/Item';
5
4
  export declare class CopilotService extends BaseService {
6
5
  constructor(apiUrl?: string, model?: string, maxNumberOfChoices?: number);
@@ -16,7 +15,7 @@ export declare class CopilotService extends BaseService {
16
15
  * this will make it easy to ask chatGpt for quick replies
17
16
  * @param conversation
18
17
  */
19
- suggest(conversation: string): Promise<CopilotSuggestion | undefined>;
18
+ suggest(conversation: string): Promise<string | undefined>;
20
19
  /**
21
20
  * this will request chatGPT to recommend product or item based on the whole conversation
22
21
  * @param conversation the whole chat conversation
@@ -53,10 +53,7 @@ class CopilotService extends BaseService_1.default {
53
53
  messages: [mainPromptMessage],
54
54
  };
55
55
  const response = yield this.request.post('', requestBody);
56
- return {
57
- firstChoice: this.getResponseFirstChoice(response),
58
- usageTokens: this.getResponseUsageTokens(response),
59
- };
56
+ return this.getResponseFirstChoice(response);
60
57
  }
61
58
  catch (e) {
62
59
  this.handleErrors(e);
@@ -8,6 +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<any | undefined>;
12
- findTextLanguage(text: string): Promise<any | undefined>;
11
+ translate(text: string, toLanguage?: string, fromLanguage?: string): Promise<string | undefined>;
13
12
  }
@@ -44,61 +44,14 @@ class TranslationService extends BaseService_1.default {
44
44
  // the body we need to send to the request
45
45
  requestBody = {
46
46
  model: this.chatGptModel,
47
- response_format: { type: "json_object" },
48
- messages: [
49
- {
50
- role: ChatGptMessage_1.ChatGptRole.SYSTEM,
51
- content: 'Your a translator that providees a response in JSON format only, the response should be a json having a boolean called error and data which is array of objects, the array of objects will contain objects, each object will have translation property which will hold the translation of the message and the translated_message property will contain the the original message before being translated',
52
- },
53
- {
47
+ messages: [{
54
48
  role: ChatGptMessage_1.ChatGptRole.USER,
55
49
  content: translateText,
56
- }
57
- ],
50
+ }],
58
51
  };
59
52
  try {
60
53
  const response = yield this.request.post('', requestBody);
61
- return {
62
- firstChoice: this.getResponseFirstChoice(response),
63
- usageTokens: this.getResponseUsageTokens(response),
64
- };
65
- }
66
- catch (e) {
67
- this.handleErrors(e);
68
- return Promise.reject(e);
69
- }
70
- });
71
- }
72
- findTextLanguage(text) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- if (!text) {
75
- logger_1.default.error('translation text should be provided');
76
- return Promise.reject(new Error('text must be provided'));
77
- }
78
- // instruction on what to do with the text
79
- const translateText = `In which language is this text in '${text}', The response should be
80
- {"languageCode": "Code_here", "isError": 'true or false based on if you did fine the language code or not'}
81
- other responses or explanation are not allowed `,
82
- // the body we need to send to the request
83
- requestBody = {
84
- model: this.chatGptModel,
85
- messages: [
86
- {
87
- role: ChatGptMessage_1.ChatGptRole.SYSTEM,
88
- content: 'Your a translator that providees a response in JSON format only, a JSON that contains languageCode and isError if you cannot detect the language',
89
- },
90
- {
91
- role: ChatGptMessage_1.ChatGptRole.USER,
92
- content: translateText,
93
- }
94
- ],
95
- };
96
- try {
97
- const response = yield this.request.post('', requestBody);
98
- return {
99
- firstChoice: this.getResponseFirstChoice(response),
100
- usageTokens: this.getResponseUsageTokens(response),
101
- };
54
+ return this.getResponseFirstChoice(response);
102
55
  }
103
56
  catch (e) {
104
57
  this.handleErrors(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "chatgpt services to connect our projects with chatgpt api",
5
5
  "keywords": [
6
6
  "chatgpt",
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@cobrowser/logger": "^1.0.0",
25
- "axios": "^1.3.5",
26
- "axios-mock-adapter": "^1.21.4"
25
+ "axios": "^1.6.1",
26
+ "axios-mock-adapter": "^1.22.0"
27
27
  },
28
28
  "directories": {
29
29
  "dist": "dist"
@@ -38,5 +38,5 @@
38
38
  "bugs": {
39
39
  "url": "https://bitbucket.org/cobrowser/cb_utils/issues"
40
40
  },
41
- "gitHead": "d63bcaa212c0bb8f870479c1e001478d3e578980"
41
+ "gitHead": "776fc3a9a455791a81880f83df04eb178aff5760"
42
42
  }
@@ -1,6 +0,0 @@
1
- interface ChatGptUsageTokens {
2
- prompt_tokens: Number;
3
- completion_tokens: Number;
4
- total_tokens: Number;
5
- }
6
- export default ChatGptUsageTokens;
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- ;
@@ -1,6 +0,0 @@
1
- import ChatGptUsageTokens from "./ChatGptUsageTokens";
2
- interface CopilotSuggestion {
3
- firstChoice?: string;
4
- usageTokens?: ChatGptUsageTokens;
5
- }
6
- export default CopilotSuggestion;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });