Setting Up Your Lab ๐งช
How to set up your Python environment, install Kern, connect local models, and run your first agent.
Let's get your local agent laboratory ready. ๐งช We will:
- Create a Python virtual environment.
- Install the
kern-aiSDK. - Configure your local model connection (Ollama, llama.cpp, etc.).
- Run your first file-inspector agent.
- Boot up an API service using AgentOS.
๐ฆ 1. Create a Virtual Environment
Kern requires Python 3.10 or newer. We recommend uv for lightning-fast package management, but standard venv works great too.
1# Create and activate2uv venv --python 3.123source .venv/bin/activate1# Create and activate2python3 -m venv .venv3source .venv/bin/activate1# Create and activate2python -m venv .venv3.venv\Scripts\activate๐พ 2. Install Kern
Install the core SDK along with the standard client dependencies:
1uv pip install -U kern-ai openai1pip install -U kern-ai openai๐ 3. Connect to a Model
Kern is designed around the OpenAI-compatible completions API, making it easy to point to local model servers or cloud providers.
Local Servers (Recommended) ๐ป
Boot up a local model runner and note its endpoint. Here are the default endpoints for popular local managers:
| Runner | Default Endpoint | Example Model |
|---|---|---|
| Ollama | http://localhost:11434/v1 | llama3.2:3b / qwen2.5:3b |
| llama.cpp server | http://localhost:8080/v1 | Any GGUF file |
| LM Studio | http://localhost:1234/v1 | Any downloaded GGUF |
| vLLM | http://localhost:8000/v1 | Any HuggingFace repo |
Cloud Providers (Optional) โ๏ธ
If you want to use cloud APIs, export your credentials to your shell environment:
1export OPENAI_API_KEY=sk-***1setx OPENAI_API_KEY sk-***๐ฉ 4. Run Your First Agent
Save the script below as sorting_hat.py. It uses a local Ollama model to scan its own directory and organize it:
1from pathlib import Path2from kern.agent import Agent3from kern.models.openai import OpenAIChat4from kern.tools.workspace import Workspace56# Determine current script folder7folder = Path(__file__).parent89# 1. Spawn the sorting hat agent! ๐ฉ10sorting_hat = Agent(11 model=OpenAIChat(12 id="llama3.2:3b",13 base_url="http://localhost:11434/v1", # Connecting to Ollama14 ),15 tools=[Workspace(root=str(folder), allowed=["read", "list"])],16 instructions="Inspect the files in this folder, group them by type, and print a tidy summary tree.",17 markdown=True,18)1920# 2. Tell the agent to inventory the workspace21sorting_hat.print_response(f"Inventory and organize the files in: {folder}", stream=True)Run your script:
1python sorting_hat.pyYou should see your local model read the folder, execute the workspace tool, and print a formatted directory breakdown in your terminal! ๐
๐ 5. Serve Your Agent with AgentOS
To go from a local test script to a production service with chat UI interfaces, session histories, and telemetry, run your agent using AgentOS.
Install the OS runtime extras:
1uv pip install -U 'kern-ai[os]'1pip install -U 'kern-ai[os]'Save the script below as workbench.py:
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.os import AgentOS4from kern.models.openai import OpenAIChat56# Build our agent7workbench = Agent(8 name="Workbench Agent",9 model=OpenAIChat(id="llama3.2:3b", base_url="http://localhost:11434/v1"),10 db=SqliteDb(db_file="tmp/workbench.db"),11 add_history_to_context=True,12 num_history_runs=3,13)1415# Wrap it in AgentOS to expose it as an API16agent_os = AgentOS(agents=[workbench], tracing=True)17app = agent_os.get_app()Run your FastAPI server:
1fastapi dev workbench.pyYour API is now live at http://localhost:8000. You can inspect the Swagger specs at http://localhost:8000/docs, or head over to the visual console at os.kern.ndx.rocks to add your server and start chatting!
๐๏ธ Running a Postgres Database
If you want to use PostgreSQL + pgvector for long-term production storage, you can run pgvector locally using Docker:
1docker run -d \2 --name kern-postgres \3 -e POSTGRES_USER=ai \4 -e POSTGRES_PASSWORD=ai \5 -e POSTGRES_DB=ai \6 -p 5432:5432 \7 -v kern-pgdata:/var/lib/postgresql/data \8 pgvector/pgvector:pg16 # Standard official pgvector image ๐ณOnce running, point your database adapter in Python to the local container:
db = PostgresDb(db_url="postgresql://ai:ai@localhost:5432/ai")
๐ Let's Start Building!
Explore the main sections: