Skip to content

Commit

Permalink
Merge pull request #264 from graalvm/bh/generative-ai
Browse files Browse the repository at this point in the history
OCI Native Generative AI Example
  • Loading branch information
bhoran committed May 30, 2024
2 parents 245fad6 + 113017f commit 6228f81
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 4 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/native-oci-generative-ai.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cd graalvm-demos
</tr>
<tr>
<td align="left" width="30%"><a href="/streams/">streams</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/streams.yml"><img alt="streams" src="https://github.com/graalvm/graalvm-demos/actions/workflows/streams.yml/badge.svg" /></a></td>
<td align="left" width="70%">Demontrates how GraalVM efficiently optimizes the Java Streams API application and how to apply PGO<br><strong>Technologies: </strong>Native Image, Native Build Tools Maven Plugin <br><strong>Reference: </strong><a href="https://www.graalvm.org/latest/reference-manual/native-image/guides/optimize-native-executable-with-pgo/">Optimize a Native Executable with Profile-Guided Optimizations</a></td>
<td align="left" width="70%">Demonstrates how GraalVM efficiently optimizes the Java Streams API application and how to apply PGO<br><strong>Technologies: </strong>Native Image, Native Build Tools Maven Plugin <br><strong>Reference: </strong><a href="https://www.graalvm.org/latest/reference-manual/native-image/guides/optimize-native-executable-with-pgo/">Optimize a Native Executable with Profile-Guided Optimizations</a></td>
</tr>
<tr>
<td align="left" width="30%"><a href="/fortune-demo/">fortune-demo</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/fortune-demo.yml"><img alt="fortune-demo" src="https://github.com/graalvm/graalvm-demos/actions/workflows/fortune-demo.yml/badge.svg" /></a></td>
Expand Down Expand Up @@ -127,7 +127,11 @@ cd graalvm-demos
<tr>
<td align="left" width="30%"><a href="/native-oci-container-instances/">native-oci-container-instances</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/native-oci-container-instances.yml"><img alt="native-oci-container-instances" src="https://github.com/graalvm/graalvm-demos/actions/workflows/native-oci-container-instances.yml/badge.svg" /></a></td>
<td align="left" width="70%">This demo covers the steps required to create a container image of a native executable application and deploy the image on OCI Container Instances.<br><strong>Technologies: </strong> Native Image, Apache Maven, Docker, OCI Container Instances<br></td>
</tr>
</tr>
<tr>
<td align="left" width="30%"><a href="/native-oci-generative-ai/">native-oci-generative-ai</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/native-oci-generative-ai.yml"><img alt="native-oci-generative-ai" src="https://github.com/graalvm/graalvm-demos/actions/workflows/native-oci-generative-ai/badge.svg" /></a></td>
<td align="left" width="70%">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.<br><strong>Technologies: </strong> Native Image, Apache Maven, Generative AI<br></td>
</tr>
</tbody>
</table>

Expand All @@ -143,7 +147,7 @@ cd graalvm-demos
<tbody>
<tr>
<td align="left" width="30%"><a href="/espresso-jshell/">espresso-jshell</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/espresso-jshell.yml"><img alt="espresso-jshell" src="https://github.com/graalvm/graalvm-demos/actions/workflows/espresso-jshell.yml/badge.svg" /></a></td>
<td align="left" width="70%">Demonstrates how to build a native executable of JShell, that executes the dynamically generated bytecodes on Espresso<br><strong>Technologies: </strong>Java on Truffle, Native Image, JShell<br><strong>Reference: </strong><a href="https://www.graalvm.org/dev/reference-manual/java-on-truffle/demos/#mixing-aot-and-jit-for-java">Mixing AOT and JIT for Java</a>, <a href="https://medium.com/graalvm/java-on-truffle-going-fully-metacircular-215531e3f840">Java on Truffle — Going Fully Metacircular</a></td>
<td align="left" width="70%">Demonstrates how to build a native executable of JShell, that executes the dynamically generated bytecode on Espresso<br><strong>Technologies: </strong>Java on Truffle, Native Image, JShell<br><strong>Reference: </strong><a href="https://www.graalvm.org/dev/reference-manual/java-on-truffle/demos/#mixing-aot-and-jit-for-java">Mixing AOT and JIT for Java</a>, <a href="https://medium.com/graalvm/java-on-truffle-going-fully-metacircular-215531e3f840">Java on Truffle — Going Fully Metacircular</a></td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -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

Expand Down
88 changes: 88 additions & 0 deletions native-oci-generative-ai/README.md
Original file line number Diff line number Diff line change
@@ -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&mdash;you can edit this in the source code later.
* (Optional) Set values for the parameters&mdash;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
```
127 changes: 127 additions & 0 deletions native-oci-generative-ai/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oracle.labs</groupId>
<artifactId>generative-ai</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Oracle Cloud Infrastructure - Generative AI Demo</name>
<description>This project contains an example of using Oracle Cloud Infrastructure to generate text.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>com.oracle.labs.GenerateTextExample</mainClass>
<native-maven-plugin.version>0.10.1</native-maven-plugin.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-maven-plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-generativeaiinference</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-bom</artifactId>
<version>3.41.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
<version>3.41.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Loading

0 comments on commit 6228f81

Please sign in to comment.