-
Notifications
You must be signed in to change notification settings - Fork 553
/
model.go
124 lines (111 loc) · 3.15 KB
/
model.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2023 Redpanda Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ai
import (
"encoding/binary"
"errors"
"math"
"strconv"
"unsafe"
"github.com/redpanda-data/redpanda/src/transform-sdk/go/transform/internal/rwbuf"
)
type modelHandle int32
var buf *rwbuf.RWBuf = rwbuf.New(10240)
// A file from hugging face, only supports gguf file format.
type HuggingFaceModel struct {
Repo string
File string
}
// LoadLargeLanguageModel from hugging face
func LoadLargeLanguageModel(m HuggingFaceModel) (*LargeLanguageModel, error) {
r := loadHfLLM(
unsafe.Pointer(unsafe.StringData(m.Repo)),
int32(len(m.Repo)),
unsafe.Pointer(unsafe.StringData(m.File)),
int32(len(m.File)),
)
if r < 0 {
return nil, errors.New("unable to load model, errorcode: " + strconv.Itoa(int(r)))
}
return &LargeLanguageModel{modelHandle(r)}, nil
}
// LoadEmbeddingsModel from hugging face
func LoadEmbeddingsModel(m HuggingFaceModel) (*EmbeddingsModel, error) {
r := loadHfEmbeddings(
unsafe.Pointer(unsafe.StringData(m.Repo)),
int32(len(m.Repo)),
unsafe.Pointer(unsafe.StringData(m.File)),
int32(len(m.File)),
)
if r < 0 {
return nil, errors.New("unable to load model, errorcode: " + strconv.Itoa(int(r)))
}
return &EmbeddingsModel{modelHandle(r)}, nil
}
// Wrapper around LLM
type LargeLanguageModel struct {
handle modelHandle
}
// Generate tokens
type GenerateTextOptions struct {
MaxTokens int
}
// GenerateText from an LLM
func (llm *LargeLanguageModel) GenerateText(prompt string, opts GenerateTextOptions) (string, error) {
buf.Reset()
r := generateText(
int32(llm.handle),
unsafe.Pointer(unsafe.StringData(prompt)),
int32(len(prompt)),
int32(opts.MaxTokens),
unsafe.Pointer(buf.WriterBufPtr()),
int32(buf.WriterLen()),
)
if r < 0 {
return "", errors.New("unable to generate text, errorcode: " + strconv.Itoa(int(r)))
}
buf.AdvanceWriter(int(r))
b, err := buf.ReadSlice(int(r))
if err != nil {
return "", err
}
return string(b), nil
}
type EmbeddingsModel struct {
handle modelHandle
}
// ComputeEmbeddings from a model
func (llm *EmbeddingsModel) ComputeEmbeddings(text string) ([]float32, error) {
buf.Reset()
r := computeEmbeddings(
int32(llm.handle),
unsafe.Pointer(unsafe.StringData(text)),
int32(len(text)),
unsafe.Pointer(buf.WriterBufPtr()),
int32(buf.WriterLen()),
)
if r < 0 {
return nil, errors.New("unable to generate text, errorcode: " + strconv.Itoa(int(r)))
}
b, err := buf.ReadSlice(int(r))
if err != nil {
return nil, err
}
out := make([]float32, int(r)/4)
for i := 0; i < len(out); i += 1 {
bits := binary.LittleEndian.Uint32(b[i*4:])
out[i] = math.Float32frombits(bits)
}
return out, nil
}