go-rag/helpers.go
Snider bf047e4494 feat: extract go-rag from go-ai as standalone RAG package
Vector search with Qdrant + Ollama embeddings, document chunking.
Zero internal go-ai dependencies. Adds CLAUDE.md/TODO.md/FINDINGS.md.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-19 18:29:59 +00:00

89 lines
2.4 KiB
Go

package rag
import (
"context"
"fmt"
)
// QueryDocs queries the RAG database with default clients.
func QueryDocs(ctx context.Context, question, collectionName string, topK int) ([]QueryResult, error) {
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
if err != nil {
return nil, err
}
defer func() { _ = qdrantClient.Close() }()
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
if err != nil {
return nil, err
}
cfg := DefaultQueryConfig()
cfg.Collection = collectionName
cfg.Limit = uint64(topK)
return Query(ctx, qdrantClient, ollamaClient, question, cfg)
}
// QueryDocsContext queries the RAG database and returns context-formatted results.
func QueryDocsContext(ctx context.Context, question, collectionName string, topK int) (string, error) {
results, err := QueryDocs(ctx, question, collectionName, topK)
if err != nil {
return "", err
}
return FormatResultsContext(results), nil
}
// IngestDirectory ingests all documents in a directory with default clients.
func IngestDirectory(ctx context.Context, directory, collectionName string, recreateCollection bool) error {
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
if err != nil {
return err
}
defer func() { _ = qdrantClient.Close() }()
if err := qdrantClient.HealthCheck(ctx); err != nil {
return fmt.Errorf("qdrant health check failed: %w", err)
}
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
if err != nil {
return err
}
if err := ollamaClient.VerifyModel(ctx); err != nil {
return err
}
cfg := DefaultIngestConfig()
cfg.Directory = directory
cfg.Collection = collectionName
cfg.Recreate = recreateCollection
_, err = Ingest(ctx, qdrantClient, ollamaClient, cfg, nil)
return err
}
// IngestSingleFile ingests a single file with default clients.
func IngestSingleFile(ctx context.Context, filePath, collectionName string) (int, error) {
qdrantClient, err := NewQdrantClient(DefaultQdrantConfig())
if err != nil {
return 0, err
}
defer func() { _ = qdrantClient.Close() }()
if err := qdrantClient.HealthCheck(ctx); err != nil {
return 0, fmt.Errorf("qdrant health check failed: %w", err)
}
ollamaClient, err := NewOllamaClient(DefaultOllamaConfig())
if err != nil {
return 0, err
}
if err := ollamaClient.VerifyModel(ctx); err != nil {
return 0, err
}
return IngestFile(ctx, qdrantClient, ollamaClient, collectionName, filePath, DefaultChunkConfig())
}