utilitas 1995.2.26 → 1995.2.28
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/README.md +13 -3
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/alan.mjs +102 -22
- package/lib/manifest.mjs +1 -1
- package/package.json +1 -1
package/lib/alan.mjs
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { ensureString, need, throwError } from './utilitas.mjs';
|
|
2
|
+
import { convert } from './storage.mjs';
|
|
2
3
|
|
|
3
|
-
const _NEED = [
|
|
4
|
-
'OpenAI',
|
|
5
|
-
];
|
|
6
|
-
|
|
4
|
+
const _NEED = ['OpenAI'];
|
|
7
5
|
const [OPENAI] = ['OPENAI'];
|
|
8
|
-
const [name] = ['Alan'];
|
|
9
|
-
const [
|
|
10
|
-
const [
|
|
6
|
+
const [name, user] = ['Alan', 'user'];
|
|
7
|
+
const [STREAM] = ['STREAM'];
|
|
8
|
+
const [GPT4, GPT4_1106] = ['gpt-4', 'gpt-4-1106-preview'];
|
|
11
9
|
const openaiProvider = { provider: OPENAI };
|
|
12
10
|
const openaiProviderBeta = { ...openaiProvider, beta: true };
|
|
13
|
-
const openaiDefaultModel =
|
|
11
|
+
const openaiDefaultModel = GPT4;
|
|
14
12
|
const getMsg = cnt => String.isString(cnt) ? { role: user, content: cnt } : cnt;
|
|
13
|
+
const errorMessage = 'Invalid file data.';
|
|
15
14
|
|
|
16
15
|
let openai;
|
|
17
16
|
|
|
@@ -33,7 +32,7 @@ const init = async (options) => {
|
|
|
33
32
|
return client;
|
|
34
33
|
};
|
|
35
34
|
|
|
36
|
-
const
|
|
35
|
+
const promptChatGPT = async (content, options) => {
|
|
37
36
|
const client = await init({ ...openaiProvider, ...options });
|
|
38
37
|
// https://github.com/openai/openai-node?tab=readme-ov-file#streaming-responses
|
|
39
38
|
// https://github.com/openai/openai-node?tab=readme-ov-file#streaming-responses-1
|
|
@@ -55,7 +54,7 @@ const createAssistant = async (options) => {
|
|
|
55
54
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
56
55
|
// https://platform.openai.com/docs/api-reference/assistants/createAssistant
|
|
57
56
|
return await client.assistants.create({
|
|
58
|
-
model:
|
|
57
|
+
model: GPT4_1106, name,
|
|
59
58
|
instructions: 'You are a helpful assistant.',
|
|
60
59
|
tools: [
|
|
61
60
|
{ type: 'code_interpreter' },
|
|
@@ -81,18 +80,22 @@ const ensureAssistant = async (options) => {
|
|
|
81
80
|
const createThread = async (options) => {
|
|
82
81
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
83
82
|
// https://platform.openai.com/docs/api-reference/threads/createThread
|
|
84
|
-
return await client.threads.create(options);
|
|
83
|
+
return await client.threads.create({ ...options?.params || {} });
|
|
85
84
|
};
|
|
86
85
|
|
|
87
86
|
const getThread = async (threadId, options) => {
|
|
88
87
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
89
|
-
assert(threadId, 'Thread ID is required.', 400)
|
|
90
88
|
// https://platform.openai.com/docs/api-reference/threads/getThread
|
|
91
89
|
return await client.threads.retrieve(threadId);
|
|
92
90
|
};
|
|
93
91
|
|
|
94
|
-
const
|
|
95
|
-
|
|
92
|
+
const deleteThread = async (threadId, options) => {
|
|
93
|
+
const client = await init({ ...openaiProviderBeta, ...options });
|
|
94
|
+
return await client.threads.del(threadId);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const ensureThread = async options => await (
|
|
98
|
+
options?.threadId ? getThread(options?.threadId) : createThread(options)
|
|
96
99
|
);
|
|
97
100
|
|
|
98
101
|
const createMessage = async (threadId, content, options) => {
|
|
@@ -101,14 +104,14 @@ const createMessage = async (threadId, content, options) => {
|
|
|
101
104
|
return await client.threads.messages.create(threadId, getMsg(content));
|
|
102
105
|
};
|
|
103
106
|
|
|
104
|
-
const listMessages = async (threadId,
|
|
105
|
-
const client = await init({ ...openaiProviderBeta, ...
|
|
107
|
+
const listMessages = async (threadId, o) => {
|
|
108
|
+
const client = await init({ ...openaiProviderBeta, ...o });
|
|
106
109
|
// https://platform.openai.com/docs/api-reference/messages/listMessages
|
|
107
|
-
return await client.threads.messages.list(threadId,
|
|
110
|
+
return await client.threads.messages.list(threadId, { ...o?.params || {} });
|
|
108
111
|
};
|
|
109
112
|
|
|
110
113
|
const getLatestMessage = async (threadId, options) => (
|
|
111
|
-
await listMessages(threadId, { limit: 1
|
|
114
|
+
await listMessages(threadId, { ...options, params: { limit: 1 } })
|
|
112
115
|
)?.data?.[0];
|
|
113
116
|
|
|
114
117
|
const run = async (assistantId, threadId, options) => {
|
|
@@ -123,7 +126,7 @@ const getRun = async (threadId, runId, options) => {
|
|
|
123
126
|
return await client.threads.runs.retrieve(threadId, runId);
|
|
124
127
|
};
|
|
125
128
|
|
|
126
|
-
const
|
|
129
|
+
const promptAssistant = async (content, options) => {
|
|
127
130
|
const assistant = await ensureAssistant(options);
|
|
128
131
|
const thread = await ensureThread(options);
|
|
129
132
|
const messageSent = await createMessage(thread.id, content, options);
|
|
@@ -143,19 +146,90 @@ const prompt = async (content, options) => {
|
|
|
143
146
|
}, 1000);
|
|
144
147
|
});
|
|
145
148
|
const messageReceived = await getLatestMessage(thread.id, options);
|
|
149
|
+
const threadDeleted = options?.deleteThread ? await deleteThread(thread.id) : null;
|
|
146
150
|
return {
|
|
147
151
|
assistant, thread, messageSent, run: objRun, messageReceived,
|
|
148
|
-
response: messageReceived.content[0].text.value,
|
|
152
|
+
threadDeleted, response: messageReceived.content[0].text.value,
|
|
149
153
|
};
|
|
150
154
|
};
|
|
151
155
|
|
|
156
|
+
const uploadFile = async (input, options) => {
|
|
157
|
+
const client = await init({ ...openaiProvider, ...options });
|
|
158
|
+
const { content: file, cleanup } = await convert(input, {
|
|
159
|
+
input: options?.input, ...options || {}, expected: STREAM, errorMessage,
|
|
160
|
+
suffix: options?.suffix,
|
|
161
|
+
withCleanupFunc: true,
|
|
162
|
+
});
|
|
163
|
+
const resp = await client.files.create({ file, ...options?.params || {} });
|
|
164
|
+
await cleanup();
|
|
165
|
+
return resp;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const uploadFileForAssistants = async (content, options) => await uploadFile(
|
|
169
|
+
content, { ...options, params: { purpose: 'assistants' } }
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const attachFileToAssistant = async (assistantId, file_id, options) => {
|
|
173
|
+
const client = await init({ ...openaiProviderBeta, ...options });
|
|
174
|
+
return await client.assistants.files.create(assistantId, { file_id });
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const detachFileFromAssistant = async (assistantId, file_id, options) => {
|
|
178
|
+
const client = await init({ ...openaiProviderBeta, ...options });
|
|
179
|
+
return await client.assistants.files.del(assistantId, file_id);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const deleteFileFromAssistant = async (assistantId, file_id, options) => {
|
|
183
|
+
const detach = await detachFileFromAssistant(assistantId, file_id, options);
|
|
184
|
+
const respDel = await deleteFile(file_id, options);
|
|
185
|
+
return { detach, delete: respDel };
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const deleteAllFilesFromAssistant = async (assistantId, options) => {
|
|
189
|
+
const files = await listAssistantFiles(assistantId, options);
|
|
190
|
+
const delPms = [];
|
|
191
|
+
for (const file of files) {
|
|
192
|
+
delPms.push(deleteFileFromAssistant(assistantId, file.id, options));
|
|
193
|
+
}
|
|
194
|
+
return await Promise.all(delPms);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const uploadFileForRetrieval = async (assistantId, content, options) => {
|
|
198
|
+
const file = await uploadFileForAssistants(content, options);
|
|
199
|
+
return await attachFileToAssistant(assistantId, file.id, options);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const listFiles = async (options) => {
|
|
203
|
+
const client = await init({ ...openaiProvider, ...options });
|
|
204
|
+
const files = [];
|
|
205
|
+
const list = await client.files.list(options?.params || {});
|
|
206
|
+
for await (const file of list) { files.push(file); }
|
|
207
|
+
return files;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const deleteFile = async (file_id, options) => {
|
|
211
|
+
const client = await init({ ...openaiProvider, ...options });
|
|
212
|
+
return await client.files.del(file_id);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const listAssistantFiles = async (assistant_id, options) => {
|
|
216
|
+
const client = await init({ ...openaiProviderBeta, ...options });
|
|
217
|
+
const resp = await client.assistants.files.list(
|
|
218
|
+
assistant_id, { limit: 100, ...options?.params }
|
|
219
|
+
);
|
|
220
|
+
return options?.raw ? resp : resp.data;
|
|
221
|
+
};
|
|
152
222
|
|
|
153
223
|
export default init;
|
|
154
224
|
export {
|
|
155
225
|
_NEED,
|
|
156
|
-
complete,
|
|
157
226
|
createAssistant,
|
|
158
227
|
createMessage,
|
|
228
|
+
deleteAllFilesFromAssistant,
|
|
229
|
+
deleteFile,
|
|
230
|
+
deleteFileFromAssistant,
|
|
231
|
+
deleteThread,
|
|
232
|
+
detachFileFromAssistant,
|
|
159
233
|
ensureAssistant,
|
|
160
234
|
ensureThread,
|
|
161
235
|
getLatestMessage,
|
|
@@ -163,7 +237,13 @@ export {
|
|
|
163
237
|
getThread,
|
|
164
238
|
init,
|
|
165
239
|
listAssistant,
|
|
240
|
+
listAssistantFiles,
|
|
241
|
+
listFiles,
|
|
166
242
|
listMessages,
|
|
167
|
-
|
|
243
|
+
promptAssistant,
|
|
244
|
+
promptChatGPT,
|
|
168
245
|
run,
|
|
246
|
+
uploadFile,
|
|
247
|
+
uploadFileForAssistants,
|
|
248
|
+
uploadFileForRetrieval,
|
|
169
249
|
};
|
package/lib/manifest.mjs
CHANGED