Skip to content

Commit

Permalink
Make the API backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Aug 6, 2024
1 parent abd7b76 commit f614010
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
5 changes: 1 addition & 4 deletions v2/cmd/describeimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 2 additions & 2 deletions v2/cmd/fortune/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 1 addition & 3 deletions v2/cmd/summarize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions v2/describeimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
42 changes: 29 additions & 13 deletions v2/ollamaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions v2/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit f614010

Please sign in to comment.