Skip to content
Merged

fixes #1572

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
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ This example is based on the <a href="../java-helloworld-with-local-dev-and-oci-

To do the OCI SDK authentication and authorization to use the GenAI services the function uses two options:
<ul>
<li><b>IAM regular user</b> for the local dev and test on mac</li>
<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>
<li><b>IAM regular user</b> for the local dev and test on mac (lines 79-84 in HelloAIFunction.java)</li>
<li><b>ResourcePrincipal</b> for the OCI Function</li>
</ul>

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

## Build and test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,21 @@ public class HelloAIFunction {
private static final String COMPARTMENT_ID = "ocid1.compartment.oc1..";
private static final String GENAI_OCID = "ocid1.generativeaimodel.oc1.eu-frankfurt-1.amaaaaaa....wtig4q";

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

public String handleRequest(String input) {
GenerativeAiInferenceClient generativeAiInferenceClient;
GenerativeAiInferenceClient generativeAiInferenceClient = null;
String answer = "";
try {

LocalDate date = LocalDate.now().minusYears(100);
String questionToAI = (input == null || input.isEmpty()) ? "What happened at " + date + " ?": input;

if(System.getenv("AUTH_INSTANCE_PRINCIPAL") != null) {
System.out.println("AUTH_INSTANCE_PRINCIPAL");
LocalDate date = LocalDate.now().minusYears(100);
String questionToAI = (input == null || input.isEmpty()) ? "What happened at " + date + " ?": input;

try {
ResourcePrincipalAuthenticationDetailsProvider resourcePrincipalAuthenticationDetailsProvider =
ResourcePrincipalAuthenticationDetailsProvider.builder().build();
generativeAiInferenceClient =
Expand All @@ -101,53 +99,60 @@ public String handleRequest(String input) {
.endpoint(ENDPOINT)
.build(resourcePrincipalAuthenticationDetailsProvider);

} else {
System.out.println("AUTH_USER");
AuthenticationDetailsProvider authenticationDetailsProvider =
SimpleAuthenticationDetailsProvider.builder()
.tenantId(TENANCY_ID)
.userId(USER_ID)
.fingerprint(FINGERPRINT)
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
.passPhrase(PASSPHRASE)
} catch (Exception e) {
try {
AuthenticationDetailsProvider authenticationDetailsProvider =
SimpleAuthenticationDetailsProvider.builder()
.tenantId(TENANCY_ID)
.userId(USER_ID)
.fingerprint(FINGERPRINT)
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
.passPhrase(PASSPHRASE)
.build();
generativeAiInferenceClient =
GenerativeAiInferenceClient.builder()
.region(REGION)
.endpoint(ENDPOINT)
.build(authenticationDetailsProvider);
} catch (Exception ee) {
answer = answer + "\n" + ee.getMessage();
}
}

if(answer.length() == 0)
{
try {
CohereChatRequest chatRequest = CohereChatRequest.builder()
.message(questionToAI)
.maxTokens(600)
.temperature((double)0)
.frequencyPenalty((double)1)
.topP((double)0.75)
.topK((int)0)
.isStream(false)
.build();
generativeAiInferenceClient =
GenerativeAiInferenceClient.builder()
.region(REGION)
.endpoint(ENDPOINT)
.build(authenticationDetailsProvider);
}

CohereChatRequest chatRequest = CohereChatRequest.builder()
.message(questionToAI)
.maxTokens(600)
.temperature((double)0)
.frequencyPenalty((double)1)
.topP((double)0.75)
.topK((int)0)
.isStream(false)
.build();

ChatDetails chatDetails = ChatDetails.builder()
.servingMode(OnDemandServingMode.builder().modelId(GENAI_OCID).build())
.compartmentId(COMPARTMENT_ID)
.chatRequest(chatRequest)
.build();

ChatRequest request = ChatRequest.builder()
.chatDetails(chatDetails)
.build();

ChatResponse chatResponse = generativeAiInferenceClient.chat(request);
String answerAI = chatResponse.toString();
answerAI = answerAI.substring(answerAI.indexOf("text=") + 5);
if(answerAI.indexOf(", chatHistory=") > 0) {
answerAI = answerAI.substring(0, answerAI.indexOf(", chatHistory="));
}
answer = questionToAI + "\n\n" + answerAI;

} catch (Exception e) {
answer = answer + "\n" + e.getMessage();
ChatDetails chatDetails = ChatDetails.builder()
.servingMode(OnDemandServingMode.builder().modelId(GENAI_OCID).build())
.compartmentId(COMPARTMENT_ID)
.chatRequest(chatRequest)
.build();

ChatRequest request = ChatRequest.builder()
.chatDetails(chatDetails)
.build();

ChatResponse chatResponse = generativeAiInferenceClient.chat(request);
String answerAI = chatResponse.toString();
answerAI = answerAI.substring(answerAI.indexOf("text=") + 5);
if(answerAI.indexOf(", chatHistory=") > 0) {
answerAI = answerAI.substring(0, answerAI.indexOf(", chatHistory="));
}
answer = questionToAI + "\n\n" + answerAI;

} catch (Exception e) {
answer = answer + "\n" + e.getMessage();
}
}
return answer;
}
Expand Down