knowledge

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KnowledgeAliyun Aliyun Bailian Knowledge Base
	KnowledgeAliyun = "aliyun"
	// KnowledgeQdrant Qdrant Vector Database
	KnowledgeQdrant = "qdrant"
)
View Source
const (
	// DefaultBailianEndpoint is the public Bailian OpenAPI host (Beijing region).
	DefaultBailianEndpoint = "bailian.cn-beijing.aliyuncs.com"
)

Variables

View Source
var ErrEmptyText = errors.New("empty text")
View Source
var ErrUnsupportedKnowledgeProvider = errors.New("unsupported knowledge provider")

Functions

This section is empty.

Types

type AliyunHandler

type AliyunHandler struct {
	WorkspaceID string
	IndexID     string
	// contains filtered or unexported fields
}

AliyunHandler calls Alibaba Cloud Model Studio (百炼) knowledge-base OpenAPI. Credentials are AccessKey ID/Secret (main or RAM user with Bailian data permissions).

Index selection: AliyunOptions.IndexID is the default knowledge base id. Per-call options.Namespace (UpsertOptions, QueryOptions, …) overrides IndexID when set.

Upsert: submits SubmitIndexAddDocumentsJob. Each Record must reference an existing Data Center document id in Record.ID or metadata keys document_id / bailian_document_id.

Query: maps to Retrieve (server-side dense retrieval + rerank). No local Embedder.

List: ListIndexDocuments; ListOptions.Offset is a 1-based page number string.

Get: scans ListIndexDocuments pages until all requested ids are found (bounded pages).

func (*AliyunHandler) Delete

func (h *AliyunHandler) Delete(ctx context.Context, ids []string, options *DeleteOptions) error

func (*AliyunHandler) Get

func (h *AliyunHandler) Get(ctx context.Context, ids []string, options *GetOptions) ([]Record, error)

func (*AliyunHandler) List

func (h *AliyunHandler) List(ctx context.Context, options *ListOptions) (*ListResult, error)

func (*AliyunHandler) Provider

func (h *AliyunHandler) Provider() string

func (*AliyunHandler) Query

func (h *AliyunHandler) Query(ctx context.Context, text string, options *QueryOptions) ([]QueryResult, error)

func (*AliyunHandler) Upsert

func (h *AliyunHandler) Upsert(ctx context.Context, records []Record, options *UpsertOptions) error

type AliyunOptions

type AliyunOptions struct {
	AccessKeyID     string
	AccessKeySecret string
	// Endpoint defaults to DefaultBailianEndpoint (Beijing public endpoint).
	Endpoint string
	// RegionID defaults to cn-beijing when Endpoint is empty.
	RegionID string
	// WorkspaceID is the business space id from the Bailian console.
	WorkspaceID string
	// IndexID is the knowledge base id (CreateIndex / console).
	IndexID string
}

AliyunOptions configures Alibaba Cloud Model Studio (百炼) knowledge access. Use AccessKey pair with Bailian data permissions (e.g. AliyunBailianDataFullAccess for RAM users).

type Chunk

type Chunk struct {
	Index    int
	Title    string
	Text     string
	Metadata map[string]any
}

Chunk is one retrieval-oriented segment produced by LLMChunker.

type ChunkOptions

type ChunkOptions struct {
	MaxChars      int
	OverlapChars  int
	MinChars      int
	DocumentTitle string
	// PreChunkClean is passed to utils.CleanText before the LLM call (UTF-8 repair, optional markdown strip, etc.).
	// If nil, StripMarkdown and DedupLines are enabled so code fences / mermaid are less likely to break JSON output.
	PreChunkClean *utils.Options
}

type Chunker

type Chunker interface {
	Provider() string
	Chunk(ctx context.Context, text string, opts *ChunkOptions) ([]Chunk, error)
}

Chunker splits long text into chunks (implementations may use an LLM).

type DeleteOptions

type DeleteOptions struct {
	Namespace string
}

type Embedder

type Embedder interface {
	Embed(ctx context.Context, inputs []string) ([][]float64, error)
}

type FactoryOptions

type FactoryOptions struct {
	Qdrant *QdrantOptions
	Aliyun *AliyunOptions
}

type GetOptions

type GetOptions struct {
	Namespace string
}

type KnowledgeHandler

type KnowledgeHandler interface {
	Provider() string

	Upsert(ctx context.Context, records []Record, options *UpsertOptions) error

	Query(ctx context.Context, text string, options *QueryOptions) ([]QueryResult, error)

	Get(ctx context.Context, ids []string, options *GetOptions) ([]Record, error)

	List(ctx context.Context, options *ListOptions) (*ListResult, error)

	Delete(ctx context.Context, ids []string, options *DeleteOptions) error
}

func New

func New(provider string, opts *FactoryOptions) (KnowledgeHandler, error)

type KnowledgeProvider

type KnowledgeProvider string

KnowledgeProvider common provider type

func (KnowledgeProvider) ToString

func (kp KnowledgeProvider) ToString() string

ToString toString for llm

type LLMChunker

type LLMChunker struct {
	LLM   llm.LLMHandler
	Model string
}

LLMChunker asks an LLM to return a JSON array of chunks.

func (*LLMChunker) Chunk

func (c *LLMChunker) Chunk(ctx context.Context, text string, opts *ChunkOptions) ([]Chunk, error)

func (*LLMChunker) Provider

func (c *LLMChunker) Provider() string

type ListOptions

type ListOptions struct {
	Namespace string
	Limit     int
	Offset    string
	Filters   map[string]any
}

type ListResult

type ListResult struct {
	Records    []Record
	NextOffset string
}

type NvidiaEmbedClient

type NvidiaEmbedClient struct {
	BaseURL    string
	APIKey     string
	Model      string
	HTTPClient *http.Client
}

func (*NvidiaEmbedClient) Embed

func (c *NvidiaEmbedClient) Embed(ctx context.Context, inputs []string) ([][]float64, error)

type QdrantHandler

type QdrantHandler struct {
	BaseURL    string
	APIKey     string
	Collection string
	HTTPClient *http.Client
	Embedder   Embedder
}

func (*QdrantHandler) Delete

func (qh *QdrantHandler) Delete(ctx context.Context, ids []string, options *DeleteOptions) error

func (*QdrantHandler) Get

func (qh *QdrantHandler) Get(ctx context.Context, ids []string, options *GetOptions) ([]Record, error)

func (*QdrantHandler) List

func (qh *QdrantHandler) List(ctx context.Context, options *ListOptions) (*ListResult, error)

func (*QdrantHandler) Provider

func (qh *QdrantHandler) Provider() string

func (*QdrantHandler) Query

func (qh *QdrantHandler) Query(ctx context.Context, text string, options *QueryOptions) ([]QueryResult, error)

func (*QdrantHandler) Upsert

func (qh *QdrantHandler) Upsert(ctx context.Context, records []Record, options *UpsertOptions) error

type QdrantOptions

type QdrantOptions struct {
	BaseURL    string
	APIKey     string
	Collection string
	HTTPClient *http.Client
	Embedder   Embedder
}

type QueryOptions

type QueryOptions struct {
	Namespace string
	TopK      int
	Filters   map[string]any
}

type QueryResult

type QueryResult struct {
	Record Record
	Score  float64
}

type Record

type Record struct {
	ID        string
	Source    string
	Title     string
	Content   string
	Tags      []string
	Metadata  map[string]any
	CreatedAt time.Time
	UpdatedAt time.Time
}

type RerankResult

type RerankResult struct {
	Index int
	Score float64
}

type SiliconFlowRerankClient

type SiliconFlowRerankClient struct {
	BaseURL    string
	APIKey     string
	Model      string
	HTTPClient *http.Client
}

func (*SiliconFlowRerankClient) Rerank

func (c *SiliconFlowRerankClient) Rerank(ctx context.Context, query string, documents []string, topN int) ([]RerankResult, error)

type UpsertOptions

type UpsertOptions struct {
	Namespace string
	Overwrite bool
}

Jump to

Keyboard shortcuts

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