Skip to content

Commit 4467424

Browse files
Add docs
* improve a few error messages * test empty prompts
1 parent e6f5100 commit 4467424

File tree

12 files changed

+128
-164
lines changed

12 files changed

+128
-164
lines changed

README.md

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,8 @@ OpenAI API compatiable LLM's already support function calling. This is our workb
3434
- work across a wider range of environments
3535
- operate in a sandboxed environment
3636

37-
3837
# Get Started
3938

40-
## Basics: Render a Prompt
41-
42-
To generate prompts for a project, clone a repo into `$PWD` and run the
43-
following command.
44-
45-
```sh
46-
docker run --rm \
47-
--pull=always \
48-
-v /var/run/docker.sock:/var/run/docker.sock \
49-
--mount type=volume,source=docker-prompts,target=/prompts \
50-
vonwig/prompts:latest --host-dir $PWD \
51-
--user jimclark106 \
52-
--platform darwin \
53-
--prompts-dir "github:docker/labs-make-runbook?ref=main&path=prompts/lazy_docker"
54-
```
55-
56-
The four arguments are
57-
* `project root dir on host`,
58-
* `docker login username`,
59-
* `platform`,
60-
* `github ref` for prompt files.
61-
62-
If you need to test prompts before you push, you can bind a local prompt directory instead of using
63-
a GitHub ref. For example, to test some local prompts in a directory named `my_prompts`, run:
64-
65-
```sh
66-
docker run --rm \
67-
--pull=always \
68-
-v /var/run/docker.sock:/var/run/docker.sock \
69-
--mount type=bind,source=$PWD,target=/app/my_prompts \
70-
--workdir /app \
71-
vonwig/prompts:latest --host-dir $PWD \
72-
--user jimclark106 \
73-
--platform darwin \
74-
--prompts-dir my_prompts
75-
```
76-
7739
## Running a Conversation Loop
7840

7941
Set OpenAI key
@@ -82,51 +44,23 @@ echo $OPENAI_API_KEY > $HOME/.openai-api-key
8244
```
8345
Run
8446
```sh
85-
docker run --rm \
86-
-it \
87-
-v /var/run/docker.sock:/var/run/docker.sock \
88-
--mount type=volume,source=docker-prompts,target=/prompts \
89-
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
90-
vonwig/prompts:latest \
91-
run \
92-
--host-dir $PWD \
93-
--user $USER \
94-
--platform "$(uname -o)" \
95-
--prompts "github:docker/labs-githooks?ref=main&path=prompts/git_hooks"
96-
```
97-
98-
### Running a Conversation Loop with Local Prompts
99-
100-
If you want to run a conversation loop with local prompts then you need to think about two different directories, the one that the root of your project ($PWD above),
101-
and the one that contains your prompts (let's call that $PROMPTS_DIR). Here's a command line for running the prompts when our $PWD is the project root and we've set the environment variable
102-
$PROMPTS_DIR to point at the directory containing our prompts.
103-
104-
Set OpenAI key
105-
```sh
106-
echo $OPENAI_API_KEY > $HOME/.openai-api-key
107-
```
108-
Run
109-
```sh
110-
docker run --rm \
111-
-it \
112-
-v /var/run/docker.sock:/var/run/docker.sock \
113-
--mount type=bind,source=$PROMPTS_DIR,target=/app/my_prompts \
114-
--workdir /app \
115-
--mount type=volume,source=docker-prompts,target=/prompts \
116-
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
117-
vonwig/prompts:latest \
118-
run \
119-
--host-dir $PWD \
120-
--user $USER \
121-
--platform "$(uname -o)" \
122-
--prompts-dir my_prompts
123-
```
124-
125-
## GitHub refs
126-
127-
Prompts can be fetched from a GitHub repository when using the `--prompts` arg. The mandatory parts of the ref are `github:{owner}/{repo}`
128-
but optional `path` and `ref` can be added to pull prompts from branches, and to specify a subdirectory
129-
where the prompt files are located in the repo.
47+
docker run
48+
--rm \
49+
--pull=always \
50+
-it \
51+
-v /var/run/docker.sock:/var/run/docker.sock \
52+
--mount type=volume,source=docker-prompts,target=/prompts \
53+
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
54+
vonwig/prompts:latest \
55+
run \
56+
--host-dir $PWD \
57+
--user $USER \
58+
--platform "$(uname -o)" \
59+
--prompts "github:docker/labs-githooks?ref=main&path=prompts/git_hooks"
60+
```
61+
62+
See [docs](https://vonwig.github.io/prompts.docs/#/page/running%20the%20prompt%20engine) for more details on how to run the conversation loop,
63+
and especially how to use it to run local prompts that are not yet in GitHub.
13064

13165
## Function volumes
13266

prompts/hub/default.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# prompt user
3+
4+
What is the meaning of life?

prompts/hub/figlet.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
---
2-
functions:
2+
tools:
33
- name: figlet
44
---
55

66
# Prompt user
77

8-
Run figlet on the message `Docker`.
9-
10-
# Result
11-
12-
run a man page
8+
Read the figlet man page.
9+
Next, run figlet on the message `Docker` using the font future.
1310

prompts/hub/fzf.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
functions:
2+
tools:
33
- name: fzf
44
---
55

@@ -13,18 +13,11 @@ searching your shell history.
1313
* `Alt-c` to switch directories
1414
* `Ctrl-r` to search command history
1515

16-
# Example user
16+
# Prompt user
1717

1818
Read the man page for `fzf` and summarize what it does.
1919

2020
# Prompt user
2121

2222
Use fzf to search the /project folder for any files that contain the word 'Prompt'
2323

24-
# Result
25-
26-
run a man page
27-
28-
what is fzf for?
29-
30-

prompts/hub/toilet.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
---
2-
functions:
2+
tools:
33
- name: toilet
44
---
55

66
# Prompt user
77

88
First run the man page for toilet.
99
After that, run the toilet command with the font `helvetica` and write the message "Hello from Docker"
10-
11-
# Result
12-
13-
run a man page

prompts/hub/typos.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
```
2-
funcions:
2+
tools:
33
- name: typos
44
```
55

66
# Prompt user
77

8-
Use the the `typos` command to find typos in the following sentence.
8+
Use the `typos` command to find typos in the following sentence.
99

1010
```
1111
Hella my nome is Jim.
1212
```
13-
14-
# Result
15-
16-
The AI should know how to use this command.

prompts/qrencode/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
---
2-
tool_choice: auto
3-
functions:
2+
tools:
43
- name: qrencode
54
---
65

76
# Prompt user
87

9-
Generate a QR code for the content `https://github.com/docker/labs-ai-tools-for-dev`. Save the generated image to `qrcode.png`.
8+
Generate a QR code for the content
9+
'https://raw.githubusercontent.com/docker/labs-ai-tools-for-devs/main/prompts/qrencode/README.md'.
10+
Save the generated image to `qrcode.png`.
1011
If the command fails, read the man page and try again.
11-
12-
# Result
13-
14-
This function generates a QR code for a URL. The QR code is saved as a PNG file.

runbook.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,21 @@ bb -m prompts run \
185185
#### Using Containerized runner
186186

187187
```sh
188-
docker run --rm \
189-
-it \
190-
-v /var/run/docker.sock:/var/run/docker.sock \
191-
--mount type=bind,source=$PWD,target=/app/local \
192-
--workdir /app \
193-
--mount type=volume,source=docker-prompts,target=/prompts \
194-
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
195-
vonwig/prompts:local \
196-
run \
197-
--host-dir /Users/slim/docker/labs-make-runbook \
198-
--user jimclark106 \
199-
--platform "$(uname -o)" \
200-
--prompts-dir local/prompts/dockerfiles
188+
docker run
189+
--rm \
190+
--pull=always \
191+
-it \
192+
-v /var/run/docker.sock:/var/run/docker.sock \
193+
--mount type=bind,source=$PWD,target=/app/local \
194+
--workdir /app \
195+
--mount type=volume,source=docker-prompts,target=/prompts \
196+
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
197+
vonwig/prompts:local \
198+
run \
199+
--host-dir /Users/slim/docker/labs-make-runbook \
200+
--user jimclark106 \
201+
--platform "$(uname -o)" \
202+
--prompts-dir local/prompts/dockerfiles
201203
```
202204

203205
### Clean up local images

src/docker.clj

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[babashka.curl :as curl]
44
[cheshire.core :as json]
5+
[clojure.core.async :as async]
56
[clojure.pprint :refer [pprint]]
67
[clojure.spec.alpha :as spec]
78
[clojure.string :as string]
@@ -195,24 +196,38 @@
195196
(spec/def ::container-definition (spec/keys :opt-un [::host-dir ::entrypoint ::command ::user ::pat]
196197
:req-un [::image]))
197198

198-
(defn run-function [m]
199+
(defn run-function [{:keys [timeout] :or {timeout 600000} :as m}]
199200
(pull (merge m
200201
{:serveraddress "https://index.docker.io/v1/"}
201202
(let [jwt (creds/credential-helper->jwt)]
202203
(when (and (:user m)
203204
(or (:pat m) jwt))
204205
{:creds {:username (:user m)
205206
:password (or (:pat m) jwt)}}))))
206-
(let [x (create m)]
207+
(let [x (create m)
208+
finished-channel (async/promise-chan)]
207209
(start x)
208-
(wait x)
210+
;; timeout process
211+
(async/go
212+
(async/<! (async/timeout timeout))
213+
(async/>! finished-channel {:timeout timeout
214+
:done :timeout
215+
:kill-container (kill-container x)}))
216+
;; watch the container
217+
(async/go
218+
(wait x)
219+
(async/>! finished-channel {:done :exited}))
220+
209221
;; body is raw PTY output
210-
(let [s (:body (attach x))
222+
(let [finish-reason (async/<!! finished-channel)
223+
s (:body (attach x))
211224
info (inspect x)]
212225
(delete x)
213-
{:pty-output s
214-
:exit-code (-> info :State :ExitCode)
215-
:info info})))
226+
(merge
227+
finish-reason
228+
{:pty-output s
229+
:exit-code (-> info :State :ExitCode)
230+
:info info}))))
216231

217232
(def extract-facts run-function)
218233

@@ -279,10 +294,12 @@
279294
:host-dir "/Users/slim/docker/genai-stack"
280295
:user "jimclark106")) keyword))
281296
(docker/delete-image {:image "vonwig/go-linguist:latest"})
282-
(extract-facts {:image "vonwig/go-linguist:latest"
283-
:command ["-json"]
284-
:host-dir "/Users/slim/docker/labs-make-runbook"
285-
:user "jimclark106"})
297+
(pprint
298+
(extract-facts {:image "vonwig/go-linguist:latest"
299+
:timeout 100
300+
:command ["-json"]
301+
:host-dir "/Users/slim/docker/labs-make-runbook"
302+
:user "jimclark106"}))
286303
(pprint
287304
(json/parse-string
288305
(extract-facts

src/markdown.clj

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
(update 0 (fn [s] (.substring s (dec c1))))
2727
(update (dec (count lines)) (fn [s] (.substring s 0 (- c2 c1))))))))
2828

29-
;; headings that include the word Prompt
30-
(defn prompt-section? [content node]
31-
(string/includes?
32-
(-> node (nth 2) (nth 3) (nth 1) (from-range content))
33-
"Prompt"))
29+
(def prompt-pattern #"(?i)\s*prompt\s+(\w+)\s?")
3430

3531
(defn extract-role [s]
36-
(-> s (string/replace "Prompt" "") (string/trim)))
32+
(second
33+
(re-find prompt-pattern s)))
34+
35+
;; headings that include the word Prompt
36+
(defn prompt-section? [content node]
37+
(re-matches
38+
prompt-pattern
39+
(-> node (nth 2) (nth 3) (nth 1) (from-range content))))
3740

3841
(defn remove-first-line [s]
3942
(->> (string/split s #"\n")
@@ -45,7 +48,7 @@
4548
{:role
4649
(-> node (nth 2) (nth 3) (nth 1) (from-range content) (extract-role))
4750
:content
48-
(remove-first-line (from-range (nth node 1) content))} )
51+
(remove-first-line (from-range (nth node 1) content))})
4952

5053
(defn extract-prompts [content ast]
5154
(->>
@@ -57,7 +60,8 @@
5760
(map (partial node-content content))))
5861

5962
(defn parse-markdown [content]
60-
(let [x (docker/function-call-with-stdin
63+
(let [content (str content "\n# END\n\n")
64+
x (docker/function-call-with-stdin
6165
{:image "docker/lsp:treesitter"
6266
:content content})
6367
{s :pty-output} (async/<! (async/thread
@@ -71,7 +75,7 @@
7175
(comment
7276
(string/split content #"\n")
7377

74-
(def content (slurp "prompts/qrencode/README.md" ))
78+
(def content (slurp "prompts/qrencode/README.md"))
7579
(pprint (parse-markdown content))
7680

7781
(def t

0 commit comments

Comments
 (0)