Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrModelMissing = errors.New("model is required") ErrVocabularyMissing = errors.New("vocabulary is required") ErrLexicalShortlistMissing = errors.New("lexical shortlist is required") )
var ErrClosed = errors.New("pool closed")
Functions ¶
func DefaultBergamotOptions ¶
DefaultBergamotOptions provides default options for WASM Bergamot translator worker like in https://github.com/browsermt/bergamot-translator/blob/v0.4.5/wasm/node-test.js#L66
Types ¶
type Config ¶
type Config struct {
wasm.CompileConfig
// From Bergamot sources:
//
// Size in History items to be stored in the cache. A value of 0 means no caching. Loosely corresponds to sentences
// to cache in the real world. Note that cache has a random-eviction policy. The peak storage at full occupancy is
// controlled by this parameter. However, whether we attain full occupancy or not is controlled by random factors -
// specifically how uniformly the hash distributes.
CacheSize uint
// Data to load into translator
FilesBundle
// From Bergamot sources:
//
// Equivalent to options based constructor, where `options` is parsed from string configuration. Configuration can be
// JSON or YAML. Keys expected correspond to those of `marian-decoder`, available at
// https://marian-nmt.github.io/docs/cmd/marian-decoder/
BergamotOptions map[string]any
WASMCache wazero.CompilationCache
// WASMUseContext defines if WASM functions execution must be canceled upon context.Context cancellation.
// Equivalent to wazero.RuntimeConfig WithCloseOnContextDone method parameter.
WASMUseContext bool
}
type FilesBundle ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
func NewPool ¶
func NewPool(ctx context.Context, cfg PoolConfig) (*Pool, error)
NewPool compiles Translator instances and runs them as workers.
func (*Pool) Translate ¶
Translate is similar to Translator.Translate except the request is asynchronously given to any free worker in the pool.
func (*Pool) TranslateMultiple ¶
func (p *Pool) TranslateMultiple(ctx context.Context, requests ...TranslationRequest) ([]string, error)
TranslateMultiple is similar to Translator.TranslateMultiple except the requests are asynchronously given to any free worker in the pool.
type PoolConfig ¶
func (PoolConfig) Validate ¶
func (cfg PoolConfig) Validate() error
type TranslationOptions ¶
type TranslationOptions struct {
// HTML defines if the Translator should remove HTML tags from text and insert them in output.
HTML bool
}
TranslationOptions are equivalent to ResponseOptions in Bergamot. From sources:
ResponseOptions dictate how to construct a Response for an input string of text to be translated.
type TranslationRequest ¶
type TranslationRequest struct {
// Text to be translated
Text string
// Options for translation
Options TranslationOptions
}
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator represents a Bergamot translator worker in Go.
func New ¶
func New(ctx context.Context, cfg Config) (*Translator, error)
New compiles Bergamot module and creates TranslationModel and BlockingService instances to be used in Translator
func (*Translator) Close ¶
func (t *Translator) Close(ctx context.Context) error
Close deletes created objects and stops the WASM runtime
func (*Translator) Translate ¶
func (t *Translator) Translate(ctx context.Context, request TranslationRequest) (string, error)
Translate translates text provided in the request into a model target language.
func (*Translator) TranslateMultiple ¶
func (t *Translator) TranslateMultiple(ctx context.Context, requests ...TranslationRequest) ([]string, error)
TranslateMultiple translates a batch of text provided in the requests into a model target language.