rag

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Agent

type Agent struct {
	// contains filtered or unexported fields
}

Agent represents a simplified RAG agent that hides OpenAI SDK details

func NewAgent

func NewAgent(
	ctx context.Context,
	agentConfig agents.Config,
	modelConfig models.Config,
	options ...any,
) (*Agent, error)

NewAgent creates a new simplified RAG agent

func (*Agent) GenerateEmbedding

func (agent *Agent) GenerateEmbedding(content string) ([]float64, error)

GenerateEmbedding creates a vector embedding for the given text content

func (*Agent) GetConfig added in v1.0.8

func (agent *Agent) GetConfig() agents.Config

GetConfig returns the agent configuration

func (*Agent) GetContext added in v1.2.2

func (agent *Agent) GetContext() context.Context

GetContext returns the agent's context

func (*Agent) GetEmbeddingDimension added in v0.0.8

func (agent *Agent) GetEmbeddingDimension() int

GetEmbeddingDimension returns the dimension of the embedding vectors generated by the agent's model

func (*Agent) GetLastRequestJSON added in v1.1.2

func (agent *Agent) GetLastRequestJSON() (string, error)

func (*Agent) GetLastRequestRawJSON added in v1.1.2

func (agent *Agent) GetLastRequestRawJSON() string

func (*Agent) GetLastResponseJSON added in v1.1.2

func (agent *Agent) GetLastResponseJSON() (string, error)

func (*Agent) GetLastResponseRawJSON added in v1.1.2

func (agent *Agent) GetLastResponseRawJSON() string

func (*Agent) GetModelConfig added in v1.0.8

func (agent *Agent) GetModelConfig() models.Config

GetModelConfig returns the model configuration

func (*Agent) GetModelID added in v0.0.7

func (agent *Agent) GetModelID() string

func (*Agent) GetName added in v0.0.3

func (agent *Agent) GetName() string

func (*Agent) Kind

func (agent *Agent) Kind() agents.Kind

Kind returns the agent type

func (*Agent) LoadStore added in v0.0.5

func (agent *Agent) LoadStore(storeFilePath string) error

func (*Agent) PersistStore added in v0.0.5

func (agent *Agent) PersistStore(storeFilePath string) error

func (*Agent) SaveEmbedding

func (agent *Agent) SaveEmbedding(content string) error

SaveEmbedding generates and saves an embedding for the given content into the in memory agent's vector store

func (*Agent) SaveEmbeddingIntoMemoryVectorStore added in v1.1.3

func (agent *Agent) SaveEmbeddingIntoMemoryVectorStore(content string) error

SaveEmbeddingIntoMemoryVectorStore generates and saves an embedding for the given content into the in memory agent's vector store

func (*Agent) SearchSimilar

func (agent *Agent) SearchSimilar(content string, limit float64) ([]VectorRecord, error)

SearchSimilar searches for similar records based on content limit is the minimum cosine similarity threshold (1.0 = exact match, 0.0 = no similarity)

func (*Agent) SearchTopN

func (agent *Agent) SearchTopN(content string, limit float64, n int) ([]VectorRecord, error)

SearchTopN searches for top N similar records based on content limit is the minimum cosine similarity threshold (1.0 = exact match, 0.0 = no similarity) n is the maximum number of results to return

func (*Agent) SetConfig added in v1.0.8

func (agent *Agent) SetConfig(config agents.Config)

SetConfig updates the agent configuration

func (*Agent) SetContext added in v1.2.2

func (agent *Agent) SetContext(ctx context.Context)

SetContext updates the agent's context

func (*Agent) SetModelConfig added in v1.0.8

func (agent *Agent) SetModelConfig(config models.Config)

SetModelConfig updates the model configuration Note: For RAG agents, changing the model config requires recreating the agent as the embedding parameters are set during initialization

func (*Agent) StoreFileExists added in v0.0.5

func (agent *Agent) StoreFileExists(storeFilePath string) bool

type AgentOption

type AgentOption func(*BaseAgent)

func WithInMemoryStore added in v1.3.1

func WithInMemoryStore() AgentOption

WithInMemoryStore configures the agent to use in-memory vector storage This is the default behavior, so this option is only needed if you want to be explicit

func WithRedisStore added in v1.3.1

func WithRedisStore(config stores.RedisConfig, dimension int) AgentOption

WithRedisStore configures the agent to use Redis as the vector storage backend Parameters:

  • config: Redis connection configuration (address, password, DB, index name)
  • dimension: the dimension of embedding vectors (must match your embedding model)

Example:

ragAgent, err := rag.NewBaseAgent(
    ctx,
    agents.Config{EngineURL: "http://localhost:12434/engines/llama.cpp/v1"},
    openai.EmbeddingNewParams{Model: "ai/mxbai-embed-large"},
    rag.WithRedisStore(stores.RedisConfig{
        Address:   "localhost:6379",
        Password:  "",
        DB:        0,
        IndexName: "my_rag_index",
    }, 1024),
)

type BaseAgent

type BaseAgent struct {
	EmbeddingParams openai.EmbeddingNewParams
	// contains filtered or unexported fields
}

func NewBaseAgent

func NewBaseAgent(
	ctx context.Context,
	agentConfig agents.Config,
	modelConfig openai.EmbeddingNewParams,
	options ...AgentOption,
) (ragAgent *BaseAgent, err error)

NewBaseAgent creates a new RAG agent with OpenAI SDK integration

func (*BaseAgent) GenerateEmbeddingVector

func (agent *BaseAgent) GenerateEmbeddingVector(content string) (embeddingVector []float64, err error)

GenerateEmbeddingVector creates a vector embedding for the given text content using the agent's embedding model

func (*BaseAgent) GenerateThenSaveEmbeddingVector

func (agent *BaseAgent) GenerateThenSaveEmbeddingVector(content string) (err error)

GenerateThenSaveEmbeddingVector creates a vector embedding for the given text content

func (*BaseAgent) GetConfig added in v1.0.8

func (agent *BaseAgent) GetConfig() agents.Config

GetConfig returns the agent configuration

func (*BaseAgent) GetContext added in v1.2.2

func (agent *BaseAgent) GetContext() context.Context

GetContext returns the agent's context

func (*BaseAgent) GetEmbeddingDimension added in v0.0.8

func (agent *BaseAgent) GetEmbeddingDimension() int

Embedding vector dimension: the size of the produced vector (e.g., 384, 768, 1024, 3072 dimensions). GetEmbeddingDimension returns the dimension of the embedding vectors generated by the agent's model

func (*BaseAgent) GetLastRequestRawJSON added in v1.1.2

func (agent *BaseAgent) GetLastRequestRawJSON() string

func (*BaseAgent) GetLastRequestSON added in v1.1.2

func (agent *BaseAgent) GetLastRequestSON() (string, error)

func (*BaseAgent) GetLastResponseJSON added in v1.1.2

func (agent *BaseAgent) GetLastResponseJSON() (string, error)

func (*BaseAgent) GetLastResponseRawJSON added in v1.1.2

func (agent *BaseAgent) GetLastResponseRawJSON() string

func (*BaseAgent) SaveLastEmbeddingRequest added in v1.1.2

func (agent *BaseAgent) SaveLastEmbeddingRequest() error

SaveLastEmbeddingRequest saves the last embedding request JSON for logging/debugging purposes

func (*BaseAgent) SaveLastEmbeddingResponse added in v1.1.2

func (agent *BaseAgent) SaveLastEmbeddingResponse(embeddingResponse *openai.CreateEmbeddingResponse) error

func (*BaseAgent) SearchSimilarities

func (agent *BaseAgent) SearchSimilarities(content string, limit float64) (results []stores.VectorRecord, err error)

SearchSimilarities searches the vector store for similar records based on the embedding of the provided content and returns the top results up to the specified limit Parameters:

  • content: the text content to generate an embedding for searching.
  • limit: the minimum cosine distance similarity threshold. 1.0 means exact match, 0.0 means no similarity.

func (*BaseAgent) SearchTopNSimilarities

func (agent *BaseAgent) SearchTopNSimilarities(content string, limit float64, n int) (results []stores.VectorRecord, err error)

SearchTopNSimilarities searches the vector store for similar records based on the embedding of the provided content and returns the top N results above the specified similarity limit Parameters:

  • content: the text content to generate an embedding for searching.
  • limit: the minimum cosine distance similarity threshold. 1.0 means exact match, 0.0 means no similarity.
  • n: the maximum number of top similar records to return.

func (*BaseAgent) SetConfig added in v1.0.8

func (agent *BaseAgent) SetConfig(config agents.Config)

SetConfig updates the agent configuration

func (*BaseAgent) SetContext added in v1.2.2

func (agent *BaseAgent) SetContext(ctx context.Context)

SetContext updates the agent's context

type RagAgentOption added in v1.2.9

type RagAgentOption func(*Agent)

RagAgentOption is a functional option for configuring an Agent during creation

func AfterCompletion added in v1.2.9

func AfterCompletion(fn func(*Agent)) RagAgentOption

AfterCompletion sets a hook that is called after each embedding generation

func BeforeCompletion added in v1.2.9

func BeforeCompletion(fn func(*Agent)) RagAgentOption

BeforeCompletion sets a hook that is called before each embedding generation

type SearchSettings added in v0.0.8

type SearchSettings struct {
	SimilarityLimit float64
	MaxSimilarities int
}

type VectorRecord

type VectorRecord struct {
	ID        string
	Prompt    string
	Embedding []float64
	Metadata  map[string]any
	// CosineSimilarity
	Similarity float64
}

VectorRecord represents a vector record with prompt and embedding

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL