added RaG

This commit is contained in:
2026-09-09 13:03:41 +03:00
parent e2ee6c2a98
commit df72dc4b8e
27 changed files with 3291 additions and 70 deletions
+1
View File
@@ -19,6 +19,7 @@ class Settings(BaseSettings):
openrouter_base_url: str = "https://openrouter.ai/api/v1"
openrouter_api_key: str = ""
openrouter_model: str = "deepseek/deepseek-chat"
openrouter_embedding_model: str = "openai/text-embedding-3-small"
# SOCKS5 proxy for OpenRouter, e.g. socks5://user:pass@host:1080
openrouter_proxy: str = ""
llm_max_context_rows: int = 80
+69
View File
@@ -8,6 +8,9 @@ from sqlalchemy.engine import Engine
def ensure_schema(engine: Engine) -> None:
"""Add missing columns/tables (create_all does not ALTER existing tables)."""
with engine.begin() as conn:
conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
insp = inspect(engine)
tables = set(insp.get_table_names())
@@ -37,6 +40,72 @@ def ensure_schema(engine: Engine) -> None:
with engine.begin() as conn:
conn.execute(text(f"ALTER TABLE {table_name} ADD COLUMN cell_stretches JSON"))
# Recreate inspector after possible alters
insp = inspect(engine)
tables = set(insp.get_table_names())
if "rag_sources" not in tables:
with engine.begin() as conn:
conn.execute(
text(
"""
CREATE TABLE rag_sources (
id SERIAL PRIMARY KEY,
scope VARCHAR(16) NOT NULL,
project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE,
kind VARCHAR(16) NOT NULL,
title VARCHAR(512) DEFAULT '',
uri VARCHAR(2048),
path VARCHAR(2048),
gost_id VARCHAR(128),
status VARCHAR(32) DEFAULT 'pending',
error_message TEXT,
meta JSON,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
)
"""
)
)
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_rag_sources_scope ON rag_sources (scope)"))
conn.execute(
text("CREATE INDEX IF NOT EXISTS ix_rag_sources_project_id ON rag_sources (project_id)")
)
conn.execute(
text("CREATE INDEX IF NOT EXISTS ix_rag_sources_gost_id ON rag_sources (gost_id)")
)
if "rag_chunks" not in tables:
with engine.begin() as conn:
conn.execute(
text(
"""
CREATE TABLE rag_chunks (
id SERIAL PRIMARY KEY,
source_id INTEGER NOT NULL REFERENCES rag_sources(id) ON DELETE CASCADE,
chunk_index INTEGER DEFAULT 0,
text TEXT DEFAULT '',
embedding vector(1536),
meta JSON,
CONSTRAINT uq_rag_chunk_source_idx UNIQUE (source_id, chunk_index)
)
"""
)
)
conn.execute(
text("CREATE INDEX IF NOT EXISTS ix_rag_chunks_source_id ON rag_chunks (source_id)")
)
try:
conn.execute(
text(
"CREATE INDEX IF NOT EXISTS ix_rag_chunks_embedding "
"ON rag_chunks USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100)"
)
)
except Exception:
# ivfflat needs data; skip if unsupported
pass
if "llm_messages" not in tables:
return