internal

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CacheBucketCaches = "caches"

	CacheBucketOrder = "order"
)
View Source
const (
	HookTypePreMessage  = "pre-message"
	HookTypePostMessage = "post-message"
	HookTypeFinish      = "finish"
)
View Source
const (
	BucketConversations = "conversations"
	BucketNameIndex     = "names"
)
View Source
const AppHomeEnvKey = "GPTX_HOME"

Variables

View Source
var (
	// Version is the version of this software.
	// This value is overwritten by the build script.
	Version = "0.0.0"

	// CommitHash is the git commit hash of this software.
	// This value is overwritten by the build script.
	CommitHash = "unknown"
)
View Source
var ChatCommand = &cli.Command{
	Name:      "chat",
	Aliases:   []string{"c"},
	Usage:     "Chat with GPT",
	ArgsUsage: "[prompt...]",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:               "no-loading",
			Usage:              "Disable loading animation",
			DisableDefaultText: true,
		},
		&cli.BoolFlag{
			Name:               "no-animation",
			Usage:              "Disable typing animation",
			DisableDefaultText: true,
		},
		&cli.StringFlag{
			Name:    "name",
			Aliases: []string{"n"},
			Usage:   "Specify a `name` for the conversation",
		},
		&cli.StringFlag{
			Name:    "label",
			Aliases: []string{"l"},
			Usage:   "Specify a `label` for the conversation",
		},
		&cli.StringFlag{
			Name:    "resume",
			Aliases: []string{"r"},
			Usage:   "Resume a `conversation`. You can specify a conversation id or name",
		},
		&cli.BoolFlag{
			Name:               "editor",
			Aliases:            []string{"e"},
			Usage:              "Open $EDITOR to make a prompt.",
			DisableDefaultText: true,
		},
		&cli.StringFlag{
			Name:  "model",
			Usage: "Specify a `model` working with ChatGPT",
		},
		&cli.Float64Flag{
			Name:  "temperature",
			Usage: "Specify a temperature",
			Value: 1,
		},
		&cli.Float64Flag{
			Name:  "top-p",
			Usage: "Specify a top_p",
			Value: 1,
		},
		&cli.StringSliceFlag{
			Name:    "hook",
			Aliases: []string{"H"},
			Usage:   "Specify a `hook`s",
		},
		&cli.StringSliceFlag{
			Name:    "env",
			Aliases: []string{"E"},
			Usage:   "Specify a environment variable for hooks. For example: -E FOO=BAR -E BAZ=QUX",
		},
		&cli.BoolFlag{
			Name:               "interactive",
			Aliases:            []string{"i"},
			Usage:              "Run in interactive mode",
			DisableDefaultText: true,
		},
		&cli.BoolFlag{
			Name:               "no-cache",
			Usage:              "Disable cache",
			DisableDefaultText: true,
		},
		&cli.BoolFlag{
			Name:               "on-memory",
			Aliases:            []string{"m"},
			Usage:              "Disable the ability to save or load the conversation to and from the disk, which also includes the behavior of the --no-cache option.",
			DisableDefaultText: true,
		},
	},
	Action: chatAction,
}
View Source
var CleanCommand = &cli.Command{
	Name:   "clean",
	Usage:  "Clean up the cache",
	Action: cleanAction,
}
View Source
var ConfigCommand = &cli.Command{
	Name:  "config",
	Usage: "Display configuration",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:               "pretty",
			Aliases:            []string{"p"},
			Usage:              "Pretty print",
			DisableDefaultText: true,
		},
	},
	Action: configAction,
}
View Source
var DeleteCommand = &cli.Command{
	Name:      "remove",
	Aliases:   []string{"rm"},
	Usage:     "Remove one or more conversations",
	ArgsUsage: `[conversation_id...]`,
	Action:    deleteAction,
}
View Source
var InitCommand = &cli.Command{
	Name:  "init",
	Usage: "Init a gptx home directory",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "directory",
			Aliases: []string{"d"},
			Usage:   "Specify a `directory` to init",
		},
	},
	Action: initAction,
}
View Source
var InspectCommand = &cli.Command{
	Name:      "inspect",
	Aliases:   []string{"i"},
	Usage:     "Display details information on one or more conversations",
	ArgsUsage: `[conversation...]`,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:               "pretty",
			Aliases:            []string{"p"},
			Usage:              "Pretty print",
			DisableDefaultText: true,
		},
	},
	Action: inspectAction,
}
View Source
var ListCommand = &cli.Command{
	Name:    "list",
	Aliases: []string{"ls"},
	Usage:   "List conversations",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "begin",
			Aliases: []string{"b"},
			Usage:   "Load the conversations starting from the given conversation `id`",
		},
		&cli.BoolFlag{
			Name:               "reverse",
			Aliases:            []string{"r"},
			Usage:              "Sort the conversations in descending id order",
			DisableDefaultText: true,
		},
		&cli.IntFlag{
			Name:        "limit",
			Aliases:     []string{"L"},
			Usage:       "Limit the `number` of displayed conversations",
			DefaultText: "0 (no limit)",
		},
		&cli.StringFlag{
			Name:    "label",
			Aliases: []string{"l"},
			Usage:   "Filter the conversations by `label`",
		},
		&cli.BoolFlag{
			Name:               "quiet",
			Aliases:            []string{"q"},
			Usage:              "Only display the conversation ids",
			DisableDefaultText: true,
		},
	},
	Action: listAction,
}
View Source
var RenameCommand = &cli.Command{
	Name:      "rename",
	Usage:     "Rename a conversation",
	ArgsUsage: `[conversation] [new name]`,
	Action:    renameAction,
}
View Source
var VersionCommand = &cli.Command{
	Name:   "version",
	Usage:  "Print the version information",
	Action: versionAction,
}

Functions

func NewApp

func NewApp(r *Repository) *cli.App

func NewSimpleTableWriter

func NewSimpleTableWriter(out io.Writer) table.Writer

func Run

func Run(args []string) error

Types

type Cache

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

func (*Cache) Close

func (c *Cache) Close() error

func (*Cache) Get

func (c *Cache) Get(key []byte) ([]byte, error)

func (*Cache) Init

func (c *Cache) Init() error

func (*Cache) Set

func (c *Cache) Set(key []byte, value []byte) error

func (*Cache) Size

func (c *Cache) Size() (size int)

type CacheItemNotFoundError

type CacheItemNotFoundError struct {
	// Key is sha256 hash
	Key []byte
}

CacheItemNotFoundError is an error that is returned when the cache is not found.

func (*CacheItemNotFoundError) Error

func (e *CacheItemNotFoundError) Error() string

Error returns the error message.

type CacheManager

type CacheManager struct {
	DBPath    string
	MaxLength int
	// contains filtered or unexported fields
}

func (*CacheManager) Close

func (m *CacheManager) Close() error

func (*CacheManager) Open

func (m *CacheManager) Open() (*Cache, error)

func (*CacheManager) Refresh

func (m *CacheManager) Refresh() error

type ChatService

type ChatService struct {
	ClientConfig openai.ClientConfig
	PathResolver *PathResolver
	StoreManager *StoreManager
	CacheManager *CacheManager
	HookFactory  *HookFactory
	Writer       *OutputWriter
	Spinner      *spinner.Spinner
	Conversation *Conversation
	Hooks        []*Hook
	NoLoading    bool
	NoCache      bool
	OnMemory     bool
	Model        string
	Temperature  float32
	TopP         float32
	HooksEnv     []string
}

func (*ChatService) Chat

func (c *ChatService) Chat(prompt string) error

func (*ChatService) DisableOutputAnimation

func (c *ChatService) DisableOutputAnimation()

func (*ChatService) InitConversation

func (c *ChatService) InitConversation(resume string, name string, label string) error

InitConversation resolves the conversation to use. If "resume" is specified, it loads the conversation from the store.

func (*ChatService) LoadHooks

func (c *ChatService) LoadHooks(hookNames []string) error

type Config

type Config struct {
	OpenAIAPIKey   string `toml:"openai_api_key"`   // OpenAI API Key
	Model          string `toml:"model"`            // Default setting for https://platform.openai.com/docs/api-reference/chat/create#chat/create-model
	MaxCacheLength int    `toml:"max_cache_length"` // The maximum number of cached responses.
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig() *Config

func (*Config) LoadFromFile

func (c *Config) LoadFromFile(path string) error

func (*Config) MarshalJSON

func (c *Config) MarshalJSON() ([]byte, error)

type Conversation

type Conversation struct {
	Id        uint64                         `json:"id"`              // Conversation ID
	Prompt    string                         `json:"prompt"`          // The initial input text that starts the conversation
	Name      string                         `json:"name,omitempty"`  // The unique name of the conversation
	Label     string                         `json:"label,omitempty"` // A label for categorizing the conversation
	CreatedAt time.Time                      `json:"created_at"`      // When the conversation was created
	Messages  []openai.ChatCompletionMessage `json:"messages"`        // Messages in the conversation
	Hooks     []string                       `json:"hooks,omitempty"` // Registered Hooks for the conversation
}

func NewConversation

func NewConversation() *Conversation

func (*Conversation) AddMessage

func (c *Conversation) AddMessage(msg openai.ChatCompletionMessage)

func (*Conversation) IsNew

func (c *Conversation) IsNew() bool

type ConversationInvalidNameError

type ConversationInvalidNameError struct {
	Name string
}

func (*ConversationInvalidNameError) Error

type ConversationKey

type ConversationKey struct {
	Value   string
	IsId    bool
	IsEmpty bool
	IdValue uint64
}

func NewConversationKey

func NewConversationKey(value string) *ConversationKey

type ConversationList

type ConversationList struct {
	Conversations []*Conversation `json:"conversations"`
	HasNext       bool            `json:"has_next"`
	Next          *uint64         `json:"next,string,omitempty"`
	Count         int             `json:"count"`
	// contains filtered or unexported fields
}

func (*ConversationList) IsLimitReached

func (l *ConversationList) IsLimitReached() bool

func (*ConversationList) TryAppendConversation

func (l *ConversationList) TryAppendConversation(c *Conversation)

type ConversationNameDuplicatedError

type ConversationNameDuplicatedError struct {
	Name string
	Id   uint64
}

func (*ConversationNameDuplicatedError) Error

type ConversationNotFoundError

type ConversationNotFoundError struct {
	Key interface{}
}

func (*ConversationNotFoundError) Error

func (e *ConversationNotFoundError) Error() string

type HomeDirNotFoundError

type HomeDirNotFoundError struct {
	Dir string
}

func (*HomeDirNotFoundError) Error

func (e *HomeDirNotFoundError) Error() string

type Hook

type Hook struct {
	Name        string
	CommandPath string
}

func (*Hook) Command

func (h *Hook) Command() *exec.Cmd

type HookFactory

type HookFactory struct{}

func (*HookFactory) NewHook

func (f *HookFactory) NewHook(name string) (*Hook, error)

type ListConversationsQuery

type ListConversationsQuery struct {
	Begin   *uint64
	Reverse bool
	Limit   int
	Label   string
}

type OutputWriter

type OutputWriter struct {
	Writer         io.Writer
	UseAnimation   bool
	AnimationSpeed time.Duration
	Color          *color.Color
}

OutputWriter is an io.Writer wrapper that enables printing text with a typewriter-style animation.

func (*OutputWriter) Println

func (w *OutputWriter) Println(text string)

type PathResolver

type PathResolver struct {
	Dir string
	// contains filtered or unexported fields
}

func NewPathResolver

func NewPathResolver(dir string) *PathResolver

func (*PathResolver) CacheDBFilePath

func (r *PathResolver) CacheDBFilePath() string

func (*PathResolver) ConfigFilePath

func (r *PathResolver) ConfigFilePath() string

func (*PathResolver) DBFilePath

func (r *PathResolver) DBFilePath() string

func (*PathResolver) HistoryFilePath

func (r *PathResolver) HistoryFilePath() string

func (*PathResolver) LibExecDir

func (r *PathResolver) LibExecDir() string

func (*PathResolver) LibExecFilePath

func (r *PathResolver) LibExecFilePath(name string) string

type Repository

type Repository struct {
	PathResolver *PathResolver
	Config       *Config
	ClientConfig openai.ClientConfig
	CacheManager *CacheManager
	StoreManager *StoreManager
	// contains filtered or unexported fields
}

Repository holds the application context data.

func NewRepository

func NewRepository(pr *PathResolver) *Repository

func (*Repository) Close

func (r *Repository) Close() error

func (*Repository) Init

func (r *Repository) Init() error

func (*Repository) NewChatService

func (r *Repository) NewChatService(w io.Writer) (*ChatService, error)

type SimpleTableWriter

type SimpleTableWriter struct {
	table.Writer
	Out io.Writer
}

SimpleTableWriter is a simple table Writer that does not draw borders.

func (*SimpleTableWriter) Render

func (t *SimpleTableWriter) Render() string

type Store

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

func (*Store) Close

func (s *Store) Close() error

func (*Store) CreateConversation

func (s *Store) CreateConversation(co *Conversation) error

func (*Store) DeleteConversationById

func (s *Store) DeleteConversationById(id uint64) error

func (*Store) GetConversationById

func (s *Store) GetConversationById(id uint64) (*Conversation, error)

func (*Store) GetConversationByKey

func (s *Store) GetConversationByKey(key *ConversationKey) (*Conversation, error)

func (*Store) GetConversationByName

func (s *Store) GetConversationByName(name string) (*Conversation, error)

func (*Store) Init

func (s *Store) Init() error

func (*Store) ListConversations

func (s *Store) ListConversations(query *ListConversationsQuery) (*ConversationList, error)

func (*Store) RenameConversationByKey

func (s *Store) RenameConversationByKey(key *ConversationKey, name string) error

func (*Store) UpdateConversation

func (s *Store) UpdateConversation(co *Conversation) error

type StoreManager

type StoreManager struct {
	DBPath string
	// contains filtered or unexported fields
}

func (*StoreManager) Close

func (m *StoreManager) Close() error

func (*StoreManager) Open

func (m *StoreManager) Open() (*Store, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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