Retrieve: When generating new content, find similar past content
Generate: LLM uses retrieved context to write consistently
graph LR
A[New Messages] --> B[Query Embedding]
B --> C[Vector Search]
D[RAG Index] --> C
C --> E[Retrieved Context]
E --> F[LLM Writer]
F --> G[New Blog Post]
G -.->|Embed & Store| D
importosfrompathlibimportPathfromgoogleimportgenaifromegregora.configimportModelConfig,load_site_configfromegregora.knowledge.ragimportVectorStore,index_post,query_similar_postsfromegregora.utils.batchimportGeminiBatchClient# Load model preferences from mkdocs.yml (used by the CLI as well)site_config=load_site_config(Path("output"))model_config=ModelConfig(site_config=site_config)embedding_model=model_config.get_model("embedding")embedding_dims=model_config.get_embedding_output_dimensionality()# Batch client for embeddingsclient=genai.Client(api_key=os.environ["GOOGLE_API_KEY"])batch_client=GeminiBatchClient(client,default_model=embedding_model)# Open (or create) the vector store managed by the CLIstore=VectorStore(Path("output/rag/chunks.parquet"))# Index a generated postindexed=index_post(Path("output/posts/2025-01-01-new-year-retro.md"),batch_client,store,embedding_model=embedding_model,output_dimensionality=embedding_dims,)# Later, retrieve similar posts for a conversation table produced by the pipelinesimilar=query_similar_posts(table=period_table,batch_client=batch_client,store=store,embedding_model=embedding_model,output_dimensionality=embedding_dims,)
Egregora relies on Google's Gemini embedding models. The active model and vector dimensionality are resolved by ModelConfig, using the same configuration hierarchy as the CLI:
frompathlibimportPathfromegregora.database.duckdb_managerimportDuckDBStorageManagerfromegregora.knowledgeimportAnnotationStorestorage=DuckDBStorageManager(db_path=Path("output/annotations.duckdb"))annotation_store=AnnotationStore(storage)annotation_store.save_annotation(msg_id="msg-2025-01-01-0001",commentary="Follow up on the launch checklist",)annotations=annotation_store.list_annotations_for_message("msg-2025-01-01-0001")
# Auto-installed on first runegregoraprocessexport.zip
# Or install manually:python-c"import duckdbconn = duckdb.connect()conn.execute('INSTALL vss')conn.execute('LOAD vss')"
fromegregora.utils.cacheimportget_cachecache=get_cache(".egregora/cache/")# Cached by content hashembedding=cache.get("text_hash")ifnotembedding:embedding=embed_text(text)cache.set("text_hash",embedding)