-
-
Notifications
You must be signed in to change notification settings - Fork 622
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
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 15edf9b
Merge branch 'chains-Add-save-function-to-serialise-chains-to-and-fro…
cduggn 40fc5fa
Create new load package which will contain funcs to serialize and dum…
cduggn 06c5e9f
Merge branch 'chains-Add-save-function-to-serialise-chains-to-and-fro…
cduggn 7475d32
Move file system operations to internal package and abstract behind i…
cduggn 8e43fdd
Refactor filesystem logic, include more test cases to cater for poten…
cduggn d580cc6
Add parallel capability when running tests
cduggn cdc949c
Add logic to deserialize prompt templates from disk, include unit tests
cduggn a43c17c
Update tests to use mockfilesystem
cduggn 6139fab
Add support for Human and Message Prompt support
cduggn c37e7da
Tidy up
cduggn f864a6a
Tidy up
cduggn fe71ccf
Incldue exported functions which allow for creation of supported prom…
cduggn 374192f
Delete test file
cduggn 9207764
Remove serialization abstraction
cduggn 6c98e28
Remove unused interface
cduggn 0d2799b
Remove reference to serializer interface
cduggn 34463cf
Reactor to reduce duplication
cduggn 7ddf332
Merge branch 'tmc:main' into chains-Add-save-function-to-serialise-ch…
cduggn 66b52fc
add unimplemented error for chat_prompt_template save func
cduggn 32fe119
Remove unused json tag
cduggn e773c43
Tidy up
cduggn ad285da
lint fixes
cduggn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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