Skip to content

Commit

Permalink
Add config migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kardolus committed Mar 21, 2024
1 parent 7908a31 commit 3fbc283
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
24 changes: 21 additions & 3 deletions config/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const (
openAIName = "openai"
openAIModel = "gpt-3.5-turbo"
openAIModelMaxTokens = 4096
openAIMaxTokens = 4096
openAIContextWindow = 8192
openAIURL = "https://api.openai.com"
openAICompletionsPath = "/v1/chat/completions"
Expand Down Expand Up @@ -77,15 +77,20 @@ func (f *FileIO) List() ([]string, error) {
}

func (f *FileIO) Read() (types.Config, error) {
return parseFile(f.configFilePath)
result, err := parseFile(f.configFilePath)
if err != nil {
return types.Config{}, err
}

return migrate(result), nil
}

func (f *FileIO) ReadDefaults() types.Config {
return types.Config{
Name: openAIName,
Model: openAIModel,
Role: openAIRole,
MaxTokens: openAIModelMaxTokens,
MaxTokens: openAIMaxTokens,
ContextWindow: openAIContextWindow,
URL: openAIURL,
CompletionsPath: openAICompletionsPath,
Expand Down Expand Up @@ -118,6 +123,19 @@ func getPath() (string, error) {
return filepath.Join(homeDir, "config.yaml"), nil
}

func migrate(config types.Config) types.Config {
// the "old" max_tokens became context_window
if config.ContextWindow == 0 && config.MaxTokens > 0 {
config.ContextWindow = config.MaxTokens
// set it to the default in case the value is small
if config.ContextWindow < openAIContextWindow {
config.ContextWindow = openAIContextWindow
}
config.MaxTokens = openAIMaxTokens
}
return config
}

func parseFile(fileName string) (types.Config, error) {
var result types.Config

Expand Down
48 changes: 42 additions & 6 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,49 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
})

it("reads the config from the file", func() {
err = configIO.Write(testConfig) // need to write before reading
Expect(err).NotTo(HaveOccurred())
when("reading the config from a file", func() {
defaults := config.New().ReadDefaults()

readConfig, err := configIO.Read()
Expect(err).NotTo(HaveOccurred())
Expect(readConfig).To(Equal(testConfig))
it("it doesn't apply a migration when max_tokens is 0", func() {
testConfig.MaxTokens = 0

err = configIO.Write(testConfig) // need to write before reading
Expect(err).NotTo(HaveOccurred())

readConfig, err := configIO.Read()
Expect(err).NotTo(HaveOccurred())
Expect(readConfig).To(Equal(testConfig))
})
it("it migrates small values of max_tokens as expected", func() {
testConfig.MaxTokens = defaults.ContextWindow - 1

err = configIO.Write(testConfig) // need to write before reading
Expect(err).NotTo(HaveOccurred())

readConfig, err := configIO.Read()
Expect(err).NotTo(HaveOccurred())

expectedConfig := testConfig
expectedConfig.MaxTokens = defaults.MaxTokens
expectedConfig.ContextWindow = defaults.ContextWindow

Expect(readConfig).To(Equal(expectedConfig))
})
it("it migrates large values of max_tokens as expected", func() {
testConfig.MaxTokens = defaults.ContextWindow + 1

err = configIO.Write(testConfig) // need to write before reading
Expect(err).NotTo(HaveOccurred())

readConfig, err := configIO.Read()
Expect(err).NotTo(HaveOccurred())

expectedConfig := testConfig
expectedConfig.MaxTokens = defaults.MaxTokens
expectedConfig.ContextWindow = testConfig.MaxTokens

Expect(readConfig).To(Equal(expectedConfig))
})
})

it("lists all the threads", func() {
Expand Down

0 comments on commit 3fbc283

Please sign in to comment.