util

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	STATE_NORMAL = iota
	STATE_NEWLINE
	STATE_ONE_TICK
	STATE_TWO_TICKS
	STATE_THREE_TICKS
	STATE_BLOCK
	STATE_BLOCK_NEWLINE
	STATE_BLOCK_ONE_TICK
	STATE_BLOCK_TWO_TICKS
	STATE_BLOCK_THREE_TICKS
	STATE_INLINE
)

Variables

This section is empty.

Functions

func ByteToString

func ByteToString(b [][]byte) []string

Cast an array of byte arrays to an array of strings

func ChunkFile

func ChunkFile(
	fs afero.Fs,
	path string,
	chunkSize int,
	maxChunks int,
	callback func(int, []byte) error) error

Read a file, break into chunks of a given number of bytes, up to a maximum number of chunks, and call the callback for each chunk

func ChunkFromReader

func ChunkFromReader(
	reader io.Reader,
	chunkSize int,
	maxChunks int,
	callback func(int, []byte) error) error

func ForEachSubdir

func ForEachSubdir(fs afero.Fs, path string,
	callback func(path string) error) error

Call a callback for each subdirectory in a given path

func GetChunks

func GetChunks(reader io.Reader, chunkSize int, maxChunks int) ([][]byte, error)

func GetFileChunks

func GetFileChunks(ctx context.Context, fs afero.Fs, path string,
	chunkSize int, maxChunks int) ([][]byte, error)

Given a filesystem, a path, a chunk size, and maximum number of chunks, return a list of chunks of the file at the given path

func HistoryBlocksToString added in v0.0.20

func HistoryBlocksToString(blocks []HistoryBlock) string

func InitLogging added in v0.2.4

func InitLogging(ctx context.Context) string

Open a log file named butterfish.log in a temporary directory

func IsPipedStdin

func IsPipedStdin() bool

Returns true if there is piped stdin data that can be read

func Min

func Min(a, b int) int

func MultilineLipglossRender

func MultilineLipglossRender(style lipgloss.Style, str string) string

Lipgloss is a little tricky - if you render a string with newlines it turns it into a "block", i.e. each line will be padding to be the same length. This is not what we want, so we split on newlines and render each line separately.

Types

type CacheWriter

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

A io.Writer that caches bytes written and forwards writes to another writer

func NewCacheWriter

func NewCacheWriter(forward io.Writer) *CacheWriter

func (*CacheWriter) GetCache

func (this *CacheWriter) GetCache() []byte

func (*CacheWriter) GetLastN

func (this *CacheWriter) GetLastN(n int) []byte

func (*CacheWriter) Write

func (this *CacheWriter) Write(p []byte) (n int, err error)

type ColorWriter added in v0.0.27

type ColorWriter struct {
	Color  string
	Writer io.Writer
}

func NewColorWriter added in v0.0.27

func NewColorWriter(writer io.Writer, color string) *ColorWriter

func (*ColorWriter) Write added in v0.0.27

func (this *ColorWriter) Write(p []byte) (n int, err error)

type CompletionRequest

type CompletionRequest struct {
	Ctx           context.Context
	Prompt        string
	Model         string
	MaxTokens     int
	Temperature   float32
	HistoryBlocks []HistoryBlock
	SystemMessage string
	Functions     []FunctionDefinition
	Tools         []ToolDefinition
	Verbose       bool
	TokenTimeout  time.Duration
}

We define types for calling LLM APIs here because I don't want the internal interfaces to depend on OpenAI-specific types.

type CompletionResponse added in v0.1.4

type CompletionResponse struct {
	Completion         string
	FunctionName       string
	FunctionParameters string
	ToolCalls          []*ToolCall
}

type FunctionCall added in v0.2.5

type FunctionCall struct {
	Name       string
	Parameters string
}

type FunctionDefinition added in v0.1.4

type FunctionDefinition struct {
	Name        string                `json:"name"`
	Description string                `json:"description,omitempty"`
	Parameters  jsonschema.Definition `json:"parameters"`
}

type HistoryBlock added in v0.0.20

type HistoryBlock struct {
	Type           int
	Content        string
	FunctionName   string
	FunctionParams string
	ToolCalls      []*ToolCall
	ToolCallId     string
}

func (HistoryBlock) String added in v0.0.20

func (this HistoryBlock) String() string

type ReplaceWriter added in v0.0.20

type ReplaceWriter struct {
	Writer io.Writer
	From   string
	To     string
}

A Writer implementation that allows you to string replace the content flowing through

func NewReplaceWriter added in v0.0.20

func NewReplaceWriter(writer io.Writer, from string, to string) *ReplaceWriter

func (*ReplaceWriter) Write added in v0.0.20

func (this *ReplaceWriter) Write(p []byte) (n int, err error)

type StripbackticksWriter added in v0.2.4

type StripbackticksWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

func NewStripbackticksWriter added in v0.2.4

func NewStripbackticksWriter(writer io.Writer) *StripbackticksWriter

func (*StripbackticksWriter) Write added in v0.2.4

func (this *StripbackticksWriter) Write(p []byte) (n int, err error)

This writer receives bytes in a stream and looks for markdown code blocks (```golang) and strips only the backticks, leaving the code.

type StyleCodeblocksWriter added in v0.2.4

type StyleCodeblocksWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

func NewStyleCodeblocksWriter added in v0.2.4

func NewStyleCodeblocksWriter(
	writer io.Writer,
	terminalWidth int,
	normalColor string,
	highlightColor string,
) *StyleCodeblocksWriter

func (*StyleCodeblocksWriter) EndOfCodeBlock added in v0.2.4

func (this *StyleCodeblocksWriter) EndOfCodeBlock(w io.Writer) error

func (*StyleCodeblocksWriter) EndOfCodeLine added in v0.2.4

func (this *StyleCodeblocksWriter) EndOfCodeLine(w io.Writer) error

func (*StyleCodeblocksWriter) Reset added in v0.2.9

func (this *StyleCodeblocksWriter) Reset()

func (*StyleCodeblocksWriter) SetTerminalWidth added in v0.2.4

func (this *StyleCodeblocksWriter) SetTerminalWidth(width int)

func (*StyleCodeblocksWriter) Write added in v0.2.4

func (this *StyleCodeblocksWriter) Write(p []byte) (n int, err error)

This writer receives bytes in a stream and looks for markdown code blocks (```) and renders them with syntax highlighting. The hard part is the stream splits the input into chunks, so we need to buffer the input in places.

type StyledWriter

type StyledWriter struct {
	Writer io.Writer
	Style  lipgloss.Style
	// contains filtered or unexported fields
}

An implementation of io.Writer that renders output with a lipgloss style and filters out the special token "NOOP". This is specially handled - we seem to get "NO" as a separate token from GPT.

func NewStyledWriter

func NewStyledWriter(writer io.Writer, style lipgloss.Style) *StyledWriter

func (*StyledWriter) Write

func (this *StyledWriter) Write(input []byte) (int, error)

Writer for StyledWriter This is a bit insane but it's a dumb way to filter out NOOP split into two tokens, should probably be rewritten

type ToolCall added in v0.2.5

type ToolCall struct {
	Id       string
	Type     string
	Function FunctionCall
}

type ToolDefinition added in v0.2.5

type ToolDefinition struct {
	Type     string             `json:"type"`
	Function FunctionDefinition `json:"function"`
}

Jump to

Keyboard shortcuts

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