Skip to content

Commit

Permalink
Add a summary toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeb26 committed Oct 9, 2024
1 parent 0547051 commit f89d38b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ clean:
deps:
rm -rf go.mod go.sum vendor
go mod init github.com/mikeb26/gptcli
go mod edit -replace=github.com/sashabaranov/go-openai=github.com/mikeb26/sashabaranov-go-openai@v1.28.2.mb1
go mod edit -replace=github.com/sashabaranov/go-openai=github.com/mikeb26/sashabaranov-go-openai@v1.32.0.mb2
GOPROXY=direct go mod tidy
go mod vendor

Expand Down
1 change: 1 addition & 0 deletions cmd/gptcli/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ Available Commands:
archive <thread#> Archive a previously created thread(conversation)
ls List available threads(conversations)
thread <thread#> Switch to a previously created thread
summary [<on|off>] Toggle thread summaries on or off
exit Exit gptcli
billing Show billing/daily usage information
63 changes: 48 additions & 15 deletions cmd/gptcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var subCommandTab = map[string]func(ctx context.Context,
"ls": lsThreadsMain,
"thread": threadSwitchMain,
"new": newThreadMain,
"summary": summaryToggleMain,
"archive": archiveThreadMain,
"exit": exitMain,
"quit": exitMain,
Expand All @@ -82,17 +83,18 @@ type Prefs struct {
}

type GptCliContext struct {
client internal.OpenAIClient
sessClient internal.OpenAIClient
input *bufio.Reader
needConfig bool
curThreadNum int
totThreads int
threads []*GptCliThread
haveSess bool
prefs Prefs
threadsDir string
archiveDir string
client internal.OpenAIClient
sessClient internal.OpenAIClient
input *bufio.Reader
needConfig bool
curThreadNum int
curSummaryToggle bool
totThreads int
threads []*GptCliThread
haveSess bool
prefs Prefs
threadsDir string
archiveDir string
}

func NewGptCliContext() *GptCliContext {
Expand Down Expand Up @@ -133,8 +135,9 @@ func NewGptCliContext() *GptCliContext {
prefs: Prefs{
SummarizePrior: false,
},
threadsDir: threadsDirLocal,
archiveDir: archiveDirLocal,
curSummaryToggle: false,
threadsDir: threadsDirLocal,
archiveDir: archiveDirLocal,
}
}

Expand Down Expand Up @@ -198,6 +201,7 @@ func (gptCliCtx *GptCliContext) loadPrefs() error {
if err != nil {
return err
}
gptCliCtx.curSummaryToggle = gptCliCtx.prefs.SummarizePrior

return nil
}
Expand Down Expand Up @@ -684,6 +688,34 @@ func archiveThreadMain(ctx context.Context, gptCliCtx *GptCliContext,
return lsThreadsMain(ctx, gptCliCtx, args)
}

func summaryToggleMain(ctx context.Context, gptCliCtx *GptCliContext,
args []string) error {

usageErr := fmt.Errorf("Syntax is 'summary [<on|off>]' e.g. 'summary on'\n")

if len(args) == 1 {
gptCliCtx.curSummaryToggle = !gptCliCtx.curSummaryToggle
} else if len(args) != 2 {
return usageErr
} else {
if strings.ToLower(args[1]) == "on" {
gptCliCtx.curSummaryToggle = true
} else if strings.ToLower(args[1]) == "off" {
gptCliCtx.curSummaryToggle = false
} else {
return usageErr
}
}

if gptCliCtx.curSummaryToggle {
fmt.Printf("summaries enabled; summaries of the thread history are sent for followups in order to reduce costs.\n")
} else {
fmt.Printf("summaries disabled; the full thread history is sent for followups in order to get more precise responses.\n")
}

return nil
}

func configMain(ctx context.Context, gptCliCtx *GptCliContext, args []string) error {
configDir, err := getConfigDir()
if err != nil {
Expand Down Expand Up @@ -761,6 +793,7 @@ func configMain(ctx context.Context, gptCliCtx *GptCliContext, args []string) er
shouldSummarize = "N"
}
gptCliCtx.prefs.SummarizePrior = (shouldSummarize[0] == 'Y')
gptCliCtx.curSummaryToggle = gptCliCtx.prefs.SummarizePrior

return gptCliCtx.savePrefs()
}
Expand Down Expand Up @@ -947,7 +980,7 @@ func interactiveThreadWork(ctx context.Context,
dialogue2Send := dialogue

var err error
if gptCliCtx.prefs.SummarizePrior && len(dialogue) > 2 {
if gptCliCtx.curSummaryToggle && len(dialogue) > 2 {
if len(gptCliCtx.threads[gptCliCtx.curThreadNum-1].SummaryDialogue) > 0 {
summaryDialogue =
gptCliCtx.threads[gptCliCtx.curThreadNum-1].SummaryDialogue
Expand Down Expand Up @@ -992,7 +1025,7 @@ func interactiveThreadWork(ctx context.Context,
gptCliCtx.threads[gptCliCtx.curThreadNum-1].Dialogue = append(dialogue, msg)
gptCliCtx.threads[gptCliCtx.curThreadNum-1].ModTime = time.Now()
gptCliCtx.threads[gptCliCtx.curThreadNum-1].AccessTime = time.Now()
if gptCliCtx.prefs.SummarizePrior {
if gptCliCtx.curSummaryToggle {
gptCliCtx.threads[gptCliCtx.curThreadNum-1].SummaryDialogue =
append(summaryDialogue, msg)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/mikeb26/gptcli

go 1.22.3

replace github.com/sashabaranov/go-openai => github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20240822171604-121bab2b7995
replace github.com/sashabaranov/go-openai => github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20241009182751-c689b9409b1b

require (
github.com/fatih/color v1.17.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20240822171604-121bab2b7995 h1:4mHelyuS+0kVf53fnUH0c27bPKJaTKKXwnRUXiI+aFY=
github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20240822171604-121bab2b7995/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20241009182751-c689b9409b1b h1:Rnd4kIr1EP1uszltyDNlsf4kS4W8SScsXlhnuH42LiQ=
github.com/mikeb26/sashabaranov-go-openai v1.17.7-0.20241009182751-c689b9409b1b/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit f89d38b

Please sign in to comment.