gpt3encoder

package module
v0.0.0-...-67773d2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 13 Imported by: 0

README

go-gpt-3-encoder

Go BPE tokenizer (Encoder+Decoder) for openai models(not including GPT2)

About

This code is inspired by and forked from samber/go-gpt-3-encoder

but main logic is rewrite from openai/tiktoken

encode logic see _byte_pair_merge in lib.rs

Install

go get github.com/YEXINGZHE54/go-gpt-3-encoder

Usage

import tokenizer "github.com/YEXINGZHE54/go-gpt-3-encoder"

encoder, err := tokenizer.NewEncoder("gpt-3.5-turbo")
if err != nil {
    log.Fatal(err)
}

str := "This is an example sentence to try encoding out on!"

encoded, err := encoder.Encode(str)
if err != nil {
    log.Fatal(err)
}

fmt.Println("We can look at each token and what it represents:")
for _, token := range encoded {
    fmt.Printf("%d -- %s\n", token, encoder.Decode([]int{token}))
}

decoded := encoder.Decode(encoded)
fmt.Printf("We can decode it back into: %s\n", decoded)

Contribute

Some corner cases are not covered by this library. See @TODO in tests.

Documentation

Index

Constants

View Source
const (
	ENDOFTEXT   = "<|endoftext|>"
	FIM_PREFIX  = "<|fim_prefix|>"
	FIM_MIDDLE  = "<|fim_middle|>"
	FIM_SUFFIX  = "<|fim_suffix|>"
	ENDOFPROMPT = "<|endofprompt|>"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BytePair

type BytePair struct {
	// contains filtered or unexported fields
}

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(model string) (enc *Encoder, err error)

func NewEncoderFromConfig

func NewEncoderFromConfig(conf *encoderConfig) (*Encoder, error)

func (*Encoder) Decode

func (e *Encoder) Decode(tokens []int) string

func (*Encoder) Encode

func (e *Encoder) Encode(text string) ([]int, error)

Jump to

Keyboard shortcuts

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