-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverbaflow.go
93 lines (82 loc) · 2.79 KB
/
verbaflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2023 NLP Odyssey Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package verbaflow
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/nlpodyssey/spago/ag"
"github.com/nlpodyssey/spago/embeddings/store/diskstore"
"github.com/nlpodyssey/verbaflow/decoder"
"github.com/nlpodyssey/verbaflow/encoder"
"github.com/nlpodyssey/verbaflow/rwkvlm"
"github.com/nlpodyssey/verbaflow/tokenizer"
"github.com/rs/zerolog/log"
)
// VerbaFlow is the core struct of the library.
type VerbaFlow struct {
Model *rwkvlm.Model
Tokenizer tokenizer.Tokenizer
embeddingsRepo *diskstore.Repository
}
// Load loads a VerbaFlow model from the given directory.
func Load(modelDir string) (*VerbaFlow, error) {
tk, err := tokenizer.Load(modelDir)
if err != nil {
return nil, err
}
model, err := rwkvlm.Load(modelDir)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("error: unable to find the model file or directory '%s'. Please ensure that the model has been successfully downloaded and converted before trying again", modelDir)
}
return nil, err
}
embeddingsRepo, err := diskstore.NewRepository(filepath.Join(modelDir, rwkvlm.DefaultEmbeddingRepoPath), diskstore.ReadOnlyMode)
if err != nil {
return nil, fmt.Errorf("failed to load embeddings repository: %w", err)
}
err = model.ApplyEmbeddings(embeddingsRepo)
if err != nil {
return nil, fmt.Errorf("failed to apply embeddings: %w", err)
}
return &VerbaFlow{
Model: model,
Tokenizer: tk,
embeddingsRepo: embeddingsRepo,
}, nil
}
// Close closes the model resources.
func (vf *VerbaFlow) Close() error {
return vf.embeddingsRepo.Close()
}
// Generate generates a text from the given prompt.
// The "out" channel is used to stream the generated text.
// The generated text will be at most `maxTokens` long (in addition to the prompt).
func (vf *VerbaFlow) Generate(ctx context.Context, nt *ag.NodesTracker, prompt string, chGen chan decoder.GeneratedToken, opts decoder.DecodingOptions) error {
log.Trace().Msgf("Tokenizing prompt: %q", prompt)
tokenized, err := vf.Tokenizer.Tokenize(prompt)
if err != nil {
return err
}
log.Trace().Msgf("Preprocessing %d token IDs: %v", len(tokenized), tokenized)
start := time.Now()
encoderOutput, err := encoder.New(vf.Model).Encode(ctx, tokenized)
if err != nil {
return err
}
log.Trace().Msgf("Preprocessing took %s", time.Since(start))
log.Trace().Msg("Generating...")
d, err := decoder.New(vf.Model, opts)
if err != nil {
return err
}
return d.Decode(ctx, nt, encoderOutput, chGen)
}
// TokenByID returns the token string for the given token ID.
func (vf *VerbaFlow) TokenByID(id int) (string, error) {
return vf.Tokenizer.ReconstructText([]int{id})
}