cve-kgrag-db / code /src /generators /rag_config.py
DuyTa's picture
Add code/: full CVE-KGRAG project source snapshot
27f6252 verified
Raw
History Blame Contribute Delete
7.36 kB
"""
Centralized configuration for the RAG system.
Edit this file to update paths, model names, and other settings in one place.
"""
import os
import torch
from pathlib import Path
# Base data directory (edit as needed)
BASE_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data/knowledge_base'))
# ── Qdrant ────────────────────────────────────────────────────────────────────
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY", None)
QDRANT_COLLECTIONS = {
"cve": "cve_chunks",
"mitre": "mitre_techniques",
"capec": "capec_patterns",
"cwe": "cwe_entries",
}
DENSE_VECTOR_NAME = "dense"
SPARSE_VECTOR_NAME = "sparse"
# ── BM25 vocabulary ───────────────────────────────────────────────────────────
BM25_VOCAB_PATH = os.path.join(BASE_DATA_DIR, "bm25", "vocab.json")
BM25_K1 = 1.5
BM25_B = 0.75
BM25_MIN_DF = 2 # drop tokens with doc-frequency < this
BM25_MAX_VOCAB = 200_000
# ── Neo4j ─────────────────────────────────────────────────────────────────────
NEO4J_URI = os.getenv("NEO4J_URI", "bolt://localhost:7687")
NEO4J_USER = os.getenv("NEO4J_USER", "neo4j")
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD", os.getenv("NEO4J_AUTH", "neo4j/password").split("/")[-1])
# ── RAG export paths ──────────────────────────────────────────────────────────
RAG_EXPORTS_DIR = os.path.join(BASE_DATA_DIR, "rag_exports")
CVE_CHUNKS_PATH = os.path.join(RAG_EXPORTS_DIR, "cve_chunks.json")
MITRE_CHUNKS_PATH = os.path.join(RAG_EXPORTS_DIR, "mitre_chunks.json")
CAPEC_CHUNKS_PATH = os.path.join(RAG_EXPORTS_DIR, "capec_chunks.json")
CWE_CHUNKS_PATH = os.path.join(RAG_EXPORTS_DIR, "cwe_chunks.json")
# Legacy alias kept for any code that still imports it
CVE_DATA_PATH = CVE_CHUNKS_PATH
# Year-based data paths (used by search_cves_by_year fallback)
CVE_YEAR_PATHS = {
str(yr): os.path.join(BASE_DATA_DIR, f"enhanced_documents_cve_{yr}.json")
for yr in range(1999, 2027)
}
# ── Embedding model ───────────────────────────────────────────────────────────
EMBEDDING_MODEL_NAME = "microsoft/harrier-oss-v1-270m" # 640-dim, MTEB 66.5
DENSE_VECTOR_SIZE = 640
# ── GPU / compute ─────────────────────────────────────────────────────────────
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
EMBEDDING_BATCH_SIZE = 256 # sweet spot for harrier on 4GB VRAM
MAX_SEQ_LENGTH = 1024 # fits full CVE description + severity sections
# ── Chunking parameters (kept for reference; real chunking is in chunkers.py) ─
CHUNK_SIZE = 512
CHUNK_OVERLAP = 50
# ── Search parameters ─────────────────────────────────────────────────────────
VECTOR_SEARCH_TOP_K = 50
RERANK_TOP_K = 10
HYBRID_ALPHA = 0.7 # weight for vector vs keyword (used if manual fusion needed)
# ── Performance ───────────────────────────────────────────────────────────────
MAX_QUERY_LENGTH = 1000
MAX_CONTEXT_LENGTH = 8000
MAX_RESPONSE_LENGTH = 2000
MAX_SEARCH_RESULTS = 50
# ── Timeouts (seconds) ────────────────────────────────────────────────────────
EMBEDDING_TIMEOUT = 5
SEARCH_TIMEOUT = 10
LLM_GENERATION_TIMEOUT= 30
# ── API rate limiting ─────────────────────────────────────────────────────────
MAX_REQUESTS_PER_MINUTE = 60
RATE_LIMIT_WINDOW = 60
# ── LLM (OpenAI-compatible API) ────────────────────────────────────────────────
# Uses the llms/ package. Configure via env vars or edit directly.
# Works with any OpenAI-compatible endpoint: Ollama, vLLM, LM Studio, OpenAI, etc.
LLM_ENABLED = os.getenv("LLM_ENABLED", "false").lower() == "true"
LLM_PROVIDER = os.getenv("LLM_PROVIDER", "openai") # "openai" | "ollama" | "vllm"
LLM_BASE_URL = os.getenv("LLM_BASE_URL", "http://localhost:11434/v1")
LLM_MODEL_NAME = os.getenv("LLM_MODEL_NAME", "llama3.1:8b-instruct-fp16")
LLM_API_KEY = os.getenv("LLM_API_KEY", os.getenv("OPENAI_API_KEY", None))
LLM_TEMPERATURE = float(os.getenv("LLM_TEMPERATURE", "0.1"))
LLM_MAX_TOKENS = int(os.getenv("LLM_MAX_TOKENS", "800"))
LLM_ENABLE_REASONING = os.getenv("LLM_ENABLE_REASONING", "false").lower() == "true"
def get_llm_config() -> dict:
"""Return a config dict compatible with llms.factory.LLMFactory.create_from_config()."""
return {
"enabled": LLM_ENABLED,
"provider": LLM_PROVIDER,
"model_name": LLM_MODEL_NAME,
"base_url": LLM_BASE_URL,
"api_key": LLM_API_KEY,
"default_temperature": LLM_TEMPERATURE,
"default_max_tokens": LLM_MAX_TOKENS,
"enable_reasoning": LLM_ENABLE_REASONING,
}
# Legacy aliases kept for any old code that still imports them
def get_hf_token():
token_file = Path(__file__).parent.parent.parent / "llama_token.txt"
if token_file.exists():
with open(token_file) as f:
return f.read().strip()
return os.getenv("HF_TOKEN", "")
HF_TOKEN = get_hf_token()
USE_OLLAMA = os.getenv("USE_OLLAMA", "false").lower() == "true"
OLLAMA_HOST = os.getenv("OLLAMA_HOST", "http://localhost:11434")
OLLAMA_MODEL_PRIMARY= os.getenv("OLLAMA_MODEL_PRIMARY", "llama3.1:8b-instruct-fp16")
OLLAMA_MODEL_FAST = os.getenv("OLLAMA_MODEL_FAST", "llama3.1:8b-instruct-fp16")
HF_MODEL_PRIMARY = "meta-llama/Llama-3.1-8B-Instruct"
HF_MODEL_FAST = "meta-llama/Llama-3.1-8B-Instruct"
# ── API server ────────────────────────────────────────────────────────────────
API_HOST = os.getenv("API_HOST", "0.0.0.0")
API_PORT = int(os.getenv("API_PORT", "8000"))
API_WORKERS = int(os.getenv("API_WORKERS", "1"))
# ── Logging ───────────────────────────────────────────────────────────────────
LOGGING_LEVEL = os.getenv("RAG_LOGGING_LEVEL", "INFO")