Skip to content

Commit c44b042

Browse files
committed
Added support for azure translations, transcriptions and images
1 parent d015cdc commit c44b042

File tree

4 files changed

+95
-22
lines changed

4 files changed

+95
-22
lines changed

doc/02-usage-azure.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,34 @@ Any of these [supported request options](https://github.com/gnarroway/hato#reque
104104

105105
* [create-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-completion)
106106

107-
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#completions)
107+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#completions)
108108

109109
### Chat
110110

111111
* [create-chat-completion](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-chat-completion)
112112

113-
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#chat-completions)
113+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)
114114

115115
### Embeddings
116116

117117
* [create-embedding](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-embedding)
118118

119-
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#embeddings)
119+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#embeddings)
120+
121+
### Transcriptions
122+
123+
* [create-transcription](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-transcription)
124+
125+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#transcriptions)
126+
127+
### Translations
128+
129+
* [create-translation](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-translation)
130+
131+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#translations)
132+
133+
### Images
134+
135+
* [create-image](https://cljdoc.org/d/net.clojars.wkok/openai-clojure/0.19.0/api/wkok.openai-clojure.api#create-image)
136+
137+
Also see the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#image-generation)

src/wkok/openai_clojure/api.clj

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
5858
Streaming of token events is supported via the `:stream` param, see [Streaming Tokens](/doc/03-streaming.md)
5959
60-
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
60+
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
6161
"
6262
([params]
6363
(create-completion params nil))
@@ -80,7 +80,7 @@
8080
8181
Streaming of token events is supported via the `:stream` param, see [Streaming Tokens](/doc/03-streaming.md)
8282
83-
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
83+
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
8484
"
8585
([params]
8686
(create-chat-completion params nil))
@@ -102,7 +102,10 @@
102102
:n 2
103103
:size \"1024x1024\"})
104104
```
105-
Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/images/create)
105+
106+
For Azure OpenAI pass `{:impl :azure}` for the `options` argument
107+
108+
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
106109
"
107110
([params]
108111
(create-image params nil))
@@ -160,7 +163,7 @@
160163
161164
For Azure OpenAI pass `{:impl :azure}` for the `options` argument
162165
163-
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
166+
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
164167
"
165168
([params]
166169
(create-embedding params nil))
@@ -183,7 +186,10 @@
183186
(create-transcription {:file (clojure.java.io/file \"path/to/audio.mp3\")
184187
:model \"whisper-1\"})
185188
```
186-
Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/audio/create)
189+
190+
For Azure OpenAI pass `{:impl :azure}` for the `options` argument
191+
192+
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
187193
"
188194
([params]
189195
(create-transcription params nil))
@@ -198,7 +204,10 @@
198204
(create-translation {:file (clojure.java.io/file \"path/to/file/german.m4a\")
199205
:model \"whisper-1\"})
200206
```
201-
Also see the [OpenAI documentation](https://platform.openai.com/docs/api-reference/audio/create)
207+
208+
For Azure OpenAI pass `{:impl :azure}` for the `options` argument
209+
210+
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
202211
"
203212
([params]
204213
(create-translation params nil))

src/wkok/openai_clojure/azure.clj

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@
2626
idx (s/index-of url "/openai")]
2727
(str endpoint (subs url idx))))))})
2828

29+
(defn- ->patched-handler
30+
[m from to]
31+
(-> (martian/handler-for m :completions-create)
32+
(assoc :route-name :create-completion)))
33+
34+
(def route-mappings
35+
{:completions-create :create-completion
36+
:chat-completions-create :create-chat-completion
37+
:embeddings-create :create-embedding
38+
:transcriptions-create :create-transcription
39+
:translations-create :create-translation
40+
:image-generations-create :create-image})
41+
2942
(defn patch-handler
3043
"Patching azure's handlers to support the same operation-id names as the standard openai api"
3144
[m]
32-
(let [patched-completions-create-handler (-> (martian/handler-for m :completions-create)
33-
(assoc :route-name :create-completion))
34-
patched-chat-completions-create-handler (-> (martian/handler-for m :chat-completions-create)
35-
(assoc :route-name :create-chat-completion))
36-
patched-embeddings-create-handler (-> (martian/handler-for m :embeddings-create)
37-
(assoc :route-name :create-embedding))
38-
39-
40-
patched-handlers [patched-completions-create-handler
41-
patched-embeddings-create-handler
42-
patched-chat-completions-create-handler]]
43-
44-
(assoc m :handlers patched-handlers)))
45+
(update m :handlers
46+
(fn [handlers]
47+
(map (fn [{route-name :route-name :as handler}]
48+
(update handler :route-name #(route-name route-mappings %)))
49+
handlers))))
4550

4651
(defn load-openai-spec []
4752
(json/decode (slurp (io/resource "azure_openai.json")) keyword))
@@ -64,3 +69,26 @@
6469
{:api-version "2024-06-01"
6570
:deployment-id (:model params)
6671
:martian.core/body (dissoc params :model)})
72+
73+
74+
(comment
75+
76+
;; Get all operations defined in spec
77+
78+
(def spec (->> (martian/explore @m)
79+
(map first)
80+
#_(map #(martian/explore @m %))
81+
set))
82+
83+
;; Get all functions defined in api
84+
85+
(require 'wkok.openai-clojure.api)
86+
87+
(def impl (->> (keys (ns-publics 'wkok.openai-clojure.api))
88+
(map keyword)
89+
set))
90+
91+
;; Compare the two
92+
93+
(clojure.set/difference impl spec)
94+
)

test/wkok/openai_clojure/api_test.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
:n 2
106106
:size "1024x1024"})))
107107

108+
(is (= :success
109+
(api/create-image {:model "dall-e"
110+
:prompt "A cute baby sea otter"
111+
:n 2
112+
:size "1024x1024"}
113+
{:impl :azure})))
114+
108115
(is (= :success
109116
(api/create-image-edit {:image (io/file "path/to/otter.png")
110117
:mask (io/file "path/to/mask.png")
@@ -134,9 +141,20 @@
134141
(api/create-transcription {:file (io/file "path/to/audio.mp3")
135142
:model "whisper-1"})))
136143

144+
(is (= :success
145+
(api/create-transcription {:file (io/file "path/to/audio.mp3")
146+
:model "whisper-1"}
147+
{:impl :azure})))
148+
137149
(is (= :success
138150
(api/create-translation {:file (io/file "path/to/file/german.m4a")
139151
:model "whisper-1"})))
152+
153+
(is (= :success
154+
(api/create-translation {:file (io/file "path/to/file/german.m4a")
155+
:model "whisper-1"}
156+
{:impl :azure})))
157+
140158
(is (= :success
141159
(api/create-speech {:model "tts-1"
142160
:input "Hey there! Nice to meet you!"

0 commit comments

Comments
 (0)