Skip to content

Commit

Permalink
Merge pull request #50 from wkok/logger
Browse files Browse the repository at this point in the history
Trace raw request and response
  • Loading branch information
wkok authored Jan 14, 2024
2 parents 71bb03d + f391cfc commit 72ceba6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
42 changes: 42 additions & 0 deletions doc/04-debugging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Debugging

When supplying a `trace` function in `:options` it will be called with the raw maps of the request and response for allowing capturing tracing information like the raw messages sent/received:

```clojure
(create-chat-completion {:model "gpt-3.5-turbo"
:messages [{:role "system" :content "You are a helpful assistant."}
{:role "user" :content "Who won the world series in 2020?"}
{:role "assistant" :content "The Los Angeles Dodgers won the World Series in 2020."}
{:role "user" :content "Where was it played?"}]}
{:trace (fn [request response]
(println (:body response)))})
```

Prints:

```json
{
"id": "chatcmpl-8gHB7Il500UxZPChGAZHtklLWTS8T",
"object": "chat.completion",
"created": 1705086501,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas."
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 53,
"completion_tokens": 17,
"total_tokens": 70
},
"system_fingerprint": null
}

```
57 changes: 53 additions & 4 deletions src/wkok/openai_clojure/sse.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns ^:no-doc wkok.openai-clojure.sse
(:require
[hato.client :as http]
[hato.middleware :as hm]
[clojure.core.async :as a]
[clojure.string :as string]
[cheshire.core :as json]
Expand Down Expand Up @@ -104,9 +105,57 @@
{:status 200
:body events}))

(defn wrap-trace
"Middleware that allows the user to supply a trace function that
will receive the raw request & response as arguments.
See: https://github.com/gnarroway/hato?tab=readme-ov-file#custom-middleware"
[trace]
(fn [client]
(fn
([req]
(let [resp (client req)]
(trace req resp)
resp))
([req respond raise]
(client req
#(respond (do (trace req %)
%))
raise)))))

(defn middleware
"The default list of middleware hato uses for wrapping requests but
with added wrap-trace in the correct position to allow tracing of error messages."
[trace]
[hm/wrap-request-timing

hm/wrap-query-params
hm/wrap-basic-auth
hm/wrap-oauth
hm/wrap-user-info
hm/wrap-url

hm/wrap-decompression
hm/wrap-output-coercion

(wrap-trace trace)

hm/wrap-exceptions
hm/wrap-accept
hm/wrap-accept-encoding
hm/wrap-multipart

hm/wrap-content-type
hm/wrap-form-params
hm/wrap-nested-params
hm/wrap-method])

(def perform-sse-capable-request
{:name ::perform-sse-capable-request
{:name ::perform-sse-capable-request
:leave (fn [{:keys [request params] :as ctx}]
(assoc ctx :response (if (:stream params)
(sse-request ctx)
(http/request request))))})
(let [{{trace :trace} :wkok.openai-clojure.core/options} params]
(assoc ctx :response (if (:stream params)
(sse-request ctx)
(http/request
(if trace
(assoc request :middleware (middleware trace))
request))))))})

0 comments on commit 72ceba6

Please sign in to comment.