firebase / genkit

An open source framework for building AI-powered apps with familiar code-centric patterns. Genkit makes it easy to integrate, test, and deploy sophisticated AI features to Firebase or Google Cloud.
Apache License 2.0
653 stars 89 forks source link

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

Open apascal07 opened 1 month ago

apascal07 commented 1 month ago

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**
jihunkim0 commented 10 hours ago

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