utilitas 1995.2.25 → 1995.2.26
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 +4 -2
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/alan.mjs +47 -12
- package/lib/manifest.mjs +1 -1
- package/package.json +1 -1
package/lib/alan.mjs
CHANGED
|
@@ -6,9 +6,12 @@ const _NEED = [
|
|
|
6
6
|
|
|
7
7
|
const [OPENAI] = ['OPENAI'];
|
|
8
8
|
const [name] = ['Alan'];
|
|
9
|
+
const [user] = ['user'];
|
|
10
|
+
const [gpt4, gpt4_1106] = ['gpt-4', 'gpt-4-1106-preview'];
|
|
9
11
|
const openaiProvider = { provider: OPENAI };
|
|
10
12
|
const openaiProviderBeta = { ...openaiProvider, beta: true };
|
|
11
|
-
const openaiDefaultModel =
|
|
13
|
+
const openaiDefaultModel = gpt4;
|
|
14
|
+
const getMsg = cnt => String.isString(cnt) ? { role: user, content: cnt } : cnt;
|
|
12
15
|
|
|
13
16
|
let openai;
|
|
14
17
|
|
|
@@ -32,9 +35,10 @@ const init = async (options) => {
|
|
|
32
35
|
|
|
33
36
|
const complete = async (content, options) => {
|
|
34
37
|
const client = await init({ ...openaiProvider, ...options });
|
|
38
|
+
// https://github.com/openai/openai-node?tab=readme-ov-file#streaming-responses
|
|
35
39
|
// https://github.com/openai/openai-node?tab=readme-ov-file#streaming-responses-1
|
|
36
40
|
let [resp, result, chunk] = [await client.chat.completions.create({
|
|
37
|
-
messages: [...options?.messages || [],
|
|
41
|
+
messages: [...options?.messages || [], getMsg(content)],
|
|
38
42
|
model: openaiDefaultModel, stream: !!options?.stream,
|
|
39
43
|
}), '', null];
|
|
40
44
|
if (!options?.stream) {
|
|
@@ -51,10 +55,11 @@ const createAssistant = async (options) => {
|
|
|
51
55
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
52
56
|
// https://platform.openai.com/docs/api-reference/assistants/createAssistant
|
|
53
57
|
return await client.assistants.create({
|
|
54
|
-
model:
|
|
58
|
+
model: gpt4_1106, name,
|
|
59
|
+
instructions: 'You are a helpful assistant.',
|
|
55
60
|
tools: [
|
|
56
61
|
{ type: 'code_interpreter' },
|
|
57
|
-
|
|
62
|
+
{ type: 'retrieval' },
|
|
58
63
|
// { type: 'function' },
|
|
59
64
|
], ...options?.params || {},
|
|
60
65
|
// description: null, file_ids: [], metadata: {},
|
|
@@ -79,7 +84,7 @@ const createThread = async (options) => {
|
|
|
79
84
|
return await client.threads.create(options);
|
|
80
85
|
};
|
|
81
86
|
|
|
82
|
-
const getThread = async (threadId) => {
|
|
87
|
+
const getThread = async (threadId, options) => {
|
|
83
88
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
84
89
|
assert(threadId, 'Thread ID is required.', 400)
|
|
85
90
|
// https://platform.openai.com/docs/api-reference/threads/getThread
|
|
@@ -90,25 +95,26 @@ const ensureThread = async opts => await (
|
|
|
90
95
|
opts?.threadId ? getThread(opts?.threadId) : createThread(opts?.params)
|
|
91
96
|
);
|
|
92
97
|
|
|
93
|
-
const createMessage = async (threadId,
|
|
98
|
+
const createMessage = async (threadId, content, options) => {
|
|
94
99
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
95
100
|
// https://platform.openai.com/docs/api-reference/messages/createMessage
|
|
96
|
-
return await client.threads.messages.create(threadId,
|
|
101
|
+
return await client.threads.messages.create(threadId, getMsg(content));
|
|
97
102
|
};
|
|
98
103
|
|
|
99
104
|
const listMessages = async (threadId, options) => {
|
|
100
105
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
101
106
|
// https://platform.openai.com/docs/api-reference/messages/listMessages
|
|
102
|
-
return
|
|
103
|
-
threadId, { limit: 1, ...options?.params || {} }
|
|
104
|
-
)).data[0], null, 2);
|
|
107
|
+
return await client.threads.messages.list(threadId, options);
|
|
105
108
|
};
|
|
106
109
|
|
|
110
|
+
const getLatestMessage = async (threadId, options) => (
|
|
111
|
+
await listMessages(threadId, { limit: 1, ...options })
|
|
112
|
+
)?.data?.[0];
|
|
113
|
+
|
|
107
114
|
const run = async (assistantId, threadId, options) => {
|
|
108
115
|
const client = await init({ ...openaiProviderBeta, ...options });
|
|
109
116
|
return await client.threads.runs.create(
|
|
110
|
-
threadId,
|
|
111
|
-
{ assistant_id: assistantId }
|
|
117
|
+
threadId, { assistant_id: assistantId }
|
|
112
118
|
);
|
|
113
119
|
};
|
|
114
120
|
|
|
@@ -117,6 +123,33 @@ const getRun = async (threadId, runId, options) => {
|
|
|
117
123
|
return await client.threads.runs.retrieve(threadId, runId);
|
|
118
124
|
};
|
|
119
125
|
|
|
126
|
+
const prompt = async (content, options) => {
|
|
127
|
+
const assistant = await ensureAssistant(options);
|
|
128
|
+
const thread = await ensureThread(options);
|
|
129
|
+
const messageSent = await createMessage(thread.id, content, options);
|
|
130
|
+
let objRun = await run(assistant.id, thread.id, options);
|
|
131
|
+
objRun = await new Promise((resolve, reject) => {
|
|
132
|
+
const timer = setInterval(async () => {
|
|
133
|
+
try {
|
|
134
|
+
const resp = await getRun(thread.id, objRun.id);
|
|
135
|
+
if (resp?.status === 'completed') {
|
|
136
|
+
clearInterval(timer);
|
|
137
|
+
resolve(resp);
|
|
138
|
+
}
|
|
139
|
+
} catch (err) {
|
|
140
|
+
clearInterval(timer);
|
|
141
|
+
reject(err);
|
|
142
|
+
}
|
|
143
|
+
}, 1000);
|
|
144
|
+
});
|
|
145
|
+
const messageReceived = await getLatestMessage(thread.id, options);
|
|
146
|
+
return {
|
|
147
|
+
assistant, thread, messageSent, run: objRun, messageReceived,
|
|
148
|
+
response: messageReceived.content[0].text.value,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
|
|
120
153
|
export default init;
|
|
121
154
|
export {
|
|
122
155
|
_NEED,
|
|
@@ -125,10 +158,12 @@ export {
|
|
|
125
158
|
createMessage,
|
|
126
159
|
ensureAssistant,
|
|
127
160
|
ensureThread,
|
|
161
|
+
getLatestMessage,
|
|
128
162
|
getRun,
|
|
129
163
|
getThread,
|
|
130
164
|
init,
|
|
131
165
|
listAssistant,
|
|
132
166
|
listMessages,
|
|
167
|
+
prompt,
|
|
133
168
|
run,
|
|
134
169
|
};
|
package/lib/manifest.mjs
CHANGED