diff --git a/.github/workflows/native-oci-generative-ai.yml b/.github/workflows/native-oci-generative-ai.yml new file mode 100644 index 00000000..90fc6a4a --- /dev/null +++ b/.github/workflows/native-oci-generative-ai.yml @@ -0,0 +1,31 @@ +name: native-oci-generative-ai +on: + push: + paths: + - 'native-oci-generative-ai/**' + - '.github/workflows/native-oci-generative-ai.yml' + pull_request: + paths: + - 'native-oci-generative-ai/**' + - '.github/workflows/native-oci-generative-ai.yml' + schedule: + - cron: "0 0 1 * *" # run every month + workflow_dispatch: +permissions: + contents: read +jobs: + run: + name: Run 'native-oci-generative-ai' + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - uses: graalvm/setup-graalvm@v1 + with: + java-version: '21.0.3' + distribution: 'graalvm' + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Run 'native-oci-generative-ai' + run: | + cd native-oci-generative-ai + mvn --batch-mode clean package -Pnative diff --git a/README.md b/README.md index da50630a..fe904368 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ cd graalvm-demos streams
streams - Demontrates how GraalVM efficiently optimizes the Java Streams API application and how to apply PGO
Technologies: Native Image, Native Build Tools Maven Plugin
Reference: Optimize a Native Executable with Profile-Guided Optimizations + Demonstrates how GraalVM efficiently optimizes the Java Streams API application and how to apply PGO
Technologies: Native Image, Native Build Tools Maven Plugin
Reference: Optimize a Native Executable with Profile-Guided Optimizations fortune-demo
fortune-demo @@ -127,7 +127,11 @@ cd graalvm-demos native-oci-container-instances
native-oci-container-instances This demo covers the steps required to create a container image of a native executable application and deploy the image on OCI Container Instances.
Technologies: Native Image, Apache Maven, Docker, OCI Container Instances
- + + + native-oci-generative-ai
native-oci-generative-ai + This demo covers the steps required to use the Generative AI service provided by Oracle Cloud Infrastructure. It uses a state-of-the-art, customizable large language model to generate text.
Technologies: Native Image, Apache Maven, Generative AI
+ @@ -143,7 +147,7 @@ cd graalvm-demos espresso-jshell
espresso-jshell - Demonstrates how to build a native executable of JShell, that executes the dynamically generated bytecodes on Espresso
Technologies: Java on Truffle, Native Image, JShell
Reference: Mixing AOT and JIT for Java, Java on Truffle — Going Fully Metacircular + Demonstrates how to build a native executable of JShell, that executes the dynamically generated bytecode on Espresso
Technologies: Java on Truffle, Native Image, JShell
Reference: Mixing AOT and JIT for Java, Java on Truffle — Going Fully Metacircular @@ -291,7 +295,7 @@ cd graalvm-demos ## Compatibility The demos are normal applications and benchmarks written in Java, JavaScript, Python, etc., so they are compatible with any virtual machine capable of running Java, JavaScript and so on. -These demos are [tested against the latest GraalVM release using GitHub Actions](https://github.com/graalvm/graalvm-demos/actions/workflows/main.yml). If you come accross an issue, please submit it [here](https://github.com/graalvm/graalvm-demos/issues). +These demos are [tested against the latest GraalVM release using GitHub Actions](https://github.com/graalvm/graalvm-demos/actions/workflows/main.yml). If you come across an issue, please submit it [here](https://github.com/graalvm/graalvm-demos/issues). ## License diff --git a/native-oci-generative-ai/README.md b/native-oci-generative-ai/README.md new file mode 100644 index 00000000..00fd4959 --- /dev/null +++ b/native-oci-generative-ai/README.md @@ -0,0 +1,88 @@ +# OCI Native Generative AI Example + +This example illustrates how to use the Generative AI service provided by Oracle Cloud Infrastructure (OCI). +It uses a state-of-the-art, customizable large language model to generate text. + +The [source code](./src/main/java/com/oracle/labs/GenerateTextExample.java) is a self-contained demonstration of using a [`GenerativeAiInferenceClient`](https://docs.oracle.com/iaas/tools/java/latest/com/oracle/bmc/generativeaiinference/GenerativeAiInferenceClient.html) to generate text, based on a prompt. + +The [_pom.xml_](pom.xml) file includes necessary dependents (such as the [OCI Java SDK](https://docs.public.oneportal.content.oci.oraclecloud.com/iaas/Content/API/SDKDocs/javasdk.htm)) and configures the build, including the [Native Maven Plugin](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html) to generate a native executable with GraalVM Native Image. + +> For more information, see [Generative AI](https://docs.oracle.com/iaas/Content/generative-ai/home.htm). + +## Prerequisites +* [GraalVM](https://www.graalvm.org/downloads/). +* You must have access to access to Generative AI resources in your tenancy/compartment. +(For more information, see [Getting Access to Generative AI](https://docs.oracle.com/iaas/Content/generative-ai/iam-policies.htm).) + +## Preparing Your Code + +Download or clone the GraalVM demos repository: +```sh +git clone https://github.com/graalvm/graalvm-demos +``` + +Edit the contents of [_GenerateTextExample.java_](./src/main/java/com/oracle/labs/GenerateTextExample.java) as follows: + +1. Update the value of the static variable `CONFIG_PROFILE` with the name of your OCI profile name. + +2. Update the value of the static variable `COMPARTMENT_ID` with the OCID of a compartment that has access to Generative AI resources. +(For more information, see [Finding the OCID of a Compartment](https://docs.oracle.com/iaas/Content/GSG/Tasks/contactingsupport_topic-Locating_Oracle_Cloud_Infrastructure_IDs.htm#Finding_the_OCID_of_a_Compartment).) + +Alternatively, you can follow the steps in [Generate Text in Generative AI](https://docs.oracle.com/iaas/Content/generative-ai/use-playground-generate.htm): + +* (Optional) Enter your own prompt—you can edit this in the source code later. +* (Optional) Set values for the parameters—you can adjust these in the source code later. +* When you are happy with your prompt and the output, click **View code**, select **Java** as the programming language, then click **Copy code**, paste the code into the file named _GenerateTextExample.java_ and save it. + > Your code should resemble [_GenerateTextExample.java_](./src/main/java/com/oracle/labs/GenerateTextExample.java). + +## Running Your Code + +Run the following command: + +```shell +mvn install exec:java +``` + +This command runs the `main()` method in _GenerateTextExample.java_ and generates a response based on the prompt and parameters of the request. +The response should resemble the following: +``` +GenerateTextResult(super=BmcModel(__explicitlySet__=[modelId, modelVersion, inferenceResponse])modelId=ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyafhwal37hxwylnpbcncidimbwteff4xha77n5xz4m7p6a, modelVersion=15.6, inferenceResponse=CohereLlmInferenceResponse(super=LlmInferenceResponse(super=BmcModel(__explicitlySet__=[generatedTexts, timeCreated])), generatedTexts=[GeneratedText(super=BmcModel(__explicitlySet__=[id, text])id=0dbcc447-2563-41e3-b742-fd1d1b465073, text= We're looking for a talented Data Visualization Expert to join our team! The ideal candidate will have at least 5 years of experience in creating compelling and insightful data visualizations, as well as a keen eye for detail and the ability to think creatively. + +As a Data Visualization Expert, you will be responsible for designing and developing visually appealing and effective representations of data and information. You will work closely with our team of data analysts and scientists to understand the data and the story it needs to tell, and then create customized visualizations that help convey that story to a wide audience in a clear and concise manner. + +Your responsibilities will include: + +Collaborating with data analysts and scientists to understand data sets and determine the best ways to visualize the information they contain. +Creating original and innovative data visualizations that effectively communicate the key insights and findings to both technical and non-technical stakeholders. +Paying close attention to details such as design, color theory, and typography to create visually appealing and accurate representations of data. +Proactively researching and staying up-to-date on industry trends and best practices in data visualization. +Conducting experiments with different visualization techniques and tools to determine the most effective ways to convey complex data sets. + +Qualifications + +At least 5 years of experience in data visualization or a related field. +A portfolio of work showcasing your data visualization projects and the impact they had on decision-making. +Excellent communication skills, both verbal and written, with the ability to explain complex data concepts to both technical and non-technical stakeholders. +Strong analytical skills with the ability to interpret and understand large data sets. +Great attention to detail, with the ability to spot trends and patterns within data. +Proficiency in data visualization tools such as Tableau, Power BI, or D3.js, or the ability to learn and master new tools quickly. +An understanding of design principles and typography is a plus. + +We are looking for someone who is passionate about data and storytelling, and who has the ability to turn data into actionable insights through innovative visualization. If you have the relevant experience and skills, we want to hear from you! + +This is a great opportunity to join a dynamic and growing team and make a meaningful impact on our company's data-driven decisions. + +We look forward to receiving your application! , likelihood=null, finishReason=null, tokenLikelihoods=null)], timeCreated=Thu May 09 16:51:49 BST 2024, prompt=null)) +``` + +## Running the Example as Native Executable + +1. Create a native executable by running the following command: + ```shell + mvn clean package -Pnative + ``` + +2. Run the native executable as shown below. (The response should resemble the one above.): + ```shell + ./target/generative-ai + ``` \ No newline at end of file diff --git a/native-oci-generative-ai/pom.xml b/native-oci-generative-ai/pom.xml new file mode 100644 index 00000000..5a321b68 --- /dev/null +++ b/native-oci-generative-ai/pom.xml @@ -0,0 +1,127 @@ + + + 4.0.0 + com.oracle.labs + generative-ai + 1.0-SNAPSHOT + Oracle Cloud Infrastructure - Generative AI Demo + This project contains an example of using Oracle Cloud Infrastructure to generate text. + + + UTF-8 + com.oracle.labs.GenerateTextExample + 0.10.1 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-sources + package + + jar-no-fork + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + ${mainClass} + + + + + + + + native + + + + org.graalvm.buildtools + native-maven-plugin + ${native-maven-plugin.version} + true + + + build-native + + compile-no-fork + + package + + + test-native + + test + + test + + + + ${mainClass} + + + + + + + + + com.oracle.oci.sdk + oci-java-sdk-common + jar + + + com.oracle.oci.sdk + oci-java-sdk-generativeaiinference + jar + + + com.oracle.oci.sdk + oci-java-sdk-common-httpclient-jersey + jar + + + org.slf4j + slf4j-simple + + + + + + com.oracle.oci.sdk + oci-java-sdk-bom + 3.41.1 + pom + import + + + com.oracle.oci.sdk + oci-java-sdk-common-httpclient-jersey + 3.41.1 + pom + import + + + + \ No newline at end of file diff --git a/native-oci-generative-ai/src/main/java/com/oracle/labs/GenerateTextExample.java b/native-oci-generative-ai/src/main/java/com/oracle/labs/GenerateTextExample.java new file mode 100644 index 00000000..19e75e45 --- /dev/null +++ b/native-oci-generative-ai/src/main/java/com/oracle/labs/GenerateTextExample.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * The Universal Permissive License (UPL), Version 1.0 + * + * Subject to the condition set forth below, permission is hereby granted to any + * person obtaining a copy of this software, associated documentation and/or + * data (collectively the "Software"), free of charge and under any and all + * copyright rights in the Software, and any and all patent rights owned or + * freely licensable by each licensor hereunder covering either (i) the + * unmodified Software as contributed to or provided by such licensor, or (ii) + * the Larger Works (as defined below), to deal in both + * + * (a) the Software, and + * + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if + * one is included with the Software each a "Larger Work" to which the Software + * is contributed by such licensors), + * + * without restriction, including without limitation the rights to copy, create + * derivative works of, display, perform, and distribute the Software and make, + * use, sell, offer for sale, import, export, have made, and have sold the + * Software and the Larger Work(s), and to sublicense the foregoing rights on + * either these or other terms. + * + * This license is subject to the following condition: + * + * The above copyright notice and either this complete permission notice or at a + * minimum a reference to the UPL must be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.oracle.labs; + +import com.oracle.bmc.ClientConfiguration; +import com.oracle.bmc.ConfigFileReader; +import com.oracle.bmc.Region; +import com.oracle.bmc.auth.AuthenticationDetailsProvider; +import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider; +import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient; +import com.oracle.bmc.generativeaiinference.model.CohereLlmInferenceRequest; +import com.oracle.bmc.generativeaiinference.model.GenerateTextDetails; +import com.oracle.bmc.generativeaiinference.model.OnDemandServingMode; +import com.oracle.bmc.generativeaiinference.requests.GenerateTextRequest; +import com.oracle.bmc.generativeaiinference.responses.GenerateTextResponse; +import com.oracle.bmc.retrier.RetryConfiguration; +/** + * This class provides an example of how to use OCI Generative AI Service to + * generate text. + *

+ * The Generative AI Service queried by this example will be assigned: + *

+ *

+ */ + +public class GenerateTextExample { + private static final String ENDPOINT = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com"; + private static final Region REGION = Region.US_CHICAGO_1; + private static final String CONFIG_LOCATION = "~/.oci/config"; + // TODO: Please update config profile name and use the compartmentId that has + // policies grant permissions for using Generative AI Service + private static final String CONFIG_PROFILE = "DEFAULT"; + private static final String COMPARTMENT_ID = "ocid1.compartment.oc1..aaaaaaaaul6555vm7yt6qm7tmsq64yvivkidstxajilzcjv2laaki46rtdzq"; + + /** + * The entry point for the example. + * + * @param args Arguments to provide to the example. This example expects no + * arguments. + * @throws java.lang.Exception + */ + public static void main(String[] args) throws Exception { + if (args.length > 0) { + throw new IllegalArgumentException( + "This example expects no argument"); + } + // Configuring the AuthenticationDetailsProvider. It's assuming there is a + // default OCI config file + // "~/.oci/config", and a profile in that config with the name defined in + // CONFIG_PROFILE variable. + final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE); + final AuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile); + + // Set up Generative AI client with credentials and endpoint + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .readTimeoutMillis(240000) + .retryConfiguration(RetryConfiguration.NO_RETRY_CONFIGURATION) + .build(); + final GenerativeAiInferenceClient generativeAiInferenceClient = GenerativeAiInferenceClient.builder() + .configuration(clientConfiguration) + .build(provider); + generativeAiInferenceClient.setEndpoint(ENDPOINT); + generativeAiInferenceClient.setRegion(REGION); + + // Build generate text request, send, and get response + CohereLlmInferenceRequest llmInferenceRequest = CohereLlmInferenceRequest.builder() + .prompt( + "Generate a job description for a data visualization expert with the following three qualifications only:\n1) At least 5 years of data visualization expert\n2) A great eye for details\n3) Ability to create original visualizations") + .maxTokens(600) + .temperature((double) 0.5) + .frequencyPenalty((double) 1) + .topP((double) 0.75) + .isStream(false) + .isEcho(false) + .build(); + + GenerateTextDetails generateTextDetails = GenerateTextDetails.builder() + .servingMode(OnDemandServingMode.builder() + .modelId( + "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyafhwal37hxwylnpbcncidimbwteff4xha77n5xz4m7p6a") + .build()) + .compartmentId(COMPARTMENT_ID) + .inferenceRequest(llmInferenceRequest) + .build(); + GenerateTextRequest generateTextRequest = GenerateTextRequest.builder() + .generateTextDetails(generateTextDetails) + .build(); + GenerateTextResponse generateTextResponse = generativeAiInferenceClient.generateText(generateTextRequest); + System.out.println(generateTextResponse.getGenerateTextResult().getInferenceResponse()); + } +} \ No newline at end of file