Skip to content

Instantly share code, notes, and snippets.

@korc
Last active October 14, 2024 09:28
Show Gist options
  • Save korc/084683a1f51ac95c2bb33be5c26e5909 to your computer and use it in GitHub Desktop.
Save korc/084683a1f51ac95c2bb33be5c26e5909 to your computer and use it in GitHub Desktop.
Start a simple dockerized stack of ollama / code-server. Run with `docker compose up` and access with browser via http://127.0.0.1:8080
CREATE EXTENSION IF NOT EXISTS plpython3u;
CREATE EXTENSION IF NOT EXISTS vector;
CREATE OR REPLACE FUNCTION ollama_embed(model text, prompt text[]) RETURNS SETOF vector
LANGUAGE plpython3u
AS $$
from http.client import HTTPConnection
import json
conn = HTTPConnection("ollama:11434")
conn.request("POST", "/api/embed", json.dumps(dict(model=model, input=prompt)))
response = conn.getresponse()
result = response.read().decode('utf-8')
return json.loads(result)["embeddings"]
$$;
CREATE OR REPLACE FUNCTION ollama_embed(model text, prompt text) RETURNS vector
LANGUAGE sql
AS $$
SELECT v from ollama_embed(model, ARRAY[prompt]) v
$$;
CREATE OR REPLACE FUNCTION ollama_list_models() RETURNS SETOF jsonb
LANGUAGE plpython3u
AS $$
from http.client import HTTPConnection
import json
conn = HTTPConnection("ollama:11434")
conn.request("GET", "/api/tags")
response = conn.getresponse()
result = response.read().decode('utf-8')
return [json.dumps(z) for z in json.loads(result)["models"]]
$$;
networks:
int:
## set net_internal=false in .env to allow direct internet access
internal: ${net_internal:-true}
volumes:
## set local_code in .env to local code directory if needed
code:
ollama:
sock:
pg_data:
services:
code-server:
build:
dockerfile: Dockerfile.code-server
volumes:
- ${local_code:-code}:/home/coder/Code
- sock:/run/sock
command:
- --auth
- none
- --socket
- /run/sock/cs.sock
networks:
- int
depends_on:
setup-volumes:
condition: service_completed_successfully
proxy:
condition: service_started
db:
build:
dockerfile: Dockerfile.db
volumes:
- pg_data:/var/lib/postgresql/data
networks:
- int
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_DB=vectordb
ollama:
build:
dockerfile: Dockerfile.ollama
volumes:
- ollama:/root/.ollama
ports: # share ollama instance for other (desktop) apps. comment out to disable.
- ${ollama_listen:-127.0.0.1:11434}:11434
networks: # internet access allowed by default for downloading models / shared usage. comment out 'default' if done.
- int
- default
## comment out if you don't have or can't use GPU
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
web-front:
image: korc/onefile-websrv
command:
- -listen
- :8080
- -map
- "/=http:unix:/run/sock/cs.sock:"
ports:
- 127.0.0.1:8080:8080
volumes:
- sock:/run/sock
setup-volumes:
build:
dockerfile: Dockerfile.setup-volumes
volumes:
- sock:/vol/sock
- code:/vol/code
proxy:
build:
dockerfile: Dockerfile.proxy
networks:
- int
- default
{
"models": [
{
"model": "AUTODETECT",
"title": "Ollama",
"provider": "ollama",
"apiBase": "http://ollama:11434"
}
],
"tabAutocompleteModel": {
"title": "StarCoder2-3b",
"model": "starcoder2:3b",
"provider": "ollama",
"apiBase": "http://ollama:11434"
},
"customCommands": [
{
"name": "test",
"prompt": "{{{ input }}}\n\nWrite a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.",
"description": "Write unit tests for highlighted code"
}
],
"contextProviders": [
{
"name": "code",
"params": {}
},
{
"name": "docs",
"params": {}
},
{
"name": "diff",
"params": {}
},
{
"name": "terminal",
"params": {}
},
{
"name": "problems",
"params": {}
},
{
"name": "folder",
"params": {}
},
{
"name": "codebase",
"params": {}
}
],
"slashCommands": [
{
"name": "edit",
"description": "Edit selected code"
},
{
"name": "comment",
"description": "Write comments for the selected code"
},
{
"name": "share",
"description": "Export the current chat session to markdown"
},
{
"name": "cmd",
"description": "Generate a shell command"
},
{
"name": "commit",
"description": "Generate a git commit message"
}
]
}
FROM codercom/code-server
RUN --mount=type=cache,target=/var/lib/apt/lists --mount=type=cache,target=/var/cache/apt/archives \
sudo apt-get update && \
sudo apt-get install -y --no-install-recommends \
acl iproute2 bash-completion command-not-found iputils-ping strace \
jq yq pgcli
RUN code-server \
--install-extension Continue.continue \
--install-extension dbcode.dbcode \
--install-extension mtxr.sqltools \
--install-extension mtxr.sqltools-driver-pg
COPY --chown=1000:1000 continue-config.json /home/coder/.continue/config.json
COPY --chown=1000:1000 user-settings.json /home/coder/.local/share/code-server/User/settings.json
RUN sudo apt update
FROM pgvector/pgvector:pg17
RUN --mount=type=cache,target=/var/lib/apt/lists --mount=type=cache,target=/var/cache/apt/archives \
apt-get update && \
apt-get install -y --no-install-recommends \
postgresql-plpython3-17
COPY 50-init-pgvec-ollama.sql /docker-entrypoint-initdb.d/
FROM ollama/ollama
ENTRYPOINT [ "sh", "-c", "ollama serve & pid=$! && ollama pull starcoder2:3b && ollama pull llama3.2 ; wait $pid" ]
FROM golang AS builder
WORKDIR /src
COPY proxy.go /src
RUN \
go mod init proxy && \
go mod tidy && \
CGO_ENABLED=0 go build -o /go/bin/proxy
FROM scratch
COPY --from=builder /go/bin/proxy /bin/
ENTRYPOINT [ "/bin/proxy" ]
FROM alpine
ENTRYPOINT [ "sh", "-c", "chown 1000:1000 /vol/sock /vol/code" ]
package main
import (
"github.com/elazarl/goproxy"
"log"
"net/http"
)
func main() {
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = true
log.Fatal(http.ListenAndServe(":3128", proxy))
}
CREATE TABLE IF NOT EXISTS texts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
t TEXT NOT NULL,
v vector
);
DELETE FROM texts;
INSERT INTO texts (t) VALUES
('PostgreSQL is an open-source relational database management system (RDBMS) that provides a powerful, scalable, and highly customizable platform for storing, managing, and querying data, offering features such as support for SQL, indexing, caching, and advanced security.'),
('The solar system consists of eight planets (Mercury, Mars, Venus, Earth, Neptune, Uranus, Saturn, and Jupiter), dwarf planets, asteroids, comets, and other smaller bodies orbiting around the Sun, a massive ball of hot, glowing gas at the center.'),
('Linux is an open-source, customizable, and highly secure operating system that provides a robust platform for various applications, devices, and use cases, allowing users to personalize and tailor it to their specific needs.');
-- this could take a while depending on your hardware..
UPDATE texts SET v = ollama_embed('llama3.2', t);
WITH q(qv) AS (SELECT ollama_embed('llama3.2', 'information on planets'))
SELECT qv<->v as l2, qv<=>v as cos, qv<+>v as l1, * FROM texts,q ORDER BY 1;
WITH q(qv) AS (SELECT ollama_embed('llama3.2', 'operating system'))
SELECT qv<->v as l2, qv<=>v as cos, qv<+>v as l1, * FROM texts,q ORDER BY 1;
WITH q(qv) AS (SELECT ollama_embed('llama3.2', 'database'))
SELECT qv<->v as l2, qv<=>v as cos, qv<+>v as l1, * FROM texts,q ORDER BY 1;
{
"terminal.integrated.allowChords": false,
"telemetry.telemetryLevel": "off",
"continue.telemetryEnabled": false,
"window.menuBarVisibility": "visible",
"json.schemaDownload.enable": false,
"debug.javascript.automaticallyTunnelRemoteServer": false,
"http.proxy": "http://proxy:3128",
"extensions.autoUpdate": false,
"extensions.autoCheckUpdates": false,
"remote.autoForwardPorts": false,
"remote.forwardOnOpen": false,
"dbcode.ai.inlineCompletion": false,
"dbcode.connections": [
{
"connectionId": "coZnS9fy7AtLZG8qy0Zdp",
"name": "db",
"driver": "postgres",
"connectionType": "host",
"host": "db",
"port": 5432,
"ssl": false,
"username": "postgres",
"password": "password",
"savePassword": "yes",
"database": "vectordb",
"connectionTimeout": 30
}
],
"sqltools.connections": [
{
"previewLimit": 50,
"server": "db",
"port": 5432,
"driver": "PostgreSQL",
"name": "db",
"database": "vectordb",
"username": "postgres",
"password": "password"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment