Add context window

This commit is contained in:
kardolus
2024-03-20 20:41:37 -04:00
parent 9422fa87ad
commit 7908a31119
6 changed files with 27 additions and 14 deletions

View File

@@ -49,8 +49,8 @@ func New(callerFactory http.CallerFactory, cs config.ConfigStore, hs history.His
}, nil
}
func (c *Client) WithCapacity(capacity int) *Client {
c.Config.MaxTokens = capacity
func (c *Client) WithContextWindow(window int) *Client {
c.Config.ContextWindow = window
return c
}
@@ -226,7 +226,7 @@ func (c *Client) processResponse(raw []byte, v interface{}) error {
func (c *Client) truncateHistory() {
tokens, rolling := countTokens(c.History)
effectiveTokenSize := calculateEffectiveTokenSize(c.Config.MaxTokens, MaxTokenBufferPercentage)
effectiveTokenSize := calculateEffectiveContextWindow(c.Config.ContextWindow, MaxTokenBufferPercentage)
if tokens <= effectiveTokenSize {
return
@@ -258,10 +258,10 @@ func (c *Client) updateHistory(response string) {
}
}
func calculateEffectiveTokenSize(maxTokenSize int, bufferPercentage int) int {
func calculateEffectiveContextWindow(window int, bufferPercentage int) int {
adjustedPercentage := 100 - bufferPercentage
effectiveTokenSize := (maxTokenSize * adjustedPercentage) / 100
return effectiveTokenSize
effectiveContextWindow := (window * adjustedPercentage) / 100
return effectiveContextWindow
}
func countTokens(messages []types.Message) (int, []int) {

View File

@@ -23,7 +23,8 @@ import (
//go:generate mockgen -destination=configmocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/config ConfigStore
const (
defaultMaxTokens = 50
defaultMaxTokens = 100
defaultContextWindow = 50
defaultURL = "https://default.openai.com"
defaultName = "default-name"
defaultModel = "gpt-3.5-turbo"
@@ -542,7 +543,7 @@ func (f *clientFactory) buildClientWithoutConfig() *client.Client {
c, err := client.New(mockCallerFactory, f.mockConfigStore, f.mockHistoryStore)
Expect(err).NotTo(HaveOccurred())
return c
return c.WithContextWindow(defaultContextWindow)
}
func (f *clientFactory) buildClientWithConfig(config types.Config) *client.Client {
@@ -552,7 +553,7 @@ func (f *clientFactory) buildClientWithConfig(config types.Config) *client.Clien
c, err := client.New(mockCallerFactory, f.mockConfigStore, f.mockHistoryStore)
Expect(err).NotTo(HaveOccurred())
return c
return c.WithContextWindow(defaultContextWindow)
}
func (f *clientFactory) withoutHistory() {