Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompts: Add Save and FromFile functions to serialize/deserialize prompts to disk #193

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
11beeca
Base logic for prompt_template save to disk fucntion
cduggn Jul 12, 2023
15edf9b
Merge branch 'chains-Add-save-function-to-serialise-chains-to-and-fro…
cduggn Jul 13, 2023
40fc5fa
Create new load package which will contain funcs to serialize and dum…
cduggn Jul 13, 2023
06c5e9f
Merge branch 'chains-Add-save-function-to-serialise-chains-to-and-fro…
cduggn Jul 14, 2023
7475d32
Move file system operations to internal package and abstract behind i…
cduggn Jul 14, 2023
8e43fdd
Refactor filesystem logic, include more test cases to cater for poten…
cduggn Jul 15, 2023
d580cc6
Add parallel capability when running tests
cduggn Jul 15, 2023
cdc949c
Add logic to deserialize prompt templates from disk, include unit tests
cduggn Jul 16, 2023
a43c17c
Update tests to use mockfilesystem
cduggn Jul 16, 2023
6139fab
Add support for Human and Message Prompt support
cduggn Jul 16, 2023
c37e7da
Tidy up
cduggn Jul 16, 2023
f864a6a
Tidy up
cduggn Jul 16, 2023
fe71ccf
Incldue exported functions which allow for creation of supported prom…
cduggn Jul 17, 2023
374192f
Delete test file
cduggn Jul 17, 2023
9207764
Remove serialization abstraction
cduggn Jul 24, 2023
6c98e28
Remove unused interface
cduggn Jul 24, 2023
0d2799b
Remove reference to serializer interface
cduggn Jul 24, 2023
34463cf
Reactor to reduce duplication
cduggn Jul 24, 2023
7ddf332
Merge branch 'tmc:main' into chains-Add-save-function-to-serialise-ch…
cduggn Jul 27, 2023
66b52fc
add unimplemented error for chat_prompt_template save func
cduggn Jul 29, 2023
32fe119
Remove unused json tag
cduggn Jul 29, 2023
e773c43
Tidy up
cduggn Jul 29, 2023
ad285da
lint fixes
cduggn Jul 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions load/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package load

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"

"gopkg.in/yaml.v3"
)

var (
ErrInvalidFileSuffix = errors.New("invalid file suffix")
ErrNoDataToSerialize = errors.New("no data to serialize")
)

func (s *FileSerializer) ToFile(data any, path string) error {
if path == "" {
return ErrInvalidSavePath
}

if reflect.ValueOf(data).IsZero() {
return ErrNoDataToSerialize
}

suffix := s.FileSystem.NormalizeSuffix(path)
switch strings.ToLower(suffix) {
case ".json":
return s.toJSON(data, path)
case ".yaml", ".yml":
return s.toYAML(data, path)
case "":
return s.toJSON(data, path+".json")
default:
return fmt.Errorf("%w:%s", ErrInvalidFileSuffix, suffix)
}
}

func (s *FileSerializer) toJSON(d any, path string) error {
data, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("failed to serialize JSON: %w", err)
}

err = s.FileSystem.Write(path, data)
if err != nil {
return err
}

return nil
}

func (s *FileSerializer) toYAML(d any, path string) error {
data, err := yaml.Marshal(d)
if err != nil {
return fmt.Errorf("failed to serialize YAML: %w", err)
}

err = s.FileSystem.Write(path, data)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}
75 changes: 75 additions & 0 deletions load/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package load

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)

var (
ErrWritingToFile = errors.New("error writing to file")
ErrInvalidSavePath = errors.New("invalid save path")
)

const filePermission = 0o600

type FileSystem interface {
Write(path string, data []byte) error
Read(path string) ([]byte, error)
NormalizeSuffix(path string) string
}

type LocalFileSystem struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm skepitcal we need this abstraction, the go stdlib has plenty of good filesystem io primitives and if anything adding this will constrain users of this code.

Copy link
Contributor Author

@cduggn cduggn Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, I was looking at this myopically, considering load package only. I'll revert the filesystem abstraction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmc I've removed the io package abstraction. There is a minimum set of prompt types supported with this PR, which doesn't include chatprompttemplate . It has a more complex interface structure. It's probably not suitable to leave an unimplemented function in the code . I can close the PR if this is not desirable

func (p ChatPromptTemplate) Save(path string) error {
	_ = path
	return ErrNotImplemented
}

FS FileSystem
}

func (f *LocalFileSystem) Write(path string, data []byte) error {
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}

err = f.makeDirectoriesIfNeeded(absPath)
if err != nil {
return err
}

err = os.WriteFile(absPath, data, filePermission)
if err != nil {
return fmt.Errorf("failed writing to file: %w", err)
}

return nil
}

func (f *LocalFileSystem) Read(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

byteData, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

return byteData, nil
}

func (f *LocalFileSystem) makeDirectoriesIfNeeded(absPath string) error {
if _, err := os.Stat(absPath); os.IsNotExist(err) {
dir := filepath.Dir(absPath)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create path directories: %w", err)
}
}
return nil
}

func (f *LocalFileSystem) NormalizeSuffix(path string) string {
return filepath.Ext(path)
}
56 changes: 56 additions & 0 deletions load/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package load

import (
"encoding/json"
"errors"
"fmt"
"strings"

"gopkg.in/yaml.v3"
)

var ErrInvalidPath = errors.New("invalid file path")

func (s *FileSerializer) FromFile(data any, path string) error {
if path == "" {
return ErrInvalidPath
}

suffix := s.FileSystem.NormalizeSuffix(path)
switch strings.ToLower(suffix) {
case ".json":
return s.fromJSON(data, path)
case ".yaml", ".yml":
return s.fromYAML(data, path)
default:
return fmt.Errorf("%w:%s", ErrInvalidPath, suffix)
}
}

func (s *FileSerializer) fromJSON(data any, path string) error {
byteData, err := s.FileSystem.Read(path)
if err != nil {
return err
}

err = json.Unmarshal(byteData, data)
if err != nil {
return fmt.Errorf("failed to deserialize JSON: %w", err)
}

return nil
}

func (s *FileSerializer) fromYAML(data any, path string) error {
byteData, err := s.FileSystem.Read(path)
if err != nil {
return err
}

err = yaml.Unmarshal(byteData, data)
if err != nil {
return fmt.Errorf("failed to deserialize JSON: %w", err)
}

return nil
}
16 changes: 16 additions & 0 deletions load/serialization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package load

type Serializer interface {
ToFile(data any, path string) error
FromFile(data any, path string) error
}

type FileSerializer struct {
FileSystem FileSystem
}

func NewSerializer(fs FileSystem) *FileSerializer {
return &FileSerializer{
FileSystem: fs,
}
}
21 changes: 17 additions & 4 deletions prompts/chat_prompt_template.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package prompts

import "github.com/tmc/langchaingo/schema"
import (
"github.com/tmc/langchaingo/load"
"github.com/tmc/langchaingo/schema"
)

// ChatPromptTemplate is a prompt template for chat messages.
type ChatPromptTemplate struct {
// Messages is the list of the messages to be formatted.
Messages []MessageFormatter

Messages []MessageFormatter `json:"messages"`
// PartialVariables represents a map of variable names to values or functions that return values.
// If the value is a function, it will be called when the prompt template is rendered.
PartialVariables map[string]any
PartialVariables map[string]any `json:"partial_variables"`
}

var (
Expand Down Expand Up @@ -66,6 +68,17 @@ func (p ChatPromptTemplate) GetInputVariables() []string {
return inputVariables
}

func (p ChatPromptTemplate) Save(path string, serializer load.Serializer) error {
if p.PartialVariables != nil {
return ErrPromptTemplateCannotBeSaved
}
err := serializer.ToFile(p, path)
if err != nil {
return err
}
return nil
}

// NewChatPromptTemplate creates a new chat prompt template from a list of message formatters.
func NewChatPromptTemplate(messages []MessageFormatter) ChatPromptTemplate {
return ChatPromptTemplate{
Expand Down
90 changes: 90 additions & 0 deletions prompts/chat_prompt_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tmc/langchaingo/load"
"github.com/tmc/langchaingo/schema"
)

Expand Down Expand Up @@ -43,3 +44,92 @@ func TestChatPromptTemplate(t *testing.T) {
})
assert.Error(t, err)
}

func TestChatPromptTemplateTypesSaveToFile(t *testing.T) {
t.Parallel()
humanMessagePrompt := NewHumanMessagePromptTemplate(
`translate this text from {{.inputLang}} to {{.outputLang}}:\n{{.input}}`,
[]string{"inputLang", "outputLang", "input"})

systemMessagePrompt := NewSystemMessagePromptTemplate(
"You are a translation engine that can only translate text and cannot interpret it.",
nil)

type args struct {
path string
template MessageFormatter
}
tests := []struct {
name string
args args
wantErr bool
}{
{"with_JSON_suffix", args{"", humanMessagePrompt}, true},
{"with_JSON_suffix", args{"", systemMessagePrompt}, true},
{"with_JSON_suffix", args{"_human_prompt_with_JSON_suffix.json", humanMessagePrompt}, false},
{"with_JSON_suffix", args{"_system_prompt_with_JSON_suffix.json", systemMessagePrompt}, false},
{"with_YAML_suffix", args{"human_prompt_with_YAML_suffix.yaml", humanMessagePrompt}, false},
{"with_YAML_suffix", args{"system_prompt_with_YAML_suffix.yaml", systemMessagePrompt}, false},
{"case_sensitive", args{"human_prompt_case_sensitive.Yaml", humanMessagePrompt}, false},
{"case_sensitive", args{"system_prompt_case_sensitive.Yaml", systemMessagePrompt}, false},
{"no_suffix", args{"human_prompt_no_suffix", humanMessagePrompt}, false},
{"no_suffix", args{"system_prompt_no_suffix", systemMessagePrompt}, false},
{"invalid_suffix", args{"human_prompt.", humanMessagePrompt}, true},
{"invalid_suffix", args{"system_prompt.", systemMessagePrompt}, true},
{"absolute_path_JSON_suffix", args{"/human_prompt_absolute_path_JSON_suffix.json", humanMessagePrompt}, false},
{"absolute_path_JSON_suffix", args{"/system_prompt_absolute_path_JSON_suffix.json", systemMessagePrompt}, false},
{"relative_path_no_suffix", args{"prompts/human_prompt_relative_path_no_suffix", humanMessagePrompt}, false},
{"relative_path_no_suffix", args{"prompts/system_prompt_relative_path_no_suffix", systemMessagePrompt}, false},
}
fileSystem := &MockFileSystem{
Storage: make(map[string][]byte, 0),
}
serializer := load.NewSerializer(fileSystem)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.args.template.Save(tt.args.path, serializer)
if (err != nil) != tt.wantErr {
t.Errorf("PromptTemplate.Save() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

func TestHumanMessagePromptTemplateReadFromFile(t *testing.T) {
t.Parallel()
expectedPrompt := NewHumanMessagePromptTemplate(
`translate this text from {{.inputLang}} to {{.outputLang}}:\n{{.input}}`,
[]string{"inputLang", "outputLang", "input"})

fileSystem := &MockFileSystem{
Storage: make(map[string][]byte, 0),
}
serializer := load.NewSerializer(fileSystem)
err := expectedPrompt.Save("prompt_data.json", serializer)
assert.NoError(t, err)

prompt, err := NewHumanMessagePromptFromFile("prompt_data.json", serializer)
assert.NoError(t, err)
assert.EqualValues(t, prompt, expectedPrompt)
}

func TestSystemMessagePromptTemplateSave(t *testing.T) {
t.Parallel()
expectedPrompt := NewSystemMessagePromptTemplate(
"You are a translation engine that can only translate text and cannot interpret it.",
nil)

fileSystem := &MockFileSystem{
Storage: make(map[string][]byte, 0),
}
serializer := load.NewSerializer(fileSystem)
err := expectedPrompt.Save("prompt_data.json", serializer)
assert.NoError(t, err)

prompt, err := NewSystemMessagePromptFromFile("prompt_data.json", serializer)
assert.NoError(t, err)
assert.EqualValues(t, prompt, expectedPrompt)
}
Loading