@crewdle/mist-connector-openai 1.0.1 → 1.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/dist/index.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import { createReadStream, unlinkSync, writeFileSync } from 'fs';
|
|
3
|
+
export class OpenAIFileConnector {
|
|
4
|
+
client;
|
|
5
|
+
constructor(apiKey) {
|
|
6
|
+
this.client = new OpenAI({
|
|
7
|
+
apiKey,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
async writeFile(file) {
|
|
11
|
+
let purpose = 'assistants';
|
|
12
|
+
if (file.type.startsWith('image/')) {
|
|
13
|
+
purpose = 'vision';
|
|
14
|
+
}
|
|
15
|
+
const buffer = Buffer.from(await file.arrayBuffer());
|
|
16
|
+
writeFileSync(`./${file.name}`, buffer);
|
|
17
|
+
const uploadedFile = await this.client.files.create({
|
|
18
|
+
file: createReadStream(`./${file.name}`),
|
|
19
|
+
purpose,
|
|
20
|
+
});
|
|
21
|
+
unlinkSync(`./${file.name}`);
|
|
22
|
+
return uploadedFile.id;
|
|
23
|
+
}
|
|
24
|
+
async deleteFile(fileId) {
|
|
25
|
+
await this.client.files.del(fileId);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -101,21 +101,55 @@ export class OpenAIGenerativeAIWorkerConnector {
|
|
|
101
101
|
if (!message.tool_calls) {
|
|
102
102
|
break;
|
|
103
103
|
}
|
|
104
|
-
messages.push(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
104
|
+
messages.push({
|
|
105
|
+
role: 'assistant',
|
|
106
|
+
content: null,
|
|
107
|
+
tool_calls: message.tool_calls,
|
|
108
|
+
refusal: null,
|
|
109
|
+
});
|
|
110
|
+
const promises = [];
|
|
111
|
+
for (const toolCall of message.tool_calls) {
|
|
112
|
+
if (toolCall?.function && parameters.functions) {
|
|
113
|
+
try {
|
|
114
|
+
const func = parameters.functions.get(toolCall.function.name);
|
|
115
|
+
if (func) {
|
|
116
|
+
const result = func.callback(JSON.parse(toolCall.function.arguments));
|
|
117
|
+
if (result instanceof Promise) {
|
|
118
|
+
promises.push(result.then((res) => {
|
|
119
|
+
messages.push({
|
|
120
|
+
role: 'tool',
|
|
121
|
+
tool_call_id: toolCall.id,
|
|
122
|
+
content: res ?? 'Tool does not exist',
|
|
123
|
+
});
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
messages.push({
|
|
128
|
+
role: 'tool',
|
|
129
|
+
tool_call_id: toolCall.id,
|
|
130
|
+
content: result ?? 'Tool does not exist',
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
messages.push({
|
|
136
|
+
role: 'tool',
|
|
137
|
+
tool_call_id: toolCall.id,
|
|
138
|
+
content: 'Tool does not exist',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
console.error('Error processing tool call', error);
|
|
144
|
+
messages.push({
|
|
145
|
+
role: 'tool',
|
|
146
|
+
tool_call_id: toolCall.id,
|
|
147
|
+
content: 'Error processing tool call',
|
|
148
|
+
});
|
|
149
|
+
}
|
|
117
150
|
}
|
|
118
151
|
}
|
|
152
|
+
await Promise.all(promises);
|
|
119
153
|
}
|
|
120
154
|
}
|
|
121
155
|
messageReducer(previous, item) {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IGenerativeAIFileConnector } from '@crewdle/web-sdk-types';
|
|
2
|
+
export declare class OpenAIFileConnector implements IGenerativeAIFileConnector {
|
|
3
|
+
private client;
|
|
4
|
+
constructor(apiKey: string);
|
|
5
|
+
writeFile(file: File): Promise<string>;
|
|
6
|
+
deleteFile(fileId: string): Promise<void>;
|
|
7
|
+
}
|