mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-20 04:30:17 +09:00
Merge remote-tracking branch 'connectai/feature-cache-storage' into feature-cache-storage
This commit is contained in:
commit
ac470a6d07
@ -3,7 +3,6 @@ import { ChatOptions, getHeaders, LLMApi, MultimodalContent } from "../api";
|
|||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { DEFAULT_API_HOST } from "@/app/constant";
|
import { DEFAULT_API_HOST } from "@/app/constant";
|
||||||
import { RequestMessage } from "@/app/typing";
|
|
||||||
import {
|
import {
|
||||||
EventStreamContentType,
|
EventStreamContentType,
|
||||||
fetchEventSource,
|
fetchEventSource,
|
||||||
@ -95,7 +94,7 @@ export class ClaudeApi implements LLMApi {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// try get base64image from local cache image_url
|
// try get base64image from local cache image_url
|
||||||
const messages = [];
|
const messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = await preProcessImageContent(v.content);
|
const content = await preProcessImageContent(v.content);
|
||||||
messages.push({ role: v.role, content });
|
messages.push({ role: v.role, content });
|
||||||
|
@ -59,7 +59,7 @@ export class GeminiProApi implements LLMApi {
|
|||||||
let multimodal = false;
|
let multimodal = false;
|
||||||
|
|
||||||
// try get base64image from local cache image_url
|
// try get base64image from local cache image_url
|
||||||
const _messages = [];
|
const _messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = await preProcessImageContent(v.content);
|
const content = await preProcessImageContent(v.content);
|
||||||
_messages.push({ role: v.role, content });
|
_messages.push({ role: v.role, content });
|
||||||
|
@ -106,7 +106,7 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
|
|
||||||
async chat(options: ChatOptions) {
|
async chat(options: ChatOptions) {
|
||||||
const visionModel = isVisionModel(options.config.model);
|
const visionModel = isVisionModel(options.config.model);
|
||||||
const messages = [];
|
const messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = visionModel
|
const content = visionModel
|
||||||
? await preProcessImageContent(v.content)
|
? await preProcessImageContent(v.content)
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { CACHE_URL_PREFIX, UPLOAD_URL } from "@/app/constant";
|
import { CACHE_URL_PREFIX, UPLOAD_URL } from "@/app/constant";
|
||||||
// import heic2any from "heic2any";
|
// import heic2any from "heic2any";
|
||||||
|
import { RequestMessage } from "@/app/client/api";
|
||||||
|
|
||||||
export function compressImage(file: File, maxSize: number): Promise<string> {
|
export function compressImage(file: Blob, maxSize: number): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = (readerEvent: any) => {
|
reader.onload = (readerEvent: any) => {
|
||||||
@ -43,10 +44,10 @@ export function compressImage(file: File, maxSize: number): Promise<string> {
|
|||||||
if (file.type.includes("heic")) {
|
if (file.type.includes("heic")) {
|
||||||
const heic2any = require("heic2any");
|
const heic2any = require("heic2any");
|
||||||
heic2any({ blob: file, toType: "image/jpeg" })
|
heic2any({ blob: file, toType: "image/jpeg" })
|
||||||
.then((blob) => {
|
.then((blob: Blob) => {
|
||||||
reader.readAsDataURL(blob as Blob);
|
reader.readAsDataURL(blob);
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e: any) => {
|
||||||
reject(e);
|
reject(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -73,7 +74,7 @@ export async function preProcessImageContent(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageCaches = {};
|
const imageCaches: Record<string, string> = {};
|
||||||
export function cacheImageToBase64Image(imageUrl: string) {
|
export function cacheImageToBase64Image(imageUrl: string) {
|
||||||
if (imageUrl.includes(CACHE_URL_PREFIX)) {
|
if (imageUrl.includes(CACHE_URL_PREFIX)) {
|
||||||
if (!imageCaches[imageUrl]) {
|
if (!imageCaches[imageUrl]) {
|
||||||
@ -85,7 +86,8 @@ export function cacheImageToBase64Image(imageUrl: string) {
|
|||||||
})
|
})
|
||||||
.then((res) => res.blob())
|
.then((res) => res.blob())
|
||||||
.then(
|
.then(
|
||||||
(blob) => (imageCaches[imageUrl] = compressImage(blob, 256 * 1024)),
|
async (blob) =>
|
||||||
|
(imageCaches[imageUrl] = await compressImage(blob, 256 * 1024)),
|
||||||
); // compressImage
|
); // compressImage
|
||||||
}
|
}
|
||||||
return Promise.resolve(imageCaches[imageUrl]);
|
return Promise.resolve(imageCaches[imageUrl]);
|
||||||
|
Loading…
Reference in New Issue
Block a user