File tree Expand file tree Collapse file tree 4 files changed +20
-9
lines changed
Expand file tree Collapse file tree 4 files changed +20
-9
lines changed Original file line number Diff line number Diff line change 9292 :is-complete (boolean finish-reason)
9393 :content {:type :text
9494 :text message}}))
95- :on-error (fn [e ]
95+ :on-error (fn [{ :keys [message exception]} ]
9696 (messenger/chat-content-received
9797 messenger
9898 {:chat-id chat-id
9999 :request-id request-id
100100 :is-complete true
101101 :role :system
102102 :content {:type :text
103- :text (str " \n Error: " (ex-message e ))}}))}))
103+ :text (or message (ex-message exception ))}}))}))
104104 {:chat-id chat-id
105105 :status :success }))
106106
Original file line number Diff line number Diff line change 1616 :api-key (:openai-api-key config)}
1717 {:on-message-received on-message-received
1818 :on-error on-error})
19- (on-error (str " Unsupported model: " model))))
19+ (on-error { :msg (str " ECA Unsupported model: " model)} )))
Original file line number Diff line number Diff line change 33 [cheshire.core :as json]
44 [clojure.java.io :as io]
55 [clojure.string :as str]
6+ [eca.logger :as logger]
67 [hato.client :as http]))
78
9+ (def ^:private logger-tag " [OPENAI]" )
10+
811(def ^:private url " https://api.openai.com/v1/chat/completions" )
912
1013(defn ^:private raw-data->messages [data]
2932 {:headers {" Authorization" (str " Bearer " api-key)
3033 " Content-Type" " application/json" }
3134 :body (json/generate-string body)
35+ :throw-exceptions? false
3236 :async? true
3337 :as :stream }
3438 (fn [{:keys [status body]}]
3539 (try
3640 (if (not= 200 status)
37- (on-error status)
41+ (do
42+ (logger/warn logger-tag " Unexpected response status" status)
43+ (on-error {:message (str " OpenAI response status: " status)}))
3844 (with-open [rdr (io/reader body)]
3945 (doseq [line (line-seq rdr)]
4046 (when (str/starts-with? line " data: " )
4349 (doseq [message (raw-data->messages data)]
4450 (on-message-received message))))))))
4551 (catch Exception e
46- ; ; TODO improve error handling
47- (println " -->" e))))
52+ (on-error {:exception e}))))
4853 (fn [e]
49- (on-error e )))))
54+ (on-error { :exception e} )))))
Original file line number Diff line number Diff line change 11(ns eca.logger )
22
3- ; ; TODO create better logger capability.
4- (defn info [& args]
3+ (defn ^:private stderr-print [& args]
54 (binding [*out* *err*]
65 (apply println args)))
76
7+ ; ; TODO create better logger capability.
8+ (defn info [& args]
9+ (apply stderr-print args))
10+
11+ (defn warn [& args]
12+ (apply stderr-print args))
13+
814(defn format-time-delta-ms [start-time end-time]
915 (format " %.0fms" (float (/ (- end-time start-time) 1000000 ))))
1016
You can’t perform that action at this time.
0 commit comments