82 lines
2 KiB
Markdown
82 lines
2 KiB
Markdown
|
|
# pkg/rag
|
||
|
|
|
||
|
|
> Retrieval-Augmented Generation: memoria, embeddings, y búsqueda semántica.
|
||
|
|
|
||
|
|
## Responsabilidad
|
||
|
|
|
||
|
|
Proveer memoria persistente y búsqueda semántica sobre el contenido del agente.
|
||
|
|
|
||
|
|
## Componentes
|
||
|
|
|
||
|
|
| Tipo | Qué guarda | Persistencia |
|
||
|
|
|---|---|---|
|
||
|
|
| **Working** | Mensajes de la sesión actual | RAM |
|
||
|
|
| **Episodic** | Eventos pasados (qué hice el día X) | Vector DB |
|
||
|
|
| **Semantic** | Conocimiento consolidado | Vector DB (curado) |
|
||
|
|
| **Procedural** | Patrones de uso (workflows) | Vector DB (auto-aprendido) |
|
||
|
|
|
||
|
|
## API pública
|
||
|
|
|
||
|
|
```go
|
||
|
|
type Memory interface {
|
||
|
|
Add(ctx context.Context, fragment Fragment) error
|
||
|
|
Search(ctx context.Context, query string, topK int) ([]Fragment, error)
|
||
|
|
Forget(ctx context.Context, id string) error
|
||
|
|
}
|
||
|
|
|
||
|
|
type Fragment struct {
|
||
|
|
ID string
|
||
|
|
Content string
|
||
|
|
Vector []float32
|
||
|
|
Metadata map[string]string
|
||
|
|
Timestamp time.Time
|
||
|
|
ProjectID string
|
||
|
|
}
|
||
|
|
|
||
|
|
type Embedder interface {
|
||
|
|
Embed(ctx context.Context, text string) ([]float32, error)
|
||
|
|
Dimensions() int
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Backends
|
||
|
|
|
||
|
|
| Backend | Cuándo usar |
|
||
|
|
|---|---|
|
||
|
|
| ChromaDB embedded | Default. Simple, suficiente para <100k docs |
|
||
|
|
| Qdrant embedded | Si necesitas >100k docs o queries muy rápidas |
|
||
|
|
| SQLite + sqlite-vec | Si quieres zero-dependency (sin CGO con `modernc.org/sqlite`) |
|
||
|
|
|
||
|
|
## Embeddings
|
||
|
|
|
||
|
|
| Provider | Modelo | Dimensiones |
|
||
|
|
|---|---|---|
|
||
|
|
| Ollama | `nomic-embed-text` | 768 |
|
||
|
|
| Ollama | `bge-m3` | 1024 |
|
||
|
|
| Local ONNX | `all-MiniLM-L6-v2` | 384 |
|
||
|
|
|
||
|
|
## Uso
|
||
|
|
|
||
|
|
```go
|
||
|
|
import "github.com/VictorVargas/go-llm-agent/pkg/rag"
|
||
|
|
import "github.com/VictorVargas/go-llm-agent/pkg/rag/backends/chroma"
|
||
|
|
|
||
|
|
backend, _ := chroma.New(chroma.Config{
|
||
|
|
Path: "~/.local/share/rony/chroma",
|
||
|
|
})
|
||
|
|
|
||
|
|
memory := rag.New(rag.Config{
|
||
|
|
Backend: backend,
|
||
|
|
Embedder: ollamaEmbedder,
|
||
|
|
})
|
||
|
|
|
||
|
|
err := memory.Add(ctx, rag.Fragment{
|
||
|
|
Content: "Refactoricé auth.go usando hexagonal",
|
||
|
|
ProjectID: "rony",
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
## Ver también
|
||
|
|
|
||
|
|
- [pkg/agent](../agent/README.md) — Inyecta memoria al loop
|
||
|
|
- [pkg/llm](../llm/README.md) — Para summarization en compaction
|