类 GeminiUtil
java.lang.Object
com.google.adk.models.GeminiUtil
-
字段摘要
Fields -
方法概述
Modifier and TypeMethodDescriptionstatic Optional<com.google.genai.types.Part> getPart0FromLlmResponse(LlmResponse llmResponse) 如果存在的话,会提取 LlmResponse 对象的第一部分内容。static StringgetTextFromLlmResponse(LlmResponse llmResponse) 如果 `LlmResponse` 中包含第一部分内容,则从中提取文本。static LlmRequestprepareGenenerateContentRequest(LlmRequest llmRequest, boolean sanitize) 准备一个……LlmRequest这是用于 GenerateContent API 的示例代码。static LlmRequestprepareGenenerateContentRequest(LlmRequest llmRequest, boolean sanitize, boolean stripThoughts) 准备一个……LlmRequest这是用于 GenerateContent API 的示例代码。static LlmRequestremoveClientFunctionCallId(LlmRequest llmRequest) 从请求中删除客户端函数调用的 ID。static LlmRequestsanitizeRequestForGeminiApi(LlmRequest llmRequest) 对请求数据进行清洗处理,以确保其能与 Gemini API 后端兼容。static booleanshouldEmitAccumulatedText(LlmResponse currentLlmResponse) 根据当前的 LlmResponse,判断是否应该输出累积起来的文本。static com.google.common.collect.ImmutableList<com.google.genai.types.Content> stripThoughts(List<com.google.genai.types.Content> originalContents) 从内容列表中删除所有仅包含一个“想法”(thought)的 `Part`。static com.google.genai.types.GenerateContentResponseUsageMetadatatoGenerateContentResponseUsageMetadata(com.google.genai.types.UsageMetadata usageMetadata)
-
字段详情
-
HANDLE_SYSTEM_INSTRUCTION_MESSAGE
当请求没有内容时,会附加用户输入的文本,以便模型能够根据系统的指令进行相应的操作。- See Also:
-
CONTINUE_OUTPUT_MESSAGE
当最后一条内容不是用户发送的时,会追加用户输入的文本,这样模型就能继续生成输出结果。- See Also:
-
-
方法详细信息(Method Details)
-
prepareGenenerateContentRequest
准备一个……LlmRequest这是用于 GenerateContent API 的示例代码。此方法可以选择性地对请求数据进行清洗(即去除其中的恶意内容),并确保请求中的最后一个数据部分确实来自用户,从而确保模型能够正确地处理用户的输入并生成相应的响应。
- Parameters:
llmRequest- The originalLlmRequest.sanitize- Whether to sanitize the request to be compatible with the Gemini API backend.- Returns:
- The prepared
LlmRequest.
-
prepareGenenerateContentRequest
public static LlmRequest prepareGenenerateContentRequest(LlmRequest llmRequest, boolean sanitize, boolean stripThoughts) 准备一个……LlmRequest这是用于 GenerateContent API 的示例代码。此方法可以选择性地对请求数据进行清洗(即去除不必要的内容),以确保请求中的最后一部分数据确实来自用户,并用于触发模型的响应。该方法还会删除所有被标记为 “thoughts” 的内容,以及客户端函数调用相关的标识信息;因为某些大型语言模型(LLM)的 API 不允许包含客户端函数调用信息的请求。
- Parameters:
llmRequest- The originalLlmRequest.sanitize- Whether to sanitize the request to be compatible with the Gemini API backend.- Returns:
- The prepared
LlmRequest.
-
sanitizeRequestForGeminiApi
会对请求数据进行清洗处理,以确保其能够与 Gemini API 后端兼容。这一步骤是必需的,因为某些参数如果被包含在请求中,且被发送到错误的后端时,可能会导致运行时错误(例如:某些图像文件名仅适用于 Vertex AI)。- Parameters:
llmRequest- The request to sanitize.- Returns:
- The sanitized request.
-
removeClientFunctionCallId
从请求中删除客户端函数调用的 ID。客户端函数调用 ID 是 ADK 内部的信息,不应被发送到模型中。 该方法会遍历所有内容及相关部分,并从其中删除所有包含该 ID 的元素。
FunctionCall或者FunctionResponseparts.- Parameters:
llmRequest- The request to process.- Returns:
- A new
LlmRequestwith function call IDs removed.
-
getPart0FromLlmResponse
public static Optional<com.google.genai.types.Part> getPart0FromLlmResponse(LlmResponse llmResponse) 如果存在的话,会提取 LlmResponse 对象的第一部分内容。- Parameters:
llmResponse- The LlmResponse to extract the first part from.- Returns:
- The first part, or an empty optional if not found.
-
getTextFromLlmResponse
如果 `LlmResponse` 中包含第一部分内容,则从中提取文本。- Parameters:
llmResponse- The LlmResponse to extract text from.- Returns:
- The text content, or an empty string if not found.
-
shouldEmitAccumulatedText
根据当前的 LlmResponse 决定是否应该输出已累积的文本。如果当前响应不是文本的继续部分(例如:没有内容、没有数据片段,或者第一个数据片段不是 inline_data 类型,这意味着该响应属于其他类型或仅仅是空内容),则系统会清除之前累积的所有文本。- Parameters:
currentLlmResponse- The current LlmResponse being processed.- Returns:
- True if accumulated text should be emitted, false otherwise.
-
stripThoughts
public static com.google.common.collect.ImmutableList<com.google.genai.types.Content> stripThoughts(List<com.google.genai.types.Content> originalContents) 从内容列表中删除所有仅包含一个“想法”(thought)的 `Part`。 -
toGenerateContentResponseUsageMetadata
public static com.google.genai.types.GenerateContentResponseUsageMetadata toGenerateContentResponseUsageMetadata(com.google.genai.types.UsageMetadata usageMetadata)
-