Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCI GenAI Integration Examples #103

Merged
merged 19 commits into from
Feb 18, 2025
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ jobs:
timeout-minutes: 30
runs-on: ubuntu-20.04
steps:
- name: Free disk space
shell: bash
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/share/powershell
- uses: actions/checkout@v4
with:
fetch-depth: '0'
Expand Down
50 changes: 50 additions & 0 deletions examples/integrations/oci/genai-cdi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Helidon Generative AI MP Examples

This example demonstrates how user can easily interact with their GenAI service running in OCI.

It requires a running OCI GenAI service endpoint. See [OCI Documentation](https://docs.oracle.com/en-us/iaas/Content/generative-ai/overview.htm) for more details.

Before running the test, make sure to update required properties in `microprofile-config.properties`

- oci.config.profile: This is name of session-token profile that you create beforehand.
- oci.genai.region: This is the subscribed region e.g. us-chicago-1, where your GenAI service is running.
- oci.genai.compartment_id: This is the OCID of your compartment, where your GenAI service is running.
- oci.genai.chat.model_id: This is the OCID of LLM that you want to use.
- oci.genai.embedding.model_id: This is the OCID of Embedding model that you want to use.

Once you have updated required properties, you can run the example:

```shell
mvn package
java -jar ./target/helidon-examples-integrations-oci-genai.jar
```

To verify that, you can retrieve wallet and do database operation:

```shell
curl curl http://localhost:8080/genai/chat?userMessage=Which%2Bare%2Bthe%2Bmost%2Bused%2BLarge%2BLanguage%2BModels%253F
```

You should see

```shell
ChatResult(super=BmcModel(__explicitlySet__=[modelId, modelVersion, chatResponse])modelId=ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyaiir6nnhmlgwvh37dr2mvragxzszqmz3hok52pcgmpqta, modelVersion=1.0.0, chatResponse=GenericChatResponse(super=BaseChatResponse(super=BmcModel(__explicitlySet__=[timeCreated, choices])), timeCreated=Thu Feb 13 12:52:17 CST 2025, choices=[ChatChoice(super=BmcModel(__explicitlySet__=[finishReason, index, message, logprobs])index=0, message=AssistantMessage(super=Message(super=BmcModel(__explicitlySet__=[content])content=[TextContent(super=ChatContent(super=BmcModel(__explicitlySet__=[text])), text=Here are some of the most widely used Large Language Models (LLMs) today:

1. **BERT (Bidirectional Encoder Representations from Transformers)**: Developed by Google in 2018, BERT is a multi-layer, bidirectional transformer encoder that has achieved state-of-the-art results in various NLP tasks, including language translation, question-answering, and sentiment analysis.

2. **RoBERTa (Robustly Optimized BERT Pretraining Approach)**: Released in 2019, RoBERTa is a variant of BERT that has been fine-tuned to achieve better performance in various NLP tasks. It's known for its effectiveness in tasks requiring long-range dependencies, such as question-answering.

3. **Transformers-XL**: Developed by Google in 2019, Transformers-XL is a neural network architecture designed to handle long-range dependencies. It's been applied in tasks such as text generation and machine translation.

4. **T5 (Text-to-Text Transfer Transformer)**: Introduced by Google in 2020, T5 is a unifying framework for NLP tasks, enabling users to convert all text-based tasks into a text-to-text format.

5. **LLaMA (Large Language Model Meta AI)**: Developed by Meta AI in 2022, LLaMA is a large language model designed for conversational AI applications. It can process up to 65,536 tokens and has achieved state-of-the-art results in various NLP tasks.

6. **PaLM (Pathways Language Model)**: Released by Google in 2022, PaLM is a large language model designed to handle the scale and complexity of modern NLP tasks. It has been fine-tuned for various NLP tasks and achieved state-of-the-art results.

7. **GPT (Generative Pre-trained Transformer)**: Developed by OpenAI in 2018, GPT is a large language model designed for text generation. Its architecture is based on the transformer model and has achieved state-of-the-art results in various NLP tasks, including language translation and text summarization. The GPT architecture has been used to develop several variants, such as GPT-2 (2019) and GPT-3 (2020).

8. **ChatGPT (Chat Generative Pre-trained Transformer)**: Developed by OpenAI in 2022, ChatGPT is an AI chatbot that uses a variant of the GPT-3 language model. It's designed for conversational applications and can process user input, generate human-like text responses, and even provide feedback.

These are just a few of the most popular large language models used today. The field of NLP is constantly evolving, and new LLMs are being developed to address specific challenges and applications.)]), name=null), finishReason=stop, logprobs=Logprobs(super=BmcModel(__explicitlySet__=[])textOffset=null, tokenLogprobs=null, tokens=null, topLogprobs=null))]))
```
100 changes: 100 additions & 0 deletions examples/integrations/oci/genai-cdi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2025 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.6</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>

<properties>
<version.lib.oci>3.56.0</version.lib.oci>
</properties>


<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,152 @@
/*
* Copyright (c) 2025 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.Region;
import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient;
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.EmbedTextDetails;
import com.oracle.bmc.generativeaiinference.model.EmbedTextResult;
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.ServingMode;
import com.oracle.bmc.generativeaiinference.model.TextContent;
import com.oracle.bmc.generativeaiinference.model.UserMessage;
import com.oracle.bmc.generativeaiinference.requests.ChatRequest;
import com.oracle.bmc.generativeaiinference.requests.EmbedTextRequest;
import com.oracle.bmc.generativeaiinference.responses.ChatResponse;
import com.oracle.bmc.generativeaiinference.responses.EmbedTextResponse;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.config.inject.ConfigProperty;

/**
* JAX-RS resource - REST API example of how to use OCI Generative AI Service.
*/
@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 compartmentId;

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

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

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

/**
* Handles HTTP GET requests to initiate a chat session with the Oracle Cloud Infrastructure (OCI)
* Generative AI service. It takes a user-provided message as input and returns the response from
* the chat model.
*
* @param userMessage the message sent by the user to initiate or continue the conversation
* @return the response from the chat model as a JSON string
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("chat")
public String chat(@QueryParam("userMessage") String userMessage) {
LOGGER.log(Level.INFO, "Start Running Chat Example ...");
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();
ServingMode servingmode = OnDemandServingMode.builder()
.modelId(chatModelId)
.build();
ChatDetails details = ChatDetails.builder()
.servingMode(servingmode)
.compartmentId(compartmentId)
.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());
generativeAiInferenceClient.close();
return chatResult.toString();
}

/**
* Handles HTTP GET requests to generate embeddings for a list of text inputs using the Oracle Cloud Infrastructure (OCI)
* Generative AI service. It takes a list of text inputs as query parameters and returns the generated embeddings as a JSON
* string.
*
* @param embeddingInputs a list of text inputs to generate embeddings for
* @return the generated embeddings as a JSON string
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("embedText")
public String embedText(@QueryParam("embeddingInputs") List<String> embeddingInputs) {
LOGGER.log(Level.INFO, "Start Running EmbedText Example ...");
LOGGER.log(Level.INFO, "Embedding Inputs is: " + embeddingInputs);
EmbedTextDetails embedTextDetails = EmbedTextDetails.builder()
.servingMode(OnDemandServingMode.builder().modelId(embedModelId).build())
.compartmentId(compartmentId)
.inputs(embeddingInputs)
.build();
EmbedTextRequest embedTextRequest = EmbedTextRequest.builder()
.embedTextDetails(embedTextDetails)
.build();
EmbedTextResponse embedTextResponse = generativeAiInferenceClient.embedText(embedTextRequest);
EmbedTextResult embedTextResult = embedTextResponse.getEmbedTextResult();
LOGGER.log(Level.INFO, embedTextResult.toString());
generativeAiInferenceClient.close();
return embedTextResult.toString();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2025 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) 2025 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>
Loading
Loading