vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.19k stars 1.54k forks source link

How to properly initialize @ai-sdk/google-vertex #3618

Open rossanodr opened 1 week ago

rossanodr commented 1 week ago

Description

I'm encountering issues when trying to use the @ai-sdk/google-vertex with Google Vertex AI. It appears that the documentation might be incomplete, as I found several forum posts from users facing the same challenges, with no clear solution available.

The documentation suggests using the following setup:

import { createVertex } from '@ai-sdk/google-vertex';

const vertex = createVertex({
  project: 'my-project', // optional
  location: 'us-central1', // optional
});

However, I'm seeing multiple errors when I attempt to implement this:

  1. Project Setting Error:

[cause]: LoadSettingError [AI_LoadSettingError]: Google Vertex project setting is missing. Pass it using the 'project' parameter or the GOOGLE_VERTEX_PROJECT environment variable.

  1. Authentication Error:
    [cause]: GoogleAuthError: [VertexAI.GoogleAuthError]:
    Unable to authenticate your request

Guidance in the error message suggested options like running gcloud auth login, or using authentication methods for Google Colab and service accounts, which I have tried but without success.

  1. Permission Denied Error:
    [cause]: ClientError: [VertexAI.ClientError]: got status: 403 Forbidden.
    {"error":{"code":403,"message":"Permission 'aiplatform.endpoints.predict' denied on resource '//aiplatform.googleapis.com/projects/'_project-id_/locations/us-central1/publishers/google/models/gemini-1.5-pro' (or it may not exist).","status":"PERMISSION_DENIED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"IAM_PERMISSION_DENIED","domain":"aiplatform.googleapis.com","metadata":{"resource":"projects/project/locations/us-central1/publishers/google/models/gemini-1.5-pro-002","permission":"aiplatform.endpoints.predict"}}]}}

I have attempted multiple approaches, including setting environment variables manually and programmatically, but none of these resolved the issue.

Code example

import { vertex } from '@ai-sdk/google-vertex';
import { generateText } from 'ai';

const { text } = await generateText({
  model: vertex('gemini-1.5-pro'),
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'What is your name?',
        },
      ],
    },
  ],
});

AI provider

@ai-sdk/google-vertex": "^0.0.33

Additional context

I've ensured my project and location are correct and that they match my Google Vertex setup. I've also followed the guidance in the documentation for environment variable settings and tried additional configurations per other user suggestions, including the authentication instructions provided in the error messages. None of these steps have resolved the issue, and I'm unsure if there's a configuration step or required permission missing in the SDK documentation.

Could you please provide more detailed guidance on using @ai-sdk/google-vertex with Google Vertex AI?

copilot-is commented 6 days ago

Look here https://github.com/copilot-is/copilot.is/blob/main/app/api/chat/vertex/route.ts I use JSON Web Tokens https://github.com/googleapis/google-auth-library-nodejs/?tab=readme-ov-file#json-web-tokens