Skip to content

Commit a57a0e2

Browse files
authored
Merge pull request #1572 from oracle-devrel/AI-func-fixes
fixes
2 parents e2a4d0c + afc585a commit a57a0e2

File tree

2 files changed

+61
-56
lines changed
  • app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions

2 files changed

+61
-56
lines changed

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ This example is based on the <a href="../java-helloworld-with-local-dev-and-oci-
5555

5656
To do the OCI SDK authentication and authorization to use the GenAI services the function uses two options:
5757
<ul>
58-
<li><b>IAM regular user</b> for the local dev and test on mac</li>
59-
<li><b>InstancePrincipal</b> for the OCI Function by passing config key <code>AUTH_INSTANCE_PRINCIPAL</code> with any value (then not being null)</li>
58+
<li><b>IAM regular user</b> for the local dev and test on mac (lines 79-84 in HelloAIFunction.java)</li>
59+
<li><b>ResourcePrincipal</b> for the OCI Function</li>
6060
</ul>
6161

6262
<p>
63-
IAM user option will work on both cases above, as local and as OCI Function.
63+
IAM user option will work on both cases above, as local and as OCI Function. ResourcePrincipal is the default for OCI Function.
6464

6565
## Build and test
6666

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/src/main/java/com/example/HelloAIFunction.java

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,21 @@ public class HelloAIFunction {
7676
private static final String COMPARTMENT_ID = "ocid1.compartment.oc1..";
7777
private static final String GENAI_OCID = "ocid1.generativeaimodel.oc1.eu-frankfurt-1.amaaaaaa....wtig4q";
7878

79-
// FILL IN PROPER VALUES FOR IAM USER WHEN NOT USING INSTANCE_PRINCIPAL IN OCI FUNCTION
79+
// FILL IN PROPER VALUES FOR IAM USER WHEN RUNNING LOCALLY WITH Fn. RUNNING AS OCI FUNCTION DO NOT NEED TO SET THESE.
8080
private static final String TENANCY_ID = "ocid1.tenancy.oc1..";
8181
private static final String USER_ID = "ocid1.user.oc1..";
8282
private static final String FINGERPRINT = "ef:4d:..";
8383
private static final String PRIVATEKEY = "-----BEGIN PRIVATE KEY-----\n .. \n-----END PRIVATE KEY-----";
8484
private static final String PASSPHRASE = "";
8585

8686
public String handleRequest(String input) {
87-
GenerativeAiInferenceClient generativeAiInferenceClient;
87+
GenerativeAiInferenceClient generativeAiInferenceClient = null;
8888
String answer = "";
89-
try {
90-
91-
LocalDate date = LocalDate.now().minusYears(100);
92-
String questionToAI = (input == null || input.isEmpty()) ? "What happened at " + date + " ?": input;
9389

94-
if(System.getenv("AUTH_INSTANCE_PRINCIPAL") != null) {
95-
System.out.println("AUTH_INSTANCE_PRINCIPAL");
90+
LocalDate date = LocalDate.now().minusYears(100);
91+
String questionToAI = (input == null || input.isEmpty()) ? "What happened at " + date + " ?": input;
92+
93+
try {
9694
ResourcePrincipalAuthenticationDetailsProvider resourcePrincipalAuthenticationDetailsProvider =
9795
ResourcePrincipalAuthenticationDetailsProvider.builder().build();
9896
generativeAiInferenceClient =
@@ -101,53 +99,60 @@ public String handleRequest(String input) {
10199
.endpoint(ENDPOINT)
102100
.build(resourcePrincipalAuthenticationDetailsProvider);
103101

104-
} else {
105-
System.out.println("AUTH_USER");
106-
AuthenticationDetailsProvider authenticationDetailsProvider =
107-
SimpleAuthenticationDetailsProvider.builder()
108-
.tenantId(TENANCY_ID)
109-
.userId(USER_ID)
110-
.fingerprint(FINGERPRINT)
111-
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
112-
.passPhrase(PASSPHRASE)
102+
} catch (Exception e) {
103+
try {
104+
AuthenticationDetailsProvider authenticationDetailsProvider =
105+
SimpleAuthenticationDetailsProvider.builder()
106+
.tenantId(TENANCY_ID)
107+
.userId(USER_ID)
108+
.fingerprint(FINGERPRINT)
109+
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
110+
.passPhrase(PASSPHRASE)
111+
.build();
112+
generativeAiInferenceClient =
113+
GenerativeAiInferenceClient.builder()
114+
.region(REGION)
115+
.endpoint(ENDPOINT)
116+
.build(authenticationDetailsProvider);
117+
} catch (Exception ee) {
118+
answer = answer + "\n" + ee.getMessage();
119+
}
120+
}
121+
122+
if(answer.length() == 0)
123+
{
124+
try {
125+
CohereChatRequest chatRequest = CohereChatRequest.builder()
126+
.message(questionToAI)
127+
.maxTokens(600)
128+
.temperature((double)0)
129+
.frequencyPenalty((double)1)
130+
.topP((double)0.75)
131+
.topK((int)0)
132+
.isStream(false)
113133
.build();
114-
generativeAiInferenceClient =
115-
GenerativeAiInferenceClient.builder()
116-
.region(REGION)
117-
.endpoint(ENDPOINT)
118-
.build(authenticationDetailsProvider);
119-
}
120-
121-
CohereChatRequest chatRequest = CohereChatRequest.builder()
122-
.message(questionToAI)
123-
.maxTokens(600)
124-
.temperature((double)0)
125-
.frequencyPenalty((double)1)
126-
.topP((double)0.75)
127-
.topK((int)0)
128-
.isStream(false)
129-
.build();
130-
131-
ChatDetails chatDetails = ChatDetails.builder()
132-
.servingMode(OnDemandServingMode.builder().modelId(GENAI_OCID).build())
133-
.compartmentId(COMPARTMENT_ID)
134-
.chatRequest(chatRequest)
135-
.build();
136-
137-
ChatRequest request = ChatRequest.builder()
138-
.chatDetails(chatDetails)
139-
.build();
140-
141-
ChatResponse chatResponse = generativeAiInferenceClient.chat(request);
142-
String answerAI = chatResponse.toString();
143-
answerAI = answerAI.substring(answerAI.indexOf("text=") + 5);
144-
if(answerAI.indexOf(", chatHistory=") > 0) {
145-
answerAI = answerAI.substring(0, answerAI.indexOf(", chatHistory="));
146-
}
147-
answer = questionToAI + "\n\n" + answerAI;
148134

149-
} catch (Exception e) {
150-
answer = answer + "\n" + e.getMessage();
135+
ChatDetails chatDetails = ChatDetails.builder()
136+
.servingMode(OnDemandServingMode.builder().modelId(GENAI_OCID).build())
137+
.compartmentId(COMPARTMENT_ID)
138+
.chatRequest(chatRequest)
139+
.build();
140+
141+
ChatRequest request = ChatRequest.builder()
142+
.chatDetails(chatDetails)
143+
.build();
144+
145+
ChatResponse chatResponse = generativeAiInferenceClient.chat(request);
146+
String answerAI = chatResponse.toString();
147+
answerAI = answerAI.substring(answerAI.indexOf("text=") + 5);
148+
if(answerAI.indexOf(", chatHistory=") > 0) {
149+
answerAI = answerAI.substring(0, answerAI.indexOf(", chatHistory="));
150+
}
151+
answer = questionToAI + "\n\n" + answerAI;
152+
153+
} catch (Exception e) {
154+
answer = answer + "\n" + e.getMessage();
155+
}
151156
}
152157
return answer;
153158
}

0 commit comments

Comments
 (0)