GeminiChatModel
public class GeminiChatModel extends ChatModelBase
io/agentscope/extensions/model/gemini/GeminiChatModel.java:64–647Gemini 聊天模型,与模型服务通信并返回生成或向量化结果。位于 io.agentscope.extensions.model.gemini 包,继承自 ChatModelBase。核心成员包括 doStream、doStream0、getModelName、close、builder、GeminiChatModel。
Gemini Chat Model implementation using the official Google GenAI Java SDK.
This implementation provides complete integration with Gemini's Content Generation API, including tool calling and multi-agent conversation support.
Supported Features:
-
Text generation with streaming and non-streaming modes
-
Tool/function calling support
-
Multi-agent conversation with history merging
-
Vision capabilities (images, audio, video)
-
Thinking mode (extended reasoning)
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | 构建器,用于构建并配置目标实例。 Builder for creating GeminiChatModel instances. |
构造器摘要Constructor Summary
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) | 以流式方式执行并返回结果。 Stream chat completion responses from Gemini's API. |
protected Flux<ChatResponse> doStream0(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) | 以流式方式执行并返回结果。 Performs the do stream 0 operation. |
public String getModelName() | 获取模型名称。 Returns the model name. |
public void close() | 关闭连接并释放相关资源。 Close the Gemini client. |
public static Builder builder()static | 创建新的构建器用于 GeminiChatModel。 Creates a new builder for GeminiChatModel. |
构造器详情Constructor Detail
GeminiChatModel
public GeminiChatModel(String apiKey, String baseUrl, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, HttpOptions httpOptions, GoogleCredentials credentials, ClientOptions clientOptions, GenerateOptions defaultOptions, Formatter<Content, GenerateContentResponse, GenerateContentConfig.Builder> formatter)
初始化 GeminiChatModel 实例。
Creates a new Gemini chat model instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication (for Gemini API) |
baseUrl | String | 基类 URL(类型 `String`)。the custom base URL for Gemini API (null for default) |
modelName | String | 模型名称(类型 `String`)。the model name to use (e.g., "gemini-2.0-flash", "gemini-1.5-pro") |
streamEnabled | boolean | 流已启用(类型 `boolean`)。whether streaming should be enabled |
project | String | project(类型 `String`)。the Google Cloud project ID (for Vertex AI) |
location | String | location(类型 `String`)。the Google Cloud location (for Vertex AI, e.g., "us-central1") |
vertexAI | Boolean | vertex AI(类型 `Boolean`)。whether to use Vertex AI APIs (null for auto-detection) |
httpOptions | HttpOptions | 用于客户端的HTTP 选项。HTTP options for the client |
credentials | GoogleCredentials | 凭证(类型 `GoogleCredentials`)。Google credentials (for Vertex AI) |
clientOptions | ClientOptions | 用于API 客户端的客户端选项。client options for the API client |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。default generation options |
formatter | Formatter<Content, GenerateContentResponse, GenerateContentConfig.Builder> | 格式化器(类型 `Formatter`)。the message formatter to use (null for default Gemini formatter) |
io/agentscope/extensions/model/gemini/GeminiChatModel.java:101–158GeminiChatModel
public GeminiChatModel(String apiKey, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, HttpOptions httpOptions, GoogleCredentials credentials, ClientOptions clientOptions, GenerateOptions defaultOptions, Formatter<Content, GenerateContentResponse, GenerateContentConfig.Builder> formatter)
初始化 GeminiChatModel 实例。
Creates a new Gemini chat model instance using the default endpoint configuration.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
apiKey | String | API 键(类型 `String`)。the API key for authentication (for Gemini API) |
modelName | String | 模型名称(类型 `String`)。the model name to use (e.g., "gemini-2.0-flash", "gemini-1.5-pro") |
streamEnabled | boolean | 流已启用(类型 `boolean`)。whether streaming should be enabled |
project | String | project(类型 `String`)。the Google Cloud project ID (for Vertex AI) |
location | String | location(类型 `String`)。the Google Cloud location (for Vertex AI, e.g., "us-central1") |
vertexAI | Boolean | vertex AI(类型 `Boolean`)。whether to use Vertex AI APIs (null for auto-detection) |
httpOptions | HttpOptions | 用于客户端的HTTP 选项。HTTP options for the client |
credentials | GoogleCredentials | 凭证(类型 `GoogleCredentials`)。Google credentials (for Vertex AI) |
clientOptions | ClientOptions | 用于API 客户端的客户端选项。client options for the API client |
defaultOptions | GenerateOptions | 默认选项(类型 `GenerateOptions`)。default generation options |
formatter | Formatter<Content, GenerateContentResponse, GenerateContentConfig.Builder> | 格式化器(类型 `Formatter`)。the message formatter to use (null for default Gemini formatter) |
io/agentscope/extensions/model/gemini/GeminiChatModel.java:182–207方法详情Method Detail
doStream
protected Flux<ChatResponse> doStream(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Stream chat completion responses from Gemini's API.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。AgentScope messages to send to the model |
tools | List<ToolSchema> | 工具列表(类型 `List`)。Optional list of tool schemas (null or empty if no tools) |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。Optional generation options (null to use defaults) |
Flux<ChatResponse>io/agentscope/extensions/model/gemini/GeminiChatModel.java:233–238doStream0
protected Flux<ChatResponse> doStream0(List<Msg> messages, List<ToolSchema> tools, GenerateOptions options)
以流式方式执行并返回结果。
Performs the do stream 0 operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。The messages. |
tools | List<ToolSchema> | 工具列表(类型 `List`)。The tools. |
options | GenerateOptions | 选项(类型 `GenerateOptions`)。The options. |
Flux<ChatResponse>io/agentscope/extensions/model/gemini/GeminiChatModel.java:240–326getModelName
public String getModelName()
获取模型名称。
Returns the model name.
Stringio/agentscope/extensions/model/gemini/GeminiChatModel.java:328–331close
public void close()
关闭连接并释放相关资源。
Close the Gemini client.
io/agentscope/extensions/model/gemini/GeminiChatModel.java:336–344builder
public static Builder builder()
创建新的构建器用于 GeminiChatModel。
Creates a new builder for GeminiChatModel.
Builderio/agentscope/extensions/model/gemini/GeminiChatModel.java:351–353嵌套类详情Nested Class Detail
Builder
public static class Builder构建器,用于构建并配置目标实例。位于 io.agentscope.extensions.model.gemini 包。核心成员包括 apiKey、baseUrl、modelName、streamEnabled、project、location。
Builder for creating GeminiChatModel instances.