fg-lib-gpt 1.1.1 → 1.1.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.
Files changed (2) hide show
  1. package/main.js +32 -31
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -1,10 +1,10 @@
1
- const axios = require('axios')
1
+ const axios = require('axios');
2
2
 
3
- const OPENAI_API_KEY = process.env.OPENAI_API_KEY
3
+ const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
4
4
 
5
- const DEFAULT_MODEL = process.env.GPT_DEFAULT_MODEL || 'gpt-4o-mini'
6
- const DEFAULT_TIMEOUT = process.env.GPT_TIMEOUT || 180000
7
- const DEFAULT_TEMPERATURE = process.env.GPT_TEMPERATURE || 0.7
5
+ const DEFAULT_MODEL = process.env.GPT_DEFAULT_MODEL || 'gpt-4o-mini';
6
+ const DEFAULT_TIMEOUT = process.env.GPT_TIMEOUT || 180000;
7
+ const DEFAULT_TEMPERATURE = process.env.GPT_TEMPERATURE || 0.7;
8
8
 
9
9
 
10
10
  const doGpt = async ({
@@ -13,51 +13,51 @@ const doGpt = async ({
13
13
  timeout = DEFAULT_TIMEOUT,
14
14
  temperature = DEFAULT_TEMPERATURE,
15
15
  validate,
16
- response_format = { 'type': 'text' }
16
+ response_format = { 'type': 'text' },
17
+ max_tokens
17
18
  }) => {
18
19
 
20
+ const json = response_format.type === 'json' || response_format.type === 'json_schema';
21
+
19
22
  try {
20
23
  const response = await Promise.race([
21
- getGptResponse({ messages, model, temperature, response_format }),
24
+ getGptResponse({ messages, model, temperature, response_format, max_tokens }),
22
25
  new Promise((_, reject) =>
23
26
  setTimeout(() => reject(new Error('Custom timeout')), timeout))
24
- ])
27
+ ]);
25
28
 
26
29
  if (typeof validate === 'function')
27
- validate(response)
28
-
29
-
30
- const json = response_format.type === 'json' || response_format.type === 'json_schema'
30
+ validate(response);
31
31
 
32
32
  // JSON.parse might also fail
33
33
  return {
34
34
  success: true,
35
35
  content: json ? JSON.parse(response) : response
36
- }
36
+ };
37
37
  }
38
38
  catch (e) {
39
- console.error('**** ERROR ***** OpenAI Error (1): ' + e.message)
39
+ console.error('**** ERROR ***** OpenAI Error (1): ' + e.message);
40
40
  try {
41
- const response = await getGptResponse({ messages, model, temperature, response_format })
41
+ const response = await getGptResponse({ messages, model, temperature, response_format, max_tokens });
42
42
 
43
43
  return {
44
44
  success: true,
45
45
  content: json ? JSON.parse(response) : response
46
- }
46
+ };
47
47
  }
48
48
  catch (e2) {
49
- console.error('**** ERROR ***** OpenAI Error (2): ' + e2.message)
49
+ console.error('**** ERROR ***** OpenAI Error (2): ' + e2.message);
50
50
  return {
51
51
  success: false,
52
52
  content: '🚫 An error occurred. Please try again. If the problem persists, please contact @fgalkov.'
53
- }
54
- }
55
- }
56
- }
53
+ };
54
+ };
55
+ };
56
+ };
57
57
 
58
58
 
59
59
 
60
- const getGptResponse = async ({ messages, model, response_format, temperature }) => {
60
+ const getGptResponse = async ({ messages, model, response_format, temperature, max_tokens }) => {
61
61
 
62
62
  const res = await axios.post(
63
63
  'https://api.openai.com/v1/chat/completions',
@@ -65,7 +65,8 @@ const getGptResponse = async ({ messages, model, response_format, temperature })
65
65
  messages,
66
66
  model,
67
67
  response_format,
68
- temperature
68
+ temperature,
69
+ max_completion_tokens: max_tokens
69
70
  },
70
71
  {
71
72
  headers: {
@@ -73,9 +74,9 @@ const getGptResponse = async ({ messages, model, response_format, temperature })
73
74
  'Content-Type': 'application/json',
74
75
  }
75
76
  }
76
- )
77
- return res.data.choices[0].message.content.trim()
78
- }
77
+ );
78
+ return res.data.choices[0].message.content.trim();
79
+ };
79
80
 
80
81
 
81
82
 
@@ -121,18 +122,18 @@ const generateSchema = (schemaObj, name = 'schema') => {
121
122
  return { type: 'boolean' }
122
123
 
123
124
  else
124
- return {}
125
- }
125
+ return {};
126
+ };
126
127
 
127
128
  return {
128
129
  name,
129
130
  strict: true,
130
131
  schema: schema(schemaObj)
131
- }
132
- }
132
+ };
133
+ };
133
134
 
134
135
 
135
136
  module.exports = {
136
137
  doGpt: doGpt,
137
138
  generateSchema: generateSchema,
138
- }
139
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fg-lib-gpt",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"