Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rag): simplify configuration and initialization #266

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gptme.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
files = ["README.md", "Makefile"]
#files = ["README.md", "Makefile", "gptme/cli.py", "docs/*.rst", "docs/*.md"]

[rag]
enabled = true
12 changes: 4 additions & 8 deletions gptme/tools/_rag_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import (
Any,
)
from typing import Any

from ..config import get_project_config
from ..message import Message
Expand Down Expand Up @@ -64,7 +62,7 @@ def get_context(self, query: str, max_tokens: int = 1000) -> list[Context]:
class RAGManager:
"""Manages RAG functionality for both context enhancement and tool use."""

def __init__(self, index_path: Path | None = None, collection: str | None = None):
def __init__(self):
if not _HAS_RAG:
raise ImportError("gptme-rag not installed")

Expand All @@ -73,10 +71,8 @@ def __init__(self, index_path: Path | None = None, collection: str | None = None
self.config = config.rag if config and config.rag else {}

# Use config values if not overridden by parameters
self.index_path = Path(
index_path or self.config.get("index_path", "~/.cache/gptme/rag")
).expanduser()
self.collection = collection or self.config.get("collection", "default")
self.index_path = Path("~/.cache/gptme/rag").expanduser()
self.collection = "default"

# Initialize the indexer
self.indexer = gptme_rag.Indexer(
Expand Down
48 changes: 23 additions & 25 deletions gptme/tools/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,26 @@
Configure RAG in your ``gptme.toml``::

[rag]
# Storage configuration
index_path = "~/.cache/gptme/rag" # Where to store the index
collection = "gptme_docs" # Collection name for documents

# Context enhancement settings
max_tokens = 2000 # Maximum tokens for context window
auto_context = true # Enable automatic context enhancement
min_relevance = 0.5 # Minimum relevance score for including context
max_results = 5 # Maximum number of results to consider

# Cache configuration
[rag.cache]
max_embeddings = 10000 # Maximum number of cached embeddings
max_searches = 1000 # Maximum number of cached search results
embedding_ttl = 86400 # Embedding cache TTL in seconds (24h)
search_ttl = 3600 # Search cache TTL in seconds (1h)
enabled = true

.. rubric:: Features

1. Manual Search and Indexing

- Index project documentation with ``rag_index``
- Search indexed documents with ``rag_search``
- Check index status with ``rag_status``

2. Automatic Context Enhancement

- Automatically adds relevant context to user messages
- Retrieves semantically similar documents
- Manages token budget to avoid context overflow
- Preserves conversation flow with hidden context messages

3. Performance Optimization

- Intelligent caching system for embeddings and search results
- Configurable cache sizes and TTLs
- Automatic cache invalidation
- Memory-efficient storage

.. rubric:: Benefits
Expand All @@ -59,12 +45,13 @@
"""

import logging
from dataclasses import replace
from pathlib import Path

from ..config import get_project_config
from ..util import get_project_dir
from ._rag_context import _HAS_RAG, RAGManager
from .base import ToolSpec, ToolUse
from ._rag_context import RAGManager, _HAS_RAG

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -119,20 +106,31 @@ def rag_status() -> str:
return f"Index contains {rag_manager.get_document_count()} documents"


_init_run = False


def init() -> ToolSpec:
"""Initialize the RAG tool."""
if not _HAS_RAG:
global _init_run
if _init_run:
return tool
_init_run = True

if not tool.available:
return tool

project_dir = get_project_dir()
index_path = Path("~/.cache/gptme/rag").expanduser()
collection = "default"
if project_dir and (config := get_project_config(project_dir)):
index_path = Path(config.rag.get("index_path", index_path)).expanduser()
collection = config.rag.get("collection", project_dir.name)
enabled = config.rag.get("enabled", False)
if not enabled:
logger.debug("RAG not enabled in the project configuration")
return replace(tool, available=False)
else:
logger.debug("Project configuration not found, not enabling")
return replace(tool, available=False)

global rag_manager
rag_manager = RAGManager(index_path=index_path, collection=collection)
rag_manager = RAGManager()
return tool


Expand Down
Loading