Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
slimslenderslacks committed Jul 10, 2024
0 parents commit 7bc4a40
Show file tree
Hide file tree
Showing 86 changed files with 3,335 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/prompts-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build and Push Prompts Docker Image

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.TEMP_DOCKER_USERNAME }}
password: ${{ secrets.TEMP_DOCKER_PAT }}

- name: Build and push
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
push: true
tags: vonwig/prompts:latest
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/.direnv/
**/result
.clj-kondo
.lsp
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM babashka/babashka:latest@sha256:4bc4beea38406782845ae8effaa9bd2f45345d46a4290ea4c96037970a0ca430 AS bb

FROM bb AS base

RUN <<EOF
apt-get update
apt-get install -y git
EOF

FROM eclipse-temurin:latest@sha256:ac1545309de7e27001a80d91df2d42865c0bacaec75e016cb4482255d7691187 AS build

WORKDIR /app

COPY --from=bb /usr/local/bin/bb /usr/local/bin/bb
COPY bb.edn bb.edn
COPY ./src ./src
RUN bb uberjar prompts.jar -m prompts

FROM base

WORKDIR /app

COPY --from=build /app/prompts.jar /app/prompts.jar

COPY prompts/docker docker
COPY prompts/npm_setup npm_setup
COPY prompts/lazy_docker lazy_docker

# Can't be shell form because we need to pass JSON as an arg
ENTRYPOINT [ "bb", "/app/prompts.jar" ]
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

## Running Prompts

To generate prompts for a project, clone a repo into `$PWD` and run the
following command.

```sh
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
--mount type=volume,source=docker-prompts,target=/prompts \
vonwig/prompts:latest $PWD \
jimclark106 \
darwin \
"github:docker/labs-make-runbook?ref=main&path=prompts/lazy_docker"
```

The four arguments are
* `project root dir on host`,
* `docker login username`,
* `platform`,
* `github ref` for prompt files.

If you need to test prompts before you push, you can bind a local prompt directory instead of using
a GitHub ref. For example, to test some local prompts in a directory named `my_prompts`, run:

```sh
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
--mount type=bind,source=$PWD,target=/app/my_prompts \
--workdir /app
vonwig/prompts:latest $PWD \
jimclark106 \
darwin \
my_prompts
```

## Running a Conversation Loop

```sh
docker run --rm \
-it \
-v /var/run/docker.sock:/var/run/docker.sock \
--mount type=volume,source=docker-prompts,target=/prompts \
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
vonwig/prompts:latest \
run \
$PWD \
$USER \
"$(uname -o)" \
"github:docker/labs-githooks?ref=main&path=prompts/git_hooks"
```

### Running a Conversation Loop with Local Prompts

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),
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
$PROMPTS_DIR to point at the directory containing our prompts.

```sh
docker run --rm \
-it \
-v /var/run/docker.sock:/var/run/docker.sock \
--mount type=bind,source=$PROMPTS_DIR,target=/app/my_prompts \
--workdir /app
--mount type=volume,source=docker-prompts,target=/prompts \
--mount type=bind,source=$HOME/.openai-api-key,target=/root/.openai-api-key \
vonwig/prompts:latest \
run \
$PWD \
$USER \
"$(uname -o)" \
my_prompts
```

## GitHub refs

Prompts are fetched from a GitHub repository. The mandatory parts of the ref are `github:{owner}/{repo}`
but optional `path` and `ref` can be added to pull prompts from branches, and to specify a subdirectory
where the prompt files are located in the repo.

### Prompt file layout

Each prompt directory should contain a README.md describing the prompts and their purpose. Each prompt file
is a markdown document that supports moustache templates for subsituting context extracted from the project.

```
prompt_dir/
├── 010_system_prompt.md
├── 020_user_prompt.md
└── README.md
```

* ordering of messages is determined by filename sorting
* the role is encoded in the name of the file

### Moustache Templates

The prompt templates can contain expressions like {{dockerfiles}} to add information
extracted from the current project. Examples of facts that can be added to the
prompts are:

* `{{platform}}` - the platform of the current development environment.
* `{{username}}` - the DockerHub username (and default namespace for image pushes)
* `{{languages}}` - names of languages discovered in the project.
* `{{project.dockerfiles}}` - the relative paths to local DockerFiles
* `{{project.composefiles}}` - the relative paths to local Docker Compose files.

The entire `project-facts` map is also available using dot-syntax
forms like `{{project-facts.project-root-uri}}`. All moustache template
expressions documented [here](https://github.com/yogthos/Selmer) are supported.

## Building

```sh
#docker:command=build

docker build -t vonwig/prompts:local -f Dockerfile .
```

5 changes: 5 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{:paths ["src" "dev"]
:deps {markdown-clj/markdown-clj {:mvn/version "1.12.1"}
pogonos/pogonos {:mvn/version "0.2.1"}
dev.weavejester/medley {:mvn/version "1.8.0"}
io.replikativ/hasch {:mvn/version "0.3.94"}}}
35 changes: 35 additions & 0 deletions dev/clean_local_images.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns clean-local-images
(:require [docker]
[clojure.pprint :refer [pprint]]))

(def images
#{"vonwig/prompts:latest"
"vonwig/github-cli"
"vonwig/function_write_file"
"vonwig/docker_scout_tag_recommendation"
"vonwig/extractor-node"
"vonwig/go-linguist"
"vonwig/codescope"
"vonwig/pre-commit"
"markdownlint/markdownlint"
"hadolint/hadolint"})

(comment
(docker/delete-image {:image "vonwig/function_write_files"}))

(defn repo? [images]
(fn [tag-or-digest]
(some (fn [image] (.startsWith tag-or-digest image)) images)))

(defn -main []
(println
(->> (docker/images {})
#_(map #(concat (:RepoTags %) (:RepoDigests %)))
(filter #(some (repo? images) (concat (:RepoTags %) (:RepoDigests %))))
(map :Id)
(map #(docker/delete-image {:image %})))))

(comment
(pprint (docker/images {}))
)

95 changes: 95 additions & 0 deletions dev/ollama.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
(ns dev.ollama
(:require
[prompts]
[babashka.curl :as curl]
[babashka.process :as p]
[cheshire.core :as json]
[clojure.pprint :as pprint]
[selmer.parser :as selmer.parser]
[clojure.string :as string]))

(def x (p/process
{:out :string :err :string}
"docker"
"run"
"--rm"
"vonwig/prompts"
"{}" "jimclark106" "darwin"))

(def f "/Users/slim/docker/genai-stack/")

(def x (p/process
{:out :string :err :string}
"docker"
"run"
"--rm"
"--interactive"
"--init"
"--mount" "type=volume,source=docker-lsp,target=/docker"
"--mount" (format "type=bind,source=%s,target=/project" f)
"--entrypoint" "/app/result/bin/docker-lsp"
"docker/lsp:staging"
"project-facts"
"--workspace" "/docker"
"--vs-machine-id" "none"))

(def y (p/process
{:out :string :err :string :extra-env {"DOCKER_LSP" "nix"}}
"nix"
"run"
"/Users/slim/docker/lsp#clj"
"project-facts"
"--"
"--pod-exe-path" "/Users/slim/docker/babashka-pod-docker/result/bin/entrypoint"
"--workspace" "/Users/slim/.docker"
"--project-root" f
"--vs-machine-id" "none"))

(def project-facts
(-> y
(deref)
:out
(json/parse-string keyword)))

(pprint/pprint project-facts)

(prompts/prompts (json/generate-string (dissoc project-facts :project/dockerfiles)) "jimclark106" "darwin")

(defn ollama [messages]
(->
(curl/post "http://localhost:11434/v1/chat/completions"
{:body (json/generate-string
{:model "llama3"
:temperature 0.1
:messages messages})})
:body
(json/parse-string keyword)))

(def api-key "")

(defn openai [messages]
(->
(curl/post "https://api.openai.com/v1/chat/completions"
{:headers {"Authorization" (format "Bearer %s" api-key)
"Content-Type" "application/json"}
:throw false
:body (json/generate-string
{:model "gpt-4"
:temperature 0.1
:messages messages})})
:body
(json/parse-string keyword)))

(-> (with-out-str (prompts/prompts
(json/generate-string project-facts)
"jimclark106"
"darwin"))
(json/parse-string keyword)
(openai)
;:choices
;first
;:message
;:content
;println
)

1 change: 1 addition & 0 deletions functions/codescope/.direnv/flake-profile
1 change: 1 addition & 0 deletions functions/codescope/.direnv/flake-profile-1-link
1 change: 1 addition & 0 deletions functions/codescope/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
30 changes: 30 additions & 0 deletions functions/codescope/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# syntax = docker/dockerfile:1.4
FROM nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:latest@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM scratch

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/codescope"]

31 changes: 31 additions & 0 deletions functions/codescope/clipboard.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{ lib
, python3Packages
, fetchPypi
}:

with python3Packages;
buildPythonPackage rec {
pname = "clipboard";
version = "0.0.4";
pyproject = true;

src = fetchPypi {
inherit pname version;
hash = "sha256-pyp46cm/aNocPynuAiQX0T7J44JLURVZ/StwKx3VuBc=";
};

buildInputs = [
setuptools
pyperclip
];

propogatedBuildInputs = [pyperclip];

dependencies = [
setuptools
];

build-system = [
setuptools
];
}
Loading

0 comments on commit 7bc4a40

Please sign in to comment.