dotprompt

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 12 Imported by: 0

README

DotPrompt

codecov

A port of the dotnet DotPrompt library to Go.

The goal is to provide the same functionality and show how the prompt files can be used across languages. The dotnet version utilises the Fluid library which is a dotnet implementation of the Liquid templating language. This library makes use of the Liquid implementation by Oliver Steele.

Fluid contains some methods which are specific to dotnet (such as format_date) but where Liquid standard methods are used the templates should be compatible.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FSStore added in v0.0.3

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

FSStore represents a file-system-based storage system for handling prompt files.

func NewFSStore added in v0.0.3

func NewFSStore(dirFs fs.ReadDirFS) *FSStore

NewFSStore creates a new FSStore instance using the provided fs.ReadDirFS for reading and managing prompt files.

func (*FSStore) Load added in v0.0.3

func (f *FSStore) Load() ([]PromptFile, error)

Load retrieves all prompt files from the root directory and its subdirectories in the file system storage. Returns a slice of PromptFile and an error if any issue occurs during the loading process.

type FewShotPromptPair

type FewShotPromptPair struct {
	User     string `yaml:"user"`
	Response string `yaml:"response"`
}

FewShotPromptPair represents a pair of user prompt and the corresponding response.

type FileStore

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

FileStore represents a file-based storage system for handling prompt files.

func NewFileStore

func NewFileStore() (*FileStore, error)

NewFileStore creates a new FileStore instance using the default file path ("prompts").

func NewFileStoreFromPath

func NewFileStoreFromPath(path string) (*FileStore, error)

NewFileStoreFromPath creates a new FileStore instance from the specified directory path.

func (*FileStore) Load

func (f *FileStore) Load() ([]PromptFile, error)

Load retrieves all prompt files from the specified file path and returns a slice of PromptFile objects or an error.

type FileStoreError

type FileStoreError struct {
	Message string
	Err     error
}

FileStoreError represents an error encountered in file store operations. It contains a message describing the error and an optional underlying error.

func (FileStoreError) Error

func (e FileStoreError) Error() string

Error returns the error message contained in the FileStoreError.

type InputSchema

type InputSchema struct {
	Parameters map[string]string      `yaml:"parameters"`
	Default    map[string]interface{} `yaml:"default,omitempty"`
}

InputSchema represents the schema for input parameters and their default values.

type Loader

type Loader interface {

	// Load loads prompt files and returns a slice of PromptFile and an error.
	Load() ([]PromptFile, error)
}

Loader defines an interface for loading prompt files.

type Manager

type Manager struct {
	PromptFiles map[string]PromptFile
}

Manager is responsible for managing and storing prompt files, with mapping from their names to PromptFile instances.

func NewManager

func NewManager() (*Manager, error)

NewManager creates a new Manager by loading prompt files from the default file store. Returns a pointer to the Manager instance or an error if the loading process fails.

Example

ExampleNewManager demonstrates the process of creating a new Manager instance which loads from the default "prompts" directory, and then fetching a prompt by name from the manager.

mgr, err := NewManager()
if err != nil {
	panic(err)
}

promptFile, err := mgr.GetPromptFile("example")
if err != nil {
	panic(err)
}

fmt.Println(promptFile.Prompts.System)
Output:

You are a helpful research assistant who will provide descriptive responses for a given topic and how it impacts society

func NewManagerFromLoader

func NewManagerFromLoader(loader Loader) (*Manager, error)

NewManagerFromLoader initializes and returns a Manager instance by loading prompt files using the provided Loader. It returns a pointer to the Manager and an error if the loading process fails.

Example (WithFSStore)
// Create a new FSStore instance using the embedded file system, see https://pkg.go.dev/embed for more details
store := NewFSStore(promptFs)

// Create a new Manager instance using the FSStore instance
mgr, err := NewManagerFromLoader(store)
if err != nil {
	panic(err)
}

// Fetch a prompt file by name from the manager
prompt, err := mgr.GetPromptFile("example")
if err != nil {
	panic(err)
}

fmt.Println(prompt.Prompts.System)
Output:

You are a helpful research assistant who will provide descriptive responses for a given topic and how it impacts society
Example (WithFileStore)

ExampleNewManagerFromLoader_withFileStore demonstrates creating a Manager from a FileStore-based Loader and retrieving a prompt file.

// Create a new FileStore instance using the "prompts" directory in the current working directory
fileStore, err := NewFileStoreFromPath("./prompts")
if err != nil {
	panic(err)
}

// Create a new Manager instance using the FileStore instance
mgr, err := NewManagerFromLoader(fileStore)
if err != nil {
	panic(err)
}

// Fetch a prompt file by name from the manager
prompt, err := mgr.GetPromptFile("example")
if err != nil {
	panic(err)
}

fmt.Println(prompt.Prompts.System)
Output:

You are a helpful research assistant who will provide descriptive responses for a given topic and how it impacts society

func (*Manager) GetPromptFile

func (m *Manager) GetPromptFile(name string) (PromptFile, error)

GetPromptFile retrieves the prompt file with the specified name from the manager's stored prompt files. Returns the PromptFile and a boolean indicating success of the retrieval.

func (*Manager) ListPromptFileNames

func (m *Manager) ListPromptFileNames() []string

ListPromptFileNames returns a list of all prompt file names managed by the Manager.

type OutputFormat

type OutputFormat int

OutputFormat represents the format of output, such as text or JSON.

const (

	// Text represents the plain text output format.
	Text OutputFormat = iota

	// Json represents the JSON output format.
	Json
)

func (OutputFormat) MarshalYAML added in v0.0.2

func (of OutputFormat) MarshalYAML() (interface{}, error)

MarshalYAML marshals the OutputFormat into a YAML-compatible representation. Returns a string representation of the format ("text" or "json") or an error if the format is invalid.

func (*OutputFormat) String added in v0.0.3

func (of *OutputFormat) String() string

func (*OutputFormat) UnmarshalYAML

func (of *OutputFormat) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML unmarshals a YAML node into an OutputFormat value, supporting "text" and "json". Returns an error if format is invalid.

type PromptConfig

type PromptConfig struct {
	Temperature  *float32     `yaml:"temperature,omitempty"`
	MaxTokens    *int         `yaml:"maxTokens,omitempty"`
	OutputFormat OutputFormat `yaml:"outputFormat"`
	Input        InputSchema  `yaml:"input"`
}

PromptConfig represents the configuration options for a prompt, including temperature, max tokens, output format, and input schema.

type PromptError

type PromptError struct {
	Message string
}

PromptError represents an error related to prompt processing.

func (PromptError) Error

func (e PromptError) Error() string

Error returns the error message associated with the PromptError.

type PromptFile

type PromptFile struct {
	Name     string              `yaml:"name,omitempty"`
	Model    string              `yaml:"model,omitempty"`
	Config   PromptConfig        `yaml:"config"`
	Prompts  Prompts             `yaml:"prompts"`
	FewShots []FewShotPromptPair `yaml:"fewShots,omitempty"`
}

PromptFile represents the structure of a file containing a prompt configuration and multiple associated prompts.

func NewPromptFile

func NewPromptFile(name string, data []byte) (*PromptFile, error)

NewPromptFile creates a new PromptFile from the provided name and prompt data. It validates the input, configures the prompt file, and returns an error if any issues are encountered.

func NewPromptFileFromFile

func NewPromptFileFromFile(path string) (*PromptFile, error)

NewPromptFileFromFile reads a file from the specified path, processes its content, and returns a PromptFile structure or an error.

Example

ExampleNewPromptFileFromFile demonstrates loading a prompt file from a given path and then passing in values to the template to generate the user prompt.

promptFile, err := NewPromptFileFromFile("test-data/basic.prompt")
if err != nil {
	panic(err)
}

prompt, err := promptFile.GetUserPrompt(map[string]interface{}{"country": "Malta"})
if err != nil {
	panic(err)
}

fmt.Println(prompt)
Output:

I am looking at going on holiday to Malta and would like to know more about it, what can you tell me?

func (*PromptFile) GetSystemPrompt

func (pf *PromptFile) GetSystemPrompt(values map[string]interface{}) (string, error)

GetSystemPrompt generates the system prompt string using the provided template values, appending JSON format instructions if required.

func (*PromptFile) GetUserPrompt

func (pf *PromptFile) GetUserPrompt(values map[string]interface{}) (string, error)

GetUserPrompt generates a user prompt string based on a provided template and a set of values. It utilizes the 'Prompts.User' template within the PromptFile and replaces template placeholders with corresponding values from the input map.

func (*PromptFile) Serialize added in v0.0.2

func (pf *PromptFile) Serialize() ([]byte, error)

Serialize serializes the PromptFile into a byte slice in YAML format and returns it, or an error if serialization fails.

func (*PromptFile) ToFile added in v0.0.2

func (pf *PromptFile) ToFile(name string) error

ToFile serializes the PromptFile and writes it to a specified file. Returns an error if the serialization or file write operation fails.

type Prompts

type Prompts struct {
	System string `yaml:"system,omitempty"`
	User   string `yaml:"user"`
}

Prompts represents a set of system and user prompts.

Jump to

Keyboard shortcuts

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