diff --git a/doc/02-usage-azure.md b/doc/02-usage-azure.md index 85d73f2..a378176 100644 --- a/doc/02-usage-azure.md +++ b/doc/02-usage-azure.md @@ -104,16 +104,34 @@ Any of these [supported request options](https://github.com/gnarroway/hato#reque * [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-completion) -Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#completions) +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#completions) ### Chat * [create-chat-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-chat-completion) -Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#chat-completions) +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions) ### Embeddings * [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-embedding) -Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#embeddings) +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#embeddings) + +### Transcriptions + +* [create-transcription](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-transcription) + +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#transcriptions) + +### Translations + +* [create-translation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-translation) + +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#translations) + +### Images + +* [create-image](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-image) + +Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#image-generation) diff --git a/src/wkok/openai_clojure/api.clj b/src/wkok/openai_clojure/api.clj index ed00267..5075c81 100644 --- a/src/wkok/openai_clojure/api.clj +++ b/src/wkok/openai_clojure/api.clj @@ -57,7 +57,7 @@ Streaming of token events is supported via the `:stream` param, see [Streaming Tokens](/doc/03-streaming.md) - Also see the [OpenAI](https://platform.openai.com/docs/api-reference/completions/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#completions) documentation + Also see the [OpenAI](https://platform.openai.com/docs/api-reference/completions/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#completions) documentation " ([params] (create-completion params nil)) @@ -80,7 +80,7 @@ Streaming of token events is supported via the `:stream` param, see [Streaming Tokens](/doc/03-streaming.md) - Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#chat-completions) documentation + Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions) documentation " ([params] (create-chat-completion params nil)) @@ -102,7 +102,10 @@ :n 2 :size \"1024x1024\"}) ``` - Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/images/create) + + For Azure OpenAI pass `{:impl :azure}` for the `options` argument + + Also see the [OpenAI](https://platform.openai.com/docs/api-reference/images/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#image-generation) documentation " ([params] (create-image params nil)) @@ -160,7 +163,7 @@ For Azure OpenAI pass `{:impl :azure}` for the `options` argument - Also see the [OpenAI](https://platform.openai.com/docs/api-reference/embeddings/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#embeddings) documentation + Also see the [OpenAI](https://platform.openai.com/docs/api-reference/embeddings/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#embeddings) documentation " ([params] (create-embedding params nil)) @@ -183,7 +186,10 @@ (create-transcription {:file (clojure.java.io/file \"path/to/audio.mp3\") :model \"whisper-1\"}) ``` - Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/audio/create) + + For Azure OpenAI pass `{:impl :azure}` for the `options` argument + + Also see the [OpenAI](https://platform.openai.com/docs/api-reference/audio/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#transcriptions) documentation " ([params] (create-transcription params nil)) @@ -198,7 +204,10 @@ (create-translation {:file (clojure.java.io/file \"path/to/file/german.m4a\") :model \"whisper-1\"}) ``` - Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/audio/create) + + For Azure OpenAI pass `{:impl :azure}` for the `options` argument + + Also see the [OpenAI](https://platform.openai.com/docs/api-reference/audio/create) / [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#translations) documentation " ([params] (create-translation params nil)) diff --git a/src/wkok/openai_clojure/azure.clj b/src/wkok/openai_clojure/azure.clj index fae7194..53ecdbc 100644 --- a/src/wkok/openai_clojure/azure.clj +++ b/src/wkok/openai_clojure/azure.clj @@ -26,22 +26,27 @@ idx (s/index-of url "/openai")] (str endpoint (subs url idx))))))}) +(defn- ->patched-handler + [m from to] + (-> (martian/handler-for m :completions-create) + (assoc :route-name :create-completion))) + +(def route-mappings + {:completions-create :create-completion + :chat-completions-create :create-chat-completion + :embeddings-create :create-embedding + :transcriptions-create :create-transcription + :translations-create :create-translation + :image-generations-create :create-image}) + (defn patch-handler "Patching azure's handlers to support the same operation-id names as the standard openai api" [m] - (let [patched-completions-create-handler (-> (martian/handler-for m :completions-create) - (assoc :route-name :create-completion)) - patched-chat-completions-create-handler (-> (martian/handler-for m :chat-completions-create) - (assoc :route-name :create-chat-completion)) - patched-embeddings-create-handler (-> (martian/handler-for m :embeddings-create) - (assoc :route-name :create-embedding)) - - - patched-handlers [patched-completions-create-handler - patched-embeddings-create-handler - patched-chat-completions-create-handler]] - - (assoc m :handlers patched-handlers))) + (update m :handlers + (fn [handlers] + (map (fn [{route-name :route-name :as handler}] + (update handler :route-name #(route-name route-mappings %))) + handlers)))) (defn load-openai-spec [] (json/decode (slurp (io/resource "azure_openai.json")) keyword)) @@ -64,3 +69,26 @@ {:api-version "2024-06-01" :deployment-id (:model params) :martian.core/body (dissoc params :model)}) + + +(comment + + ;; Get all operations defined in spec + + (def spec (->> (martian/explore @m) + (map first) + #_(map #(martian/explore @m %)) + set)) + + ;; Get all functions defined in api + + (require 'wkok.openai-clojure.api) + + (def impl (->> (keys (ns-publics 'wkok.openai-clojure.api)) + (map keyword) + set)) + + ;; Compare the two + + (clojure.set/difference impl spec) + ) diff --git a/test/wkok/openai_clojure/api_test.clj b/test/wkok/openai_clojure/api_test.clj index ddfef3d..bb9a8f8 100644 --- a/test/wkok/openai_clojure/api_test.clj +++ b/test/wkok/openai_clojure/api_test.clj @@ -105,6 +105,13 @@ :n 2 :size "1024x1024"}))) + (is (= :success + (api/create-image {:model "dall-e" + :prompt "A cute baby sea otter" + :n 2 + :size "1024x1024"} + {:impl :azure}))) + (is (= :success (api/create-image-edit {:image (io/file "path/to/otter.png") :mask (io/file "path/to/mask.png") @@ -134,9 +141,20 @@ (api/create-transcription {:file (io/file "path/to/audio.mp3") :model "whisper-1"}))) + (is (= :success + (api/create-transcription {:file (io/file "path/to/audio.mp3") + :model "whisper-1"} + {:impl :azure}))) + (is (= :success (api/create-translation {:file (io/file "path/to/file/german.m4a") :model "whisper-1"}))) + + (is (= :success + (api/create-translation {:file (io/file "path/to/file/german.m4a") + :model "whisper-1"} + {:impl :azure}))) + (is (= :success (api/create-speech {:model "tts-1" :input "Hey there! Nice to meet you!"