-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(genai): Adding live_code_exec_with_txt with test #13516
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
10
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/test_one
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
972ae62
adding live_code_exec_with_txt with test
11a09e4
adding live_code_exec_with_txt with test
4c066e6
adding live_code_exec_with_txt with test
8d4753a
adding live_code_exec_with_txt with test
36114d5
adding live_ground_googsearch_with_txt with test
4327734
adding live_ground_ragengine_with_txt with test
8317d3a
adding live_func_call_with_txt with test
9a1e37b
adding live_func_call_with_txt with test
bdc23e4
adding live_func_call_with_txt with test
3fcb287
adding live_func_call_with_txt with test
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,69 @@ | ||||||
# 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 | ||||||
|
||||||
|
||||||
async def generate_content() -> list[str]: | ||||||
# [START googlegenaisdk_live_code_exec_with_txt] | ||||||
from google import genai | ||||||
from google.genai.types import ( | ||||||
LiveConnectConfig, | ||||||
Modality, | ||||||
Tool, | ||||||
ToolCodeExecution, | ||||||
Content, | ||||||
Part, | ||||||
) | ||||||
|
||||||
client = genai.Client() | ||||||
# model_id = "gemini-live-2.5-flash" #todo | ||||||
model_id = "gemini-2.0-flash-live-preview-04-09" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
config = LiveConnectConfig( | ||||||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
response_modalities=[Modality.TEXT], | ||||||
tools=[Tool(code_execution=ToolCodeExecution())], | ||||||
) | ||||||
async with client.aio.live.connect(model=model_id, config=config) as session: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
text_input = "Compute the largest prime palindrome under 10" | ||||||
print("> ", text_input, "\n") | ||||||
await session.send_client_content( | ||||||
turns=Content(role="user", parts=[Part(text=text_input)]) | ||||||
) | ||||||
|
||||||
response = [] | ||||||
|
||||||
async for chunk in session.receive(): | ||||||
if chunk.server_content: | ||||||
if chunk.text is not None: | ||||||
response.append(chunk.text) | ||||||
|
||||||
model_turn = chunk.server_content.model_turn | ||||||
if model_turn: | ||||||
for part in model_turn.parts: | ||||||
if part.executable_code is not None: | ||||||
print(part.executable_code.code) | ||||||
|
||||||
if part.code_execution_result is not None: | ||||||
print(part.code_execution_result.output) | ||||||
|
||||||
print("".join(response)) | ||||||
# Example output: | ||||||
# > Compute the largest prime palindrome under 10 | ||||||
# Final Answer: The final answer is $\boxed{7}$ | ||||||
# [END googlegenaisdk_live_code_exec_with_txt] | ||||||
return response | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
asyncio.run(generate_content()) |
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,72 @@ | ||
# 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 | ||
|
||
|
||
async def generate_content() -> list[str]: | ||
# [START googlegenaisdk_live_func_call_with_txt] | ||
from google import genai | ||
from google.genai.types import ( | ||
LiveConnectConfig, | ||
Modality, | ||
Tool, | ||
FunctionDeclaration, | ||
FunctionResponse, | ||
) | ||
|
||
client = genai.Client() | ||
# model = "gemini-live-2.5-flash" | ||
model_id = "gemini-2.0-flash-live-preview-04-09" | ||
|
||
turn_on_the_lights = FunctionDeclaration(name="turn_on_the_lights") | ||
turn_off_the_lights = FunctionDeclaration(name="turn_off_the_lights") | ||
|
||
config = LiveConnectConfig( | ||
response_modalities=[Modality.TEXT], | ||
tools=[Tool(function_declarations=[turn_on_the_lights, turn_off_the_lights])], | ||
) | ||
async with client.aio.live.connect(model=model_id, config=config) as session: | ||
text_input = "Turn on the lights please" | ||
print("> ", text_input, "\n") | ||
await session.send_client_content(turns={"parts": [{"text": text_input}]}) | ||
|
||
async for chunk in session.receive(): | ||
if chunk.server_content: | ||
if chunk.text is not None: | ||
print(chunk.text) | ||
|
||
elif chunk.tool_call: | ||
function_responses = [] | ||
for fc in chunk.tool_call.function_calls: | ||
function_response = FunctionResponse( | ||
name=fc.name, | ||
response={ | ||
"result": "ok" | ||
}, # simple, hard-coded function response | ||
) | ||
function_responses.append(function_response) | ||
print(function_response.response["result"]) | ||
|
||
await session.send_tool_response(function_responses=function_responses) | ||
|
||
# Example output: | ||
# > Turn on the lights please | ||
# ok | ||
# [END googlegenaisdk_live_func_call_with_txt] | ||
return function_responses | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(generate_content()) |
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,70 @@ | ||
# 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 | ||
|
||
|
||
async def generate_content() -> list[str]: | ||
# [START googlegenaisdk_live_ground_googsearch_with_txt] | ||
from google import genai | ||
from google.genai.types import ( | ||
LiveConnectConfig, | ||
Modality, | ||
Tool, | ||
GoogleSearch, | ||
Content, | ||
Part, | ||
) | ||
|
||
client = genai.Client() | ||
# model = "gemini-live-2.5-flash" #todo | ||
model_id = "gemini-2.0-flash-live-preview-04-09" | ||
config = LiveConnectConfig( | ||
response_modalities=[Modality.TEXT], | ||
tools=[Tool(google_search=GoogleSearch())], | ||
) | ||
async with client.aio.live.connect(model=model_id, config=config) as session: | ||
text_input = "When did the last Brazil vs. Argentina soccer match happen?" | ||
await session.send_client_content( | ||
turns=Content(role="user", parts=[Part(text=text_input)]) | ||
) | ||
|
||
response = [] | ||
|
||
async for chunk in session.receive(): | ||
if chunk.server_content: | ||
if chunk.text is not None: | ||
response.append(chunk.text) | ||
|
||
# The model might generate and execute Python code to use Search | ||
model_turn = chunk.server_content.model_turn | ||
if model_turn: | ||
for part in model_turn.parts: | ||
if part.executable_code is not None: | ||
print(part.executable_code.code) | ||
|
||
if part.code_execution_result is not None: | ||
print(part.code_execution_result.output) | ||
|
||
print("".join(response)) | ||
# Example output: | ||
# > When did the last Brazil vs. Argentina soccer match happen? | ||
# The last Brazil vs. Argentina soccer match was on March 25, 2025, a 2026 World Cup qualifier, where Argentina defeated Brazil 4-1. | ||
# [END googlegenaisdk_live_ground_googsearch_with_txt] | ||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(generate_content()) |
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 | ||
|
||
|
||
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-live-2.5-flash" | ||
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
Oops, something went wrong.
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.
This commented-out line with a
todo
appears to be a leftover from development. It should be removed to keep the code clean and avoid confusion.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.
As suggested here, please remove
TODO
lineThere 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.
We will discuss this model_id on today's call. Afterwards, I'll remove that or update a model