Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GCS FileData in inference #8223

Merged
merged 1 commit into from May 6, 2024

Conversation

dlarocque
Copy link
Contributor

This chain of branches is getting a bit messy- I'm just putting this PR out so it can be reviewed :)

This PR follows #8216, and uses Firebase Authorization tokens to allow users to pass URLs in generateContent to GS resources that are secured by Firebase Security Rules.

This currently works against the production backend. I live tested in a React application configured with Firebase auth, storage, and vertexai.

Live tested using the following:

const firebaseConfig = { /* OMITTED */ };
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const vertex = getVertexAI(app);
const model = getGenerativeModel(vertex, { model: 'gemini-1.5-pro-preview-0409'});
const storage = getStorage(app)

async function run() {
  await signInWithEmailAndPassword(auth, "username@gmail.com", "password")
  const catRef = ref(storage, 'public/cat.jpeg')
  const googleStorageURL = catRef.toString();

  await model.generateContent({
    contents: [
        {
          role: "user",
          parts: [
            { text: "What is this?" },
            {
              fileData: {
                mimeType: 'image/jpeg', // Note: getMetadata(catRef) could be a convenient way to get the mimeType
                fileUri: googleStorageURL 
              }
            },
          ],
        },
    ],
  });
} 

Storage rules:

rules_version = '2';

service firebase.storage {
  match /b/{bucket}/o {
    match /public/{allPaths=**} {
      allow read: if true;
      allow write: if false;
    }
    match /images/{allPaths=**} {
      allow read: if request.auth != null;
      allow write: if false;
    }
    match /user/{userId}/{allPaths=**} {
      allow read: if request.auth != null && request.auth.uid == userId;
      allow write: if false;
    }
  }
}

Test cases:

  1. A signed in client provides a GS URL they have permission to read: Success
  2. A signed in client provides a GS URL they don't have permission to read: 403 Permission Denied
  3. A client that is not signed in provides a GS URL they have permission to read: Success
  4. A client that is not signed in provides a GS URL that they don't have permission to read: 403 Permission Denied

Sample request payload:

{
  "generationConfig": {},
  "safetySettings": [],
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "What is this?"
        },
        {
          "fileData": {
            "mimeType": "image/jpeg",
            "fileUri": "gs://<project>.appspot.com/public/cat.jpeg"
          }
        }
      ]
    }
  ]
}

Copy link

changeset-bot bot commented May 6, 2024

⚠️ No Changeset found

Latest commit: 2c1c31b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dlarocque dlarocque changed the title Add support for GCS FileData upload in inference Add support for GCS FileData in inference May 6, 2024
@google-oss-bot
Copy link
Contributor

google-oss-bot commented May 6, 2024

@google-oss-bot
Copy link
Contributor

google-oss-bot commented May 6, 2024

Copy link
Contributor

@hsubox76 hsubox76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good minus the stray yarn.lock change. I guess the design for now is to send the auth token on every request though it's only needed on requests that send fileData? I guess this is in anticipation of more auth gating in the future. We can discuss it further offline.

packages/rules-unit-testing/functions/yarn.lock Outdated Show resolved Hide resolved
@dlarocque dlarocque requested a review from hsubox76 May 6, 2024 17:39
@dlarocque dlarocque force-pushed the dlarocque/vertexai-auth-interop branch from dddb566 to d24567b Compare May 6, 2024 19:03
Base automatically changed from dlarocque/vertexai-auth-interop to ch-vertex-feature May 6, 2024 19:14
@dlarocque dlarocque merged commit 774570a into ch-vertex-feature May 6, 2024
19 of 22 checks passed
@dlarocque dlarocque deleted the dlarocque/gcs-filedata branch May 6, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants