public class GeminiClient extends Object
| Modifier and Type | Field and Description |
|---|---|
static boolean |
debug |
static okhttp3.OkHttpClient |
httpClient |
| Constructor and Description |
|---|
GeminiClient() |
| Modifier and Type | Method and Description |
|---|---|
static String |
chatWithModel(String model,
String role,
String prompt) |
static String |
chatWithModel(String apiKey,
String model,
String role,
String prompt) |
static GeminiCacheVo |
createCache(GeminiCreateCacheRequestVo requestVo)
Creates a new cached content entry using API key from environment.
|
static GeminiCacheVo |
createCache(String googleApiKey,
GeminiCreateCacheRequestVo requestVo)
Creates a new cached content entry.
|
static void |
deleteCache(String cacheName)
Deletes a cached content entry using API key from environment.
|
static void |
deleteCache(String googleApiKey,
String cacheName)
Deletes a cached content entry.
|
static GeminiChatResponseVo |
generate(String modelName,
GeminiChatRequestVo requestVo) |
static okhttp3.Response |
generate(String modelName,
String bodyString) |
static GeminiChatResponseVo |
generate(String googleApiKey,
String modelName,
GeminiChatRequestVo requestVo)
单次生成内容 (同步请求)
POST https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={GOOGLE_API_KEY}
|
static okhttp3.Response |
generate(String googleApiKey,
String modelName,
String bodyString) |
static GeminiChatResponseVo |
generate(String baseUrl,
String googleApiKey,
String modelName,
GeminiChatRequestVo requestVo) |
static GeminiCacheVo |
getCache(String cacheName)
Gets metadata for a specific cached content entry using API key from environment.
|
static GeminiCacheVo |
getCache(String googleApiKey,
String cacheName)
Gets metadata for a specific cached content entry.
|
static GeminiListCachesResponseVo |
listCaches()
Lists metadata for all cached content entries using API key from environment.
|
static GeminiListCachesResponseVo |
listCaches(String googleApiKey)
Lists metadata for all cached content entries.
|
static String |
parseYoutubeSubtitle(String model,
String url,
String userPrompt) |
static okhttp3.Call |
stream(String modelName,
GeminiChatRequestVo requestVo,
okhttp3.Callback callback) |
static okhttp3.sse.EventSource |
stream(String modelName,
GeminiChatRequestVo requestVo,
okhttp3.sse.EventSourceListener listener) |
static okhttp3.Call |
stream(String modelName,
String bodyString,
okhttp3.Callback callback) |
static okhttp3.sse.EventSource |
stream(String modelName,
String bodyString,
okhttp3.sse.EventSourceListener listener) |
static okhttp3.Call |
stream(String googleApiKey,
String modelName,
GeminiChatRequestVo requestVo,
okhttp3.Callback callback)
流式生成内容 (SSE,异步请求)
POST https://generativelanguage.googleapis.com/v1beta/models/{model_name}:streamGenerateContent?alt=sse&key={GOOGLE_API_KEY}
注:请求体还是 JSON,但响应会通过 SSE 分段返回.
|
static okhttp3.sse.EventSource |
stream(String googleApiKey,
String modelName,
GeminiChatRequestVo requestVo,
okhttp3.sse.EventSourceListener listener) |
static okhttp3.Call |
stream(String googleApiKey,
String modelName,
String bodyString,
okhttp3.Callback callback) |
static okhttp3.sse.EventSource |
stream(String googleApiKey,
String modelName,
String bodyString,
okhttp3.sse.EventSourceListener listener) |
static GeminiCacheVo |
updateCache(String cacheName,
GeminiUpdateCacheRequestVo updateRequestVo)
Updates a cached content entry using API key from environment.
|
static GeminiCacheVo |
updateCache(String googleApiKey,
String cacheName,
GeminiUpdateCacheRequestVo updateRequestVo)
Updates a cached content entry (only ttl or expireTime can be updated).
|
static FileUploadResponseVo |
uploadFile(byte[] bytes)
重载方法,从环境变量中获取 API Key
|
static String |
uploadFile(String googleApiKey,
byte[] bytes)
上传文件到 Google Gemini API
|
public static boolean debug
public static final okhttp3.OkHttpClient httpClient
public static GeminiChatResponseVo generate(String googleApiKey, String modelName, GeminiChatRequestVo requestVo)
googleApiKey - - 你的 Google API KeymodelName - - 要使用的模型名称,如 "gemini-1.5-flash"requestVo - - 请求体实体public static GeminiChatResponseVo generate(String baseUrl, String googleApiKey, String modelName, GeminiChatRequestVo requestVo)
public static GeminiChatResponseVo generate(String modelName, GeminiChatRequestVo requestVo)
public static okhttp3.Response generate(String googleApiKey, String modelName, String bodyString)
public static String chatWithModel(String model, String role, String prompt)
model - role - prompt - public static String chatWithModel(String apiKey, String model, String role, String prompt)
apiKey - model - role - prompt - public static okhttp3.Call stream(String googleApiKey, String modelName, GeminiChatRequestVo requestVo, okhttp3.Callback callback)
googleApiKey - - 你的 Google API KeymodelName - - 要使用的模型名称requestVo - - 请求体callback - - 用于处理 SSE 流式回调public static okhttp3.sse.EventSource stream(String googleApiKey, String modelName, GeminiChatRequestVo requestVo, okhttp3.sse.EventSourceListener listener)
public static okhttp3.Call stream(String modelName, GeminiChatRequestVo requestVo, okhttp3.Callback callback)
public static okhttp3.sse.EventSource stream(String modelName, GeminiChatRequestVo requestVo, okhttp3.sse.EventSourceListener listener)
public static okhttp3.Call stream(String modelName, String bodyString, okhttp3.Callback callback)
public static okhttp3.sse.EventSource stream(String modelName, String bodyString, okhttp3.sse.EventSourceListener listener)
public static okhttp3.Call stream(String googleApiKey, String modelName, String bodyString, okhttp3.Callback callback)
public static okhttp3.sse.EventSource stream(String googleApiKey, String modelName, String bodyString, okhttp3.sse.EventSourceListener listener)
public static String uploadFile(String googleApiKey, byte[] bytes)
googleApiKey - - 你的 Google API Keypublic static String parseYoutubeSubtitle(String model, String url, String userPrompt)
public static FileUploadResponseVo uploadFile(byte[] bytes)
displayName - - 文件的显示名称file - - 要上传的文件mimeType - - 文件的 MIME 类型public static GeminiCacheVo createCache(String googleApiKey, GeminiCreateCacheRequestVo requestVo)
googleApiKey - - Your Google API KeyrequestVo - - The request body for creating the cachepublic static GeminiCacheVo createCache(GeminiCreateCacheRequestVo requestVo)
requestVo - - The request body for creating the cachepublic static GeminiCacheVo getCache(String googleApiKey, String cacheName)
googleApiKey - - Your Google API KeycacheName - - The full name of the cache (e.g., "cachedContents/12345")public static GeminiCacheVo getCache(String cacheName)
cacheName - - The full name of the cache (e.g., "cachedContents/12345")public static GeminiListCachesResponseVo listCaches(String googleApiKey)
googleApiKey - - Your Google API Keypublic static GeminiListCachesResponseVo listCaches()
public static GeminiCacheVo updateCache(String googleApiKey, String cacheName, GeminiUpdateCacheRequestVo updateRequestVo)
googleApiKey - - Your Google API KeycacheName - - The full name of the cache (e.g., "cachedContents/12345")updateRequestVo - - The request body containing the fields to update (ttl or expireTime)public static GeminiCacheVo updateCache(String cacheName, GeminiUpdateCacheRequestVo updateRequestVo)
cacheName - - The full name of the cache (e.g., "cachedContents/12345")updateRequestVo - - The request body containing the fields to update (ttl or expireTime)public static void deleteCache(String googleApiKey, String cacheName)
googleApiKey - - Your Google API KeycacheName - - The full name of the cache (e.g., "cachedContents/12345")public static void deleteCache(String cacheName)
cacheName - - The full name of the cache (e.g., "cachedContents/12345")Copyright © 2025. All rights reserved.