Skip to content

Commit 3c29275

Browse files
committed
Support custom api urls for models
1 parent 83a98d1 commit 3c29275

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add env support for MCPs
66
- Add web_search capability
77
- Add `o3` model support.
8+
- Support custom API urls for OpenAI and Anthropic
89

910
## 0.0.3
1011

src/eca/features/mcp.clj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@
5555
(defn cache-tools! [db*]
5656
(let [obj-mapper (ObjectMapper.)]
5757
(doseq [[name {:keys [client]}] (:mcp-clients @db*)]
58-
(doseq [^McpSchema$Tool tool-client (.tools (.listTools ^McpSyncClient client))]
59-
(let [tool {:name (.name tool-client)
60-
:mcp-name name
61-
:mcp-client client
62-
:description (.description tool-client)
63-
;; We convert to json to then read so we have the clojrue map
64-
;; TODO avoid this converting to clojure map directly
65-
:parameters (json/parse-string (.writeValueAsString obj-mapper (.inputSchema tool-client)) true)}]
66-
(swap! db* assoc-in [:mcp-tools (:name tool)] tool))))))
58+
(when (.isInitialized client)
59+
(doseq [^McpSchema$Tool tool-client (.tools (.listTools ^McpSyncClient client))]
60+
(let [tool {:name (.name tool-client)
61+
:mcp-name name
62+
:mcp-client client
63+
:description (.description tool-client)
64+
;; We convert to json to then read so we have the clojrue map
65+
;; TODO avoid this converting to clojure map directly
66+
:parameters (json/parse-string (.writeValueAsString obj-mapper (.inputSchema tool-client)) true)}]
67+
(swap! db* assoc-in [:mcp-tools (:name tool)] tool)))))))
6768

6869
(defn list-tools [db]
6970
(vals (:mcp-tools db)))

src/eca/llm_providers/anthropic.clj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010

1111
(def ^:private logger-tag "[ANTHROPIC]")
1212

13-
(def ^:private url "https://api.anthropic.com/v1/messages")
13+
(def ^:private anthropic-url "https://api.anthropic.com")
14+
(def ^:private messages-path "/v1/messages")
15+
16+
(defn ^:private url [path]
17+
(format "%s%s"
18+
(or (System/getenv "ANTHROPIC_API_URL")
19+
anthropic-url)
20+
path))
1421

1522
(defn ^:private ->tools [tools web-search]
1623
(cond->
@@ -25,7 +32,7 @@
2532
(let [api-key (or api-key
2633
(System/getenv "ANTHROPIC_API_KEY"))]
2734
(http/post
28-
url
35+
(url messages-path)
2936
{:headers {"x-api-key" api-key
3037
"anthropic-version" "2023-06-01"
3138
"Content-Type" "application/json"}

src/eca/llm_providers/openai.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010

1111
(def ^:private logger-tag "[OPENAI]")
1212

13-
(def ^:private url "https://api.openai.com/v1/responses")
13+
(def ^:private openai-url "https://api.openai.com/")
14+
(def ^:private responses-path "/v1/responses")
1415

15-
(defn ^:private base-request! [{:keys [body api-key on-error on-response]}]
16+
(defn ^:private url [path]
17+
(format "%s%s"
18+
(or (System/getenv "OPENAI_API_URL")
19+
openai-url)
20+
path))
21+
22+
(defn ^:private base-completion-request! [{:keys [body api-key on-error on-response]}]
1623
(let [api-key (or api-key
1724
(System/getenv "OPENAI_API_KEY"))]
1825
(http/post
19-
url
26+
(url responses-path)
2027
{:headers {"Authorization" (str "Bearer " api-key)
2128
"Content-Type" "application/json"}
2229
:body (json/generate-string body)
@@ -58,7 +65,7 @@
5865
function-args (-> data :item :arguments)
5966
response (on-tool-called {:name function-name
6067
:arguments (json/parse-string function-args)})]
61-
(base-request!
68+
(base-completion-request!
6269
{:body (assoc body :input (concat input
6370
[{:type "function_call"
6471
:call_id (-> data :item :call_id)
@@ -84,7 +91,7 @@
8491
(on-message-received {:type :finish
8592
:finish-reason (-> data :response :status)}))
8693
nil))]
87-
(base-request!
94+
(base-completion-request!
8895
{:body body
8996
:api-key api-key
9097
:on-error on-error

0 commit comments

Comments
 (0)