gogoogle-ai-platform

How to use aiplatformpb GenerateContentRequest in Go (getting a 404)?


I'm getting the following error when trying to use Gemini Pro via Go (1.22.1) and the aiplatform (1.64) package:

rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"

Here's my Golang code:

func googleGemini() string {
    ctx := context.Background()

    client, err := aiplatform.NewPredictionClient(ctx)
    if err != nil {
        log.Printf("[googleGemini] ERROR! %v", err)
        return ""
    }

    var parts []*aiplatformpb.Part
    parts = append(parts, &aiplatformpb.Part{
        Data: &aiplatformpb.Part_Text{
            Text: "Hello!",
        },
    })

    content := &aiplatformpb.Content{
        Role:  "user",
        Parts: parts,
    }
    // endpoint := "us-central1-aiplatform.googleapis.com"
    model := "gemini-1.0-pro-001"
    location := "us-central1"
    contentRequest := aiplatformpb.GenerateContentRequest{
        Model: fmt.Sprintf(
            "projects/%s/locations/%s/publishers/google/models/%s",
            PROJECT_ID(), location, model),
        Contents: []*aiplatformpb.Content{content},
    }

    resp, err := client.GenerateContent(ctx, &contentRequest)
    if err != nil {
        log.Printf("[googleGemini] ERROR! %v", err)
        return ""
    }

    return resp.Candidates[0].Content.Parts[0].GetText()
}

What am I getting wrong here? I can get this to work using curl in bash:

cat << EOF > request.json
{
    "contents": [
        {
            "role": "user",
            "parts": [
                {
                    "text": "Hello!"
                }
            ]
        }
    ]
}
EOF

API_ENDPOINT="us-central1-aiplatform.googleapis.com"
PROJECT_ID="$(gcloud config get project)"
MODEL_ID="gemini-1.0-pro-001"
LOCATION_ID="us-central1"

curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${API_ENDPOINT}/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/publishers/google/models/${MODEL_ID}:streamGenerateContent" -d '@request.json'

Solution

  • You may need to provide a service-specific endpoint:

    // If not this, try "https://" instead of ":443"
    endpoint := "us-central1-aiplatform.googleapis.com:443"
    opts := []option.ClientOption{
        option.WithEndpoint(endpoint),
    }
    client, err := aiplatform.NewPredictionClient(ctx, opts...)
    if err != nil {
        log.Printf("[googleGemini] ERROR! %v", err)
        return ""
    }
    

    APIs Explorer documents Method: projects.locations.publishers.models.generateContent and says:

    POST https://{service-endpoint}/v1/{model}:generateContent

    Where {service-endpoint} is one of the supported service endpoints.

    I don't use Vertex AI and so am not able to easily test this. But, after running your code as-is and getting the 404 too, I then revised the code to define the endpoint and received an RESOURCE_PROJECT_INVALID (as expected).