mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-24 22:50:22 +09:00
feat: support paste upload image
This commit is contained in:
parent
d8983c283e
commit
666e62d75a
3
.gitignore
vendored
3
.gitignore
vendored
@ -45,4 +45,5 @@ dev
|
|||||||
*.key
|
*.key
|
||||||
*.key.pub
|
*.key.pub
|
||||||
|
|
||||||
/public/uploads
|
/public/uploads
|
||||||
|
.vercel
|
||||||
|
@ -24,6 +24,7 @@ import {
|
|||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { makeAzurePath } from "@/app/azure";
|
import { makeAzurePath } from "@/app/azure";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
export interface OpenAIListModelResponse {
|
export interface OpenAIListModelResponse {
|
||||||
object: string;
|
object: string;
|
||||||
@ -75,6 +76,12 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
|
|
||||||
async chat(options: ChatOptions) {
|
async chat(options: ChatOptions) {
|
||||||
const messages: any[] = [];
|
const messages: any[] = [];
|
||||||
|
|
||||||
|
const getImageBase64Data = async (url: string) => {
|
||||||
|
const response = await axios.get(url, { responseType: "arraybuffer" });
|
||||||
|
const base64 = Buffer.from(response.data, "binary").toString("base64");
|
||||||
|
return base64;
|
||||||
|
};
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
let message: {
|
let message: {
|
||||||
role: string;
|
role: string;
|
||||||
@ -88,22 +95,13 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
text: v.content,
|
text: v.content,
|
||||||
});
|
});
|
||||||
if (v.image_url) {
|
if (v.image_url) {
|
||||||
await fetch(v.image_url)
|
var base64Data = await getImageBase64Data(v.image_url);
|
||||||
.then((response) => response.arrayBuffer())
|
message.content.push({
|
||||||
.then((buffer) => {
|
type: "image_url",
|
||||||
const base64Data = btoa(
|
image_url: {
|
||||||
String.fromCharCode(...new Uint8Array(buffer)),
|
url: `data:image/jpeg;base64,${base64Data}`,
|
||||||
);
|
},
|
||||||
message.content.push({
|
});
|
||||||
type: "image_url",
|
|
||||||
image_url: {
|
|
||||||
url: `data:image/jpeg;base64,${base64Data}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
}
|
}
|
||||||
|
@ -458,6 +458,10 @@ export function ChatActions(props: {
|
|||||||
document.getElementById("chat-image-file-select-upload")?.click();
|
document.getElementById("chat-image-file-select-upload")?.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function closeImageButton() {
|
||||||
|
document.getElementById("chat-input-image-close")?.click();
|
||||||
|
}
|
||||||
|
|
||||||
const onImageSelected = async (e: any) => {
|
const onImageSelected = async (e: any) => {
|
||||||
const file = e.target.files[0];
|
const file = e.target.files[0];
|
||||||
const fileName = await api.file.upload(file);
|
const fileName = await api.file.upload(file);
|
||||||
@ -488,7 +492,28 @@ export function ChatActions(props: {
|
|||||||
);
|
);
|
||||||
showToast(nextModel);
|
showToast(nextModel);
|
||||||
}
|
}
|
||||||
}, [chatStore, currentModel, models]);
|
const onPaste = (event: ClipboardEvent) => {
|
||||||
|
const items = event.clipboardData?.items || [];
|
||||||
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
if (items[i].type.indexOf("image") === -1) continue;
|
||||||
|
const file = items[i].getAsFile();
|
||||||
|
if (file !== null) {
|
||||||
|
api.file.upload(file).then((fileName) => {
|
||||||
|
props.imageSelected({
|
||||||
|
fileName,
|
||||||
|
fileUrl: `/api/file/${fileName}`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (currentModel === "gpt-4-vision-preview") {
|
||||||
|
window.addEventListener("paste", onPaste);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("paste", onPaste);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, [chatStore, currentModel, models, props]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles["chat-input-actions"]}>
|
<div className={styles["chat-input-actions"]}>
|
||||||
@ -594,6 +619,7 @@ export function ChatActions(props: {
|
|||||||
chatStore.updateCurrentSession((session) => {
|
chatStore.updateCurrentSession((session) => {
|
||||||
session.mask.modelConfig.model = s[0] as ModelType;
|
session.mask.modelConfig.model = s[0] as ModelType;
|
||||||
session.mask.syncGlobalConfig = false;
|
session.mask.syncGlobalConfig = false;
|
||||||
|
closeImageButton();
|
||||||
});
|
});
|
||||||
showToast(s[0]);
|
showToast(s[0]);
|
||||||
}}
|
}}
|
||||||
@ -1436,6 +1462,7 @@ function _Chat() {
|
|||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
className={styles["chat-input-image-close"]}
|
className={styles["chat-input-image-close"]}
|
||||||
|
id="chat-input-image-close"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setUserImage(null);
|
setUserImage(null);
|
||||||
}}
|
}}
|
||||||
|
Loading…
Reference in New Issue
Block a user