Skip to content

Commit f5168ce

Browse files
authored
Merge pull request #32 from wkok/override-openai-api-endpoint
Added ability to override the OpenAI API endpoint
2 parents bd59fab + ae2da47 commit f5168ce

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

doc/01-usage-openai.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ An API key can be generated in your [OpenAI account](https://platform.openai.com
3030

3131
*Optional* - If your OpenAI account uses multiple organizations, set the environment variable `OPENAI_ORGANIZATION` to the one used for your app.
3232

33+
### Endpoint
34+
35+
It is not necessary to specify the endpoint url if using the default OpenAI service. If you do need to point to a different endpoint set the environment variable `OPENAI_API_ENDPOINT` for example: *https://myendpoint.my-openai.com*
36+
3337
### Options
3438

35-
Alternatively the `api-key` and/or `organization` can be passed in the `options` argument of each api function
39+
Alternatively the `api-key` and/or `organization` and/or `api-endpoint` can be passed in the `options` argument of each api function
3640

3741
```
3842
(api/create-completion {:model "text-davinci-003"
3943
:prompt "Say this is a test"}
4044
{:api-key "xxxxx"
41-
:organization "abcd"})
45+
:organization "abcd"
46+
:api-endpoint "https://myendpoint.my-openai.com"})
4247
```
4348

4449
## Quickstart

src/wkok/openai_clojure/openai.clj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[wkok.openai-clojure.sse :as sse]
99
[martian.encoders :as encoders]
1010
[martian.interceptors :as interceptors]
11-
[schema.core :as s]))
11+
[schema.core :as s]
12+
[clojure.string :as string]))
1213

1314
(def add-headers
1415
{:name ::add-headers
@@ -23,6 +24,17 @@
2324
(not-empty api-key) (assoc "Authorization" (str "Bearer " api-key))
2425
(not-empty organization) (assoc "OpenAI-Organization" organization))))))})
2526

27+
(defn override-api-endpoint
28+
[base-url]
29+
{:name ::override-api-endpoint
30+
:enter (fn [ctx]
31+
(update-in ctx [:request :url]
32+
(fn [url]
33+
(let [endpoint (or (-> ctx :params :wkok.openai-clojure.core/options :api-endpoint)
34+
(System/getenv "OPENAI_API_ENDPOINT")
35+
base-url)]
36+
(str endpoint (subs url (count base-url)))))))})
37+
2638
(defn- multipart-form-data?
2739
[handler]
2840
(-> handler :openapi-definition :requestBody :content :multipart/form-data))
@@ -72,6 +84,7 @@
7284
(-> (remove #(#{:martian.hato/perform-request} (:name %))
7385
interceptors)
7486
(concat [add-headers
87+
(override-api-endpoint base-url)
7588
(interceptors/encode-body encoders)
7689
multipart-form-data
7790
sse/perform-sse-capable-request]))))]

test/wkok/openai_clojure/openai_test.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@
4242
:organization "my-company"}}})
4343
:request
4444
:headers))))))
45+
46+
(deftest override-api-endpoint-test
47+
(let [base-url "https://api.openai.com/v2"
48+
override-api-endpoint-fn (-> base-url openai/override-api-endpoint :enter)]
49+
(testing "api endpoint gets correctly overridden"
50+
51+
(let [api-endpoint "https://myendpoint.my-openai.com/v2"
52+
path "/some/chat/prompt"
53+
test-fn (fn [url]
54+
(is (= (str api-endpoint path)
55+
(-> (override-api-endpoint-fn {:request {:url url}
56+
:params {:wkok.openai-clojure.core/options {:api-endpoint api-endpoint}}})
57+
:request
58+
:url))))]
59+
(test-fn (str base-url "/some/chat/prompt"))))))

0 commit comments

Comments
 (0)