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: add support for vertex ai #1042

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

Conversation

jayeshp19
Copy link
Collaborator

No description provided.

Copy link

changeset-bot bot commented Nov 5, 2024

🦋 Changeset detected

Latest commit: 877b7d5

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

This PR includes changesets to release 1 package
Name Type
livekit-plugins-google Minor

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

@jayeshp19 jayeshp19 marked this pull request as draft November 5, 2024 11:55
@jayeshp19 jayeshp19 changed the title Vertex ai feat: add support for vertex ai Nov 5, 2024
@jayeshp19 jayeshp19 marked this pull request as ready for review November 5, 2024 16:50
Comment on lines 92 to 94
logger.info(
"Starting chat with temperature: %s, candidate count: %s", temperature, n
)
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove logs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

on it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated

@theomonnom
Copy link
Member

Do you think there is a way we reuse https://github.com/livekit/agents/blob/main/livekit-agents/livekit/agents/llm/_oai_api.py when running/checking function calls. The good think about it is that it verify the types are correct before running any code

@jayeshp19
Copy link
Collaborator Author

@theomonnom I think we could reuse create_ai_function_info from _oai_api.py, but build_oai_function_description wouldn't be compatible, since Vertex AI doesn’t accept the same function description format as OpenAI.

@jayeshp19
Copy link
Collaborator Author

@theomonnom I've updated code to utilize create_ai_function_info from_oai_api.py

Comment on lines +71 to +82
if not self._project:
logger.error("Project is required for Vertex AI initialization")
raise ValueError
if not self._location:
logger.error("Location is required for Vertex AI initialization")
raise ValueError
if not self._gac:
logger.error(
"`GOOGLE_APPLICATION_CREDENTIALS` environment variable is not set. please set it to the path of the service account key file."
)
raise ValueError

Copy link
Member

Choose a reason for hiding this comment

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

Can you add the messages directly inside the Exception instead of logging them.

self._location = location
self._model_name = model
self._kwargs = kwargs
self._gac = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
Copy link
Member

Choose a reason for hiding this comment

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

Can you follow what we did for auth on the google.STT.

As well as the extra arguments for the credentials

project: Optional[str] = None,
location: Optional[str] = None,
model: str | GeminiModels = "gemini-1.5-flash-002",
**kwargs,
Copy link
Member

Choose a reason for hiding this comment

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

Let's not use **kwargs inside our constructor. This is error-prone


def _build_vertex_context(chat_ctx: llm.ChatContext, cache_key: Any) -> List[dict]:
contents = []
current_entry: Optional[dict] = None
Copy link
Member

Choose a reason for hiding this comment

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

The typing isn't very useful here

from typing import Any, AsyncIterator, Awaitable, Dict, List, Optional
from uuid import uuid4 as uuid

import vertexai # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should ignore the types of those packages

self._function_calls_info.append(function_call_info)
choice_delta.tool_calls = self._function_calls_info
return llm.ChatChunk(
request_id=str(uuid()),
Copy link
Member

Choose a reason for hiding this comment

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

Can you use utils.shortuuid()
Also it seems like we're generating a new request_id for each chunk?
It should be the same for the entire request

cmp = model.generate_content_async(
contents=contents,
generation_config=generation_config,
tools=tools or None,
Copy link
Member

Choose a reason for hiding this comment

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

This can be done above on line 96

)
raise ValueError

vertexai.init(project=project, location=location)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like it is a singleton, what happens if the user already initialized it himself?
Is this function returning smthg?

model = GenerativeModel(
model_name=self._model_name,
system_instruction=system_instruction,
**self._kwargs,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
**self._kwargs,

def _build_parameters(arguments: Dict[str, llm.FunctionArgInfo]) -> Dict[str, Any]:
properties = {
arg_name: {
"type": JSON_SCHEMA_TYPE_MAP.get(arg_info.type, "string"),
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should default to string?

Copy link
Member

Choose a reason for hiding this comment

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

Let's raise an error instead if the type isn't supported

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.

3 participants