Skip to content

Commit 3e32412

Browse files
committed
refactor: extracting create content helper function
Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 97a756d commit 3e32412

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

service/src/chatgpt/index.ts

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,7 @@ async function chatReplyProcess(options: RequestOptions) {
8585
}
8686

8787
// Prepare the user message content (text and images)
88-
let content: string | OpenAI.Chat.ChatCompletionContentPart[] = message
89-
90-
// Handle image uploads if present
91-
if (uploadFileKeys && uploadFileKeys.length > 0) {
92-
content = [
93-
{
94-
type: 'text',
95-
text: message,
96-
},
97-
]
98-
for (const uploadFileKey of uploadFileKeys) {
99-
content.push({
100-
type: 'image_url',
101-
image_url: {
102-
url: await convertImageUrl(uploadFileKey),
103-
},
104-
})
105-
}
106-
}
88+
const content: string | OpenAI.Chat.ChatCompletionContentPart[] = await createContent(message, uploadFileKeys)
10789

10890
// Add the user message
10991
messages.push({
@@ -244,26 +226,7 @@ async function getMessageById(id: string): Promise<ChatMessage | undefined> {
244226
}
245227
else {
246228
if (isPrompt) { // prompt
247-
let content: string | OpenAI.Chat.ChatCompletionContentPart[] = chatInfo.prompt
248-
if (chatInfo.images && chatInfo.images.length > 0) {
249-
content = [
250-
{
251-
type: 'text',
252-
text: chatInfo.prompt,
253-
},
254-
]
255-
for (const image of chatInfo.images) {
256-
const imageUrlBase64 = await convertImageUrl(image)
257-
if (imageUrlBase64) {
258-
content.push({
259-
type: 'image_url',
260-
image_url: {
261-
url: await convertImageUrl(image),
262-
},
263-
})
264-
}
265-
}
266-
}
229+
const content: string | OpenAI.Chat.ChatCompletionContentPart[] = await createContent(chatInfo.prompt, chatInfo.images)
267230
return {
268231
id,
269232
parentMessageId,
@@ -311,6 +274,35 @@ async function getRandomApiKey(user: UserInfo, chatModel: string): Promise<KeyCo
311274
return randomKeyConfig(keys)
312275
}
313276

277+
// Helper function to create content with text and optional images
278+
async function createContent(text: string, images?: string[]): Promise<string | OpenAI.Chat.ChatCompletionContentPart[]> {
279+
// If no images or empty array, return just the text
280+
if (!images || images.length === 0)
281+
return text
282+
283+
// Create content with text and images
284+
const content: OpenAI.Chat.ChatCompletionContentPart[] = [
285+
{
286+
type: 'text',
287+
text,
288+
},
289+
]
290+
291+
for (const image of images) {
292+
const imageUrl = await convertImageUrl(image)
293+
if (imageUrl) {
294+
content.push({
295+
type: 'image_url',
296+
image_url: {
297+
url: imageUrl,
298+
},
299+
})
300+
}
301+
}
302+
303+
return content
304+
}
305+
314306
// Helper function to add previous messages to the conversation context
315307
async function addPreviousMessages(parentMessageId: string, maxContextCount: number, messages: OpenAI.Chat.ChatCompletionMessageParam[]): Promise<void> {
316308
// Recursively get previous messages

0 commit comments

Comments
 (0)