-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Reuse Google GenAI Client #24
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 all commits
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 |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2024-05-21 - [Blender Modal Redraws] | ||
| **Learning:** Blender `modal` operators run frequently (e.g. every 0.3s). Calling `area.tag_redraw()` unconditionally in the loop forces constant viewport rendering, even when no state has changed, causing unnecessary CPU/GPU load. | ||
| **Action:** Only call `tag_redraw()` when the operator actually processes data or updates the UI state (e.g., via a `refresh_needed` flag). | ||
|
|
||
| ## 2024-05-23 - [API Client Overhead] | ||
| **Learning:** Instantiating `google.genai.Client` is not cheap (measured ~100ms per call). When making multiple sequential or parallel API calls, re-creating the client inside the loop adds significant cumulative overhead (~500ms for 5 calls). | ||
| **Action:** Initialize API clients once at the start of the workflow (e.g. in the operator or main function) and pass the instance to helper functions via dependency injection. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,9 +16,10 @@ def get_client(api_key): | |
| return genai.Client(api_key=api_key) | ||
|
|
||
|
|
||
| def refine_prompt(api_key, prompt): | ||
| def refine_prompt(api_key, prompt, client=None): | ||
| """Use Gemini to refine a prompt for 3D model generation.""" | ||
| client = get_client(api_key) | ||
| if client is None: | ||
| client = get_client(api_key) | ||
|
|
||
| instruction = ( | ||
| f"Refine this prompt for generating a high-quality 3D model reference image. " | ||
|
|
@@ -36,15 +37,16 @@ def refine_prompt(api_key, prompt): | |
| return response.text.strip() | ||
|
|
||
|
|
||
| def generate_image(api_key, prompt, output_path, input_image_path=None): | ||
| def generate_image(api_key, prompt, output_path, input_image_path=None, client=None): | ||
| """ | ||
| Generate an image using Gemini. | ||
| If input_image_path is provided, use it as reference for the generation. | ||
| """ | ||
| from google.genai import types | ||
| from PIL import Image | ||
|
|
||
| client = get_client(api_key) | ||
| if client is None: | ||
| client = get_client(api_key) | ||
|
Comment on lines
+40
to
+49
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. Similar to This change will require updating the call sites in def generate_image(prompt, output_path, input_image_path=None, client=None, api_key=None):
"""
Generate an image using Gemini.
If input_image_path is provided, use it as reference for the generation.
"""
from google.genai import types
from PIL import Image
if client is None:
if not api_key:
raise ValueError("Either 'client' or 'api_key' must be provided.")
client = get_client(api_key) |
||
|
|
||
| config = types.GenerateContentConfig( | ||
| response_modalities=["Image"], | ||
|
|
||
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.
The function signature can be improved to better reflect its dependencies. Currently,
api_keyis a required positional argument, but it's only used ifclientis not provided. This can be confusing for callers. Consider makingapi_keyan optional keyword argument and reordering arguments for clarity. You should also raise an error if neitherclientnorapi_keyis provided. This makes the API contract clearer.This change will require updating the call sites in
operators.py.