Skip to content

Commit 3fbc283

Browse files
committed
Add config migration
1 parent 7908a31 commit 3fbc283

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

config/store.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
const (
1212
openAIName = "openai"
1313
openAIModel = "gpt-3.5-turbo"
14-
openAIModelMaxTokens = 4096
14+
openAIMaxTokens = 4096
1515
openAIContextWindow = 8192
1616
openAIURL = "https://api.openai.com"
1717
openAICompletionsPath = "/v1/chat/completions"
@@ -77,15 +77,20 @@ func (f *FileIO) List() ([]string, error) {
7777
}
7878

7979
func (f *FileIO) Read() (types.Config, error) {
80-
return parseFile(f.configFilePath)
80+
result, err := parseFile(f.configFilePath)
81+
if err != nil {
82+
return types.Config{}, err
83+
}
84+
85+
return migrate(result), nil
8186
}
8287

8388
func (f *FileIO) ReadDefaults() types.Config {
8489
return types.Config{
8590
Name: openAIName,
8691
Model: openAIModel,
8792
Role: openAIRole,
88-
MaxTokens: openAIModelMaxTokens,
93+
MaxTokens: openAIMaxTokens,
8994
ContextWindow: openAIContextWindow,
9095
URL: openAIURL,
9196
CompletionsPath: openAICompletionsPath,
@@ -118,6 +123,19 @@ func getPath() (string, error) {
118123
return filepath.Join(homeDir, "config.yaml"), nil
119124
}
120125

126+
func migrate(config types.Config) types.Config {
127+
// the "old" max_tokens became context_window
128+
if config.ContextWindow == 0 && config.MaxTokens > 0 {
129+
config.ContextWindow = config.MaxTokens
130+
// set it to the default in case the value is small
131+
if config.ContextWindow < openAIContextWindow {
132+
config.ContextWindow = openAIContextWindow
133+
}
134+
config.MaxTokens = openAIMaxTokens
135+
}
136+
return config
137+
}
138+
121139
func parseFile(fileName string) (types.Config, error) {
122140
var result types.Config
123141

integration/integration_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,49 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) {
141141
Expect(err).NotTo(HaveOccurred())
142142
})
143143

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

148-
readConfig, err := configIO.Read()
149-
Expect(err).NotTo(HaveOccurred())
150-
Expect(readConfig).To(Equal(testConfig))
147+
it("it doesn't apply a migration when max_tokens is 0", func() {
148+
testConfig.MaxTokens = 0
149+
150+
err = configIO.Write(testConfig) // need to write before reading
151+
Expect(err).NotTo(HaveOccurred())
152+
153+
readConfig, err := configIO.Read()
154+
Expect(err).NotTo(HaveOccurred())
155+
Expect(readConfig).To(Equal(testConfig))
156+
})
157+
it("it migrates small values of max_tokens as expected", func() {
158+
testConfig.MaxTokens = defaults.ContextWindow - 1
159+
160+
err = configIO.Write(testConfig) // need to write before reading
161+
Expect(err).NotTo(HaveOccurred())
162+
163+
readConfig, err := configIO.Read()
164+
Expect(err).NotTo(HaveOccurred())
165+
166+
expectedConfig := testConfig
167+
expectedConfig.MaxTokens = defaults.MaxTokens
168+
expectedConfig.ContextWindow = defaults.ContextWindow
169+
170+
Expect(readConfig).To(Equal(expectedConfig))
171+
})
172+
it("it migrates large values of max_tokens as expected", func() {
173+
testConfig.MaxTokens = defaults.ContextWindow + 1
174+
175+
err = configIO.Write(testConfig) // need to write before reading
176+
Expect(err).NotTo(HaveOccurred())
177+
178+
readConfig, err := configIO.Read()
179+
Expect(err).NotTo(HaveOccurred())
180+
181+
expectedConfig := testConfig
182+
expectedConfig.MaxTokens = defaults.MaxTokens
183+
expectedConfig.ContextWindow = testConfig.MaxTokens
184+
185+
Expect(readConfig).To(Equal(expectedConfig))
186+
})
151187
})
152188

153189
it("lists all the threads", func() {

0 commit comments

Comments
 (0)