This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-18
Channels
- # ai (2)
- # announcements (11)
- # beginners (34)
- # biff (14)
- # clerk (14)
- # clj-kondo (25)
- # clojure (27)
- # clojure-austin (1)
- # clojure-conj (6)
- # clojure-denmark (1)
- # clojure-europe (20)
- # clojure-hamburg (1)
- # clojure-nl (1)
- # clojure-norway (28)
- # clojure-uk (2)
- # clojuredesign-podcast (6)
- # clojurescript (43)
- # cursive (4)
- # data-science (2)
- # emacs (9)
- # hyperfiddle (9)
- # introduce-yourself (2)
- # jobs (3)
- # lsp (32)
- # missionary (31)
- # nbb (8)
- # off-topic (23)
- # rdf (23)
- # re-frame (10)
- # reitit (11)
- # releases (3)
- # rewrite-clj (4)
- # shadow-cljs (7)
- # specter (6)
- # sql (7)
- # xtdb (7)
I’m trying to understand how I would use specter as a replacement for complex select-keys
commands. For example, from the following payload:
{:fn "get-openai-reply",
:openai-response
{:created 1681730312,
:choices
[{:finish_reason "stop",
:index 0,
:message
{:role "assistant",
:content
"Great! If you have any more questions, feel free to ask."}}],
:usage
{:total_tokens 788, :completion_tokens 14, :prompt_tokens 774},
:id "chatcmpl-76HAObDzLQB58BILV05rQvhoCLd3Z",
:object "chat.completion",
:model "gpt-3.5-turbo-0301"}}
I want to extract the following output:
{:created 1681730312, :usage {:total_tokens 788}}
The following command:
(s/select-first [:openai-response (s/submap [:created :usage])] data)
gives me:
{:created 1681730312, :usage {:total_tokens 788, :completion_tokens 14, :prompt_tokens 774}}
How do I get the last bit of the selection?you can add (s/tranformed [:usage] #(select-keys % [:total_tokens]))
to you path. I'm not sure specter is the right approach
Pretty much the same thing…
(select-one [:openai-response
(submap [:created :usage])
(view
(fn [m]
(update m :usage
#(select-keys % [:total_tokens]))))])
Another try
(->> m
(select
[:openai-response
(multi-path
(submap [:created])
[(submap [:usage])
(transformed :usage
#(select-one (submap [:total_tokens]) %))])])
(apply merge)
Probably more clear to transform from the start, instead of expecting to stack a bunch of localized selections in one path.
(transform
[(collect-one :openai-response :created)
(collect-one :openai-response :usage :total_tokens)]
(fn [created total m]
{:created created
:usage {:total_tokens total}}))