Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transform-sdk/go/ai: embeddings example
Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
- Loading branch information
Showing
3 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ PKGS=( | |
wasi | ||
dynamic | ||
llm-prompt-forward | ||
make-embeddings | ||
) | ||
|
||
for PKG in "${PKGS[@]}"; do | ||
|
42 changes: 42 additions & 0 deletions
42
src/transform-sdk/go/transform/internal/testdata/make-embeddings/transform.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"unsafe" | ||
|
||
"github.com/redpanda-data/redpanda/src/transform-sdk/go/transform" | ||
"github.com/redpanda-data/redpanda/src/transform-sdk/go/transform/ai" | ||
) | ||
|
||
var model *ai.EmbeddingsModel | ||
|
||
func main() { | ||
m, err := ai.LoadEmbeddingsModel(ai.HuggingFaceModel{ | ||
Repo: "leliuga/all-MiniLM-L12-v2-GGUF", | ||
File: "all-MiniLM-L12-v2.Q8_0.gguf", | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
model = m | ||
transform.OnRecordWritten(myTransform) | ||
} | ||
|
||
func myTransform(e transform.WriteEvent, w transform.RecordWriter) error { | ||
// zero copy compute embeddings | ||
val := e.Record().Value | ||
str := unsafe.String(&val[0], len(val)) | ||
resp, err := model.ComputeEmbeddings(str) | ||
if err != nil { | ||
return err | ||
} | ||
// Turn the embeddings into JSON | ||
b, err := json.Marshal(resp) | ||
if err != nil { | ||
return err | ||
} | ||
return w.Write(transform.Record{ | ||
Key: e.Record().Key, | ||
Value: b, | ||
}) | ||
} |