Skip to content

Commit d242b02

Browse files
committed
Support Ollama and unify OpenAI client
- Add LLM_PROVIDER to .env.example and include Ollama config/examples - Update README, SETUP_CHECKLIST, and copilot instructions to document provider choice and Ollama setup - Remove azure_openai_client.py and add unified openai_client.py (UnifiedOpenAIClient) that supports both Azure OpenAI and Ollama - Update brain_core/config.py to validate LLM_PROVIDER, load provider-specific settings, and return the UnifiedOpenAIClient
1 parent 50e854f commit d242b02

File tree

7 files changed

+354
-186
lines changed

7 files changed

+354
-186
lines changed

.env.example

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
# Diary Configuration
1+
# Second Brain Configuration
22
# Copy this file to .env and fill in your paths
3+
#
4+
# LLM Provider Options:
5+
# - azure: Cloud-based, requires API key, costs money, very capable
6+
# - ollama: Local, free, private, no internet needed (requires installation)
7+
#
8+
# Quick Start with Ollama (Free & Local):
9+
# 1. Install: https://ollama.com
10+
# 2. Run: ollama pull llama3.1
11+
# 3. Set: LLM_PROVIDER=ollama
12+
#
13+
# Quick Start with Azure OpenAI (Cloud):
14+
# 1. Get API key from Azure Portal
15+
# 2. Set: LLM_PROVIDER=azure
16+
# 3. Fill in AZURE_OPENAI_* settings below
317

418
# Required: Path to your Obsidian vault (or any markdown directory)
519
DIARY_PATH=/path/to/obsidian/vault
620

721
# Required: Path to planner directory for extracted todos
822
PLANNER_PATH=/path/to/planner
923

10-
# Required: Azure OpenAI configuration
24+
# Required: LLM Provider ("azure" or "ollama")
25+
LLM_PROVIDER=azure
26+
27+
# Azure OpenAI Configuration (required if LLM_PROVIDER=azure)
1128
AZURE_OPENAI_API_KEY=your-api-key-here
1229
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
1330
AZURE_OPENAI_DEPLOYMENT=gpt-4o
1431
AZURE_OPENAI_API_VERSION=2024-02-15-preview
1532

33+
# Ollama Configuration (required if LLM_PROVIDER=ollama)
34+
# OLLAMA_BASE_URL=http://localhost:11434
35+
# OLLAMA_MODEL=llama3.1
36+
1637
# Optional: Cost Tracking Configuration
1738
BRAIN_COST_DB_PATH=~/.brain/costs.db
1839

.github/copilot-instructions.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ AI-powered journaling system with markdown entries, semantic backlinks, and LLM-
99
- Package manager: `uv` (ALWAYS use `uv`, never `pip`)
1010
- Testing: pytest with 57 tests, 47% coverage
1111
- CLI: typer (add_completion=False) + rich
12-
- LLM: Azure OpenAI (required)
12+
- LLM: Azure OpenAI or Ollama (configurable via LLM_PROVIDER)
1313

1414
## Architecture
1515

@@ -22,7 +22,7 @@ Core functionality with comprehensive type hints and dataclasses:
2222
- `constants.py` - Shared constants (100% coverage)
2323
- `entry_manager.py` - Entry I/O and parsing (93% coverage)
2424
- `llm_client.py` - Abstract LLM interface (73% coverage)
25-
- `azure_openai_client.py` - Azure OpenAI client (40% coverage)
25+
- `openai_client.py` - Unified OpenAI client for both Azure and Ollama (0% coverage)
2626
- `llm_analysis.py` - Atomic LLM operations: entities, backlinks, tags (17% coverage)
2727
- `report_generator.py` - Report orchestration (18% coverage)
2828
- `template_generator.py` - AI prompt generation (42% coverage)
@@ -63,12 +63,20 @@ uv run pytest tests/ --cov # With coverage
6363
- `DIARY_PATH` - Path to Obsidian vault or markdown directory (for reflection entries)
6464
- `PLANNER_PATH` - Path to directory for daily plan files (separate from diary)
6565

66-
**Azure OpenAI (required for all LLM features):**
66+
**LLM Provider (choose one):**
67+
- `LLM_PROVIDER` - Set to "azure" or "ollama" (default: azure)
68+
69+
**Azure OpenAI (cloud-based):**
6770
- `AZURE_OPENAI_API_KEY` - API key
6871
- `AZURE_OPENAI_ENDPOINT` - Service endpoint
6972
- `AZURE_OPENAI_DEPLOYMENT` - Model deployment name (e.g., gpt-4o)
7073
- `AZURE_OPENAI_API_VERSION` - Default: 2024-02-15-preview
71-
-**Full functionality:** Diary prompts, task extraction, semantic analysis, backlinks, tags, reports
74+
-**Full functionality:** All features, cost tracking enabled
75+
76+
**Ollama (local, free):**
77+
- `OLLAMA_BASE_URL` - API URL (default: http://localhost:11434)
78+
- `OLLAMA_MODEL` - Model name (default: llama3.1)
79+
-**Full functionality:** All features, no cost tracking (local is free!)
7280

7381
## Entry Structure
7482

@@ -176,16 +184,21 @@ llm_client.py → azure_openai_client.py
176184
## Recent Refactoring
177185

178186
**Architecture Simplification:**
179-
- Removed Ollama client (195 lines) - Azure OpenAI only
180187
- Removed scheduler/daemon system (312 lines, 0% coverage)
181188
- Removed todos command (redundant with plan command)
182189
- Removed `generate_planning_prompts()` - plans no longer have prompts
183190
- Removed notes search functionality (238 lines) - focused on journaling/planning only
184-
- Total code reduction: ~838 lines
191+
- Total code reduction: ~643 lines
192+
193+
**LLM Provider Support:**
194+
- Unified OpenAI client supports both Azure OpenAI and Ollama
195+
- Single implementation using OpenAI package's compatibility
196+
- Ollama uses OpenAI-compatible `/v1` endpoint
197+
- Configurable via `LLM_PROVIDER` environment variable
185198

186199
**Module Improvements:**
187200
- Renamed `analysis.py``report_generator.py` (clearer purpose)
188-
- Renamed `azure_client.py``azure_openai_client.py` (clarity)
201+
- Renamed `azure_client.py``openai_client.py` (unified client)
189202
- Moved plan command from diary to separate `plan_commands.py` module
190203
- Updated `EntryManager` to accept separate diary_path and planner_path
191204

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,24 @@ Create `~/.config/brain/.env` with these settings:
139139
- `DIARY_PATH` - Path to Obsidian vault or markdown directory for reflection entries
140140
- `PLANNER_PATH` - Path to directory for daily plan files (separate from diary)
141141

142-
**Azure OpenAI** (required for all LLM features):
142+
**LLM Provider** (choose one):
143+
- `LLM_PROVIDER` - Set to `azure` or `ollama` (default: `azure`)
144+
145+
**Option 1: Azure OpenAI** (cloud-based, requires API key):
143146
- `AZURE_OPENAI_API_KEY` - Your Azure OpenAI API key
144147
- `AZURE_OPENAI_ENDPOINT` - Your Azure OpenAI endpoint
145-
- `AZURE_OPENAI_DEPLOYMENT` - Deployment name (default: gpt-4o)
146-
- `AZURE_OPENAI_API_VERSION` - API version (default: 2024-02-15-preview)
148+
- `AZURE_OPENAI_DEPLOYMENT` - Deployment name (default: `gpt-4o`)
149+
- `AZURE_OPENAI_API_VERSION` - API version (default: `2024-02-15-preview`)
150+
151+
**Option 2: Ollama** (local, free, private):
152+
- `OLLAMA_BASE_URL` - Ollama API URL (default: `http://localhost:11434`)
153+
- `OLLAMA_MODEL` - Model name (default: `llama3.1`)
154+
- First install Ollama from https://ollama.com
155+
- Then pull a model: `ollama pull llama3.1`
156+
157+
> **Switching Providers:** Just change `LLM_PROVIDER` in your `.env` file and restart. No code changes needed!
158+
>
159+
> **Privacy:** Ollama runs completely locally. Your journal entries never leave your machine.
147160
148161
**Logging & Cost Tracking** (optional configuration):
149162
- `BRAIN_COST_DB_PATH` - Path to cost tracking database (default: ~/.brain/costs.db)
@@ -207,7 +220,10 @@ After writing, run `brain diary link today` to add semantic backlinks:
207220

208221
- Python 3.13+
209222
- uv package manager (ALWAYS use `uv`, never `pip`)
210-
- **Azure OpenAI** - Required for all LLM operations:
223+
- **LLM Provider** (choose one):
224+
- **Azure OpenAI** - Cloud-based, requires API key and subscription
225+
- **Ollama** - Local, free, completely private (requires installation from https://ollama.com)
226+
- All LLM features supported with either provider:
211227
- Diary prompt generation
212228
- Task extraction from diary entries
213229
- Semantic backlinks and tags

SETUP_CHECKLIST.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,18 @@ Edit your `.env` file with the following **required** variables:
7171
DIARY_PATH=/Users/yourname/Documents/second-brain/diary
7272
PLANNER_PATH=/Users/yourname/Documents/second-brain/planner
7373

74-
# Required: Azure OpenAI (get from Azure Portal)
74+
# Required: LLM Provider (choose one)
75+
LLM_PROVIDER=azure # or "ollama" for local
76+
77+
# Option 1: Azure OpenAI (cloud-based)
7578
AZURE_OPENAI_API_KEY=your-api-key-here
7679
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
7780
AZURE_OPENAI_DEPLOYMENT=gpt-4o
7881
AZURE_OPENAI_API_VERSION=2024-02-15-preview
82+
83+
# Option 2: Ollama (local, free)
84+
# OLLAMA_BASE_URL=http://localhost:11434
85+
# OLLAMA_MODEL=llama3.1
7986
```
8087

8188
**Optional variables** (with defaults):
@@ -89,9 +96,11 @@ BRAIN_LOG_LEVEL=INFO
8996
BRAIN_LOG_FILE=~/.brain/logs/brain.log
9097
```
9198

92-
### Step 4: Get Azure Credentials
99+
### Step 4: Setup LLM Provider
100+
101+
Choose **one** of the following:
93102

94-
#### Azure OpenAI (Required)
103+
#### Option 1: Azure OpenAI (Cloud)
95104

96105
1. Go to [Azure Portal](https://portal.azure.com)
97106
2. Navigate to your Azure OpenAI resource
@@ -101,6 +110,25 @@ BRAIN_LOG_FILE=~/.brain/logs/brain.log
101110
- **Endpoint**`AZURE_OPENAI_ENDPOINT`
102111
5. Go to **Model deployments**
103112
6. Copy your deployment name (e.g., "gpt-4o") → `AZURE_OPENAI_DEPLOYMENT`
113+
7. Set `LLM_PROVIDER=azure` in `.env`
114+
115+
#### Option 2: Ollama (Local)
116+
117+
1. Install Ollama from https://ollama.com
118+
2. Start Ollama (it runs in the background)
119+
3. Pull a model:
120+
```bash
121+
ollama pull llama3.1 # Fast, balanced (4.7GB)
122+
# OR
123+
ollama pull llama3.2 # Smaller, faster (2GB)
124+
# OR
125+
ollama pull qwen2.5:7b # Great for structured output
126+
```
127+
4. Set in `.env`:
128+
```bash
129+
LLM_PROVIDER=ollama
130+
OLLAMA_MODEL=llama3.1 # or whichever model you pulled
131+
```
104132

105133
### Step 5: Test Configuration
106134

@@ -136,12 +164,19 @@ The directory doesn't exist. Create it:
136164
mkdir -p /path/to/dir
137165
```
138166

139-
### "Azure OpenAI credentials required"
167+
### "Azure OpenAI credentials required" or "LLM_PROVIDER must be set"
168+
169+
**If using Azure OpenAI:**
170+
1. Set `LLM_PROVIDER=azure` in `.env`
171+
2. Ensure `AZURE_OPENAI_API_KEY` is set correctly
172+
3. Ensure `AZURE_OPENAI_ENDPOINT` ends with a `/`
173+
4. Ensure `AZURE_OPENAI_DEPLOYMENT` matches your deployment name in Azure Portal
140174

141-
Your Azure OpenAI credentials are missing or incorrect. Double-check:
142-
1. `AZURE_OPENAI_API_KEY` is set correctly
143-
2. `AZURE_OPENAI_ENDPOINT` ends with a `/`
144-
3. `AZURE_OPENAI_DEPLOYMENT` matches your deployment name in Azure Portal
175+
**If using Ollama:**
176+
1. Set `LLM_PROVIDER=ollama` in `.env`
177+
2. Make sure Ollama is running (check with `ollama list`)
178+
3. Pull a model if you haven't: `ollama pull llama3.1`
179+
4. Set `OLLAMA_MODEL` to match the model you pulled
145180

146181
### .env File Not Loading
147182

brain_core/azure_openai_client.py

Lines changed: 0 additions & 138 deletions
This file was deleted.

0 commit comments

Comments
 (0)