|
60 | 60 | (defn ^:private tool-name->origin [name all-tools] |
61 | 61 | (:origin (first (filter #(= name (:name %)) all-tools)))) |
62 | 62 |
|
63 | | -(defn ^:private tokens->cost [input-tokens output-tokens model db] |
| 63 | +(defn ^:private tokens->cost [input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model db] |
64 | 64 | (let [normalized-model (if (string/includes? model "/") |
65 | 65 | (last (string/split model #"/")) |
66 | 66 | model) |
67 | | - {:keys [input-token-cost output-token-cost]} (get-in db [:models normalized-model])] |
| 67 | + {:keys [input-token-cost output-token-cost |
| 68 | + input-cache-creation-token-cost input-cache-read-token-cost]} (get-in db [:models normalized-model]) |
| 69 | + input-cost (* input-tokens input-token-cost) |
| 70 | + input-cost (if input-cache-creation-tokens |
| 71 | + (+ input-cost (* input-cache-creation-tokens input-cache-creation-token-cost)) |
| 72 | + input-cost) |
| 73 | + input-cost (if input-cache-read-tokens |
| 74 | + (+ input-cost (* input-cache-read-tokens input-cache-read-token-cost)) |
| 75 | + input-cost)] |
68 | 76 | (when (and input-token-cost output-token-cost) |
69 | | - (format "%.2f" (+ (* input-tokens input-token-cost) |
| 77 | + (format "%.2f" (+ input-cost |
70 | 78 | (* output-tokens output-token-cost)))))) |
71 | 79 |
|
| 80 | +(defn ^:private usage-msg->usage |
| 81 | + [{:keys [input-tokens output-tokens |
| 82 | + input-cache-creation-tokens input-cache-read-tokens]} |
| 83 | + model |
| 84 | + {:keys [chat-id db*] :as chat-ctx}] |
| 85 | + (when (and output-tokens input-tokens) |
| 86 | + (swap! db* update-in [:chats chat-id :total-input-tokens] (fnil + 0) input-tokens) |
| 87 | + (swap! db* update-in [:chats chat-id :total-output-tokens] (fnil + 0) output-tokens) |
| 88 | + (when input-cache-creation-tokens |
| 89 | + (swap! db* update-in [:chats chat-id :total-input-cache-creation-tokens] (fnil + 0) input-cache-creation-tokens)) |
| 90 | + (when input-cache-read-tokens |
| 91 | + (swap! db* update-in [:chats chat-id :total-input-cache-read-tokens] (fnil + 0) input-cache-read-tokens)) |
| 92 | + (let [db @db* |
| 93 | + total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0) |
| 94 | + total-input-cache-creation-tokens (get-in db [:chats chat-id :total-input-cache-creation-tokens] nil) |
| 95 | + total-input-cache-read-tokens (get-in db [:chats chat-id :total-input-cache-read-tokens] nil) |
| 96 | + message-input-cache-tokens (or input-cache-creation-tokens 0) |
| 97 | + total-input-cache-tokens (or total-input-cache-creation-tokens 0) |
| 98 | + total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)] |
| 99 | + (send-content! chat-ctx :system |
| 100 | + (assoc-some {:type :usage |
| 101 | + :message-output-tokens output-tokens |
| 102 | + :message-input-tokens (+ input-tokens message-input-cache-tokens) |
| 103 | + :session-tokens (+ total-input-tokens total-input-cache-tokens total-output-tokens)} |
| 104 | + :message-cost (tokens->cost input-tokens input-cache-creation-tokens input-cache-read-tokens output-tokens model db) |
| 105 | + :session-cost (tokens->cost total-input-tokens total-input-cache-creation-tokens total-input-cache-read-tokens total-output-tokens model db)))))) |
| 106 | + |
72 | 107 | (defn prompt |
73 | 108 | [{:keys [message model behavior contexts chat-id request-id]} |
74 | 109 | db* |
|
104 | 139 | received-thinking* (atom "") |
105 | 140 | tool-call-args-by-id* (atom {}) |
106 | 141 | add-to-history! (fn [msg] |
107 | | - (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg)) |
108 | | - sum-sesison-tokens! (fn [input-tokens output-tokens] |
109 | | - (swap! db* update-in [:chats chat-id :total-input-tokens] (fnil + 0) input-tokens) |
110 | | - (swap! db* update-in [:chats chat-id :total-output-tokens] (fnil + 0) output-tokens))] |
| 142 | + (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg))] |
111 | 143 | (send-content! chat-ctx :system {:type :progress |
112 | 144 | :state :running |
113 | 145 | :text "Waiting model"}) |
|
143 | 175 | (finish-chat-prompt! :idle chat-ctx)) |
144 | 176 | :finish (do |
145 | 177 | (add-to-history! {:role "assistant" :content @received-msgs*}) |
146 | | - (when-let [{:keys [output-tokens input-tokens]} (:usage msg)] |
147 | | - (when (and output-tokens input-tokens) |
148 | | - (sum-sesison-tokens! input-tokens output-tokens) |
149 | | - (let [db @db* |
150 | | - total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0) |
151 | | - total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)] |
152 | | - (send-content! chat-ctx :system |
153 | | - (assoc-some {:type :usage |
154 | | - :message-output-tokens output-tokens |
155 | | - :message-input-tokens input-tokens |
156 | | - :session-tokens (+ total-input-tokens total-output-tokens)} |
157 | | - :message-cost (tokens->cost input-tokens output-tokens chosen-model db) |
158 | | - :session-cost (tokens->cost total-input-tokens total-output-tokens chosen-model db)))))) |
| 178 | + (when-let [usage (usage-msg->usage (:usage msg) chosen-model chat-ctx)] |
| 179 | + (send-content! chat-ctx :system |
| 180 | + (merge usage |
| 181 | + {:type :usage}))) |
159 | 182 | (finish-chat-prompt! :idle chat-ctx)))) |
160 | 183 | :on-prepare-tool-call (fn [{:keys [id name arguments-text]}] |
161 | 184 | (assert-chat-not-stopped! chat-ctx) |
|
0 commit comments