Skip to content

Commit

Permalink
OCI GenAI Integration Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
arjav-desai committed Nov 14, 2024
1 parent 0e76cfa commit 98a9f64
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 0 deletions.
95 changes: 95 additions & 0 deletions examples/integrations/oci/genai-cdi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-mp</artifactId>
<version>4.1.2</version>
<relativePath/>
</parent>

<groupId>io.helidon.examples.integrations.oci</groupId>
<artifactId>helidon-examples-integrations-oci-genai-cdi</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Helidon Examples Integration OCI GenAI CDI</name>
<description>CDI integration with OCI GenAI.</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-jul</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci.sdk</groupId>
<artifactId>helidon-integrations-oci-sdk-cdi</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-generativeaiinference</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Test-scoped dependencies. -->
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
<execution>
<id>make-index</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.examples.integrations.oci.genai.cdi;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient;
import com.oracle.bmc.generativeaiinference.requests.ChatRequest;
import com.oracle.bmc.generativeaiinference.responses.ChatResponse;
import com.oracle.bmc.generativeaiinference.model.ChatContent;
import com.oracle.bmc.generativeaiinference.model.ChatDetails;
import com.oracle.bmc.generativeaiinference.model.ChatResult;
import com.oracle.bmc.generativeaiinference.model.GenericChatRequest;
import com.oracle.bmc.generativeaiinference.model.Message;
import com.oracle.bmc.generativeaiinference.model.OnDemandServingMode;
import com.oracle.bmc.generativeaiinference.model.TextContent;
import com.oracle.bmc.generativeaiinference.model.UserMessage;
import com.oracle.bmc.Region;

import jakarta.inject.Inject;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import org.eclipse.microprofile.config.inject.ConfigProperty;

/**
* JAX-RS resource - REST API for the Gen AI example.
*/
@Path("/genai")
public class GenAiResource {
private static final Logger LOGGER = Logger.getLogger(GenAiResource.class.getName());

private final GenerativeAiInferenceClient generativeAiInferenceClient;

@Inject
@ConfigProperty(name="oci.genai.compartment.id")
private String COMPARTMENT_ID;

@Inject
@ConfigProperty(name="oci.genai.model.id")
private String MODEL_ID;

@Inject
GenAiResource(GenerativeAiInferenceClient generativeAiInferenceClient,
@ConfigProperty(name = "oci.genai.region") String region) {
this.generativeAiInferenceClient = generativeAiInferenceClient;
generativeAiInferenceClient.setRegion(Region.valueOf(region));
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("chat")
public String chatModelAsk(@QueryParam("userMessage") String userMessage) {
LOGGER.log(Level.INFO, "UserMessage is: " + userMessage);
ChatContent content = TextContent.builder()
.text(userMessage)
.build();
List<ChatContent> contents = new ArrayList<>();
contents.add(content);
Message message = UserMessage.builder()
.content(contents)
.build();
List<Message> messages = new ArrayList<>();
messages.add(message);
GenericChatRequest chatRequest = GenericChatRequest.builder()
.messages(messages)
.isStream(false)
.build();
ChatDetails details = ChatDetails.builder()
.servingMode(OnDemandServingMode.builder().modelId(MODEL_ID).build())
.compartmentId(COMPARTMENT_ID)
.chatRequest(chatRequest)
.build();
ChatRequest request = ChatRequest.builder()
.chatDetails(details)
.build();
ChatResponse response = generativeAiInferenceClient.chat(request);
ChatResult chatResult = response.getChatResult();
LOGGER.log(Level.INFO, "Chat Result is: " + chatResult.toString());
return chatResult.toString();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Example of integration with OCI GenAI in Helidon MP application.
*/
package io.helidon.examples.integrations.oci.genai.cdi;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
version="4.0"
bean-discovery-mode="annotated">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Microprofile server properties
server.port=8080
server.host=0.0.0.0

# OCI Auth related properties
# https://helidon.io/docs/latest/apidocs/io.helidon.integrations.oci.sdk.cdi/io/helidon/integrations/oci/sdk/cdi/OciExtension.html
# config,config-file,session-token-config-file,session-token-builder,instance-principals,resource-principal,oke-workload-identity
oci.auth-strategies=session-token-config-file
oci.config.profile=helidonocidev

# OCI GenAI related properties
# https://docs.oracle.com/en-us/iaas/tools/java/3.53.1/com/oracle/bmc/Region.html
oci.genai.region=us-chicago-1
oci.genai.compartment.id=ocid1.compartment.oc1..aaaaaaaaiwoctrgf6w6rtq7az62fabscwgzbax2iepwu2qlub5qzcvabn5wa
oci.genai.model.id=ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyaiir6nnhmlgwvh37dr2mvragxzszqmz3hok52pcgmpqta
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.logging.jul.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

# Global logging level. Can be overridden by specific loggers
.level=INFO

# Quiet Weld
org.jboss.level=WARNING

# Component specific log levels
#io.helidon.config.level=INFO
#io.helidon.security.level=INFO
#io.helidon.common.level=INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.helidon.examples.integrations.oci.genai.cdi;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import io.helidon.microprofile.testing.junit5.HelidonTest;
import jakarta.inject.Inject;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;

@HelidonTest
public class GenAiResourceTest {

private static final String APP_URL = "/genai/";

@Inject
private WebTarget target;

private static String appUrl(String path) {
return APP_URL + path;
}

@Test
public void testChatModelAsk() {
String answer = target.path(appUrl("chat"))
.queryParam("userMessage", "Which are the most used Large Language Models?")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
assertTrue(answer.contains("BERT"), "actual: " + answer);
}

}
Loading

0 comments on commit 98a9f64

Please sign in to comment.