feat(bym): 好像应该没问题支持了多图片处理?

This commit is contained in:
ycxom 2025-01-01 23:35:50 +08:00
parent e3fe14c6e6
commit 6033d93028
3 changed files with 172 additions and 60 deletions

View file

@ -81,7 +81,7 @@ export const HarmBlockThreshold = {
*/
export class CustomGoogleGeminiClient extends GoogleGeminiClient {
constructor (props) {
constructor(props) {
super(props)
this.model = props.model
this.baseUrl = props.baseUrl || BASEURL
@ -99,7 +99,8 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
* onProgress: function?,
* functionResponse: FunctionResponse?,
* system: string?,
* image: string?,
* image: string?, // 保留旧版单图片支持
* images: string[], // 新增多图片支持
* maxOutputTokens: number?,
* temperature: number?,
* topP: number?,
@ -111,7 +112,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
* }} opt
* @returns {Promise<{conversationId: string?, parentMessageId: string, text: string, id: string}>}
*/
async sendMessage (text, opt = {}) {
async sendMessage(text, opt = {}) {
let history = await this.getHistory(opt.parentMessageId)
let systemMessage = opt.system
if (systemMessage) {
@ -138,26 +139,40 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
const idModel = crypto.randomUUID()
const thisMessage = opt.functionResponse
? {
role: 'user',
parts: [{
functionResponse: opt.functionResponse
}],
id: idThis,
parentMessageId: opt.parentMessageId || undefined
}
role: 'user',
parts: [{
functionResponse: opt.functionResponse
}],
id: idThis,
parentMessageId: opt.parentMessageId || undefined
}
: {
role: 'user',
parts: [{ text }],
id: idThis,
parentMessageId: opt.parentMessageId || undefined
role: 'user',
parts: [{ text }],
id: idThis,
parentMessageId: opt.parentMessageId || undefined
}
if (opt.image || opt.images) {
// 兼容旧版单图片
if (opt.image) {
thisMessage.parts.push({
inline_data: {
mime_type: 'image/jpeg',
data: opt.image
}
})
}
// 处理多图片
if (opt.images && Array.isArray(opt.images)) {
for (let imageData of opt.images) {
thisMessage.parts.push({
inline_data: {
mime_type: 'image/jpeg',
data: imageData
}
})
}
if (opt.image) {
thisMessage.parts.push({
inline_data: {
mime_type: 'image/jpeg',
data: opt.image
}
})
}
}
history.push(_.cloneDeep(thisMessage))
let url = `${this.baseUrl}/v1beta/models/${this.model}:generateContent`
@ -220,7 +235,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
if (opt.codeExecution) {
body.tools.push({ code_execution: {} })
}
if (opt.image) {
if (opt.image || (opt.images && opt.images.length > 0)) {
delete body.tools
}
body.contents.forEach(content => {
@ -365,7 +380,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
* @param {Content} responseContent
* @returns {{final: string, responseContent}}
*/
function handleSearchResponse (responseContent) {
function handleSearchResponse(responseContent) {
let final = ''
// 遍历每个 part 并处理