Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace raw request and response #50

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))))))})
Loading