Skip to content

Commit def0e7f

Browse files
New graph representation
1 parent 5a5002a commit def0e7f

File tree

5 files changed

+88
-24
lines changed

5 files changed

+88
-24
lines changed

deps-lock.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,16 @@
930930
"mvn-repo": "https://repo.maven.apache.org/maven2/",
931931
"hash": "sha256-OeNB9nv+85PkeDkNSYjxGad5ykSQZssNM/gLQv8E9D0="
932932
},
933+
{
934+
"mvn-path": "org/clojure/core.match/1.1.0/core.match-1.1.0.jar",
935+
"mvn-repo": "https://repo.maven.apache.org/maven2/",
936+
"hash": "sha256-10V6tjEIWae9cTmEM4IEX6PN7A0T97qSEpfy8/uZj1M="
937+
},
938+
{
939+
"mvn-path": "org/clojure/core.match/1.1.0/core.match-1.1.0.pom",
940+
"mvn-repo": "https://repo.maven.apache.org/maven2/",
941+
"hash": "sha256-NnHYN2UlIwq6Ah8fYmx54g86ELYrXfgXIiWJDsSv4EU="
942+
},
933943
{
934944
"mvn-path": "org/clojure/core.memoize/1.0.253/core.memoize-1.0.253.jar",
935945
"mvn-repo": "https://repo.maven.apache.org/maven2/",

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{:paths ["src" "dev"]
22
:deps {org.clojure/clojure {:mvn/version "1.11.4"}
3+
org.clojure/core.match {:mvn/version "1.1.0"}
34
markdown-clj/markdown-clj {:mvn/version "1.12.1"}
45
pogonos/pogonos {:mvn/version "0.2.1"}
56
dev.weavejester/medley {:mvn/version "1.8.0"}

src/graph.clj

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[babashka.fs :as fs]
44
[clojure.core.async :as async]
55
[clojure.pprint :refer [pprint]]
6+
[clojure.core.match :refer [match]]
67
git
78
jsonrpc
89
openai
@@ -141,14 +142,14 @@
141142

142143
(declare stream chat-with-tools)
143144

144-
(defn add-last-message-as-tool-call
145+
(defn add-last-message-as-tool-call
145146
[state sub-graph-state]
146147
{:messages [(-> sub-graph-state
147148
:messages
148149
last
149150
(state/add-tool-call-id (-> state :messages last :tool_calls first :id)))]})
150151

151-
(defn append-new-messages
152+
(defn append-new-messages
152153
[state sub-graph-state]
153154
{:messages (->> (:messages sub-graph-state)
154155
(filter (complement (fn [m] (some #(= m %) (:messages state))))))})
@@ -220,6 +221,36 @@
220221
;; TODO handling missing edges
221222
(recur new-state ((get-in graph [:edges node]) new-state)))))))
222223

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+
223254
; ============================================================
224255
; this is the graph we tend to use in our experiments thus far
225256
; ============================================================
@@ -238,6 +269,17 @@
238269
(add-edge "tools-query" "completion")
239270
(add-conditional-edges "completion" tool-or-end)))
240271

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+
241283
(defn one-tool-call [_]
242284
(-> {}
243285
(add-node "start" start)
@@ -250,6 +292,16 @@
250292
(add-edge "tool" "end")
251293
(add-conditional-edges "completion" tool-or-end)))
252294

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+
253305
(comment
254306
(alter-var-root #'jsonrpc/notify (fn [_] (partial jsonrpc/-println {:debug true})))
255307
(let [x {:prompts (fs/file "/Users/slim/docker/labs-ai-tools-for-devs/prompts/curl/README.md")

src/graphs/sql.clj

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,19 @@
106106
(update-in [:messages] concat [(last (:messages state))])))
107107

108108
(defn graph [_]
109-
(-> {}
110-
(graph/add-node "start" graph/start)
111-
(graph/add-node "list-tables-inject-tool" list-tables-inject-tool)
112-
(graph/add-edge "start" "list-tables-inject-tool")
109+
(graph/construct-graph
110+
[[["start" graph/start]
111+
["list-tables-inject-tool" list-tables-inject-tool]
112+
["list-tables-tool" (graph/tool-node {})]
113+
["model-get-schema" (graph/sub-graph-node
114+
{:init-state seed-get-schema-conversation
115+
:next-state graph/append-new-messages})]
116+
["query-gen" query-gen]
117+
[:edge should-continue]]
118+
[["correct-query" (graph/sub-graph-node
119+
{:init-state seed-correct-query-conversation
120+
:construct-graph graph/one-tool-call
121+
:next-state graph/append-new-messages})]
122+
["query-gen"]]
123+
[["end" graph/end]]]))
113124

114-
(graph/add-node "list-tables-tool" (graph/tool-node nil))
115-
(graph/add-edge "list-tables-inject-tool" "list-tables-tool")
116-
117-
; TODO replace the conversation state, don't append
118-
(graph/add-node "model-get-schema" (graph/sub-graph-node {:init-state seed-get-schema-conversation
119-
:next-state graph/append-new-messages})) ; assistant
120-
(graph/add-edge "list-tables-tool" "model-get-schema")
121-
122-
(graph/add-node "query-gen" query-gen)
123-
(graph/add-edge "model-get-schema" "query-gen")
124-
125-
(graph/add-node "end" graph/end)
126-
(graph/add-node "correct-query" (graph/sub-graph-node {:init-state seed-correct-query-conversation
127-
:construct-graph graph/one-tool-call
128-
:next-state graph/append-new-messages}))
129-
(graph/add-conditional-edges "query-gen" should-continue)
130-
131-
(graph/add-edge "correct-query" "query-gen")))

test/graph_t.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(ns graph_t
2+
(:require [clojure.test :as t]
3+
[graph]
4+
[graphs.sql]))
5+
6+
(t/deftest
7+
(graph/construct-graph graph/chat-with-tools-representation)
8+
(graph/construct-graph graphs.sql/graph-data))

0 commit comments

Comments
 (0)