feat: support local storage

This commit is contained in:
Hk-Gosuto
2024-01-05 18:41:16 +08:00
parent 8e10354109
commit 296df592e0
18 changed files with 181 additions and 56 deletions

View File

@@ -1,5 +1,7 @@
import { getServerSideConfig } from "@/app/config/server";
import LocalFileStorage from "@/app/utils/local_file_storage";
import S3FileStorage from "@/app/utils/s3_file_storage";
import { NextRequest, NextResponse } from "next/server";
import S3FileStorage from "../../../utils/s3_file_storage";
async function handle(
req: NextRequest,
@@ -10,12 +12,22 @@ async function handle(
}
try {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": "image/png",
},
});
const serverConfig = getServerSideConfig();
if (serverConfig.isStoreFileToLocal) {
var fileBuffer = await LocalFileStorage.get(params.path[0]);
return new Response(fileBuffer, {
headers: {
"Content-Type": "image/png",
},
});
} else {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": "image/png",
},
});
}
} catch (e) {
return new Response("not found", {
status: 404,
@@ -25,5 +37,5 @@ async function handle(
export const GET = handle;
export const runtime = "edge";
export const runtime = "nodejs";
export const revalidate = 0;

View File

@@ -1,7 +1,9 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import S3FileStorage from "../../../utils/s3_file_storage";
import { ModelProvider } from "@/app/constant";
import { auth } from "@/app/api/auth";
import LocalFileStorage from "@/app/utils/local_file_storage";
import { getServerSideConfig } from "@/app/config/server";
import S3FileStorage from "@/app/utils/s3_file_storage";
async function handle(req: NextRequest) {
if (req.method === "OPTIONS") {
@@ -31,10 +33,17 @@ async function handle(req: NextRequest) {
const buffer = Buffer.from(imageData);
var fileName = `${Date.now()}.png`;
await S3FileStorage.put(fileName, buffer);
var filePath = "";
const serverConfig = getServerSideConfig();
if (serverConfig.isStoreFileToLocal) {
filePath = await LocalFileStorage.put(fileName, buffer);
} else {
filePath = await S3FileStorage.put(fileName, buffer);
}
return NextResponse.json(
{
fileName: fileName,
filePath: filePath,
},
{
status: 200,
@@ -55,4 +64,4 @@ async function handle(req: NextRequest) {
export const POST = handle;
export const runtime = "edge";
export const runtime = "nodejs";

View File

@@ -0,0 +1,22 @@
import { getServerSideConfig } from "@/app/config/server";
import { DallEAPIWrapper } from "./dalle_image_generator";
import S3FileStorage from "@/app/utils/s3_file_storage";
import LocalFileStorage from "@/app/utils/local_file_storage";
export class DallEAPINodeWrapper extends DallEAPIWrapper {
async saveImageFromUrl(url: string) {
const response = await fetch(url);
const content = await response.arrayBuffer();
const buffer = Buffer.from(content);
var filePath = "";
const serverConfig = getServerSideConfig();
var fileName = `${Date.now()}.png`;
if (serverConfig.isStoreFileToLocal) {
filePath = await LocalFileStorage.put(fileName, buffer);
} else {
filePath = await S3FileStorage.put(fileName, buffer);
}
return filePath;
}
}

View File

@@ -4,13 +4,8 @@ import { StableDiffusionWrapper } from "@/app/api/langchain-tools/stable_diffusi
import { BaseLanguageModel } from "langchain/dist/base_language";
import { Calculator } from "langchain/tools/calculator";
import { WebBrowser } from "langchain/tools/webbrowser";
import { BaiduSearch } from "@/app/api/langchain-tools/baidu_search";
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
import { Tool, DynamicTool } from "langchain/tools";
import * as langchainTools from "langchain/tools";
import { Embeddings } from "langchain/dist/embeddings/base.js";
import { WolframAlphaTool } from "./wolframalpha";
import { WolframAlphaTool } from "@/app/api/langchain-tools/wolframalpha";
export class EdgeTool {
private apiKey: string | undefined;
@@ -52,7 +47,6 @@ export class EdgeTool {
const arxivAPITool = new ArxivAPIWrapper();
const wolframAlphaTool = new WolframAlphaTool();
let tools = [
// searchTool,
calculatorTool,
webBrowserTool,
dallEAPITool,

View File

@@ -1,7 +1,12 @@
import { BaseLanguageModel } from "langchain/dist/base_language";
import { PDFBrowser } from "@/app/api/langchain-tools/pdf_browser";
import { Embeddings } from "langchain/dist/embeddings/base.js";
import { ArxivAPIWrapper } from "@/app/api/langchain-tools/arxiv";
import { DallEAPINodeWrapper } from "@/app/api/langchain-tools/dalle_image_generator_node";
import { StableDiffusionNodeWrapper } from "@/app/api/langchain-tools/stable_diffusion_image_generator_node";
import { Calculator } from "langchain/tools/calculator";
import { WebBrowser } from "langchain/tools/webbrowser";
import { WolframAlphaTool } from "@/app/api/langchain-tools/wolframalpha";
export class NodeJSTool {
private apiKey: string | undefined;
@@ -29,7 +34,29 @@ export class NodeJSTool {
}
async getCustomTools(): Promise<any[]> {
const webBrowserTool = new WebBrowser({
model: this.model,
embeddings: this.embeddings,
});
const calculatorTool = new Calculator();
const dallEAPITool = new DallEAPINodeWrapper(
this.apiKey,
this.baseUrl,
this.callback,
);
const stableDiffusionTool = new StableDiffusionNodeWrapper();
const arxivAPITool = new ArxivAPIWrapper();
const wolframAlphaTool = new WolframAlphaTool();
const pdfBrowserTool = new PDFBrowser(this.model, this.embeddings);
return [pdfBrowserTool];
let tools = [
calculatorTool,
webBrowserTool,
dallEAPITool,
stableDiffusionTool,
arxivAPITool,
wolframAlphaTool,
pdfBrowserTool,
];
return tools;
}
}

View File

@@ -8,6 +8,13 @@ export class StableDiffusionWrapper extends Tool {
super();
}
async saveImage(imageBase64: string) {
const buffer = Buffer.from(imageBase64, "base64");
var fileName = `${Date.now()}.png`;
const filePath = await S3FileStorage.put(fileName, buffer);
return filePath;
}
/** @ignore */
async _call(prompt: string) {
let url = process.env.STABLE_DIFFUSION_API_URL;
@@ -40,8 +47,7 @@ export class StableDiffusionWrapper extends Tool {
const json = await response.json();
let imageBase64 = json.images[0];
if (!imageBase64) return "No image was generated";
const buffer = Buffer.from(imageBase64, "base64");
const filePath = await S3FileStorage.put(`${Date.now()}.png`, buffer);
const filePath = await this.saveImage(imageBase64);
console.log(`[${this.name}]`, filePath);
return filePath;
}

View File

@@ -0,0 +1,19 @@
import S3FileStorage from "@/app/utils/s3_file_storage";
import { StableDiffusionWrapper } from "./stable_diffusion_image_generator";
import { getServerSideConfig } from "@/app/config/server";
import LocalFileStorage from "@/app/utils/local_file_storage";
export class StableDiffusionNodeWrapper extends StableDiffusionWrapper {
async saveImage(imageBase64: string) {
var filePath = "";
var fileName = `${Date.now()}.png`;
const buffer = Buffer.from(imageBase64, "base64");
const serverConfig = getServerSideConfig();
if (serverConfig.isStoreFileToLocal) {
filePath = await LocalFileStorage.put(fileName, buffer);
} else {
filePath = await S3FileStorage.put(fileName, buffer);
}
return filePath;
}
}

View File

@@ -55,13 +55,6 @@ async function handle(req: NextRequest) {
);
};
var edgeTool = new EdgeTool(
apiKey,
baseUrl,
model,
embeddings,
dalleCallback,
);
var nodejsTool = new NodeJSTool(
apiKey,
baseUrl,
@@ -69,9 +62,8 @@ async function handle(req: NextRequest) {
embeddings,
dalleCallback,
);
var edgeTools = await edgeTool.getCustomTools();
var nodejsTools = await nodejsTool.getCustomTools();
var tools = [...edgeTools, ...nodejsTools];
var tools = [...nodejsTools];
return await agentApi.getApiHandler(req, reqBody, tools);
} catch (e) {
return new Response(JSON.stringify({ error: (e as any).message }), {

View File

@@ -1,11 +1,12 @@
import { getHeaders } from "../api";
export class FileApi {
async upload(file: any): Promise<void> {
async upload(file: any): Promise<any> {
const formData = new FormData();
formData.append("file", file);
var headers = getHeaders(true);
var res = await fetch("/api/file/upload", {
const api = "/api/file/upload";
var res = await fetch(api, {
method: "POST",
body: formData,
headers: {
@@ -14,6 +15,9 @@ export class FileApi {
});
const resJson = await res.json();
console.log(resJson);
return resJson.fileName;
return {
fileName: resJson.fileName,
filePath: resJson.filePath,
};
}
}

View File

@@ -465,10 +465,10 @@ export function ChatActions(props: {
const onImageSelected = async (e: any) => {
const file = e.target.files[0];
const api = new ClientApi();
const fileName = await api.file.upload(file);
const uploadFile = await api.file.upload(file);
props.imageSelected({
fileName,
fileUrl: `/api/file/${fileName}`,
fileName: uploadFile.fileName,
fileUrl: uploadFile.filePath,
});
e.target.value = null;
};

View File

@@ -101,5 +101,10 @@ export const getServerSideConfig = () => {
hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
disableFastLink: !!process.env.DISABLE_FAST_LINK,
customModels,
isStoreFileToLocal:
!!process.env.NEXT_PUBLIC_ENABLE_NODEJS_PLUGIN &&
!process.env.R2_ACCOUNT_ID &&
!process.env.S3_ENDPOINT,
};
};

View File

@@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";
export default class LocalFileStorage {
static async get(fileName: string) {
const filePath = path.resolve(`./uploads`, fileName);
const file = fs.readFileSync(filePath);
if (!file) {
throw new Error("not found.");
}
return file;
}
static async put(fileName: string, data: Buffer) {
try {
const filePath = path.resolve(`./uploads`, fileName);
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
await fs.promises.writeFile(filePath, data);
console.log("[LocalFileStorage]", filePath);
return `/api/file/${fileName}`;
} catch (e) {
console.error("[LocalFileStorage]", e);
throw e;
}
}
}

View File

@@ -55,7 +55,7 @@ export default class S3FileStorage {
{ expiresIn: 60 },
);
console.log(signedUrl);
console.log("[S3]", signedUrl);
try {
await fetch(signedUrl, {