Skip to content

jovylle/context-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Context API

Context API is a small FastAPI backend for memory-backed chat. It keeps recent conversation, durable memories, and rolling summaries in SQLite, then injects only the most relevant context into each model call.

V1 features

  • POST /chat for authenticated chat with bounded memory retrieval
  • POST /remember for explicit memory storage
  • GET /sessions/{id} for chat history and current rolling summary
  • GET /health for deployment checks
  • API-key auth backed by the database, with an environment bootstrap key for day-one setup

Stack

  • Python 3.11+
  • FastAPI
  • SQLAlchemy + SQLite
  • OpenAI
  • Docker Compose for Mac mini deployment

Local development

  1. Clone the repo and enter it.
  2. Copy the environment file:
cp .env.example .env
  1. Set at least:
  • CONTEXT_API_OPENAI_API_KEY
  • CONTEXT_API_BOOTSTRAP_API_KEY
  1. Install dependencies:
python -m pip install -e ".[dev]"
  1. Run the API:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Run tests

pytest

API usage

Health check

curl http://127.0.0.1:8000/health

Store a memory

curl -X POST http://127.0.0.1:8000/remember \
  -H "Content-Type: application/json" \
  -H "X-API-Key: change-me" \
  -d '{
    "content": "our domain is context-api.uft1.com",
    "kind": "fact",
    "project_tag": "launch",
    "tags": ["domain"],
    "importance": 3
  }'

Chat

curl -X POST http://127.0.0.1:8000/chat \
  -H "Content-Type: application/json" \
  -H "X-API-Key: change-me" \
  -d '{
    "message": "What domain should I use for the API?",
    "project_tag": "launch",
    "title": "Launch prep"
  }'

Frontend example

const response = await fetch("https://context-api.uft1.com/chat", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-API-Key": import.meta.env.VITE_CONTEXT_API_KEY,
  },
  body: JSON.stringify({
    message: "Continue my launch plan",
    session_id,
    project_tag: "launch",
  }),
});

const data = await response.json();

Memory model

  • Recent chat: only the last configured N messages are considered for replay
  • Durable memories: explicit facts plus light heuristic extraction from user messages
  • Rolling summaries: older session history is compressed once the message count passes a threshold

The prompt builder enforces separate budgets for summary, memory, and history before the final request goes to OpenAI.

Mac mini deployment

Docker Compose

git clone git@github.com:jovylle/context-api.git
cd context-api
cp .env.example .env
docker compose up -d --build

The container listens on 127.0.0.1:8000, which is ideal for pairing with Cloudflare Tunnel.

Cloudflare Tunnel shape

Use a cloudflared ingress rule like this:

tunnel: your-tunnel-id
credentials-file: /Users/you/.cloudflared/your-tunnel-id.json

ingress:
  - hostname: context-api.uft1.com
    service: http://127.0.0.1:8000
  - service: http_status:404

Then run cloudflared as a persistent service on the Mac mini. Your external traffic flow becomes:

client -> Cloudflare -> context-api.uft1.com -> localhost:8000 -> Context API

Future-ready parts

  • Swap SQLite for Postgres and keep the route/service shape
  • Replace the simple memory ranking with embeddings later
  • Add more providers behind the LLMService
  • Add worker jobs and tool execution without changing the public chat contract

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors