Created
September 18, 2025 14:46
-
-
Save owainlewis/5ae8eeff9c3c340e9e01c5a9be17d6c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns oci-generative-ai | |
(:import [com.oracle.bmc.auth ConfigFileAuthenticationDetailsProvider] | |
[com.oracle.bmc.generativeaiinference GenerativeAiInferenceClient] | |
[com.oracle.bmc.generativeaiinference.model ChatDetails | |
CohereChatRequest | |
OnDemandServingMode | |
ChatRequest] | |
[com.oracle.bmc.generativeaiinference.requests ChatRequest])) | |
(defn create-auth-provider | |
"Creates an OCI authentication provider from config file" | |
([] | |
(create-auth-provider "~/.oci/config" "DEFAULT")) | |
([config-file profile] | |
(ConfigFileAuthenticationDetailsProvider. config-file profile))) | |
(defn create-client | |
"Creates a GenerativeAiInferenceClient with the given auth provider" | |
[auth-provider] | |
(-> (GenerativeAiInferenceClient/builder) | |
(.build auth-provider))) | |
(defn create-chat-request | |
"Creates a CohereChatRequest with the given message" | |
[message] | |
(-> (CohereChatRequest/builder) | |
(.message message) | |
(.build))) | |
(defn create-serving-mode | |
"Creates an OnDemandServingMode with the given model ID" | |
[model-id] | |
(-> (OnDemandServingMode/builder) | |
(.modelId model-id) | |
(.build))) | |
(defn create-chat-details | |
"Creates ChatDetails with compartment ID, serving mode, and chat request" | |
[compartment-id serving-mode chat-request] | |
(-> (ChatDetails/builder) | |
(.compartmentId compartment-id) | |
(.servingMode serving-mode) | |
(.chatRequest chat-request) | |
(.build))) | |
(defn extract-response-text | |
"Extracts text from the chat response" | |
[chat-result] | |
(try | |
(if (and (.getChatResponse chat-result) | |
(.getText (.getChatResponse chat-result))) | |
(.getText (.getChatResponse chat-result)) | |
(str chat-result)) | |
(catch Exception _ | |
(str chat-result)))) | |
(defn make-chat-call | |
"Makes the chat API call and returns the response text" | |
[client chat-details] | |
(try | |
(let [request (-> (ChatRequest/builder) | |
(.chatDetails chat-details) | |
(.build)) | |
response (.chat client request)] | |
(if (= 200 (.get__httpStatusCode__ response)) | |
{:success true | |
:text (extract-response-text (.getChatResult response))} | |
{:success false | |
:error (str "Request failed with status " (.get__httpStatusCode__ response))})) | |
(catch Exception e | |
{:success false | |
:error (str "Error: " (.getMessage e))}))) | |
(defn chat-with-oci | |
"Main function to chat with OCI Generative AI" | |
[message compartment-id model-id] | |
(let [auth-provider (create-auth-provider) | |
client (create-client auth-provider) | |
chat-request (create-chat-request message) | |
serving-mode (create-serving-mode model-id) | |
chat-details (create-chat-details compartment-id serving-mode chat-request) | |
result (make-chat-call client chat-details)] | |
(if (:success result) | |
(println (:text result)) | |
(println (:error result))))) | |
;; Example usage | |
(defn -main | |
[& args] | |
(chat-with-oci | |
"Hello, how are you?" | |
"ocid1.compartment.oc1..xyz" | |
"cohere.command-a-03-2025")) | |
;; Alternative: more functional approach with higher-order functions | |
(defn with-oci-client | |
"Higher-order function that handles client creation and cleanup" | |
[f & args] | |
(let [auth-provider (create-auth-provider) | |
client (create-client auth-provider)] | |
(try | |
(apply f client args) | |
(finally | |
;; Client cleanup would go here if needed | |
nil)))) | |
;; Example of using the higher-order function approach | |
(defn chat-functional-style | |
[message compartment-id model-id] | |
(with-oci-client | |
(fn [client] | |
(let [chat-request (create-chat-request message) | |
serving-mode (create-serving-mode model-id) | |
chat-details (create-chat-details compartment-id serving-mode chat-request) | |
result (make-chat-call client chat-details)] | |
(if (:success result) | |
(println (:text result)) | |
(println (:error result))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment