|
3 | 3 | [babashka.fs :as fs]
|
4 | 4 | [clojure.core.async :as async]
|
5 | 5 | [clojure.pprint :refer [pprint]]
|
| 6 | + [clojure.core.match :refer [match]] |
6 | 7 | git
|
7 | 8 | jsonrpc
|
8 | 9 | openai
|
|
141 | 142 |
|
142 | 143 | (declare stream chat-with-tools)
|
143 | 144 |
|
144 |
| -(defn add-last-message-as-tool-call |
| 145 | +(defn add-last-message-as-tool-call |
145 | 146 | [state sub-graph-state]
|
146 | 147 | {:messages [(-> sub-graph-state
|
147 | 148 | :messages
|
148 | 149 | last
|
149 | 150 | (state/add-tool-call-id (-> state :messages last :tool_calls first :id)))]})
|
150 | 151 |
|
151 |
| -(defn append-new-messages |
| 152 | +(defn append-new-messages |
152 | 153 | [state sub-graph-state]
|
153 | 154 | {:messages (->> (:messages sub-graph-state)
|
154 | 155 | (filter (complement (fn [m] (some #(= m %) (:messages state))))))})
|
|
220 | 221 | ;; TODO handling missing edges
|
221 | 222 | (recur new-state ((get-in graph [:edges node]) new-state)))))))
|
222 | 223 |
|
| 224 | +(defn update-graph [m l x] |
| 225 | + (match [x] |
| 226 | + |
| 227 | + ; create an edge from the last node to this one |
| 228 | + [[(n1 :guard string?)]] (cond-> m |
| 229 | + true (add-edge l n1)) |
| 230 | + |
| 231 | + ; add a conditional edge from the last node to this function |
| 232 | + [[:edge (n2 :guard (comp not string?))]] (cond-> m |
| 233 | + l (add-conditional-edges |
| 234 | + l |
| 235 | + n2)) |
| 236 | + |
| 237 | + ; |
| 238 | + [[n1 (n2 :guard (comp not string?))]] (cond-> m |
| 239 | + true (add-node n1 n2) |
| 240 | + l (add-edge l n1)) |
| 241 | + |
| 242 | + :else m)) |
| 243 | + |
| 244 | +(defn path-item [{:keys [m l]} [n1 :as item]] |
| 245 | + {:m (update-graph m l item) |
| 246 | + :l (when (not (= :edge n1)) n1)}) |
| 247 | + |
| 248 | +(defn paths [agg p] |
| 249 | + (:m (reduce path-item {:m agg} p))) |
| 250 | + |
| 251 | +(defn construct-graph [x] |
| 252 | + (reduce paths {} x)) |
| 253 | + |
223 | 254 | ; ============================================================
|
224 | 255 | ; this is the graph we tend to use in our experiments thus far
|
225 | 256 | ; ============================================================
|
|
238 | 269 | (add-edge "tools-query" "completion")
|
239 | 270 | (add-conditional-edges "completion" tool-or-end)))
|
240 | 271 |
|
| 272 | +(def chat-with-tools-representation |
| 273 | + [[["start" start] |
| 274 | + ["tools-query" tools-query] |
| 275 | + ["completion" completion] |
| 276 | + [:edge tool-or-end]] |
| 277 | + [["sub-graph" (sub-graph-node nil)] |
| 278 | + ["tools-query"]] |
| 279 | + [["tool" (tool-node nil)] |
| 280 | + ["tools-query"]] |
| 281 | + [["end" end]]]) |
| 282 | + |
241 | 283 | (defn one-tool-call [_]
|
242 | 284 | (-> {}
|
243 | 285 | (add-node "start" start)
|
|
250 | 292 | (add-edge "tool" "end")
|
251 | 293 | (add-conditional-edges "completion" tool-or-end)))
|
252 | 294 |
|
| 295 | +(def one-tool-call-representation |
| 296 | + [[["start" start] |
| 297 | + ["completion" completion] |
| 298 | + [:edge tool-or-end]] |
| 299 | + [["sub-graph" (sub-graph-node nil)] |
| 300 | + ["completion"]] |
| 301 | + [["tool" (tool-node nil)] |
| 302 | + ["completion"]] |
| 303 | + [["end" end]]]) |
| 304 | + |
253 | 305 | (comment
|
254 | 306 | (alter-var-root #'jsonrpc/notify (fn [_] (partial jsonrpc/-println {:debug true})))
|
255 | 307 | (let [x {:prompts (fs/file "/Users/slim/docker/labs-ai-tools-for-devs/prompts/curl/README.md")
|
|
0 commit comments