feat: support search for gemini2: #chatgpt开启gemini搜索 #chatgpt开启代码执行 暂时不可并存

This commit is contained in:
ikechan8370 2024-12-12 20:31:39 +08:00
parent 8fc2ca5621
commit 5f6c4e5abb
5 changed files with 118 additions and 13 deletions

View file

@ -27,13 +27,37 @@ export const HarmBlockThreshold = {
* parts: Array<{
* text?: string,
* functionCall?: FunctionCall,
* functionResponse?: FunctionResponse
* functionResponse?: FunctionResponse,
* executableCode?: {
* language: string,
* code: string
* },
* codeExecutionResult?: {
* outcome: string,
* output: string
* }
* }>
* }} Content
*
* Gemini消息的基本格式
*/
/**
* @typedef {{
* searchEntryPoint: {
* renderedContent: string,
* },
* groundingChunks: Array<{
* web: {
* uri: string,
* title: string
* }
* }>,
* webSearchQueries: Array<string>
* }} GroundingMetadata
* 搜索结果的元数据
*/
/**
* @typedef {{
* name: string,
@ -80,7 +104,9 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
* temperature: number?,
* topP: number?,
* tokK: number?,
* replyPureTextCallback: Function
* replyPureTextCallback: Function,
* search: boolean,
* codeExecution: boolean
* }} opt
* @returns {Promise<{conversationId: string?, parentMessageId: string, text: string, id: string}>}
*/
@ -163,15 +189,15 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
temperature: opt.temperature || 0.9,
topP: opt.topP || 0.95,
topK: opt.tokK || 16
}
},
tools: []
}
if (this.tools?.length > 0) {
body.tools = [
{
function_declarations: this.tools.map(tool => tool.function())
// codeExecution: {}
}
]
body.tools.push({
function_declarations: this.tools.map(tool => tool.function())
// codeExecution: {}
})
// ANY要笑死人的效果
body.tool_config = {
function_calling_config: {
@ -179,6 +205,12 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
}
}
}
if (opt.search) {
body.tools.push({ google_search: {} })
}
if (opt.codeExecution) {
body.tools.push({ code_execution: {} })
}
if (opt.image) {
delete body.tools
}
@ -202,13 +234,14 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
*/
let responseContent
/**
* @type {{candidates: Array<{content: Content}>}}
* @type {{candidates: Array<{content: Content, groundingMetadata: GroundingMetadata}>}}
*/
let response = await result.json()
if (this.debug) {
console.log(JSON.stringify(response))
}
responseContent = response.candidates[0].content
let groundingMetadata = response.candidates[0].groundingMetadata
if (responseContent.parts.find(i => i.functionCall)) {
// functionCall
const functionCall = responseContent.parts.find(i => i.functionCall).functionCall
@ -265,6 +298,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
// 递归直到返回text
// 先把这轮的消息存下来
await this.upsertMessage(thisMessage)
responseContent = handleSearchResponse(responseContent).responseContent
const respMessage = Object.assign(responseContent, {
id: idModel,
parentMessageId: idThis
@ -290,11 +324,54 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
})
await this.upsertMessage(respMessage)
}
let { final } = handleSearchResponse(responseContent)
try {
if (groundingMetadata?.groundingChunks) {
final += '\n参考资料\n'
groundingMetadata.groundingChunks.forEach(chunk => {
// final += `[${chunk.web.title}](${chunk.web.uri})\n`
final += `[${chunk.web.title}]\n`
})
groundingMetadata.webSearchQueries.forEach(q => {
logger.info('search query: ' + q)
})
}
} catch (err) {
logger.warn(err)
}
return {
text: responseContent.parts[0].text.trim(),
text: final,
conversationId: '',
parentMessageId: idThis,
id: idModel
}
}
}
/**
* 处理成单独的text
* @param {Content} responseContent
* @returns {{final: string, responseContent}}
*/
function handleSearchResponse (responseContent) {
let final = ''
for (let part of responseContent.parts) {
if (part.text) {
final += part.text
}
if (part.executableCode) {
final += '\n执行代码\n' + '```' + part.executableCode.language + '\n' + part.executableCode.code.trim() + '\n```\n\n'
}
if (part.codeExecutionResult) {
final += `\n执行结果(${part.codeExecutionResult.outcome})\n` + '```\n' + part.codeExecutionResult.output + '\n```\n\n'
}
}
responseContent.parts = [{
text: final
}]
return {
final,
responseContent
}
}