π³ Context-Tree Chat Service
A tree-structured conversational system.
Each conversation forms a rooted tree of chat nodesβfork any message, explore tangents without losing context, and replay any branch by climbing back to the root.
β¨ Features
- Session Management β create & list chat sessions (per-user API key).
- Branching Conversation β fork any node to start a tangent.
- LLM-Driven Replies β send user history to an LLM and store its reply as a child node.
- Tree Navigation β fetch the entire session or any branch as nested JSON.
- API-Key Authentication β protect all endpoints with per-user keys.
π Quick-start
Clone & install
git clone <YOUR_FORK_URL>
cd context-tree-service
poetry install
Run locally
poetry run uvicorn branched_backend.main:app --reload
π Authentication Flow
- User onboarding β frontend signs up / logs in via Clerk β receives session JWT.
- Generate an API key
bash curl -X POST http://localhost:8000/api/v1/keys/generate \ -H "Authorization: Bearer <CLERK_JWT>" - Call the API β include your key on every request:
π API Endpoints
All routes clients should call with cURL live under /api/v1/sessions and require Authorization: Bearer <API_KEY>.
| Category | Method | Path | Description |
|---|---|---|---|
| Sessions | POST | /sessions |
Create a new chat session |
| Sessions | GET | /sessions/{sid} |
Fetch session metadata & nodes |
| Branching | POST | /sessions/{sid}/branches |
Fork a branch under the given parent |
| Messaging | POST | /sessions/{sid}/branches/{bid}/msgs |
Append user message β LLM reply β save |
| Messaging | GET | /sessions/{sid}/branches/{bid}/msgs |
List all replies (children) for a branch |
π‘ cURL Walk-through
# 1. Create a session
curl -X POST http://localhost:8000/api/v1/sessions \
-H "Authorization: Bearer $API_KEY"
# 2. Fork a branch
curl -X POST http://localhost:8000/api/v1/sessions/$SID/branches \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"parent_id":"<root_branch_id>","user_message":""}'
# 3. Append a message
curl -X POST http://localhost:8000/api/v1/sessions/$SID/branches/$BID/msgs \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"parent_id":"<BID>","user_message":"Hello, world!"}'
# 4. List replies
curl http://localhost:8000/api/v1/sessions/$SID/branches/$BID/msgs \
-H "Authorization: Bearer $API_KEY"
ποΈ Tech Stack
- FastAPI β HTTP server & routing
- SQLAlchemy β ORM for Postgres / SQLite
- Pydantic β request & response models
- Clerk β user authentication & onboarding
- Poetry β dependency management
- Uvicorn β ASGI server
- Alembic β database migrations
π― Next Steps
- Add rate-limiting & analytics via Redis
- Summary / prompt trimming for very long histories
- Containerize with Docker & set up CI/CD for auto-releases
- Integrate Stripe for payments
Built With
- alembic
- clerk
- fastapi
- nextjs
- poetry
- pydanti
- python
- sqlalchemy
- uvicorn
Log in or sign up for Devpost to join the conversation.