|
12 | 12 |
|
13 | 13 | (def ^:private url "https://api.anthropic.com/v1/messages") |
14 | 14 |
|
15 | | -(defn ^:private ->tools [tools] |
16 | | - (mapv (fn [tool] |
17 | | - (assoc (select-keys tool [:name :description]) |
18 | | - :input_schema (:parameters tool))) tools)) |
| 15 | +(defn ^:private ->tools [tools web-search] |
| 16 | + (cond-> |
| 17 | + (mapv (fn [tool] |
| 18 | + (assoc (select-keys tool [:name :description]) |
| 19 | + :input_schema (:parameters tool))) tools) |
| 20 | + web-search (conj {:type "web_search_20250305" |
| 21 | + :name "web_search" |
| 22 | + :max_uses 10}))) |
19 | 23 |
|
20 | 24 | (defn ^:private base-request! [{:keys [body api-key on-error on-response]}] |
21 | 25 | (let [api-key (or api-key |
|
45 | 49 |
|
46 | 50 | (defn completion! |
47 | 51 | [{:keys [model user-prompt temperature context max-tokens |
48 | | - api-key past-messages tools] |
| 52 | + api-key past-messages tools web-search] |
49 | 53 | :or {max-tokens 1024 |
50 | 54 | temperature 1.0}} |
51 | 55 | {:keys [on-message-received on-error on-tool-called]}] |
|
56 | 60 | :temperature temperature |
57 | 61 | ;; TODO support :thinking |
58 | 62 | :stream true |
59 | | - :tools (->tools tools) |
| 63 | + :tools (->tools tools web-search) |
60 | 64 | :system context} |
61 | 65 | content-block* (atom nil) |
62 | 66 | on-response-fn (fn handle-response [event data] |
63 | 67 | (case event |
64 | 68 | "content_block_delta" (case (-> data :delta :type) |
65 | | - "text_delta" (on-message-received {:message (-> data :delta :text)}) |
| 69 | + "text_delta" (on-message-received {:type :text |
| 70 | + :text (-> data :delta :text)}) |
66 | 71 | "input_json_delta" (swap! content-block* update-in [(:index data) :input-json] str (-> data :delta :partial_json)) |
67 | 72 | (logger/warn "Unkown response delta type" (-> data :delta :type))) |
68 | 73 | "content_block_start" (case (-> data :content_block :type) |
|
91 | 96 | :api-key api-key |
92 | 97 | :on-error on-error |
93 | 98 | :on-response handle-response})))) |
94 | | - "end_turn" (on-message-received {:finish-reason (-> data :delta :stop_reason)}) |
| 99 | + "end_turn" (on-message-received {:type :finish |
| 100 | + :finish-reason (-> data :delta :stop_reason)}) |
95 | 101 | nil) |
96 | 102 | nil))] |
97 | 103 | (base-request! |
|
0 commit comments