diff --git a/v2/cmd/describeimage/main.go b/v2/cmd/describeimage/main.go index 12f8fa3..3cc4740 100644 --- a/v2/cmd/describeimage/main.go +++ b/v2/cmd/describeimage/main.go @@ -102,14 +102,11 @@ func main() { promptAndImages := append([]string{prompt}, images...) logVerbose("[%s] Generating... ", oc.ModelName) - response, err := oc.GetOutput(promptAndImages...) + output, err := oc.GetOutput(promptAndImages...) if err != nil { fmt.Printf("error: %s\n", err) os.Exit(1) } - - output := response.Response - logVerbose("OK\n") if output == "" { diff --git a/v2/cmd/fortune/main.go b/v2/cmd/fortune/main.go index 9eef794..271f3d4 100644 --- a/v2/cmd/fortune/main.go +++ b/v2/cmd/fortune/main.go @@ -31,9 +31,9 @@ func main() { oc.SetRandom() generatedOutput := oc.MustOutput(prompt) - if generatedOutput.Response == "" { + if generatedOutput == "" { log.Println("Could not generate output.") } - fmt.Println(ollamaclient.Massage(generatedOutput.Response)) + fmt.Println(ollamaclient.Massage(generatedOutput)) } diff --git a/v2/cmd/summarize/main.go b/v2/cmd/summarize/main.go index 40f6259..775f4d4 100644 --- a/v2/cmd/summarize/main.go +++ b/v2/cmd/summarize/main.go @@ -92,14 +92,12 @@ func main() { } logVerbose("[%s] Generating... ", oc.ModelName) - response, err := oc.GetOutput(prompt) + output, err := oc.GetOutput(prompt) if err != nil { fmt.Printf("error: %s\n", err) os.Exit(1) } - output := response.Response - logVerbose("OK\n") if wrapWidth > 0 { diff --git a/v2/describeimage_test.go b/v2/describeimage_test.go index 0e78fb3..5676a71 100644 --- a/v2/describeimage_test.go +++ b/v2/describeimage_test.go @@ -36,8 +36,8 @@ func TestDescribeImage(t *testing.T) { prompt := "Describe this image:" generatedOutput := oc.MustOutput(prompt, base64image) - if generatedOutput.Response == "" { + if generatedOutput == "" { t.Fatalf("Generated output for the prompt %s is empty.\n", prompt) } - fmt.Println(Massage(generatedOutput.Response)) + fmt.Println(Massage(generatedOutput)) } diff --git a/v2/ollamaclient.go b/v2/ollamaclient.go index 4ac1090..aa81db8 100644 --- a/v2/ollamaclient.go +++ b/v2/ollamaclient.go @@ -156,7 +156,7 @@ func (oc *Config) SetTool(tool Tool) { oc.Tools = append(oc.Tools, tool) } -// GetOutputChat sends a request to the Ollama API and returns the generated output. +// GetOutputChat sends a request to the Ollama API and returns the generated response func (oc *Config) GetOutputChat(promptAndOptionalImages ...string) (OutputResponse, error) { var ( temperature float64 @@ -249,8 +249,8 @@ func (oc *Config) GetOutputChat(promptAndOptionalImages ...string) (OutputRespon return res, nil } -// GetOutput sends a request to the Ollama API and returns the generated output. -func (oc *Config) GetOutput(promptAndOptionalImages ...string) (OutputResponse, error) { +// GetOutputResponse sends a request to the Ollama API and returns the generated response +func (oc *Config) GetOutputResponse(promptAndOptionalImages ...string) (OutputResponse, error) { var ( temperature float64 cacheKey string @@ -341,26 +341,42 @@ func (oc *Config) GetOutput(promptAndOptionalImages ...string) (OutputResponse, outputString = strings.TrimSpace(outputString) } response.Response = outputString - if cacheKey != "" { var data []byte json.Unmarshal([]byte(data), &response) Cache.Set(cacheKey, []byte(data)) } - return response, nil } -// MustOutput returns the output from Ollama, or the error as a string if not -func (oc *Config) MustOutput(promptAndOptionalImages ...string) OutputResponse { +// GetOutput sends a request to the Ollama API and returns the generated output string +func (oc *Config) GetOutput(promptAndOptionalImages ...string) (string, error) { + resp, err := oc.GetOutputResponse(promptAndOptionalImages...) + if err != nil { + return "", err + } + return resp.Response, nil +} + +// MustOutput returns the generated output string from Ollama, or the error as a string if not +func (oc *Config) MustOutput(promptAndOptionalImages ...string) string { output, err := oc.GetOutput(promptAndOptionalImages...) if err != nil { - return OutputResponse{Error: err.Error()} + return err.Error() } return output } -// MustOutputChat returns the output from Ollama, or the error as a string if not +// MustOutputResponse returns the response from Ollama, or an error if not +func (oc *Config) MustOutputResponse(promptAndOptionalImages ...string) OutputResponse { + resp, err := oc.GetOutputResponse(promptAndOptionalImages...) + if err != nil { + return OutputResponse{Error: err.Error()} + } + return resp +} + +// MustOutputChat returns the response from Ollama, or a response with an error if not func (oc *Config) MustOutputChat(promptAndOptionalImages ...string) OutputResponse { output, err := oc.GetOutputChat(promptAndOptionalImages...) if err != nil { @@ -468,18 +484,18 @@ func ClearCache() { // DescribeImages can load a slice of image filenames into base64 encoded strings // and build a prompt that starts with "Describe this/these image(s):" followed // by the encoded images, and return a result. Typically used together with the "llava" model. -func (oc *Config) DescribeImages(imageFilenames []string, desiredWordCount int) (OutputResponse, error) { +func (oc *Config) DescribeImages(imageFilenames []string, desiredWordCount int) (string, error) { var errNoImages = errors.New("must be given at least one image file to describe") if len(imageFilenames) == 0 { - return OutputResponse{}, errNoImages + return "", errNoImages } var images []string for _, imageFilename := range imageFilenames { base64image, err := Base64EncodeFile(imageFilename) if err != nil { - return OutputResponse{}, fmt.Errorf("could not base64 encode %s: %v", imageFilename, err) + return "", fmt.Errorf("could not base64 encode %s: %v", imageFilename, err) } // append the base64 encoded image to the "images" string slice images = append(images, base64image) @@ -488,7 +504,7 @@ func (oc *Config) DescribeImages(imageFilenames []string, desiredWordCount int) var prompt string switch len(images) { case 0: - return OutputResponse{}, errNoImages + return "", errNoImages case 1: if desiredWordCount > 0 { prompt = fmt.Sprintf("Describe this image using a maximum of %d words:", desiredWordCount) diff --git a/v2/pull_test.go b/v2/pull_test.go index f7a9b2a..f4ab78e 100644 --- a/v2/pull_test.go +++ b/v2/pull_test.go @@ -22,8 +22,8 @@ func TestPullGemmaIntegration(t *testing.T) { prompt := "Generate an imperative sentence. Keep it brief. Only output the sentence itself. Skip explanations, introductions or preamble." generatedOutput := oc.MustOutput(prompt) - if generatedOutput.Response == "" { + if generatedOutput == "" { t.Fatalf("Generated output for the prompt %s is empty.\n", prompt) } - fmt.Println(Massage(generatedOutput.Response)) + fmt.Println(Massage(generatedOutput)) }