|
| 1 | +(ns claude |
| 2 | + (:require |
| 3 | + [babashka.http-client :as http] |
| 4 | + [cheshire.core :as json] |
| 5 | + [clojure.core.async :as async] |
| 6 | + [clojure.java.io :as io] |
| 7 | + [clojure.pprint :refer [pprint]] |
| 8 | + [clojure.string :as string] |
| 9 | + [jsonrpc])) |
| 10 | + |
| 11 | +(set! *warn-on-reflection* true) |
| 12 | + |
| 13 | +(defn claude-api-key [] |
| 14 | + (try |
| 15 | + (string/trim (slurp (io/file (or (System/getenv "CLAUDE_API_KEY_LOCATION") (System/getenv "HOME")) ".claude-api-key"))) |
| 16 | + (catch Throwable _ nil))) |
| 17 | + |
| 18 | +(defn parse-sse [s] |
| 19 | + (when (string/starts-with? s "data:") |
| 20 | + (string/replace s "data: " ""))) |
| 21 | + |
| 22 | +(defn prepare-system-messages [request] |
| 23 | + (let [system-message (->> request :messages (filter #(= "system" (:role %))) (map :content) (apply str))] |
| 24 | + (-> request |
| 25 | + (assoc :system system-message) |
| 26 | + (update-in [:messages] (fn [messages] (filter (complement #(= "system" (:role %))) messages)))))) |
| 27 | + |
| 28 | +(comment |
| 29 | + (prepare-system-messages {}) |
| 30 | + (prepare-system-messages {:messages [{:role "system" :content "hello"} |
| 31 | + {:role "user" :content "world"}]})) |
| 32 | + |
| 33 | +(defn map-to-claude [tools] |
| 34 | + (->> tools |
| 35 | + (map (fn [{:keys [function]}] (-> function |
| 36 | + (select-keys [:name :description]) |
| 37 | + (assoc :input_schema (or (:parameters function) {:type "object" :properties {}})) |
| 38 | + (assoc-in [:input_schema :required] [])))))) |
| 39 | + |
| 40 | +;; tool messages in claude are user messages with a tool_use_id instead of a tool_call_id |
| 41 | +(defn filter-tool-messages [messages] |
| 42 | + (->> messages |
| 43 | + (map (fn [{:keys [role] :as m}] |
| 44 | + (if (= role "tool") |
| 45 | + {:role "user" :content [{:type "tool_result" :content (:content m) :tool_use_id (:tool_call_id m)}]} |
| 46 | + m))))) |
| 47 | + |
| 48 | +(defn map-tool-use-messages [messages] |
| 49 | + (->> messages |
| 50 | + (map (fn [{:keys [tool_calls] :as message}] |
| 51 | + (if tool_calls |
| 52 | + {:role (:role message) |
| 53 | + :content (concat |
| 54 | + (when (:content message) [{:type "text" :text (:content message)}]) |
| 55 | + (->> tool_calls |
| 56 | + (map (fn [{:keys [id function]}] |
| 57 | + {:type "tool_use" |
| 58 | + :id id |
| 59 | + :name (:name function) |
| 60 | + :input (or (json/parse-string (:arguments function) true) {})}))))} |
| 61 | + message))))) |
| 62 | + |
| 63 | +(comment |
| 64 | + (filter-tool-messages [{:role "tool" :content "hello" :tool_call_id "1234"}])) |
| 65 | + |
| 66 | +; tool |
| 67 | +(defn sample |
| 68 | + "get a response |
| 69 | + response stream handled by callback |
| 70 | + returns nil |
| 71 | + throws exception if response can't be initiated or if we get a non 200 status code" |
| 72 | + [request cb] |
| 73 | + (jsonrpc/notify :start {:level (or (:level request) 0) :role "assistant"}) |
| 74 | + (let [b (merge |
| 75 | + {:model "claude-3-5-sonnet-20241022" |
| 76 | + :max_tokens 8192} |
| 77 | + (when (seq (:tools request)) |
| 78 | + {:tool_choice {:type "auto" |
| 79 | + :disable_parallel_tool_use true}}) |
| 80 | + (-> request |
| 81 | + (prepare-system-messages) |
| 82 | + (update-in [:messages] filter-tool-messages) |
| 83 | + (update-in [:messages] map-tool-use-messages) |
| 84 | + (update-in [:tools] map-to-claude) |
| 85 | + (dissoc :url :level))) |
| 86 | + |
| 87 | + response |
| 88 | + (http/post |
| 89 | + (or (:url request) "https://api.anthropic.com/v1/messages") |
| 90 | + (merge |
| 91 | + {:body (json/encode b) |
| 92 | + :headers {"x-api-key" (or (claude-api-key) |
| 93 | + (System/getenv "CLAUDE_API_KEY")) |
| 94 | + "anthropic-version" "2023-06-01" |
| 95 | + "Content-Type" "application/json"} |
| 96 | + :throw false} |
| 97 | + (when (true? (:stream b)) |
| 98 | + {:as :stream})))] |
| 99 | + (if (= 200 (:status response)) |
| 100 | + (if (not (true? (:stream b))) |
| 101 | + (some-> (if (string? (:body response)) |
| 102 | + (:body response) |
| 103 | + (slurp (:body response))) |
| 104 | + (json/parse-string true) |
| 105 | + (cb)) |
| 106 | + (doseq [chunk (line-seq (io/reader (:body response)))] |
| 107 | + (some-> chunk |
| 108 | + (parse-sse) |
| 109 | + (json/parse-string true) |
| 110 | + (cb)))) |
| 111 | + (let [s (if (string? (:body response)) |
| 112 | + (:body response) |
| 113 | + (slurp (:body response)))] |
| 114 | + (jsonrpc/notify :message {:content s}) |
| 115 | + (throw (ex-info "Failed to call Claude API" {:body s})))))) |
| 116 | + |
| 117 | +(comment |
| 118 | + (sample {:messages [{:role "user" :content "hello"}] |
| 119 | + :stream true} println) |
| 120 | + (sample {:messages [{:role "user" :content "run the curl command for https://www.google.com"}] |
| 121 | + :tools [{:name "curl" |
| 122 | + :description "run the curl command" |
| 123 | + :input_schema {:type "object" |
| 124 | + :properties {:url {:type "string"}}}}] |
| 125 | + :stream true} println)) |
| 126 | + |
| 127 | +(def stop-reasons |
| 128 | + {:end_turn "stopped normally" |
| 129 | + :max_tokens "max response length reached" |
| 130 | + :stop_sequence "making tool calls" |
| 131 | + :tool_use "content filter applied"}) |
| 132 | + |
| 133 | +(defn update-tool-calls [m tool-calls] |
| 134 | + (reduce |
| 135 | + (fn [m {:keys [id name arguments]}] |
| 136 | + (if id |
| 137 | + (-> m |
| 138 | + (update-in [:tool-calls id :function] (constantly {:name name})) |
| 139 | + (assoc-in [:tool-calls id :id] id) |
| 140 | + (assoc :current-tool-call id)) |
| 141 | + (update-in m [:tool-calls (:current-tool-call m) :function :arguments] (fnil str "") arguments))) |
| 142 | + m tool-calls)) |
| 143 | + |
| 144 | +(defn response-loop |
| 145 | + "handle one response stream that we read from input channel c |
| 146 | + adds content or tool_calls while streaming and call any functions when done |
| 147 | + returns channel that will emit the an event with a ::response" |
| 148 | + [c] |
| 149 | + (let [response (atom {})] |
| 150 | + (async/go-loop |
| 151 | + [] |
| 152 | + (let [e (async/<! c)] |
| 153 | + (if (:done e) |
| 154 | + (let [{calls :tool-calls content :content finish-reason :finish-reason} @response |
| 155 | + r {:messages [(merge |
| 156 | + {:role "assistant" |
| 157 | + :content (or content "")} |
| 158 | + (when (seq (vals calls)) |
| 159 | + {:tool_calls (->> (vals calls) |
| 160 | + (map #(assoc % :type "function")))}))] |
| 161 | + :finish-reason finish-reason}] |
| 162 | + |
| 163 | + (jsonrpc/notify :message {:debug (str @response)}) |
| 164 | + (jsonrpc/notify :functions-done (or (vals calls) "")) |
| 165 | + ;; make-tool-calls returns a channel with results of tool call messages |
| 166 | + ;; so we can continue the conversation |
| 167 | + r) |
| 168 | + |
| 169 | + (let [{:keys [content tool_calls finish-reason]} e] |
| 170 | + (when content |
| 171 | + (swap! response update-in [:content] (fnil str "") content) |
| 172 | + (jsonrpc/notify :message {:content content})) |
| 173 | + (when tool_calls |
| 174 | + (swap! response update-tool-calls tool_calls) |
| 175 | + (jsonrpc/notify :functions (->> @response :tool-calls vals))) |
| 176 | + (when finish-reason (swap! response assoc :finish-reason finish-reason)) |
| 177 | + |
| 178 | + (recur))))))) |
| 179 | + |
| 180 | +(defn chunk-handler |
| 181 | + "sets up a response handler loop for use with an OpenAI API call |
| 182 | + returns [channel openai-handler] - channel will emit the complete fully streamed response" |
| 183 | + [] |
| 184 | + (let [c (async/chan 1)] |
| 185 | + [(response-loop c) |
| 186 | + (fn [{:keys [delta type] :as chunk}] |
| 187 | + (cond |
| 188 | + (and |
| 189 | + (= "message_delta" type) |
| 190 | + (:stop_reason delta)) |
| 191 | + (async/>!! c {:finish-reason (if (= "tool_use" (:stop_reason delta)) |
| 192 | + "tool_calls" |
| 193 | + (:stop_reason delta))}) |
| 194 | + |
| 195 | + (and |
| 196 | + (= "content_block_start" type) |
| 197 | + (= "text" (-> chunk :content_block :type))) |
| 198 | + (async/>!! c {:content (-> chunk :content_block :text)}) |
| 199 | + |
| 200 | + (and |
| 201 | + (= "content_block_delta" type) |
| 202 | + (= "text_delta" (-> delta :type))) |
| 203 | + (async/>!! c {:content (-> delta :text)}) |
| 204 | + |
| 205 | + (= "message_stop" type) |
| 206 | + (async/>!! c {:done true}) |
| 207 | + |
| 208 | + (and |
| 209 | + (= "content_block_delta" type) |
| 210 | + (= "input_json_delta" (-> delta :type))) |
| 211 | + ;; partial_json |
| 212 | + (async/>!! c {:tool_calls [{:arguments (-> delta :partial_json)}]}) |
| 213 | + |
| 214 | + (and |
| 215 | + (= "content_block_start" type) |
| 216 | + (= "tool_use" (-> chunk :content_block :type))) |
| 217 | + ; id, name and input |
| 218 | + (async/>!! c {:tool_calls [(:content_block chunk)]})))])) |
| 219 | + |
| 220 | +(comment |
| 221 | + |
| 222 | + (let [[c h] (chunk-handler)] |
| 223 | + (sample {:messages [{:role "user" :content "hello"}] |
| 224 | + :stream true} h) |
| 225 | + (println "post-stream:\n" (-> (async/<!! c) |
| 226 | + (pprint) |
| 227 | + (with-out-str)))) |
| 228 | + |
| 229 | + (let [[c h] (chunk-handler)] |
| 230 | + (sample {:messages [{:role "user" :content "run the curl command for https://www.google.com"}] |
| 231 | + :tools [{:name "curl" |
| 232 | + :description "run the curl command" |
| 233 | + :input_schema {:type "object" |
| 234 | + :properties {:url {:type "string"}}}}] |
| 235 | + :stream true} h) |
| 236 | + (println "post-stream:\n" (-> (async/<!! c) |
| 237 | + (pprint) |
| 238 | + (with-out-str))))) |
0 commit comments