fix unexpected end of response errors when cancelling in ollama run

This commit is contained in:
Jeffrey Morgan 2023-11-30 00:30:16 -05:00
parent 7eda3d0c55
commit 5687f1a0cf

View file

@ -496,12 +496,10 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
sigChan := make(chan os.Signal, 1) sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT) signal.Notify(sigChan, syscall.SIGINT)
var abort bool
go func() { go func() {
<-sigChan <-sigChan
cancel() cancel()
abort = true
}() }()
var currentLineLength int var currentLineLength int
@ -548,7 +546,7 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
} }
if err := client.Generate(cancelCtx, &request, fn); err != nil { if err := client.Generate(cancelCtx, &request, fn); err != nil {
if strings.Contains(err.Error(), "context canceled") && abort { if errors.Is(err, context.Canceled) {
return nil return nil
} }
return err return err
@ -559,11 +557,8 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
} }
if !latest.Done { if !latest.Done {
if abort {
return nil return nil
} }
return errors.New("unexpected end of response")
}
verbose, err := cmd.Flags().GetBool("verbose") verbose, err := cmd.Flags().GetBool("verbose")
if err != nil { if err != nil {