@leikeduntech/leiai-js 4.0.1 → 4.0.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.
- package/LICENSE.txt +5 -5
- package/bin/cli.js +146 -146
- package/build/index.d.ts +1 -0
- package/build/index.js +1287 -1284
- package/package.json +83 -83
package/LICENSE.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
Proprietary License
|
|
2
|
-
|
|
3
|
-
This software is proprietary and confidential. Unauthorized copying of this file, via any medium, is strictly prohibited.
|
|
4
|
-
The usage of this software is governed by the terms set out in the license agreement between the user and the software provider.
|
|
5
|
-
© 2023 Shanghai Leikedun Technology Co., Ltd. All rights reserved.
|
|
1
|
+
Proprietary License
|
|
2
|
+
|
|
3
|
+
This software is proprietary and confidential. Unauthorized copying of this file, via any medium, is strictly prohibited.
|
|
4
|
+
The usage of this software is governed by the terms set out in the license agreement between the user and the software provider.
|
|
5
|
+
© 2023 Shanghai Leikedun Technology Co., Ltd. All rights reserved.
|
package/bin/cli.js
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import crypto from 'node:crypto'
|
|
3
|
-
|
|
4
|
-
import * as url from 'url'
|
|
5
|
-
import { cac } from 'cac'
|
|
6
|
-
import Conf from 'conf'
|
|
7
|
-
import { readPackageUp } from 'read-pkg-up'
|
|
8
|
-
|
|
9
|
-
import { ChatGPTAPI } from '../build/index.js'
|
|
10
|
-
|
|
11
|
-
async function main() {
|
|
12
|
-
const dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
|
13
|
-
const pkg = await readPackageUp({ cwd: dirname })
|
|
14
|
-
const version = (pkg && pkg.packageJson && pkg.packageJson.version) || '4'
|
|
15
|
-
const config = new Conf({ projectName: 'chatgpt' })
|
|
16
|
-
|
|
17
|
-
const cli = cac('chatgpt')
|
|
18
|
-
cli
|
|
19
|
-
.command('<prompt>', 'Ask ChatGPT a question')
|
|
20
|
-
.option('-c, --continue', 'Continue last conversation', {
|
|
21
|
-
default: false
|
|
22
|
-
})
|
|
23
|
-
.option('-d, --debug', 'Enables debug logging', {
|
|
24
|
-
default: false
|
|
25
|
-
})
|
|
26
|
-
.option('-s, --stream', 'Streams the response', {
|
|
27
|
-
default: true
|
|
28
|
-
})
|
|
29
|
-
.option('-s, --store', 'Enables the local message cache', {
|
|
30
|
-
default: true
|
|
31
|
-
})
|
|
32
|
-
.option('-t, --timeout <timeout>', 'Timeout in milliseconds')
|
|
33
|
-
.option('-k, --apiKey <apiKey>', 'OpenAI API key')
|
|
34
|
-
.option('-o, --apiOrg <apiOrg>', 'OpenAI API key')
|
|
35
|
-
.option('-m, --model <model>', 'Model (gpt-3.5-turbo, gpt-4)', {
|
|
36
|
-
default: 'gpt-3.5-turbo'
|
|
37
|
-
})
|
|
38
|
-
.option(
|
|
39
|
-
'-n, --conversationName <conversationName>',
|
|
40
|
-
'Unique name for the conversation'
|
|
41
|
-
)
|
|
42
|
-
.action(async (prompt, options) => {
|
|
43
|
-
const apiOrg = options.apiOrg || process.env.OPENAI_API_ORG
|
|
44
|
-
const apiKey = options.apiKey || process.env.OPENAI_API_KEY
|
|
45
|
-
if (!apiKey) {
|
|
46
|
-
console.error('error: either set OPENAI_API_KEY or use --apiKey\n')
|
|
47
|
-
cli.outputHelp()
|
|
48
|
-
process.exit(1)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const apiKeyHash = hash(apiKey)
|
|
52
|
-
const conversationName = options.conversationName || 'default'
|
|
53
|
-
const conversationKey = `${conversationName}:${apiKeyHash}`
|
|
54
|
-
const conversation =
|
|
55
|
-
options.continue && options.store
|
|
56
|
-
? config.get(conversationKey, {}) || {}
|
|
57
|
-
: {}
|
|
58
|
-
const model = options.model
|
|
59
|
-
let conversationId = undefined
|
|
60
|
-
let parentMessageId = undefined
|
|
61
|
-
|
|
62
|
-
if (conversation.lastMessageId) {
|
|
63
|
-
const lastMessage = conversation[conversation.lastMessageId]
|
|
64
|
-
if (lastMessage) {
|
|
65
|
-
conversationId = lastMessage.conversationId
|
|
66
|
-
parentMessageId = lastMessage.id
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (options.debug) {
|
|
71
|
-
console.log('using config', config.path)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const api = new ChatGPTAPI({
|
|
75
|
-
apiKey,
|
|
76
|
-
apiOrg,
|
|
77
|
-
debug: options.debug,
|
|
78
|
-
completionParams: {
|
|
79
|
-
model
|
|
80
|
-
},
|
|
81
|
-
getMessageById: async (id) => {
|
|
82
|
-
if (options.store) {
|
|
83
|
-
return conversation[id]
|
|
84
|
-
} else {
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
upsertMessage: async (message) => {
|
|
89
|
-
if (options.store) {
|
|
90
|
-
conversation[message.id] = message
|
|
91
|
-
conversation.lastMessageId = message.id
|
|
92
|
-
config.set(conversationKey, conversation)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const res = await api.sendMessage(prompt, {
|
|
98
|
-
conversationId,
|
|
99
|
-
parentMessageId,
|
|
100
|
-
timeoutMs: options.timeout || undefined,
|
|
101
|
-
onProgress: options.stream
|
|
102
|
-
? (progress) => {
|
|
103
|
-
if (progress.delta) {
|
|
104
|
-
process.stdout.write(progress.delta)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
: undefined
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
if (options.stream) {
|
|
111
|
-
process.stdout.write('\n')
|
|
112
|
-
} else {
|
|
113
|
-
console.log(res.text)
|
|
114
|
-
}
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
cli.command('rm-cache', 'Clears the local message cache').action(() => {
|
|
118
|
-
config.clear()
|
|
119
|
-
console.log('cleared cache', config.path)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
cli.command('ls-cache', 'Prints the local message cache path').action(() => {
|
|
123
|
-
console.log(config.path)
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
cli.help()
|
|
127
|
-
cli.version(version)
|
|
128
|
-
|
|
129
|
-
try {
|
|
130
|
-
cli.parse()
|
|
131
|
-
} catch (err) {
|
|
132
|
-
console.error(`error: ${err.message}\n`)
|
|
133
|
-
cli.outputHelp()
|
|
134
|
-
process.exit(1)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function hash(d) {
|
|
139
|
-
const buffer = Buffer.isBuffer(d) ? d : Buffer.from(d.toString())
|
|
140
|
-
return crypto.createHash('sha256').update(buffer).digest('hex')
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
main().catch((err) => {
|
|
144
|
-
console.error(err)
|
|
145
|
-
process.exit(1)
|
|
146
|
-
})
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import crypto from 'node:crypto'
|
|
3
|
+
|
|
4
|
+
import * as url from 'url'
|
|
5
|
+
import { cac } from 'cac'
|
|
6
|
+
import Conf from 'conf'
|
|
7
|
+
import { readPackageUp } from 'read-pkg-up'
|
|
8
|
+
|
|
9
|
+
import { ChatGPTAPI } from '../build/index.js'
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
const dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
|
13
|
+
const pkg = await readPackageUp({ cwd: dirname })
|
|
14
|
+
const version = (pkg && pkg.packageJson && pkg.packageJson.version) || '4'
|
|
15
|
+
const config = new Conf({ projectName: 'chatgpt' })
|
|
16
|
+
|
|
17
|
+
const cli = cac('chatgpt')
|
|
18
|
+
cli
|
|
19
|
+
.command('<prompt>', 'Ask ChatGPT a question')
|
|
20
|
+
.option('-c, --continue', 'Continue last conversation', {
|
|
21
|
+
default: false
|
|
22
|
+
})
|
|
23
|
+
.option('-d, --debug', 'Enables debug logging', {
|
|
24
|
+
default: false
|
|
25
|
+
})
|
|
26
|
+
.option('-s, --stream', 'Streams the response', {
|
|
27
|
+
default: true
|
|
28
|
+
})
|
|
29
|
+
.option('-s, --store', 'Enables the local message cache', {
|
|
30
|
+
default: true
|
|
31
|
+
})
|
|
32
|
+
.option('-t, --timeout <timeout>', 'Timeout in milliseconds')
|
|
33
|
+
.option('-k, --apiKey <apiKey>', 'OpenAI API key')
|
|
34
|
+
.option('-o, --apiOrg <apiOrg>', 'OpenAI API key')
|
|
35
|
+
.option('-m, --model <model>', 'Model (gpt-3.5-turbo, gpt-4)', {
|
|
36
|
+
default: 'gpt-3.5-turbo'
|
|
37
|
+
})
|
|
38
|
+
.option(
|
|
39
|
+
'-n, --conversationName <conversationName>',
|
|
40
|
+
'Unique name for the conversation'
|
|
41
|
+
)
|
|
42
|
+
.action(async (prompt, options) => {
|
|
43
|
+
const apiOrg = options.apiOrg || process.env.OPENAI_API_ORG
|
|
44
|
+
const apiKey = options.apiKey || process.env.OPENAI_API_KEY
|
|
45
|
+
if (!apiKey) {
|
|
46
|
+
console.error('error: either set OPENAI_API_KEY or use --apiKey\n')
|
|
47
|
+
cli.outputHelp()
|
|
48
|
+
process.exit(1)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const apiKeyHash = hash(apiKey)
|
|
52
|
+
const conversationName = options.conversationName || 'default'
|
|
53
|
+
const conversationKey = `${conversationName}:${apiKeyHash}`
|
|
54
|
+
const conversation =
|
|
55
|
+
options.continue && options.store
|
|
56
|
+
? config.get(conversationKey, {}) || {}
|
|
57
|
+
: {}
|
|
58
|
+
const model = options.model
|
|
59
|
+
let conversationId = undefined
|
|
60
|
+
let parentMessageId = undefined
|
|
61
|
+
|
|
62
|
+
if (conversation.lastMessageId) {
|
|
63
|
+
const lastMessage = conversation[conversation.lastMessageId]
|
|
64
|
+
if (lastMessage) {
|
|
65
|
+
conversationId = lastMessage.conversationId
|
|
66
|
+
parentMessageId = lastMessage.id
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (options.debug) {
|
|
71
|
+
console.log('using config', config.path)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const api = new ChatGPTAPI({
|
|
75
|
+
apiKey,
|
|
76
|
+
apiOrg,
|
|
77
|
+
debug: options.debug,
|
|
78
|
+
completionParams: {
|
|
79
|
+
model
|
|
80
|
+
},
|
|
81
|
+
getMessageById: async (id) => {
|
|
82
|
+
if (options.store) {
|
|
83
|
+
return conversation[id]
|
|
84
|
+
} else {
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
upsertMessage: async (message) => {
|
|
89
|
+
if (options.store) {
|
|
90
|
+
conversation[message.id] = message
|
|
91
|
+
conversation.lastMessageId = message.id
|
|
92
|
+
config.set(conversationKey, conversation)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const res = await api.sendMessage(prompt, {
|
|
98
|
+
conversationId,
|
|
99
|
+
parentMessageId,
|
|
100
|
+
timeoutMs: options.timeout || undefined,
|
|
101
|
+
onProgress: options.stream
|
|
102
|
+
? (progress) => {
|
|
103
|
+
if (progress.delta) {
|
|
104
|
+
process.stdout.write(progress.delta)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
: undefined
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
if (options.stream) {
|
|
111
|
+
process.stdout.write('\n')
|
|
112
|
+
} else {
|
|
113
|
+
console.log(res.text)
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
cli.command('rm-cache', 'Clears the local message cache').action(() => {
|
|
118
|
+
config.clear()
|
|
119
|
+
console.log('cleared cache', config.path)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
cli.command('ls-cache', 'Prints the local message cache path').action(() => {
|
|
123
|
+
console.log(config.path)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
cli.help()
|
|
127
|
+
cli.version(version)
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
cli.parse()
|
|
131
|
+
} catch (err) {
|
|
132
|
+
console.error(`error: ${err.message}\n`)
|
|
133
|
+
cli.outputHelp()
|
|
134
|
+
process.exit(1)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function hash(d) {
|
|
139
|
+
const buffer = Buffer.isBuffer(d) ? d : Buffer.from(d.toString())
|
|
140
|
+
return crypto.createHash('sha256').update(buffer).digest('hex')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
main().catch((err) => {
|
|
144
|
+
console.error(err)
|
|
145
|
+
process.exit(1)
|
|
146
|
+
})
|