|
10 | 10 | [eca.llm-api :as llm-api] |
11 | 11 | [eca.logger :as logger] |
12 | 12 | [eca.messenger :as messenger] |
13 | | - [eca.shared :as shared])) |
| 13 | + [eca.shared :as shared :refer [assoc-some]])) |
14 | 14 |
|
15 | 15 | (set! *warn-on-reflection* true) |
16 | 16 |
|
|
79 | 79 | (defn ^:private tool-name->origin [name all-tools] |
80 | 80 | (:origin (first (filter #(= name (:name %)) all-tools)))) |
81 | 81 |
|
| 82 | +(defn ^:private tokens->cost [input-tokens output-tokens model db] |
| 83 | + (let [normalized-model (if (string/includes? model "/") |
| 84 | + (last (string/split model #"/")) |
| 85 | + model) |
| 86 | + {:keys [input-token-cost output-token-cost]} (get-in db [:models normalized-model])] |
| 87 | + (when (and input-token-cost output-token-cost) |
| 88 | + (format "%.2f" (+ (* input-tokens input-token-cost) |
| 89 | + (* output-tokens output-token-cost)))))) |
| 90 | + |
82 | 91 | (defn prompt |
83 | 92 | [{:keys [message model behavior contexts chat-id request-id]} |
84 | 93 | db* |
|
119 | 128 | received-msgs* (atom "") |
120 | 129 | tool-call-args-by-id* (atom {}) |
121 | 130 | add-to-history! (fn [msg] |
122 | | - (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg))] |
| 131 | + (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg)) |
| 132 | + sum-sesison-tokens! (fn [input-tokens output-tokens] |
| 133 | + (swap! db* update-in [:chats chat-id :total-input-tokens] (fnil + 0) input-tokens) |
| 134 | + (swap! db* update-in [:chats chat-id :total-output-tokens] (fnil + 0) output-tokens))] |
123 | 135 | (messenger/chat-content-received |
124 | 136 | messenger |
125 | 137 | {:chat-id chat-id |
|
178 | 190 | (finish-chat-prompt! chat-id :idle messenger db*)) |
179 | 191 | :finish (do |
180 | 192 | (add-to-history! {:role "assistant" :content @received-msgs*}) |
| 193 | + (when-let [{:keys [output-tokens input-tokens]} (:usage msg)] |
| 194 | + (when (and output-tokens input-tokens) |
| 195 | + (sum-sesison-tokens! input-tokens output-tokens) |
| 196 | + (let [db @db* |
| 197 | + total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0) |
| 198 | + total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)] |
| 199 | + (messenger/chat-content-received |
| 200 | + messenger |
| 201 | + {:chat-id chat-id |
| 202 | + :request-id request-id |
| 203 | + :role :system |
| 204 | + :content (assoc-some {:type :usage |
| 205 | + :message-output-tokens output-tokens |
| 206 | + :message-input-tokens input-tokens |
| 207 | + :session-tokens (+ total-input-tokens total-output-tokens)} |
| 208 | + :message-cost (tokens->cost input-tokens output-tokens chosen-model db) |
| 209 | + :session-cost (tokens->cost total-input-tokens total-output-tokens chosen-model db))})))) |
181 | 210 | (finish-chat-prompt! chat-id :idle messenger db*)))) |
182 | 211 | :on-prepare-tool-call (fn [{:keys [id name arguments-text]}] |
183 | 212 | (assert-chat-not-stopped! chat-id db* messenger) |
|
0 commit comments