Skip to content

Commit 267c200

Browse files
authored
Merge pull request #1100 from oracle-devrel/java-helloworld-AI-with-local-dev-and-oci-functions
Java helloworld ai with local dev and oci functions
2 parents 3b75450 + 402d73b commit 267c200

File tree

7 files changed

+326
-0
lines changed

7 files changed

+326
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM fnproject/fn-java-fdk-build:jdk17-1.0-latest as build-stage
2+
WORKDIR /function
3+
ENV MAVEN_OPTS -Dhttp.proxyHost= -Dhttp.proxyPort= -Dhttps.proxyHost= -Dhttps.proxyPort= -Dhttp.nonProxyHosts= -Dmaven.repo.local=/usr/share/maven/ref/repository
4+
ADD pom.xml /function/pom.xml
5+
RUN ["mvn", "package", "dependency:copy-dependencies", "-DincludeScope=runtime", "-DskipTests=true", "-Dmdep.prependGroupId=true", "-DoutputDirectory=target", "--fail-never"]
6+
ADD src /function/src
7+
RUN ["mvn", "package"]
8+
FROM fnproject/fn-java-fdk:jre17-1.0.187
9+
WORKDIR /function
10+
COPY --from=build-stage /function/target/*.jar /function/app/
11+
CMD ["com.example.HelloAIFunction::handleRequest"]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Copyright (c) 2024 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
one is included with the Software (each a "Larger Work" to which the Software
16+
is contributed by such licensors),
17+
18+
without restriction, including without limitation the rights to copy, create
19+
derivative works of, display, perform, and distribute the Software and make,
20+
use, sell, offer for sale, import, export, have made, and have sold the
21+
Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
either these or other terms.
23+
24+
This license is subject to the following condition:
25+
The above copyright notice and either this complete permission notice or at
26+
a minimum a reference to the UPL must be included in all copies or
27+
substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!--
2+
Copyright (c) 2024 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
-->
38+
39+
## Functions overview
40+
[Document functions-overview](files/Fn.pdf)
41+
42+
### Author
43+
<a href="https://github.com/mikarinneoracle">mikarinneoracle</a>
44+
45+
# A Java AI featured function with local dev and then building and deploying to OCI Functions
46+
47+
This is an example how I'm locally developing and testing OCI Functions on my mac with Apple silicon that uses mainly ARM architecture and then building and deploying the same to Functions in OCI.
48+
49+
<p>
50+
51+
This example is based on the <a href="../java-helloworld-with-local-dev-and-oci-functions">java-helloworld-with-local-dev-and-oci-functions</a> and hence building and deploying is exactly the same but this time uses <code>OCI SDK</code> and <code>OCI Generative AI services</code> to produce the function output instead of just a simple "Hello World".
52+
53+
<p>
54+
55+
To do the OCI SDK authentication and authorization to use the GenAI services the function uses two options:
56+
<ul>
57+
<li>IAM regular <b>user auth</b> for the local dev and test on mac</li>
58+
<li><b>InstancePrincipal</b> auth for the OCI Function by passing config key <code>AUTH_INSTANCE_PRINCIPAL</code> with any value (then not being null)</li>
59+
</ul>
60+
61+
<p>
62+
IAM user option will work on both cases above, as local and as OCI Function.
63+
64+
<p>
65+
66+
Testing with curl (or pasting the APi Gateway url to a browser):
67+
68+
<p>
69+
70+
<pre>
71+
curl https://n3yu.....ghhi.apigateway.eu-frankfurt-1.oci.customer-oci.com/
72+
What happened today 05/31/2024 100 years ago ?
73+
74+
May 31, 2024 is May 31, 1924. Here are some events that occurred on this date:
75+
...
76+
</pre>
77+
78+
## License
79+
80+
Copyright (c) 2024 Oracle and/or its affiliates.
81+
82+
Licensed under the Universal Permissive License (UPL), Version 1.0.
83+
84+
See [LICENSE](LICENSE) for more details.
85+
86+
ORACLE AND ITS AFFILIATES DO NOT PROVIDE ANY WARRANTY WHATSOEVER, EXPRESS OR IMPLIED, FOR ANY SOFTWARE, MATERIAL OR CONTENT OF ANY KIND CONTAINED OR PRODUCED WITHIN THIS REPOSITORY, AND IN PARTICULAR SPECIFICALLY DISCLAIM ANY AND ALL IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. FURTHERMORE, ORACLE AND ITS AFFILIATES DO NOT REPRESENT THAT ANY CUSTOMARY SECURITY REVIEW HAS BEEN PERFORMED WITH RESPECT TO ANY SOFTWARE, MATERIAL OR CONTENT CONTAINED OR PRODUCED WITHIN THIS REPOSITORY. IN ADDITION, AND WITHOUT LIMITING THE FOREGOING, THIRD PARTIES MAY HAVE POSTED SOFTWARE, MATERIAL OR CONTENT TO THIS REPOSITORY WITHOUT ANY REVIEW. USE AT YOUR OWN RISK.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
schema_version: 20180708
2+
name: helloaifunc
3+
version: 0.0.1
4+
runtime: java
5+
build_image: fnproject/fn-java-fdk-build:jdk17-1.0.187
6+
run_image: fnproject/fn-java-fdk:jre17-1.0.187
7+
cmd: com.example.HelloAIFunction::handleRequest
8+
timeout: 120
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>Helloaifunc</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.fnproject.fn</groupId>
20+
<artifactId>api</artifactId>
21+
<version>1.0.187</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.fnproject.fn</groupId>
25+
<artifactId>runtime</artifactId>
26+
<version>1.0.187</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fnproject.fn</groupId>
30+
<artifactId>testing-core</artifactId>
31+
<version>1.0.187</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.fasterxml.jackson.core</groupId>
41+
<artifactId>jackson-annotations</artifactId>
42+
<version>2.17.1</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.oracle.oci.sdk</groupId>
46+
<artifactId>oci-java-sdk-core</artifactId>
47+
<version>3.37.2</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.oracle.oci.sdk</groupId>
51+
<artifactId>oci-java-sdk-common</artifactId>
52+
<version>3.37.2</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.oracle.oci.sdk</groupId>
56+
<artifactId>oci-java-sdk-generativeai</artifactId>
57+
<version>3.37.2</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.oracle.oci.sdk</groupId>
61+
<artifactId>oci-java-sdk-generativeaiinference</artifactId>
62+
<version>3.37.2</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.oracle.oci.sdk</groupId>
66+
<artifactId>oci-java-sdk-common-httpclient-jersey3</artifactId>
67+
<scope>runtime</scope>
68+
<version>3.37.2</version>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.example;
2+
3+
4+
import com.oracle.bmc.ClientConfiguration;
5+
import com.oracle.bmc.ConfigFileReader;
6+
import com.oracle.bmc.Region;
7+
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
8+
import com.oracle.bmc.auth.ResourcePrincipalAuthenticationDetailsProvider;
9+
import com.oracle.bmc.auth.StringPrivateKeySupplier;
10+
import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider;
11+
import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient;
12+
import com.oracle.bmc.generativeaiinference.model.CohereLlmInferenceRequest;
13+
import com.oracle.bmc.generativeaiinference.model.DedicatedServingMode;
14+
import com.oracle.bmc.generativeaiinference.model.GenerateTextDetails;
15+
import com.oracle.bmc.generativeaiinference.model.LlamaLlmInferenceRequest;
16+
import com.oracle.bmc.generativeaiinference.model.OnDemandServingMode;
17+
import com.oracle.bmc.generativeaiinference.model.SummarizeTextDetails;
18+
import com.oracle.bmc.generativeaiinference.requests.GenerateTextRequest;
19+
import com.oracle.bmc.generativeaiinference.requests.SummarizeTextRequest;
20+
import com.oracle.bmc.generativeaiinference.responses.GenerateTextResponse;
21+
import com.oracle.bmc.retrier.RetryConfiguration;
22+
23+
import java.io.*;
24+
import java.util.*;
25+
import java.text.*;
26+
27+
public class HelloAIFunction {
28+
29+
// FILL IN PROPER VALUES FOR OCI GENAI SERVICE
30+
private static final String ENDPOINT = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com";
31+
private static final Region REGION = Region.US_CHICAGO_1;
32+
33+
// FILL IN PROPER VALUES FOR IAM USER WHEN NOT USING INSTANCE_PRINCIPAL IN OCI FUNCTION
34+
private static final String COMPARTMENT_ID = "ocid1.compartment.oc1..";
35+
private static final String TENANCY_ID = "ocid1.tenancy.oc1..";
36+
private static final String USER_ID = "ocid1.user.oc1..";
37+
private static final String FINGERPRINT = "ef:4d:..";
38+
private static final String PRIVATEKEY = "-----BEGIN PRIVATE KEY-----\n .. \n-----END PRIVATE KEY-----";
39+
private static final String PASSPHRASE = "";
40+
41+
public String handleRequest(String input) {
42+
GenerativeAiInferenceClient generativeAiInferenceClient;
43+
String answer = "";
44+
try {
45+
Date date = new Date();
46+
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
47+
String currentDate = dateFormat. format(date);
48+
String questionToAI = (input == null || input.isEmpty()) ? "What happened today " + currentDate + " 100 years ago ?": input;
49+
50+
// OCI FUNCTION CONFIG VAR "AUTH_INSTANCE_PRINCIPAL" FOR INSTANCE_PRINCIPAL AUTH (ANY VALUE)
51+
if(System.getenv("AUTH_INSTANCE_PRINCIPAL") != null) {
52+
System.out.println("AUTH_INSTANCE_PRINCIPAL");
53+
ResourcePrincipalAuthenticationDetailsProvider resourcePrincipalAuthenticationDetailsProvider =
54+
ResourcePrincipalAuthenticationDetailsProvider.builder().build();
55+
generativeAiInferenceClient =
56+
GenerativeAiInferenceClient.builder()
57+
.region(REGION)
58+
.endpoint(ENDPOINT)
59+
.build(resourcePrincipalAuthenticationDetailsProvider);
60+
61+
} else {
62+
System.out.println("AUTH_USER");
63+
AuthenticationDetailsProvider authenticationDetailsProvider =
64+
SimpleAuthenticationDetailsProvider.builder()
65+
.tenantId(TENANCY_ID)
66+
.userId(USER_ID)
67+
.fingerprint(FINGERPRINT)
68+
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
69+
.passPhrase(PASSPHRASE)
70+
.build();
71+
generativeAiInferenceClient =
72+
GenerativeAiInferenceClient.builder()
73+
.region(REGION)
74+
.endpoint(ENDPOINT)
75+
.build(authenticationDetailsProvider);
76+
}
77+
78+
CohereLlmInferenceRequest llmInferenceRequest =
79+
CohereLlmInferenceRequest.builder()
80+
.prompt(questionToAI)
81+
.maxTokens(600)
82+
.temperature((double)1)
83+
.frequencyPenalty((double)0)
84+
.topP((double)0.99)
85+
.isStream(false)
86+
.isEcho(false)
87+
.build();
88+
89+
GenerateTextDetails generateTextDetails = GenerateTextDetails.builder()
90+
.servingMode(OnDemandServingMode.builder()
91+
.modelId("ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyafhwal37hxwylnpbcncidimbwteff4xha77n5xz4m7p6a").build())
92+
.compartmentId(COMPARTMENT_ID)
93+
.inferenceRequest(llmInferenceRequest)
94+
.build();
95+
96+
GenerateTextRequest generateTextRequest = GenerateTextRequest.builder()
97+
.generateTextDetails(generateTextDetails)
98+
.build();
99+
100+
GenerateTextResponse generateTextResponse = generativeAiInferenceClient.generateText(generateTextRequest);
101+
String answerAI = generateTextResponse.toString();
102+
answerAI = answerAI.substring(answerAI.indexOf("text= ") + 6);
103+
if(answerAI.indexOf(", likelihood") > 0) {
104+
answerAI = answerAI.substring(0, answerAI.indexOf(", likelihood"));
105+
}
106+
answer = questionToAI + "\n\n" + answerAI;
107+
108+
} catch (Exception e) {
109+
answer = answer + "\n" + e.getMessage();
110+
}
111+
return answer;
112+
}
113+
114+
}

0 commit comments

Comments
 (0)