Skip to content

Python API

The CLI is a thin layer over three modules, which you can use directly:

Module
notecast.db SQLite storage: notes, themes, config, schema migrations
notecast.llm Ollama, a small client for Ollama's HTTP API (standard library only)
notecast.pipeline the stages: process_notes, classify_notes, organize_themes, consolidate_themes, auto_scan

They are not a stable API yet: pin the notecast version if you build on them.

Build a graph

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import os
from notecast import db, pipeline
from notecast.llm import Ollama

conn = db.connect(os.environ.get("NOTECAST_DB", "notecast.db"))
db.init_db(conn)                       # creates or migrates the schema

llm = Ollama(base_url=os.environ.get("OLLAMA_URL", "http://localhost:11434"))
assert llm.ping() and not llm.missing_models()

db.add_theme(conn, "Tech", is_base=True)
db.add_theme(conn, "Cooking", is_base=True)
db.upsert_note_from_source(conn, "Pods", "Tech notes: Kubernetes pods and services.", "mem:pods")
db.upsert_note_from_source(conn, "Bread", "Cooking notes: sourdough and starters.", "mem:bread")

assert pipeline.process_notes(conn, llm) == 2
assert pipeline.classify_notes(conn, llm) == 2
for note in db.list_notes(conn):
    print(note.title, note.status, db.note_theme_names(conn, [note.id])[note.id])

upsert_note_from_source is what add uses: the third argument identifies where the note came from, so calling it again with the same content does nothing, and with new content updates the note.

Errors

1
2
3
4
5
6
7
8
9
from notecast.llm import Ollama, OllamaError, OllamaUnavailable

llm = Ollama(base_url="http://127.0.0.1:9")   # nothing listens here
try:
    llm.generate("hello")
except OllamaUnavailable as e:      # unreachable, or model not pulled (HTTP 404)
    print("stop and retry later:", e)
except OllamaError as e:            # any other failed request
    print("this request failed:", e)

The pipeline functions raise OllamaUnavailable and leave the notes they had not reached untouched; every other model failure is handled per note, batch or theme, as described in The pipeline.

Similar notes

Listing functions skip the (large) embeddings unless you ask for them:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import os
from notecast import db, pipeline

conn = db.connect(os.environ.get("NOTECAST_DB", "notecast.db"))
db.init_db(conn)
notes = db.list_notes(conn, with_vectors=True)
if notes:
    target, others = notes[0], notes[1:]
    for note in pipeline.find_similar(target.vector, others, k=3):
        print(note.title)