genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[Go] Error when running Vertex AI `Generate()` with both `WithStreaming()` and `WithTools()`

Open apascal07 opened this issue 1 year ago • 2 comments

Describe the bug This fails to run:

package main

import (
	"context"
	"errors"
	"fmt"
	"log"

	// Import the Genkit core libraries.
	"github.com/firebase/genkit/go/ai"
	"github.com/firebase/genkit/go/genkit"

	// Import the Google Cloud Vertex AI plugin.
	"github.com/firebase/genkit/go/plugins/vertexai"
)

func main() {
	ctx := context.Background()

	if err := vertexai.Init(ctx, nil); err != nil {
		log.Fatal(err)
	}

	storyFlow := genkit.DefineStreamingFlow("storyFlow",
		func(
			ctx context.Context,
			input string,
			callback func(context.Context, string) error,
		) (string, error) {
			m := vertexai.Model("gemini-1.5-flash")
			if m == nil {
				return "", errors.New("storyFlow: failed to find model")
			}

			storyTool := ai.DefineTool(
				"storyTool",
				"useful when you need a story to tell",
				func(ctx context.Context, input *any) (string, error) {
					return "I don't have a story to tell", nil
				},
			)

			resp, err := ai.Generate(ctx, m,
				ai.WithTextPrompt(fmt.Sprintf("Tell a story about %s.", input)),
				ai.WithTools(storyTool),
				ai.WithStreaming(
					func(ctx context.Context, grc *ai.GenerateResponseChunk) error {
						fmt.Printf("Chunk: %s\n", grc.Text())
						return nil
					},
				),
			)
			if err != nil {
				return "", err
			}
			return resp.Text(), nil
		})

	storyFlow.Stream(
		context.Background(),
		"apple and banana",
	)(func(sfv *genkit.StreamFlowValue[string, string], err error) bool {
		for !sfv.Done {
			log.Printf("Chunk received. Length: %d, Content: '%s'", len(sfv.Stream), sfv.Stream)
			return true
		}
		return false
	})
}

With this error:

**rpc error: code = InvalidArgument desc = Unable to submit request because it has an empty text parameter. Add a value to the parameter and try again. Learn more: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini**

apascal07 avatar Aug 23 '24 16:08 apascal07

Can we take a look this bug please? Streaming with tools is a fundamental feature to use Genkit Go in production.

jihunkim0 avatar Sep 27 '24 08:09 jihunkim0

@jihunkim0 Thanks for your patience, we're looking into it now.

apascal07 avatar Oct 10 '24 18:10 apascal07