-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(genai): Add Live API samples v2 #13523
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
Draft
Guiners
wants to merge
8
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/rest-of-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c71d1c3
adding working samples with tests
c6e6fcb
adding working samples with tests
e6b2750
adding working samples with tests
d2b9d09
adding working samples with tests
f1b47d4
adding working samples with tests
e84d628
adding working samples with tests
5d4fb00
adding working samples with tests
c0c844c
Merge branch 'main' into sample/rest-of-samples
Guiners File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import asyncio | ||
|
||
_memory_corpus = "projects/cloud-ai-devrel-softserve/locations/us-central1/ragCorpora/2305843009213693952" | ||
|
||
|
||
async def generate_content(memory_corpus: str) -> list[str]: | ||
# [START googlegenaisdk_live_ground_ragengine_with_txt] | ||
from google import genai | ||
from google.genai.types import ( | ||
Content, | ||
LiveConnectConfig, | ||
Modality, | ||
Part, | ||
Tool, | ||
Retrieval, | ||
VertexRagStore, | ||
VertexRagStoreRagResource, | ||
) | ||
|
||
client = genai.Client() | ||
model_id = "gemini-2.0-flash-live-preview-04-09" | ||
rag_store = VertexRagStore( | ||
rag_resources=[ | ||
VertexRagStoreRagResource( | ||
rag_corpus=memory_corpus # Use memory corpus if you want to store context. | ||
) | ||
], | ||
# Set `store_context` to true to allow Live API sink context into your memory corpus. | ||
store_context=True, | ||
) | ||
config = LiveConnectConfig( | ||
response_modalities=[Modality.TEXT], | ||
tools=[Tool(retrieval=Retrieval(vertex_rag_store=rag_store))], | ||
) | ||
|
||
async with client.aio.live.connect(model=model_id, config=config) as session: | ||
text_input = "What year did Mariusz Pudzianowski win World's Strongest Man?" | ||
print("> ", text_input, "\n") | ||
|
||
await session.send_client_content( | ||
turns=Content(role="user", parts=[Part(text=text_input)]) | ||
) | ||
|
||
response = [] | ||
|
||
async for message in session.receive(): | ||
if message.text: | ||
response.append(message.text) | ||
continue | ||
|
||
print("".join(response)) | ||
# Example output: | ||
# > What year did Mariusz Pudzianowski win World's Strongest Man? | ||
# Mariusz Pudzianowski won World's Strongest Man in 2002, 2003, 2005, 2007, and 2008. | ||
# [END googlegenaisdk_live_ground_ragengine_with_txt] | ||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(generate_content(_memory_corpus)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better reusability and to avoid exposing project-specific details, it's recommended to construct the
_memory_corpus
string from environment variables for the project ID and a placeholder for the user-specific RAG corpus ID. This requires importing theos
module and aligns with the practices in other samples.