Skip to content
Draft
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
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
target/

### IntelliJ IDEA ###
.idea/*
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

### .env files contain local environment variables ###
.env
langchain4j-core/target_test-classes/
91 changes: 71 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.google.genai</groupId>
Expand Down Expand Up @@ -61,7 +61,9 @@
<repository>
<id>ossrh</id>
<name>Central Repository OSSRH</name>
<url>https://google.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<url>
https://google.oss.sonatype.org/service/local/staging/deploy/maven2/
</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
Expand Down Expand Up @@ -131,7 +133,7 @@
<artifactId>Java-WebSocket</artifactId>
<version>${java-websocket.version}</version>
</dependency>
<!-- JUnit 5 -->

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -156,14 +158,14 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- jspecify -->

<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
Expand All @@ -178,14 +180,14 @@
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->

<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
Expand All @@ -207,16 +209,17 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<dependencies>
<!-- tree view output test results: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#external-extensions-for-the-plugin -->
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>0.1.0</version>
</dependency>

<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
<configuration>
<reportFormat>plain</reportFormat>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter"/>
<reportFormat>plain</reportFormat>
<statelessTestsetInfoReporter
implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter"/>
</configuration>
</plugin>
<plugin>
Expand All @@ -231,7 +234,7 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->

<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
Expand Down Expand Up @@ -261,6 +264,7 @@
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
Expand All @@ -285,7 +289,8 @@
<show>public</show>
<doclint>html</doclint>
<nohelp>true</nohelp>
<outputDirectory>${project.build.directory}/javadoc</outputDirectory>
<outputDirectory>${project.build.directory}/javadoc
</outputDirectory>
<doctitle>Google Gen AI Java SDK</doctitle>
<source>${maven.compiler.source}</source>
<encoding>UTF-8</encoding>
Expand Down Expand Up @@ -333,6 +338,52 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.10.2</version>
<extensions>true</extensions>
<configuration>
<buildArgs>
<buildArg>--no-fallback</buildArg>
<buildArg>--verbose</buildArg>
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<filtering>false</filtering>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
<profiles>

<profile>
<id>native-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -agentlib:native-image-agent=config-output-dir=target/native-image</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
37 changes: 26 additions & 11 deletions src/main/java/com/google/genai/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.genai.errors.GenAiIOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -27,14 +28,17 @@
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jspecify.annotations.Nullable;
import com.google.genai.errors.GenAiIOException;

/** Common utility methods for the GenAI SDK. */
final class Common {

private Common() {}

private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{([^}]+)\\}");

/**
* Sets the value of an object by a path.
*
Expand Down Expand Up @@ -161,7 +165,8 @@ static void setValueByPath(ObjectNode jsonObject, String[] path, Object value) {
} else {
if (currentObject.isObject() && ((ObjectNode) currentObject).has(key)) {
currentObject = ((ObjectNode) currentObject).get(key);
} else {
}
else {
return null;
}
}
Expand All @@ -171,20 +176,30 @@ static void setValueByPath(ObjectNode jsonObject, String[] path, Object value) {
}

static String formatMap(String template, JsonNode data) {
if (data == null) {
if (data == null || !data.isObject()) {
return template;
}

Iterator<Map.Entry<String, JsonNode>> fields = data.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
String key = field.getKey();
String placeholder = "{" + key + "}";
if (template.contains(placeholder)) {
template = template.replace(placeholder, data.get(key).asText());
System.out.println("Template " + template);
System.out.println("Data " + data);

Matcher matcher = PLACEHOLDER_PATTERN.matcher(template);
StringBuffer resultBuffer = new StringBuffer();

while (matcher.find()) {
String key = matcher.group(1);
// JsonNode valueNode = data.get(key);
JsonNode valueNode = data.path(key);

if (valueNode != null && valueNode.isValueNode()) {
String replacement = valueNode.asText();
matcher.appendReplacement(resultBuffer, Matcher.quoteReplacement(replacement));
}
}
return template;
matcher.appendTail(resultBuffer);

System.out.println("Template " + template);
return resultBuffer.toString();
}

static boolean isZero(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"name": "sun.management.VMManagementImpl",
"fields": [
{
"name": "compTimeMonitoringSupport"
},
{
"name": "currentThreadCpuTimeSupport"
},
{
"name": "objectMonitorUsageSupport"
},
{
"name": "otherThreadCpuTimeSupport"
},
{
"name": "remoteDiagnosticCommandsSupport"
},
{
"name": "synchronizerUsageSupport"
},
{
"name": "threadAllocatedMemorySupport"
},
{
"name": "threadContentionMonitoringSupport"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = --initialize-at-build-time=org.junit.platform.commons.util.LruCache,org.mockito,net.bytebuddy,org.objenesis,com.google.genai.Client
Loading
Loading