Bladeren bron

refacto(app/utils/chat.ts)r: optimize function preProcessImageContentBase

EvanWu 11 maanden geleden
bovenliggende
commit
0a25a1a8cb
1 gewijzigde bestanden met toevoegingen van 15 en 19 verwijderingen
  1. 15 19
      app/utils/chat.ts

+ 15 - 19
app/utils/chat.ts

@@ -70,8 +70,9 @@ export function compressImage(file: Blob, maxSize: number): Promise<string> {
   });
 }
 
-export async function preProcessImageContent(
+export async function preProcessImageContentBase(
   content: RequestMessage["content"],
+  transformImageUrl: (url: string) => Promise<{ [key: string]: any }>,
 ) {
   if (typeof content === "string") {
     return content;
@@ -81,7 +82,7 @@ export async function preProcessImageContent(
     if (part?.type == "image_url" && part?.image_url?.url) {
       try {
         const url = await cacheImageToBase64Image(part?.image_url?.url);
-        result.push({ type: part.type, image_url: { url } });
+        result.push(await transformImageUrl(url));
       } catch (error) {
         console.error("Error processing image URL:", error);
       }
@@ -92,26 +93,21 @@ export async function preProcessImageContent(
   return result;
 }
 
+export async function preProcessImageContent(
+  content: RequestMessage["content"],
+) {
+  return preProcessImageContentBase(content, async (url) => ({
+    type: "image_url",
+    image_url: { url },
+  }));
+}
+
 export async function preProcessImageContentForAlibabaDashScope(
   content: RequestMessage["content"],
 ) {
-  if (typeof content === "string") {
-    return content;
-  }
-  const result = [];
-  for (const part of content) {
-    if (part?.type == "image_url" && part?.image_url?.url) {
-      try {
-        const url = await cacheImageToBase64Image(part?.image_url?.url);
-        result.push({ image: url });
-      } catch (error) {
-        console.error("Error processing image URL:", error);
-      }
-    } else {
-      result.push({ ...part });
-    }
-  }
-  return result;
+  return preProcessImageContentBase(content, async (url) => ({
+    image: url,
+  }));
 }
 
 const imageCaches: Record<string, string> = {};