gpt3encoder

package module
v0.0.0-...-c970ae9 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 8 Imported by: 0

README

go-gpt-3-encoder

Go BPE tokenizer (Encoder+Decoder) for GPT2 and GPT3.

About

GPT2 and GPT3 use byte pair encoding to turn text into a series of integers to feed into the model. This is a Go implementation of OpenAI's original Python encoder/decoder which can be found here.

This code was inspired by Javascript implementation and partially generated by OpenAI himself!

Install

github.com/danielswe88/go-gpt-3-encoder

Usage

Compatible with Node >= 12

package main

import (
 "fmt"
 "log"

 tokenizer "github.com/danielswe88/go-gpt-3-encoder"
)

func main() {
 encoder, err := tokenizer.NewEncoder()
 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.Printf("String contains %d tokens\n", len(encoded))

 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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

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

func NewEncoder

func NewEncoder() (*Encoder, error)

func NewEncoderWithVocab

func NewEncoderWithVocab(bpeData []byte, jsonEncoder []byte) (*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