Skip to content

Commit d88bcb6

Browse files
Switch prompt refs to github refs
1 parent 5cfeee8 commit d88bcb6

File tree

11 files changed

+242
-181
lines changed

11 files changed

+242
-181
lines changed

deps-lock.json

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@
7676
"hash": "sha256-LaaIDz3x3lGTb/GfarGHaT5MAc3MVCiFWE5Z85qs6z4="
7777
},
7878
{
79-
"mvn-path": "clj-commons/clj-yaml/1.0.26/clj-yaml-1.0.26.jar",
79+
"mvn-path": "clj-commons/clj-yaml/1.0.28/clj-yaml-1.0.28.jar",
8080
"mvn-repo": "https://repo.clojars.org/",
81-
"hash": "sha256-d5eTQ8uSCabGzgSCJiW+B6j4+tWfxTmbpXhzN6fYpTs="
81+
"hash": "sha256-QQlD2Qyz5bEzvYqZ4xDrVwZKUE5+642sFrLJi5C3lCw="
8282
},
8383
{
84-
"mvn-path": "clj-commons/clj-yaml/1.0.26/clj-yaml-1.0.26.pom",
84+
"mvn-path": "clj-commons/clj-yaml/1.0.28/clj-yaml-1.0.28.pom",
8585
"mvn-repo": "https://repo.clojars.org/",
86-
"hash": "sha256-89d/qo4tN1NX89YB/1Rr2FnUfeMR/Hpi2q1yHmmwO9I="
86+
"hash": "sha256-Un5S4+y7Wzi660rKZ65rPbBNDsImViNc16wLE6FezmU="
8787
},
8888
{
8989
"mvn-path": "com/cognitect/aws/api/0.8.612/api-0.8.612.jar",
@@ -450,16 +450,6 @@
450450
"mvn-repo": "https://repo.maven.apache.org/maven2/",
451451
"hash": "sha256-Rm9ue5kB6VKuGtJPDpSUpWgJYfBOQiObWuZOjcr2KXo="
452452
},
453-
{
454-
"mvn-path": "markdown-clj/markdown-clj/1.12.1/markdown-clj-1.12.1.jar",
455-
"mvn-repo": "https://repo.clojars.org/",
456-
"hash": "sha256-WQa0wuwux6osu79nZasLWJIG7AOAk1s2SClZZqhL7L4="
457-
},
458-
{
459-
"mvn-path": "markdown-clj/markdown-clj/1.12.1/markdown-clj-1.12.1.pom",
460-
"mvn-repo": "https://repo.clojars.org/",
461-
"hash": "sha256-dm4mOOdVHfiYd8zVKAxtkKf9L5AcewxrTbOl39KPPI4="
462-
},
463453
{
464454
"mvn-path": "net/java/dev/jna/jna-platform/5.15.0/jna-platform-5.15.0.jar",
465455
"mvn-repo": "https://repo.maven.apache.org/maven2/",
@@ -1401,14 +1391,14 @@
14011391
"hash": "sha256-broJAu/Yma7A2NGaw8vFMSPNQROf4OHSnMXIdKeRud4="
14021392
},
14031393
{
1404-
"mvn-path": "org/flatland/ordered/1.5.9/ordered-1.5.9.jar",
1394+
"mvn-path": "org/flatland/ordered/1.15.12/ordered-1.15.12.jar",
14051395
"mvn-repo": "https://repo.clojars.org/",
1406-
"hash": "sha256-Vl9mSXhByNY9CbtLueqzabUl/HQwGGVRIPowqS+uq5Q="
1396+
"hash": "sha256-NwD9NzndCSj5SRi3EEWl+IxD+Vr+Z+GjVeuGPi+ZJeA="
14071397
},
14081398
{
1409-
"mvn-path": "org/flatland/ordered/1.5.9/ordered-1.5.9.pom",
1399+
"mvn-path": "org/flatland/ordered/1.15.12/ordered-1.15.12.pom",
14101400
"mvn-repo": "https://repo.clojars.org/",
1411-
"hash": "sha256-wF56Bv8RMDM5GzVmk6rhLh6auW0QbXEXwKqVaO7MYo0="
1401+
"hash": "sha256-vWUBX+NY7K6ZF+LJ46oSAAY6ika0F5KVjRo1R13H8Oo="
14121402
},
14131403
{
14141404
"mvn-path": "org/fressian/fressian/0.6.6/fressian-0.6.6.jar",
@@ -1546,14 +1536,14 @@
15461536
"hash": "sha256-UGG6hMmFNuWmtM4oD7zssA4zXzsExdSEYpFi/LRiR3g="
15471537
},
15481538
{
1549-
"mvn-path": "org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar",
1539+
"mvn-path": "org/yaml/snakeyaml/2.3/snakeyaml-2.3.jar",
15501540
"mvn-repo": "https://repo.maven.apache.org/maven2/",
1551-
"hash": "sha256-Ef9Fl4jwoteB9WpKhtfmkgLOus0Cc9UmnErp8C8/2PA="
1541+
"hash": "sha256-Y6dv5mtlI2C9TCwQfm8CWNqn1LtJIAi6jCb80jD/kUY="
15521542
},
15531543
{
1554-
"mvn-path": "org/yaml/snakeyaml/1.33/snakeyaml-1.33.pom",
1544+
"mvn-path": "org/yaml/snakeyaml/2.3/snakeyaml-2.3.pom",
15551545
"mvn-repo": "https://repo.maven.apache.org/maven2/",
1556-
"hash": "sha256-6n1I/UUyGmAz2XzSiBhtSOXpLMDHBm5ziNfEzrSvWVc="
1546+
"hash": "sha256-D1omWgYzGwBJ41K+MsoyLeGLF/PU27cGNdQNppLjWC8="
15571547
},
15581548
{
15591549
"mvn-path": "pogonos/pogonos/0.2.1/pogonos-0.2.1.jar",

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{:paths ["src" "dev"]
22
:deps {org.clojure/clojure {:mvn/version "1.11.4"}
33
org.clojure/core.match {:mvn/version "1.1.0"}
4-
markdown-clj/markdown-clj {:mvn/version "1.12.1"}
4+
clj-commons/clj-yaml {:mvn/version "1.0.28"}
55
pogonos/pogonos {:mvn/version "0.2.1"}
66
dev.weavejester/medley {:mvn/version "1.8.0"}
77
io.replikativ/hasch {:mvn/version "0.3.94"}

graphs/prompts/journals/2024_09_03.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@
7070
- find
7171
- .
7272
- -name
73-
model: llama3.1
74-
url: http://localhost/v1/chat/completions
75-
stream: false
73+
model: llama3.1 # override model
74+
url: http://localhost/v1/chat/completions # override model runner
75+
stream: false # disable streaming
76+
host-dir: /Users/slim/vonwig/altaservice # override host-dir for testing
77+
timeout: 60000 # timeout for tools used in this prompt
7678
---
7779
7880
# Prompt system

prompts/sql/prompt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
agent: sql
3+
host-dir: /Users/slim/docker/labs-ai-tools-for-devs/prompts/sql # override host-dir while testing
34
---
45

56
# prompt user

src/docker/main.clj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,20 @@
178178
(user-loop/create-step
179179
(fn [state]
180180
(let [m (state/construct-initial-state-from-prompts
181-
(assoc state :opts
182-
(-> (with-options opts (rest args))
183-
(assoc :thread-id thread-id))))]
181+
(assoc state :opts
182+
(-> (with-options opts (rest args))
183+
(assoc :thread-id thread-id))))]
184184
(graph/stream
185-
(if (= (-> m :metadata :agent) "sql")
186-
(graphs.sql/graph state)
187-
(graph/chat-with-tools state))
185+
(if (= (-> m :metadata :agent) "sql")
186+
(graphs.sql/graph state)
187+
(graph/chat-with-tools state))
188188
m))))
189189
user-loop/state-reducer
190190
in
191191
{})))
192192
opts)))
193193
(fn []
194-
(prompts/get-prompts (with-options opts args)))))
194+
(:messages (prompts/get-prompts (with-options opts args))))))
195195

196196
(defn -main [& args]
197197
(try

src/graph.clj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
[{:keys [messages functions metadata] {:keys [url model stream level]} :opts}]
3131
(let [[c h] (openai/chunk-handler)
3232
request (merge
33-
(dissoc metadata :agent)
33+
(dissoc metadata :agent :host-dir) ; TODO should we just select relevant keys instead of removing bad ones
3434
{:messages messages
3535
:level level}
3636
(when (seq functions) {:tools functions})
37+
;; overrides from cli opts, NOT from metadata
3738
(when url {:url url})
3839
(when model {:model model})
39-
;; stream is a special case where we don't want to overwrite the metadata
40+
;; stream is a special case where we don't want to allow override of the metadata val if the cli val is set
4041
(when (and stream (nil? (:stream metadata))) {:stream stream}))]
4142
(try
4243
(if (seq messages)
@@ -82,8 +83,15 @@
8283
(into []
8384
(async/<!
8485
(->> (tools/make-tool-calls
85-
(or (-> state :opts :level) 0)
86-
(partial tools/function-handler (assoc (:opts state) :functions (:functions state)))
86+
(or (-> state :opts :level) 0)
87+
(partial
88+
tools/function-handler
89+
;; defaults for tool handling are opts, current state functions, and a host-dir override
90+
(merge
91+
(:opts state)
92+
(select-keys state [:functions])
93+
;; note that host-dir, if it exists, is an override here
94+
(select-keys (:metadata state) [:host-dir :timeout])))
8795
calls)
8896
(async/reduce conj []))))})))
8997

@@ -115,11 +123,13 @@
115123
((or construct-graph chat-with-tools) state)
116124
(->
117125
((or
126+
;; the sub-graph might have a function or a vector of state overlays to apply
118127
(and
119128
init-state
120129
(if (coll? init-state)
121130
(apply-functions init-state)
122131
init-state))
132+
;; default is to assume there's a tool call with a function that contains a prompt
123133
(comp state/construct-initial-state-from-prompts state/add-prompt-ref)) state)
124134
(update-in [:opts :level] (fnil inc 0)))))]
125135
((or next-state state/add-last-message-as-tool-call) state sub-graph-state)))))

src/graphs/sql.clj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
(let [x ((graph/apply-functions
5050
[(state/messages-reset)
5151
(state/tools-reset)
52-
(state/messages-from-prompt "prompts/sql/query-gen.md")
52+
(state/messages-from-prompt "github:docker/labs-ai-tools-for-devs?path=prompts/sql/query-gen.md")
5353
(state/messages-append-all)]) state)
5454
{:keys [messages _finish-reason]} (async/<! (graph/run-llm x))]
5555

@@ -75,8 +75,6 @@
7575
;; how many times should we try to correct because correct-query will always end up back here
7676
:else "correct-query")))
7777

78-
;; query-gen has a prompt
79-
;; seed-correct-query-conversation has a prompt
8078
(defn graph [_]
8179
(graph/construct-graph
8280
[[["start" graph/start]
@@ -96,7 +94,7 @@
9694
[["correct-query" (graph/sub-graph-node
9795
{:init-state
9896
[(state/messages-reset)
99-
(state/messages-from-prompt "prompts/sql/query-check.md")
97+
(state/messages-from-prompt "github:docker/labs-ai-tools-for-devs?path=prompts/sql/query-check.md")
10098
(state/messages-take 1)
10199
(state/messages-take-last 1)]
102100
:construct-graph graph/generate-one-tool-call

src/markdown.clj

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns markdown
22
(:require
3+
[cheshire.core :as json]
4+
[clj-yaml.core :as clj-yaml]
35
[clojure.core.async :as async]
46
[clojure.edn :as edn]
57
[clojure.pprint :refer [pprint]]
@@ -61,18 +63,98 @@
6163
(filter (partial prompt-section? content))
6264
(map (partial node-content content))))
6365

64-
(defn parse-markdown [content]
66+
(defn metadata-section? [loc]
67+
(= "minus_metadata" (-> loc (zip/node) first)))
68+
69+
(defn remove-markers [s]
70+
(and s (when-let [[_ x] (re-find (re-pattern "(?sm).*---(.*)---.*") s)] x)))
71+
72+
(defn extract-metadata [content ast]
73+
(try
74+
(when-let [loc (->>
75+
(iterate zip/next (zip/seq-zip ast))
76+
(take-while (complement zip/end?))
77+
(some (fn [loc] (when (metadata-section? loc) loc))))]
78+
(->
79+
(from-range (-> loc zip/node second) content)
80+
(remove-markers)
81+
(clj-yaml/parse-string)))
82+
(catch Throwable _ nil)))
83+
84+
(defn html-comment? [loc]
85+
(and
86+
(= "html_block" (-> loc (zip/node) first))
87+
(= "-->" (-> loc (zip/children) last first))))
88+
89+
(defn extract-first-comment [content ast]
90+
(try
91+
(when-let [loc (->>
92+
(iterate zip/next (zip/seq-zip ast))
93+
(take-while (complement zip/end?))
94+
(some (fn [loc] (when (html-comment? loc) loc))))]
95+
(->
96+
(from-range (-> loc zip/node second) content)
97+
(remove-markers)
98+
(clj-yaml/parse-string)))
99+
(catch Throwable ex
100+
(println ex)
101+
nil)))
102+
103+
(defn parse-new [content query]
65104
(let [content (str content "\n# END\n\n")
66105
x (docker/function-call-with-stdin
106+
{:image "vonwig/tree-sitter:latest"
107+
:content content
108+
:command (concat
109+
["-lang" "markdown"]
110+
["-query" query])})
111+
{s :pty-output} (async/<!! (async/thread
112+
(Thread/sleep 10)
113+
(docker/finish-call x)))]
114+
(->> s)))
115+
116+
(comment
117+
; TODO - migrate to tree-sitter queries but can we express this with tree-sitter
118+
(parse-new (slurp "./tprompt1.md") "(document) @doc")
119+
(json/parse-string (parse-new (slurp "./tprompt1.md") "(document (minus_metadata) @doc)"))
120+
(json/parse-string (parse-new (slurp "./tprompt1.md") "(document (section (html_block) @html))"))
121+
(json/parse-string (parse-new (slurp "./tprompt1.md") "(document (section (atx_heading (atx_h1_marker)))* @top-section)")))
122+
123+
(defn parse-markdown
124+
"use the custom sexp representation"
125+
[content]
126+
(let [x (docker/function-call-with-stdin
67127
{:image "docker/lsp:treesitter"
68128
:content content})
69129
{s :pty-output} (async/<!! (async/thread
70130
(Thread/sleep 10)
71131
(docker/finish-call x)))]
72-
(->> s
73-
(edn/read-string)
74-
(extract-prompts content)
75-
(into []))))
132+
(->> (edn/read-string s))))
133+
134+
(defn parse-prompts
135+
"parse out the h1 prompt sections"
136+
[content]
137+
(let [content (str content "\n# END\n\n")
138+
ast (parse-markdown content)]
139+
{:messages
140+
(->> ast
141+
(extract-prompts content)
142+
(into []))
143+
:metadata (or
144+
(extract-metadata content ast)
145+
(extract-first-comment content ast)) }))
146+
147+
(comment
148+
; inline same line !,[,],(,) in that order after filtering out other irrelevant things
149+
; ^ those are imgages and the content between the [ ] should be put into a separate message
150+
; the first minus_metadata block of the doc
151+
; the first html_block section that ends with -->
152+
; get content and then check of --- --- pre-amble
153+
; then try to parse the yaml out of that
154+
(parse-markdown (slurp "./tprompt2.md"))
155+
(parse-prompts (slurp "./tprompt1.md"))
156+
(parse-prompts (slurp "./tprompt2.md"))
157+
)
76158

77159
(comment
78160
(string/split content #"\n")

0 commit comments

Comments
 (0)