From 4113a0e94b5518f115868f9452fd976a643b8513 Mon Sep 17 00:00:00 2001 From: Mika Rinne Date: Mon, 14 Oct 2024 11:08:02 +0300 Subject: [PATCH] HelloAI function change to upgrade from text generation to chat --- .../pom.xml | 8 +-- .../java/com/example/HelloAIFunction.java | 62 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/pom.xml b/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/pom.xml index 378b21c4d..8fa8354c5 100644 --- a/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/pom.xml +++ b/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/pom.xml @@ -44,22 +44,22 @@ com.oracle.oci.sdk oci-java-sdk-core - 3.37.2 + 3.50.2 com.oracle.oci.sdk oci-java-sdk-common - 3.37.2 + 3.50.2 com.oracle.oci.sdk oci-java-sdk-generativeai - 3.37.2 + 3.50.2 com.oracle.oci.sdk oci-java-sdk-generativeaiinference - 3.37.2 + 3.50.2 com.oracle.oci.sdk diff --git a/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/src/main/java/com/example/HelloAIFunction.java b/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/src/main/java/com/example/HelloAIFunction.java index 6af767282..eaf06da17 100644 --- a/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/src/main/java/com/example/HelloAIFunction.java +++ b/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/src/main/java/com/example/HelloAIFunction.java @@ -1,6 +1,5 @@ package com.example; - import com.oracle.bmc.ClientConfiguration; import com.oracle.bmc.ConfigFileReader; import com.oracle.bmc.Region; @@ -9,15 +8,21 @@ import com.oracle.bmc.auth.StringPrivateKeySupplier; import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider; import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient; -import com.oracle.bmc.generativeaiinference.model.CohereLlmInferenceRequest; +import com.oracle.bmc.generativeaiinference.model.ChatDetails; +import com.oracle.bmc.generativeaiinference.model.CohereChatRequest; +import com.oracle.bmc.generativeaiinference.model.CohereMessage; import com.oracle.bmc.generativeaiinference.model.DedicatedServingMode; -import com.oracle.bmc.generativeaiinference.model.GenerateTextDetails; -import com.oracle.bmc.generativeaiinference.model.LlamaLlmInferenceRequest; +import com.oracle.bmc.generativeaiinference.requests.ChatRequest; +import com.oracle.bmc.generativeaiinference.responses.ChatResponse; +import com.oracle.bmc.generativeaiinference.model.BaseChatRequest.ApiFormat; +import com.oracle.bmc.generativeaiinference.model.ChatChoice; +import com.oracle.bmc.generativeaiinference.model.ChatContent; +import com.oracle.bmc.generativeaiinference.model.ChatContent.Type; +import com.oracle.bmc.generativeaiinference.model.ChatResult; +import com.oracle.bmc.generativeaiinference.model.Message; import com.oracle.bmc.generativeaiinference.model.OnDemandServingMode; -import com.oracle.bmc.generativeaiinference.model.SummarizeTextDetails; -import com.oracle.bmc.generativeaiinference.requests.GenerateTextRequest; -import com.oracle.bmc.generativeaiinference.requests.SummarizeTextRequest; -import com.oracle.bmc.generativeaiinference.responses.GenerateTextResponse; +import com.oracle.bmc.generativeaiinference.model.ServingMode; +import com.oracle.bmc.generativeaiinference.model.TextContent; import com.oracle.bmc.retrier.RetryConfiguration; import java.io.*; @@ -47,7 +52,6 @@ public String handleRequest(String input) { String currentDate = dateFormat. format(date); String questionToAI = (input == null || input.isEmpty()) ? "What happened today " + currentDate + " 100 years ago ?": input; - // OCI FUNCTION CONFIG VAR "AUTH_INSTANCE_PRINCIPAL" FOR INSTANCE_PRINCIPAL AUTH (ANY VALUE) if(System.getenv("AUTH_INSTANCE_PRINCIPAL") != null) { System.out.println("AUTH_INSTANCE_PRINCIPAL"); ResourcePrincipalAuthenticationDetailsProvider resourcePrincipalAuthenticationDetailsProvider = @@ -75,33 +79,31 @@ public String handleRequest(String input) { .build(authenticationDetailsProvider); } - CohereLlmInferenceRequest llmInferenceRequest = - CohereLlmInferenceRequest.builder() - .prompt(questionToAI) - .maxTokens(600) - .temperature((double)1) - .frequencyPenalty((double)0) - .topP((double)0.99) - .isStream(false) - .isEcho(false) - .build(); + CohereChatRequest chatRequest = CohereChatRequest.builder() + .message(questionToAI) + .maxTokens(600) + .temperature((double)0) + .frequencyPenalty((double)1) + .topP((double)0.75) + .topK((int)0) + .isStream(false) + .build(); - GenerateTextDetails generateTextDetails = GenerateTextDetails.builder() - .servingMode(OnDemandServingMode.builder() - .modelId("ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyafhwal37hxwylnpbcncidimbwteff4xha77n5xz4m7p6a").build()) + ChatDetails chatDetails = ChatDetails.builder() + .servingMode(OnDemandServingMode.builder().modelId("ocid1.generativeaimodel.oc1.eu-frankfurt-1.amaaaaaask7dceyazi3cpmptwa52f7dgwyskloughcxtjgrqre3pngwtig4q").build()) .compartmentId(COMPARTMENT_ID) - .inferenceRequest(llmInferenceRequest) + .chatRequest(chatRequest) .build(); - GenerateTextRequest generateTextRequest = GenerateTextRequest.builder() - .generateTextDetails(generateTextDetails) + ChatRequest request = ChatRequest.builder() + .chatDetails(chatDetails) .build(); - GenerateTextResponse generateTextResponse = generativeAiInferenceClient.generateText(generateTextRequest); - String answerAI = generateTextResponse.toString(); - answerAI = answerAI.substring(answerAI.indexOf("text= ") + 6); - if(answerAI.indexOf(", likelihood") > 0) { - answerAI = answerAI.substring(0, answerAI.indexOf(", likelihood")); + 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;