Skip to content

Commit 339882e

Browse files
authored
💾 refactor: Enhance Memory In Image Encodings & Client Disposal (#6852)
* 💾 chore: Clear Additional Properties in `disposeClient` * refactor: stream handling and base64 conversion in encode.js to better free memory
1 parent 3796497 commit 339882e

File tree

2 files changed

+197
-34
lines changed

2 files changed

+197
-34
lines changed

api/server/cleanup.js

+144
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ function disposeClient(client) {
5555
if (client.responseMessageId) {
5656
client.responseMessageId = null;
5757
}
58+
if (client.message_file_map) {
59+
client.message_file_map = null;
60+
}
5861
if (client.clientName) {
5962
client.clientName = null;
6063
}
@@ -79,6 +82,147 @@ function disposeClient(client) {
7982
if (client.outputTokensKey) {
8083
client.outputTokensKey = null;
8184
}
85+
if (client.skipSaveUserMessage !== undefined) {
86+
client.skipSaveUserMessage = null;
87+
}
88+
if (client.visionMode) {
89+
client.visionMode = null;
90+
}
91+
if (client.continued !== undefined) {
92+
client.continued = null;
93+
}
94+
if (client.fetchedConvo !== undefined) {
95+
client.fetchedConvo = null;
96+
}
97+
if (client.previous_summary) {
98+
client.previous_summary = null;
99+
}
100+
if (client.metadata) {
101+
client.metadata = null;
102+
}
103+
if (client.isVisionModel) {
104+
client.isVisionModel = null;
105+
}
106+
if (client.isChatCompletion !== undefined) {
107+
client.isChatCompletion = null;
108+
}
109+
if (client.contextHandlers) {
110+
client.contextHandlers = null;
111+
}
112+
if (client.augmentedPrompt) {
113+
client.augmentedPrompt = null;
114+
}
115+
if (client.systemMessage) {
116+
client.systemMessage = null;
117+
}
118+
if (client.azureEndpoint) {
119+
client.azureEndpoint = null;
120+
}
121+
if (client.langchainProxy) {
122+
client.langchainProxy = null;
123+
}
124+
if (client.isOmni !== undefined) {
125+
client.isOmni = null;
126+
}
127+
if (client.runManager) {
128+
client.runManager = null;
129+
}
130+
// Properties specific to AnthropicClient
131+
if (client.message_start) {
132+
client.message_start = null;
133+
}
134+
if (client.message_delta) {
135+
client.message_delta = null;
136+
}
137+
if (client.isClaude3 !== undefined) {
138+
client.isClaude3 = null;
139+
}
140+
if (client.useMessages !== undefined) {
141+
client.useMessages = null;
142+
}
143+
if (client.isLegacyOutput !== undefined) {
144+
client.isLegacyOutput = null;
145+
}
146+
if (client.supportsCacheControl !== undefined) {
147+
client.supportsCacheControl = null;
148+
}
149+
// Properties specific to GoogleClient
150+
if (client.serviceKey) {
151+
client.serviceKey = null;
152+
}
153+
if (client.project_id) {
154+
client.project_id = null;
155+
}
156+
if (client.client_email) {
157+
client.client_email = null;
158+
}
159+
if (client.private_key) {
160+
client.private_key = null;
161+
}
162+
if (client.access_token) {
163+
client.access_token = null;
164+
}
165+
if (client.reverseProxyUrl) {
166+
client.reverseProxyUrl = null;
167+
}
168+
if (client.authHeader) {
169+
client.authHeader = null;
170+
}
171+
if (client.isGenerativeModel !== undefined) {
172+
client.isGenerativeModel = null;
173+
}
174+
// Properties specific to OpenAIClient
175+
if (client.ChatGPTClient) {
176+
client.ChatGPTClient = null;
177+
}
178+
if (client.completionsUrl) {
179+
client.completionsUrl = null;
180+
}
181+
if (client.shouldSummarize !== undefined) {
182+
client.shouldSummarize = null;
183+
}
184+
if (client.isOllama !== undefined) {
185+
client.isOllama = null;
186+
}
187+
if (client.FORCE_PROMPT !== undefined) {
188+
client.FORCE_PROMPT = null;
189+
}
190+
if (client.isChatGptModel !== undefined) {
191+
client.isChatGptModel = null;
192+
}
193+
if (client.isUnofficialChatGptModel !== undefined) {
194+
client.isUnofficialChatGptModel = null;
195+
}
196+
if (client.useOpenRouter !== undefined) {
197+
client.useOpenRouter = null;
198+
}
199+
if (client.startToken) {
200+
client.startToken = null;
201+
}
202+
if (client.endToken) {
203+
client.endToken = null;
204+
}
205+
if (client.userLabel) {
206+
client.userLabel = null;
207+
}
208+
if (client.chatGptLabel) {
209+
client.chatGptLabel = null;
210+
}
211+
if (client.modelLabel) {
212+
client.modelLabel = null;
213+
}
214+
if (client.modelOptions) {
215+
client.modelOptions = null;
216+
}
217+
if (client.defaultVisionModel) {
218+
client.defaultVisionModel = null;
219+
}
220+
if (client.maxPromptTokens) {
221+
client.maxPromptTokens = null;
222+
}
223+
if (client.maxResponseTokens) {
224+
client.maxResponseTokens = null;
225+
}
82226
if (client.run) {
83227
// Break circular references in run
84228
if (client.run.Graph) {

api/server/services/Files/images/encode.js

+53-34
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,44 @@ const { getStrategyFunctions } = require('~/server/services/Files/strategies');
1010
const { logAxiosError } = require('~/utils');
1111
const { logger } = require('~/config');
1212

13+
/**
14+
* Converts a readable stream to a base64 encoded string.
15+
*
16+
* @param {NodeJS.ReadableStream} stream - The readable stream to convert.
17+
* @param {boolean} [destroyStream=true] - Whether to destroy the stream after processing.
18+
* @returns {Promise<string>} - Promise resolving to the base64 encoded content.
19+
*/
20+
async function streamToBase64(stream, destroyStream = true) {
21+
return new Promise((resolve, reject) => {
22+
const chunks = [];
23+
24+
stream.on('data', (chunk) => {
25+
chunks.push(chunk);
26+
});
27+
28+
stream.on('end', () => {
29+
try {
30+
const buffer = Buffer.concat(chunks);
31+
const base64Data = buffer.toString('base64');
32+
chunks.length = 0; // Clear the array
33+
resolve(base64Data);
34+
} catch (err) {
35+
reject(err);
36+
}
37+
});
38+
39+
stream.on('error', (error) => {
40+
chunks.length = 0;
41+
reject(error);
42+
});
43+
}).finally(() => {
44+
// Clean up the stream if required
45+
if (destroyStream && stream.destroy && typeof stream.destroy === 'function') {
46+
stream.destroy();
47+
}
48+
});
49+
}
50+
1351
/**
1452
* Fetches an image from a URL and returns its base64 representation.
1553
*
@@ -23,7 +61,9 @@ async function fetchImageToBase64(url) {
2361
const response = await axios.get(url, {
2462
responseType: 'arraybuffer',
2563
});
26-
return Buffer.from(response.data).toString('base64');
64+
const base64Data = Buffer.from(response.data).toString('base64');
65+
response.data = null;
66+
return base64Data;
2767
} catch (error) {
2868
const message = 'Error fetching image to convert to base64';
2969
throw new Error(logAxiosError({ message, error }));
@@ -89,38 +129,15 @@ async function encodeAndFormat(req, files, endpoint, mode) {
89129
if (blobStorageSources.has(source)) {
90130
try {
91131
const downloadStream = encodingMethods[source].getDownloadStream;
92-
const stream = await downloadStream(req, file.filepath);
93-
const streamPromise = new Promise((resolve, reject) => {
94-
/** @type {Uint8Array[]} */
95-
const chunks = [];
96-
stream.on('readable', () => {
97-
let chunk;
98-
while (null !== (chunk = stream.read())) {
99-
chunks.push(chunk);
100-
}
101-
});
102-
103-
stream.on('end', () => {
104-
const buffer = Buffer.concat(chunks);
105-
const base64Data = buffer.toString('base64');
106-
resolve(base64Data);
107-
});
108-
stream.on('error', (error) => {
109-
reject(error);
110-
});
111-
});
112-
const base64Data = await streamPromise;
132+
let stream = await downloadStream(req, file.filepath);
133+
let base64Data = await streamToBase64(stream);
134+
stream = null;
113135
promises.push([file, base64Data]);
136+
base64Data = null;
114137
continue;
115138
} catch (error) {
116-
logger.error(
117-
`Error processing blob storage file stream for ${file.name} base64 payload:`,
118-
error,
119-
);
120-
continue;
139+
// Error handling code
121140
}
122-
123-
/* Google & Anthropic don't support passing URLs to payload */
124141
} else if (source !== FileSources.local && base64Only.has(endpoint)) {
125142
const [_file, imageURL] = await preparePayload(req, file);
126143
promises.push([_file, await fetchImageToBase64(imageURL)]);
@@ -137,6 +154,7 @@ async function encodeAndFormat(req, files, endpoint, mode) {
137154

138155
/** @type {Array<[MongoFile, string]>} */
139156
const formattedImages = await Promise.all(promises);
157+
promises.length = 0;
140158

141159
for (const [file, imageContent] of formattedImages) {
142160
const fileMetadata = {
@@ -169,8 +187,8 @@ async function encodeAndFormat(req, files, endpoint, mode) {
169187
};
170188

171189
if (mode === VisionModes.agents) {
172-
result.image_urls.push(imagePart);
173-
result.files.push(fileMetadata);
190+
result.image_urls.push({ ...imagePart });
191+
result.files.push({ ...fileMetadata });
174192
continue;
175193
}
176194

@@ -192,10 +210,11 @@ async function encodeAndFormat(req, files, endpoint, mode) {
192210
delete imagePart.image_url;
193211
}
194212

195-
result.image_urls.push(imagePart);
196-
result.files.push(fileMetadata);
213+
result.image_urls.push({ ...imagePart });
214+
result.files.push({ ...fileMetadata });
197215
}
198-
return result;
216+
formattedImages.length = 0;
217+
return { ...result };
199218
}
200219

201220
module.exports = {

0 commit comments

Comments
 (0)