tiktokengrpc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

README

tiktoken-grpc-go

tiktoken-grpc-go provides Go client bindings for the tiktoken gRPC service.

Usage

This example targets a tiktoken grpc service running locally. You can modify the host param in the NewClient call to target a different instance.

package main

import (
	"context"
	"fmt"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	tiktokengrpc "github.com/yorinasub17/tiktoken-grpc-go"
)

func main() {
	clt, err := tiktokengrpc.NewClient("localhost", 50051, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	defer clt.Close()

	ctx := context.Background()
	text := "antidisestablishmentarianism"

	cl100k, err := clt.NumTokens(ctx, tiktokengrpc.CL100kBase, text)
	if err != nil {
		panic(err)
	}
	fmt.Printf("cl100k_base: %d tokens\n", cl100k)

	p50k, err := clt.NumTokens(ctx, tiktokengrpc.P50kBase, text)
	if err != nil {
		panic(err)
	}
	fmt.Printf("p50k_base: %d tokens\n", p50k)

	gpt2, err := clt.NumTokens(ctx, tiktokengrpc.GPT2Encoding, text)
	if err != nil {
		panic(err)
	}
	fmt.Printf("gpt2: %d tokens\n", gpt2)
}

Installation

go get github.com/yorinasub17/tiktoken-grpc-go

License

This project is licensed under the 3-clause BSD License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client manages the connection to the Tiktoken gRPC service and provides the main interface for sending requests and receiving responses to/from the service.

func NewClient

func NewClient(host string, port int, opts ...grpc.DialOption) (Client, error)

NewClient initializes a new connection to the grpc service using the provided options.

func (Client) Close

func (c Client) Close() error

Close tears down the client connection to the service.

func (Client) NumTokens

func (c Client) NumTokens(ctx context.Context, encodingName TiktokenEncodingName, text string) (int32, error)

NumTokens calls the NumTokens API of the tiktoken gRPC service for the given text, with the encoding identified by name.

func (Client) NumTokensByModel

func (c Client) NumTokensByModel(ctx context.Context, modelName string, text string) (int32, error)

NumTokensByModel calls the NumTokens API of the tiktoken gRPC service for the given text, with the encoding identified by model name.

type InvalidEncodingNameErr

type InvalidEncodingNameErr TiktokenEncodingName

InvalidEncodingNameErr is returned if the given TiktokenEncodingName is not recognized by the library.

func (InvalidEncodingNameErr) Error

func (e InvalidEncodingNameErr) Error() string

type TiktokenEncodingName

type TiktokenEncodingName string

TiktokenEncodingName is an enum for valid encoding names accepted by the tiktoken service.

const (
	CL100kBase   TiktokenEncodingName = "cl100k_base"
	P50kBase     TiktokenEncodingName = "p50k_base"
	R50kBase     TiktokenEncodingName = "r50k_base"
	GPT2Encoding TiktokenEncodingName = "gpt2"
)

func (TiktokenEncodingName) IsValid

func (n TiktokenEncodingName) IsValid() bool

IsValid returns whether the given tiktoken encoding name fits one of the enum values, since go enums can be casted with bogus values easily.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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