Skip to content

Commit

Permalink
Added support for azure translations, transcriptions and images
Browse files Browse the repository at this point in the history
  • Loading branch information
wkok committed Aug 28, 2024
1 parent d015cdc commit c44b042
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
24 changes: 21 additions & 3 deletions doc/02-usage-azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 15 additions & 6 deletions src/wkok/openai_clojure/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
54 changes: 41 additions & 13 deletions src/wkok/openai_clojure/azure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
)
18 changes: 18 additions & 0 deletions test/wkok/openai_clojure/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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!"
Expand Down

0 comments on commit c44b042

Please sign in to comment.