-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
972ae62
11a09e4
4c066e6
8d4753a
36114d5
4327734
8317d3a
9a1e37b
bdc23e4
3fcb287
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||
# 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, | ||||||
) | ||||||
|
||||||
config = LiveConnectConfig( | ||||||
response_modalities=[Modality.TEXT], | ||||||
tools=[Tool(code_execution=ToolCodeExecution())], | ||||||
) | ||||||
|
||||||
client = genai.Client() | ||||||
# model_id = "gemini-live-2.5-flash" #todo | ||||||
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. 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. As suggested here, please remove 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. We will discuss this model_id on today's call. Afterwards, I'll remove that or update a model |
||||||
model_id = "gemini-2.0-flash-live-preview-04-09" | ||||||
|
||||||
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: | ||||||
# STRING | ||||||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
# [END googlegenaisdk_live_code_exec_with_txt] | ||||||
return response | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
asyncio.run(generate_content()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
google-genai==1.20.0 | ||
google-genai==1.27.0 | ||
scipy==1.15.3 | ||
websockets==15.0.1 | ||
websockets==15.0.1 | ||
soundfile==0.13.1 | ||
librosa==0.11.0 | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.