Back

keywordize-keys (clj)

(source)

function

(keywordize-keys m)
Recursively transforms all map keys from strings to keywords.

Examples

oakmac/cuttle
(ns cuttle.config
  (:require
    [clojure.walk :refer [keywordize-keys]]
    [cuttle.util :refer [file-exists? js-log log]]))

(def config
  (if (file-exists? config-file-path)
    (-> (js/require config-file-path) js->clj keywordize-keys)
    default-config))
retrogradeorbit/bootleg
(ns bootleg.namespaces
  (:require [bootleg.utils :as utils]
            [bootleg.enlive :as enlive]
            [bootleg.selmer :as selmer]
            [bootleg.markdown :as markdown]
            [bootleg.mustache :as mustache]
            [bootleg.asciidoc :as asciidoc]
            [bootleg.html :as html]
            [bootleg.yaml :as yaml]
            [bootleg.json :as json]
            [bootleg.edn :as edn]
            [bootleg.glob :as glob]
            [bootleg.file :as file]
            [bootleg.minify :as minify]
            [hickory.convert]
            [hickory.hiccup-utils]
            [hickory.render]
            [hickory.select]
            [hickory.utils]
            [hickory.zip]
            [net.cgrand.enlive-html :as enlive-html]
            [net.cgrand.jsoup]
            [net.cgrand.reload]
            [net.cgrand.tagsoup]
            [net.cgrand.xml]
            [selmer.filter-parser]
            [selmer.filters]
            [selmer.middleware]
            [selmer.node]
            [selmer.parser]
            [selmer.tags]
            [selmer.template-parser]
            [selmer.util]
            [selmer.validator]
            [edamame.core :refer [parse-string]]
            [sci.core :as sci]
            [clojure.walk]
            [clojure.zip]
            [clojure.tools.cli]
            ))

   'clojure.walk {
                  'walk clojure.walk/walk
                  'postwalk clojure.walk/postwalk
                  'prewalk clojure.walk/prewalk
                  'postwalk-demo clojure.walk/postwalk-demo
                  'prewalk-demo clojure.walk/prewalk-demo
                  'keywordize-keys clojure.walk/keywordize-keys
                  'stringify-keys clojure.walk/stringify-keys
                  'prewalk-replace clojure.walk/prewalk-replace
                  'postwalk-replace clojure.walk/postwalk-replace
                  }
ryrobes/flowmaps
(ns flowmaps.examples.simple-flows
  (:refer-clojure :exclude [abs update-vals update-keys])
  (:require [clojure.java.jdbc :as jdbc]
            [clojure.string :as cstr]
            [clojure.walk :as walk]
            [debux.core :as dx]
            [clj-http.client :as client]
            [clojure.data.json :as json]
            [flowmaps.utility :as ut]))

;; flow defs for examples. work in progress.
(def openai-calls
  {:description "a simple HTTP loop using OpenAI API endpoint that keeps adding to chat history"
   :components {:prompt "Top O' the morning!"
                :openai-api-key (System/getenv "OAI_KEY")
                :ai-ask {:fn (fn [prompt openai-api-key history]
                               (let [question {:role "user"
                                               :content (str prompt)}]
                                 (defonce last-prompt (atom nil))
                                 (reset! last-prompt prompt) ;; to keep the loop from running amok (see :pre-when? below)
                                 {:question question
                                  :history (vec (conj history question))
                                  :answer (clojure.walk/keywordize-keys
                                           (clojure.data.json/read-str
                                            (get (clj-http.client/post
                                                  "https://api.openai.com/v1/chat/completions"
                                                  {:body (clojure.data.json/write-str
                                                          {:model "gpt-4" ; "gpt-3.5-turbo"
                                                           :messages (vec (conj history question))})
                                                   :headers {"Content-Type" "application/json"
                                                             "Authorization" (str "Bearer " openai-api-key)}
                                                   :socket-timeout 300000
                                                   :connection-timeout 300000
                                                   :content-type :json
                                                   :accept :json}) :body)))}))
                         :pre-when? (fn [prompt _ _] ;; pre-when gets the same payload as the main fn
                                      (not (= prompt @last-prompt)))
                         :inputs [:prompt :openai-api-key :history]}
                :last-response (fn [x] x) ;; just to have a data block for the UI
                :last-answer (fn [x] (get-in x [:answer :choices 0 :message :content]))
                :memory {:fn (fn [{:keys [question history answer]}]
                               (let [aa (get-in answer [:choices 0 :message])]
                                 (conj history aa)))
                         :speak (fn [x] (str (get (last x) :content))) ;; if in Rabbit, and ElevenLabs KEY, read the answer
                         :starter [{:role "system" ;; ""bootstrap"" history with sys prompt
                                    :content "You are a helpful assistant, you responses will be framed as if you are Buffy from the 1992 film."}]}}
   ;; canned REST / sub-flow endpoints 
   :points {"question" [[:prompt :ai-ask/prompt] ;; (channel to insert into)
                        [:ai-ask :memory]]} ;; (channel to snatch out of downstream)
   ;; ^^ send question, get answer (keeps convo state)
   :hide [:openai-api-key]
   :connections [[:prompt :ai-ask/prompt]
                 [:openai-api-key :ai-ask/openai-api-key]
                 [:ai-ask :memory]
                 [:ai-ask :last-response]
                 [:last-response :last-answer]
                 [:last-answer :done]
                 [:memory :ai-ask/history]]
   :canvas {:ai-ask/openai-api-key {:x 430 :y 430 :h 255 :w 240 :view-mode "text" :hidden? true}
            :ai-ask/prompt {:x 430 :y 100 :h 255 :w 240 :view-mode "text" :hidden? true}
            :ai-ask/history {:x 466 :y 962 :h 350 :w 894 :view-mode "text" :hidden? true}
            :ai-ask {:x 815 :y 347 :h 522 :w 434 :view-mode "text"}
            :memory {:x -199 :y 750 :h 369 :w 538 :view-mode "data"}
            :openai-api-key {:x -243 :y 505 :h 141 :w 565 :view-mode "input"}
            :prompt {:x -136 :y 145 :h 212 :w 415 :view-mode "input"}
            :last-response {:x 1383 :y 380 :h 755 :w 818 :view-mode "data"}
            "just-the-answer" {:inputs [[:last-response [:text [:map [:v :answer :choices 0 :message :content]]]]]
                               :x 2585 :y 984 :h 215 :w 400}
            "just-the-question" {:inputs [[:last-response [:text [:map [:v :question :content]]]]]
                                 :x 2575 :y 577 :h 215 :w 400}}})

(def ask-buffy
  {:description "simple chat call"
   :components {:prompt "Top O' the morning!"
                :openai-api-key (let [k (System/getenv "OAI_KEY")] (if (not (empty? k)) k "missing OAI_KEY var!"))
                :ai-ask {:fn (fn [prompt openai-api-key history]
                               (let [question {:role "user"
                                               :content (str prompt)}]
                                 {:question question
                                  :history (vec (conj history question))
                                  :answer (clojure.walk/keywordize-keys
                                           (clojure.data.json/read-str
                                            (get (clj-http.client/post
                                                  "https://api.openai.com/v1/chat/completions"
                                                  {:body (clojure.data.json/write-str
                                                          {:model "gpt-4"
                                                           :messages (vec (conj history question))})
                                                   :headers {"Content-Type" "application/json"
                                                             "Authorization" (str "Bearer " openai-api-key)}
                                                   :socket-timeout 300000
                                                   :connection-timeout 300000
                                                   :content-type :json
                                                   :accept :json}) :body)))}))
                         :inputs [:prompt :openai-api-key :history]
                         :view (fn [x] [:markdown [(str "## " (get-in x [:question :content]))
                                                   (str "## " (get-in x [:answer :choices 0 :message :content]))] {:color "orange"}])
                         :pre-print (fn [prompt _ _] [:question prompt])
                         :post-print (fn [x] [:answer (get-in x [:answer :choices 0 :message :content])])
                         :doc ["### Ask Buffy!" 
                               "#### She can be totally helpful, kinda. Sometimes." ;"---"
                               "#### Block requires 3 inputs"
                               "#### *prompt*" 
                               "######   - The question to ask / prompt string."
                               "#### *openai-api-key*" 
                               "######   - Your OpenAI API key."
                               "#### *history*" 
                               "######   - Either a starter set of messages / system prompts, or the feedback loop of recursive chat histories."]}
                :last-response {:fn (fn [x] (get-in x [:answer :choices 0 :message :content]))
                                :speak (fn [x] (str x))}
                :memory [{:role "system" ;; ""bootstrap"" history with sys prompt
                          :content "You are a helpful assistant, you responses will be framed as if you are Buffy from the 1992 film."}]}
      ;; canned REST / sub-flow endpoints 
   :points {"question" [[:prompt :ai-ask/prompt] [:last-response :done]]}
   :hide [:openai-api-key]
   :connections [[:prompt :ai-ask/prompt]
                 [:memory :ai-ask/history]
                 [:openai-api-key :ai-ask/openai-api-key]
                 [:ai-ask :last-response]
                 [:last-response :done]]
   :speak (fn [x] (str x)) ;; will speak when treated as a subflow only (since it will be treated as a fn!)
   :canvas {:ai-ask/openai-api-key
            {:x 430 :y 430 :h 255 :w 240 :view-mode "text" :hidden? true}
            :ai-ask/prompt {:x 430 :y 100 :h 255 :w 240 :view-mode "text" :hidden? true}
            :ai-ask/history {:x 466 :y 962 :h 350 :w 894 :view-mode "text" :hidden? true}
            :memory {:x -199 :y 750 :h 369 :w 538 :view-mode "data"}
            :openai-api-key {:x -243 :y 505 :h 141 :w 565 :view-mode "input"}
            :prompt {:x -136 :y 145 :h 212 :w 415 :view-mode "input"}
            :last-response {:x 1504 :y 373 :h 366 :w 489 :view-mode "text"}
            :ai-ask {:x 701 :y 278 :h 510 :w 597 :view-mode "text"}}})
cljsinfo/cljs.info
(ns cljsinfo-server.config
  (:require
    [clojure.walk :refer [keywordize-keys]]
    [cljsinfo-server.util :refer [ts-log]]))

(def config
  (if-let [js-config (.readJsonSync fs "config.json" (js-obj "throws" false))]
    (do (ts-log "Loaded config settings from config.json")
        (->> js-config
             js->clj
             keywordize-keys
             (merge default-config-options)))
    (do (ts-log "Loaded default config settings (config.json not found or invalid)")
        default-config-options)))
Flexiana/framework
(ns xiana.cookies
  "Cookie parser"
  (:require
    [clojure.walk :refer [keywordize-keys]]
    [ring.middleware.cookies :as cookies]))

(def interceptor
  "Parses request and response cookies"
  (letfn [(move-cookies [{headers :headers :as req}]
            (cond-> req
              (not (get headers "cookie")) (assoc-in
                                             [:headers "cookie"]
                                             (:cookie headers))))
          (parse-request-cookies [req]
            (-> req move-cookies cookies/cookies-request keywordize-keys))]
    {:name ::interceptor
     :enter (fn [state]
              (update state :request parse-request-cookies))
     :leave (fn [state]
              (update state :response cookies/cookies-response))}))