feat: support gemini-pro-vision
This commit is contained in:
parent
5c389db596
commit
e62b4c15e4
|
@ -13,6 +13,13 @@ import {
|
||||||
LLMUsage,
|
LLMUsage,
|
||||||
} from "../api";
|
} from "../api";
|
||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
const getImageBase64Data = async (url: string) => {
|
||||||
|
const response = await axios.get(url, { responseType: "arraybuffer" });
|
||||||
|
const base64 = Buffer.from(response.data, "binary").toString("base64");
|
||||||
|
return base64;
|
||||||
|
};
|
||||||
|
|
||||||
export class GeminiProApi implements LLMApi {
|
export class GeminiProApi implements LLMApi {
|
||||||
toolAgentChat(options: AgentChatOptions): Promise<void> {
|
toolAgentChat(options: AgentChatOptions): Promise<void> {
|
||||||
|
@ -28,11 +35,32 @@ export class GeminiProApi implements LLMApi {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
async chat(options: ChatOptions): Promise<void> {
|
async chat(options: ChatOptions): Promise<void> {
|
||||||
const apiClient = this;
|
const messages: any[] = [];
|
||||||
const messages = options.messages.map((v) => ({
|
if (options.config.model.includes("vision")) {
|
||||||
role: v.role.replace("assistant", "model").replace("system", "user"),
|
for (const v of options.messages) {
|
||||||
parts: [{ text: v.content }],
|
let message: any = {
|
||||||
}));
|
role: v.role.replace("assistant", "model").replace("system", "user"),
|
||||||
|
parts: [{ text: v.content }],
|
||||||
|
};
|
||||||
|
if (v.image_url) {
|
||||||
|
var base64Data = await getImageBase64Data(v.image_url);
|
||||||
|
message.parts.push({
|
||||||
|
inline_data: {
|
||||||
|
mime_type: "image/jpeg",
|
||||||
|
data: base64Data,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
messages.push(message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
options.messages.map((v) =>
|
||||||
|
messages.push({
|
||||||
|
role: v.role.replace("assistant", "model").replace("system", "user"),
|
||||||
|
parts: [{ text: v.content }],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// google requires that role in neighboring messages must not be the same
|
// google requires that role in neighboring messages must not be the same
|
||||||
for (let i = 0; i < messages.length - 1; ) {
|
for (let i = 0; i < messages.length - 1; ) {
|
||||||
|
@ -92,7 +120,9 @@ export class GeminiProApi implements LLMApi {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
options.onController?.(controller);
|
options.onController?.(controller);
|
||||||
try {
|
try {
|
||||||
const chatPath = this.path(Google.ChatPath);
|
const chatPath = this.path(
|
||||||
|
Google.ChatPath.replace("{{model}}", options.config.model),
|
||||||
|
);
|
||||||
const chatPayload = {
|
const chatPayload = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(requestPayload),
|
body: JSON.stringify(requestPayload),
|
||||||
|
|
|
@ -140,10 +140,9 @@ export class ChatGPTApi implements LLMApi {
|
||||||
presence_penalty: modelConfig.presence_penalty,
|
presence_penalty: modelConfig.presence_penalty,
|
||||||
frequency_penalty: modelConfig.frequency_penalty,
|
frequency_penalty: modelConfig.frequency_penalty,
|
||||||
top_p: modelConfig.top_p,
|
top_p: modelConfig.top_p,
|
||||||
max_tokens:
|
max_tokens: modelConfig.model.includes("vision")
|
||||||
modelConfig.model == "gpt-4-vision-preview"
|
? modelConfig.max_tokens
|
||||||
? modelConfig.max_tokens
|
: null,
|
||||||
: null,
|
|
||||||
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
// max_tokens: Math.max(modelConfig.max_tokens, 1024),
|
||||||
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
// Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore.
|
||||||
};
|
};
|
||||||
|
|
|
@ -538,7 +538,7 @@ export function ChatActions(props: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (currentModel === "gpt-4-vision-preview") {
|
if (currentModel.includes("vision")) {
|
||||||
window.addEventListener("paste", onPaste);
|
window.addEventListener("paste", onPaste);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("paste", onPaste);
|
window.removeEventListener("paste", onPaste);
|
||||||
|
@ -620,7 +620,7 @@ export function ChatActions(props: {
|
||||||
icon={usePlugins ? <EnablePluginIcon /> : <DisablePluginIcon />}
|
icon={usePlugins ? <EnablePluginIcon /> : <DisablePluginIcon />}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{currentModel == "gpt-4-vision-preview" && (
|
{currentModel.includes("vision") && (
|
||||||
<ChatAction
|
<ChatAction
|
||||||
onClick={selectImage}
|
onClick={selectImage}
|
||||||
text="选择图片"
|
text="选择图片"
|
||||||
|
@ -1412,7 +1412,7 @@ function _Chat() {
|
||||||
defaultShow={i >= messages.length - 6}
|
defaultShow={i >= messages.length - 6}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{!isUser && message.model == "gpt-4-vision-preview" && (
|
{!isUser && message.model?.includes("vision") && (
|
||||||
<div
|
<div
|
||||||
className={[
|
className={[
|
||||||
styles["chat-message-actions"],
|
styles["chat-message-actions"],
|
||||||
|
|
|
@ -91,7 +91,7 @@ export const Azure = {
|
||||||
|
|
||||||
export const Google = {
|
export const Google = {
|
||||||
ExampleEndpoint: "https://generativelanguage.googleapis.com/",
|
ExampleEndpoint: "https://generativelanguage.googleapis.com/",
|
||||||
ChatPath: "v1beta/models/gemini-pro:generateContent",
|
ChatPath: "v1beta/models/{{model}}:generateContent",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang
|
export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang
|
||||||
|
@ -253,6 +253,15 @@ export const DEFAULT_MODELS = [
|
||||||
providerType: "google",
|
providerType: "google",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "gemini-pro-vision",
|
||||||
|
available: true,
|
||||||
|
provider: {
|
||||||
|
id: "google",
|
||||||
|
providerName: "Google",
|
||||||
|
providerType: "google",
|
||||||
|
},
|
||||||
|
},
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export const CHAT_PAGE_SIZE = 15;
|
export const CHAT_PAGE_SIZE = 15;
|
||||||
|
|
Loading…
Reference in New Issue