Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,35 @@ Client client = Client.builder()
The Google Gen AI Java SDK allows you to access the service programmatically.
The following code snippets are some basic usages of model inferencing.

#### Seed Live API conversation history

Gemini 3.1 Live sessions can accept conversation history through client content
messages before realtime input begins. Enable initial history processing when
connecting:

```java
import com.google.genai.Client;
import com.google.genai.types.HistoryConfig;
import com.google.genai.types.LiveConnectConfig;
import com.google.genai.types.Modality;

Client client = new Client();

LiveConnectConfig config =
LiveConnectConfig.builder()
.responseModalities(Modality.Known.AUDIO)
.historyConfig(
HistoryConfig.builder()
.initialHistoryInClientContent(true))
.build();

client.async.live.connect("gemini-3.1-flash-live-preview", config);
```

After the session connects, use `sendClientContent` to send the initial history
and finish it with `turnComplete(true)`. Use `sendRealtimeInput` for subsequent
conversation input.

#### Generate Content
Use `generateContent` method for the most basic content generation.

Expand Down Expand Up @@ -1027,4 +1056,4 @@ Apache 2.0 - See [LICENSE][license] for more information.
[maven-version-link]: https://central.sonatype.com/artifact/com.google.genai/google-genai
[javadoc-image]: https://img.shields.io/badge/JavaDoc-Online-green
[javadoc-link]: https://googleapis.github.io/java-genai/javadoc/
[license]: https://github.com/googleapis/java-genai/blob/main/LICENSE
[license]: https://github.com/googleapis/java-genai/blob/main/LICENSE
14 changes: 14 additions & 0 deletions src/main/java/com/google/genai/LiveConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,13 @@ ObjectNode liveConnectConfigToMldev(JsonNode fromObject, ObjectNode parentObject
Common.getValueByPath(fromObject, new String[] {"translationConfig"}));
}

if (Common.getValueByPath(fromObject, new String[] {"historyConfig"}) != null) {
Common.setValueByPath(
parentObject,
new String[] {"setup", "historyConfig"},
Common.getValueByPath(fromObject, new String[] {"historyConfig"}));
}

return toObject;
}

Expand Down Expand Up @@ -1457,6 +1464,13 @@ ObjectNode liveConnectConfigToVertex(JsonNode fromObject, ObjectNode parentObjec
+ " Gemini Enterprise Agent Platform mode.");
}

if (Common.getValueByPath(fromObject, new String[] {"historyConfig"}) != null) {
Common.setValueByPath(
parentObject,
new String[] {"setup", "historyConfig"},
Common.getValueByPath(fromObject, new String[] {"historyConfig"}));
}

return toObject;
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/google/genai/types/LiveConnectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public abstract class LiveConnectConfig extends JsonSerializable {
@JsonProperty("translationConfig")
public abstract Optional<TranslationConfig> translationConfig();

/** Configures the exchange of history between the client and the server. */
@JsonProperty("historyConfig")
public abstract Optional<HistoryConfig> historyConfig();

/** Instantiates a builder for LiveConnectConfig. */
@ExcludeFromGeneratedCoverageReport
public static Builder builder() {
Expand Down Expand Up @@ -898,6 +902,34 @@ public Builder clearTranslationConfig() {
return translationConfig(Optional.empty());
}

/**
* Setter for historyConfig.
*
* <p>historyConfig: Configures the exchange of history between the client and the server.
*/
@JsonProperty("historyConfig")
public abstract Builder historyConfig(HistoryConfig historyConfig);

/**
* Setter for historyConfig builder.
*
* <p>historyConfig: Configures the exchange of history between the client and the server.
*/
@CanIgnoreReturnValue
public Builder historyConfig(HistoryConfig.Builder historyConfigBuilder) {
return historyConfig(historyConfigBuilder.build());
}

@ExcludeFromGeneratedCoverageReport
abstract Builder historyConfig(Optional<HistoryConfig> historyConfig);

/** Clears the value of historyConfig field. */
@ExcludeFromGeneratedCoverageReport
@CanIgnoreReturnValue
public Builder clearHistoryConfig() {
return historyConfig(Optional.empty());
}

public abstract LiveConnectConfig build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,22 @@
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true
},
{
"name": "com.google.genai.types.AutoValue_HistoryConfig",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true
},
{
"name": "com.google.genai.types.AutoValue_HistoryConfig$Builder",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true
},
{
"name": "com.google.genai.types.AutoValue_HttpOptions",
"allDeclaredConstructors": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,13 @@
"queryAllDeclaredConstructors":true,
"methods":[{"name":"dynamicRetrievalConfig","parameterTypes":[] }]
},
{
"name":"com.google.genai.types.AutoValue_HistoryConfig",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"initialHistoryInClientContent","parameterTypes":[] }]
},
{
"name":"com.google.genai.types.AutoValue_HttpOptions",
"allDeclaredFields":true,
Expand Down Expand Up @@ -1595,7 +1602,7 @@
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"avatarConfig","parameterTypes":[] }, {"name":"contextWindowCompression","parameterTypes":[] }, {"name":"enableAffectiveDialog","parameterTypes":[] }, {"name":"explicitVadSignal","parameterTypes":[] }, {"name":"httpOptions","parameterTypes":[] }, {"name":"inputAudioTranscription","parameterTypes":[] }, {"name":"maxOutputTokens","parameterTypes":[] }, {"name":"mediaResolution","parameterTypes":[] }, {"name":"outputAudioTranscription","parameterTypes":[] }, {"name":"proactivity","parameterTypes":[] }, {"name":"realtimeInputConfig","parameterTypes":[] }, {"name":"responseModalities","parameterTypes":[] }, {"name":"safetySettings","parameterTypes":[] }, {"name":"seed","parameterTypes":[] }, {"name":"sessionResumption","parameterTypes":[] }, {"name":"speechConfig","parameterTypes":[] }, {"name":"systemInstruction","parameterTypes":[] }, {"name":"temperature","parameterTypes":[] }, {"name":"thinkingConfig","parameterTypes":[] }, {"name":"tools","parameterTypes":[] }, {"name":"topK","parameterTypes":[] }, {"name":"topP","parameterTypes":[] }, {"name":"translationConfig","parameterTypes":[] }]
"methods":[{"name":"avatarConfig","parameterTypes":[] }, {"name":"contextWindowCompression","parameterTypes":[] }, {"name":"enableAffectiveDialog","parameterTypes":[] }, {"name":"explicitVadSignal","parameterTypes":[] }, {"name":"historyConfig","parameterTypes":[] }, {"name":"httpOptions","parameterTypes":[] }, {"name":"inputAudioTranscription","parameterTypes":[] }, {"name":"maxOutputTokens","parameterTypes":[] }, {"name":"mediaResolution","parameterTypes":[] }, {"name":"outputAudioTranscription","parameterTypes":[] }, {"name":"proactivity","parameterTypes":[] }, {"name":"realtimeInputConfig","parameterTypes":[] }, {"name":"responseModalities","parameterTypes":[] }, {"name":"safetySettings","parameterTypes":[] }, {"name":"seed","parameterTypes":[] }, {"name":"sessionResumption","parameterTypes":[] }, {"name":"speechConfig","parameterTypes":[] }, {"name":"systemInstruction","parameterTypes":[] }, {"name":"temperature","parameterTypes":[] }, {"name":"thinkingConfig","parameterTypes":[] }, {"name":"tools","parameterTypes":[] }, {"name":"topK","parameterTypes":[] }, {"name":"topP","parameterTypes":[] }, {"name":"translationConfig","parameterTypes":[] }]
},
{
"name":"com.google.genai.types.AutoValue_LiveConnectConstraints",
Expand Down Expand Up @@ -3722,6 +3729,18 @@
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"com.google.genai.types.HistoryConfig",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true
},
{
"name":"com.google.genai.types.HistoryConfig$Builder",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"build","parameterTypes":[] }, {"name":"create","parameterTypes":[] }, {"name":"initialHistoryInClientContent","parameterTypes":["boolean"] }]
},
{
"name":"com.google.genai.types.HttpElementLocation",
"allDeclaredFields":true,
Expand Down Expand Up @@ -4134,7 +4153,7 @@
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"build","parameterTypes":[] }, {"name":"create","parameterTypes":[] }, {"name":"responseModalities","parameterTypes":["java.util.List"] }, {"name":"temperature","parameterTypes":["java.lang.Float"] }, {"name":"translationConfig","parameterTypes":["com.google.genai.types.TranslationConfig"] }]
"methods":[{"name":"build","parameterTypes":[] }, {"name":"create","parameterTypes":[] }, {"name":"historyConfig","parameterTypes":["com.google.genai.types.HistoryConfig"] }, {"name":"responseModalities","parameterTypes":["java.util.List"] }, {"name":"temperature","parameterTypes":["java.lang.Float"] }, {"name":"translationConfig","parameterTypes":["com.google.genai.types.TranslationConfig"] }]
},
{
"name":"com.google.genai.types.LiveConnectConstraints",
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/google/genai/LiveConvertersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.genai.types.Blob;
import com.google.genai.types.HistoryConfig;
import com.google.genai.types.LiveClientMessage;
import com.google.genai.types.LiveConnectConfig;
import com.google.genai.types.LiveSendRealtimeInputParameters;
Expand Down Expand Up @@ -189,4 +190,44 @@ public void testMultiSpeakerVoiceConfig_throws() {
.createObjectNode()) // Code that should throw the exception
);
}

@Test
public void testLiveConnectConfig_HistoryConfig_mldev() {
final LiveConnectConfig config =
LiveConnectConfig.builder()
.historyConfig(HistoryConfig.builder().initialHistoryInClientContent(true))
.build();
final ObjectNode transformed = JsonSerializable.objectMapper.createObjectNode();

new LiveConverters(GEMINI_API_CLIENT)
.liveConnectConfigToMldev(JsonSerializable.toJsonNode(config), transformed);

assertEquals(
true,
transformed
.get("setup")
.get("historyConfig")
.get("initialHistoryInClientContent")
.asBoolean());
}

@Test
public void testLiveConnectConfig_HistoryConfig_vertex() {
final LiveConnectConfig config =
LiveConnectConfig.builder()
.historyConfig(HistoryConfig.builder().initialHistoryInClientContent(true))
.build();
final ObjectNode transformed = JsonSerializable.objectMapper.createObjectNode();

new LiveConverters(VERTEX_AI_CLIENT)
.liveConnectConfigToVertex(JsonSerializable.toJsonNode(config), transformed);

assertEquals(
true,
transformed
.get("setup")
.get("historyConfig")
.get("initialHistoryInClientContent")
.asBoolean());
}
}