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

feat: Gemini Access via Vertex AI #838

Merged

Conversation

parhammmm
Copy link
Contributor

@marcusschiesser here's the Vertex implementation. It's a little different than the others because with vertex there isn't any api key auth.

To authenticate for local development:

npm install @google-cloud/vertexai
gcloud auth application-default login

For production the preferred method is via a service account, more details: https://cloud.google.com/docs/authentication/ . The GeminiVertexSession accepts all the params supported by import { VertexAI } from "@google-cloud/vertexai";. e.g.

new GeminiVertexSession({
  location: "",
  project: "",
  googleAuthOptions: {...},
})

Google Auth Options takes all the values from GoogleAuthOptions in https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts incase someone wants to directly pass their own keyFile or it's values via credentials

Once logged in you can test via:

export GOOGLE_VERTEX_PROJECT="" GOOGLE_VERTEX_LOCATION="us-central1" && npx ts-node ./gemini/chatVertex.ts

Some notes:

The @google-cloud/vertexai sdk is lagging behind @google/generative-ai, main differences affecting this PR:

Copy link

changeset-bot bot commented May 15, 2024

🦋 Changeset detected

Latest commit: a5114b7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

Copy link

vercel bot commented May 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
llama-index-ts-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 17, 2024 4:24pm

@parhammmm
Copy link
Contributor Author

Not sure I quite follow why the example checks are failing, would you have any ideas?

@marcusschiesser
Copy link
Collaborator

@parhammmm you have to run pnpm type-check to get more details about what is failing

Copy link
Collaborator

@marcusschiesser marcusschiesser left a comment

Choose a reason for hiding this comment

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

Looks great thanks @parhammmm! Please fix the builld, then we can merge it

if (options.backend === GEMINI_BACKENDS.VERTEX)
return `${options.location}/${options.project}`;

return "";
Copy link
Collaborator

Choose a reason for hiding this comment

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

i guess that's not something that should ever happen - throw an exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, sure!


/* To use Google's Vertex AI backend, it doesn't use api key authentication.
*
* To authenticate for local development:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do!

@parhammmm
Copy link
Contributor Author

parhammmm commented May 15, 2024

@marcusschiesser I found the issue; it's because Vertex can only be used via the backend and here webpack is attempting to build a node package https://github.com/googleapis/google-auth-library-nodejs for the browser.

Have you dealt with such cases before?

Update: if someone stumbles on this in the future, the solution is to add the package name to packages/core/src/next.ts

Update 2: also you'll have to make sure the export of the said module is done in packages/core/src/index.ts and not anywhere else so that the cloudflare worker doesn't break

@parhammmm
Copy link
Contributor Author

@marcusschiesser done. I had to restructure the code a little bit to make sure node specific code doesn't break the browser/cloudflare builds.... on to bedrock implementation (pretty close)!

@parhammmm
Copy link
Contributor Author

parhammmm commented May 17, 2024

@marcusschiesser @himself65 how do I use exported items from packages/core/src/index.ts seems like they aren't actually exported during the build... I've tried with HuggingFaceEmbedding (and GeminiVertexSession) and in nextjs I get:

Attempted import error: 'HuggingFaceEmbedding' is not exported from 'llamaindex' (imported as 'HuggingFaceEmbedding').

UPDATE: Seems like this is only an issue when pnpm run build & pnpm run dev are ran locally the official release seems to work fine.

@@ -10,3 +10,6 @@ export {
HuggingFaceEmbedding,
HuggingFaceEmbeddingModelType,
} from "./embeddings/HuggingFaceEmbedding.js";

export { type VertexGeminiSessionOptions } from "./llm/gemini/types.js";
Copy link
Collaborator

Choose a reason for hiding this comment

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

@parhammmm the exports that you add here are only used for NodeJS - so this change means VertexGemini does only work with NodeJS (not edge for example) - if that' ok, nothing to do - otherwise you can send a PR and move this to src/llm/index.ts

Copy link
Collaborator

@marcusschiesser marcusschiesser left a comment

Choose a reason for hiding this comment

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

@parhammmm looks great thanks!

@marcusschiesser marcusschiesser merged commit 37525df into run-llama:main May 20, 2024
15 checks passed
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

2 participants