fg-lib-gpt 1.0.0 → 1.1.1

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