file

package module
v0.0.0-...-8fe9471 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 10

README

File Loader for Eino

Introduction

This is a file loader component for Eino. It implements the Loader interface and can be seamlessly integrated into Eino's document processing workflow to load local files.

Features

  • Implements github.com/cloudwego/eino/components/document.Loader interface
  • Easy integration into Eino workflows
  • Supports automatic file parsing based on extension
  • Customizable parser configuration
  • Built-in callback support

Installation

go get github.com/cloudwego/eino-ext/components/document/loader/file

Quick Start

package main

import (
	"context"
	"log"

	"github.com/cloudwego/eino/components/document"
	"github.com/cloudwego/eino-ext/components/document/loader/file"
)

func main() {
	ctx := context.Background()

	loader, err := file.NewFileLoader(ctx, &file.FileLoaderConfig{
		UseNameAsID: true,
	})
	if err != nil {
		log.Fatalf("file.NewFileLoader failed, err=%v", err)
	}

	filePath := "./document.txt"
	docs, err := loader.Load(ctx, document.Source{
		URI: filePath,
	})
	if err != nil {
		log.Fatalf("loader.Load failed, err=%v", err)
	}

	log.Printf("doc content: %v", docs[0].Content)
	log.Printf("Extension: %s\n", docs[0].MetaData[file.MetaKeyExtension])
	log.Printf("Source: %s\n", docs[0].MetaData[file.MetaKeySource])
}

Configuration

The loader can be configured through the FileLoaderConfig structure:

type FileLoaderConfig struct {
    // UseNameAsID uses the file name as the document ID
    // Optional. Default: false
    UseNameAsID bool
    
    // Parser specifies the parser to use for file content
    // Optional. Default: ExtParser with TextParser fallback
    Parser parser.Parser
}

Metadata

The loader automatically adds the following metadata to loaded documents:

  • _file_name: File name (basename)
  • _extension: File extension
  • _source: File path (URI)

Using in Chain

chain := compose.NewChain[document.Source, []*schema.Document]()
chain.AppendLoader(loader)

run, err := chain.Compile(ctx)
if err != nil {
    log.Fatalf("chain.Compile failed, err=%v", err)
}

docs, err := run.Invoke(ctx, document.Source{URI: filePath})

Examples

For more examples, please refer to the examples directory:

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	MetaKeyFileName  = "_file_name"
	MetaKeyExtension = "_extension"
	MetaKeySource    = "_source"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FileLoader

type FileLoader struct {
	FileLoaderConfig
}

FileLoader loads a local file and use its content directly as Document's content.

func NewFileLoader

func NewFileLoader(ctx context.Context, config *FileLoaderConfig) (*FileLoader, error)

NewFileLoader creates a new FileLoader.

func (*FileLoader) GetType

func (f *FileLoader) GetType() string

func (*FileLoader) IsCallbacksEnabled

func (f *FileLoader) IsCallbacksEnabled() bool

func (*FileLoader) Load

func (f *FileLoader) Load(ctx context.Context, src document.Source, opts ...document.LoaderOption) (docs []*schema.Document, err error)

type FileLoaderConfig

type FileLoaderConfig struct {
	UseNameAsID bool
	Parser      parser.Parser
}

Directories

Path Synopsis
examples
customloader command
fileloader command

Jump to

Keyboard shortcuts

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