Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = { file = "LICENSE.md" }
requires-python = ">=3.10"
dependencies = [
"google-cloud-core>=2.4.3",
"google-genai>=1.27.0",
"google-genai>=1.59.0",
"json-repair~=0.40.0",
"pydantic>=2.11.7",
"rapidfuzz>=3.13.0",
Expand Down
40 changes: 40 additions & 0 deletions src/genai_utils/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,36 @@ def check_grounding_ran(response: types.GenerateContentResponse) -> bool:
return bool(n_searches and n_chunks and n_supports)


def get_thinking_config(
model_name: str, do_thinking: bool
) -> types.ThinkingConfig | None:
if "gemini-2.5-pro" in model_name:
if not do_thinking:
_logger.warning(
"It is not possible to turn off thinking with this model. Setting to minimum."
)
return types.ThinkingConfig(thinking_budget=128) # minimum thinking
return types.ThinkingConfig(thinking_budget=-1) # dynamic budget

if model_name <= "gemini-2.5":
if do_thinking:
return types.ThinkingConfig(thinking_budget=-1) # dynamic budget
return types.ThinkingConfig(thinking_budget=0) # disable thinking

if model_name >= "gemini-3":
if not do_thinking:
if "pro" in model_name:
_logger.warning(
"Cannot disable thinking in this model. Setting thinking to low."
)
return types.ThinkingConfig(thinking_level=types.ThinkingLevel.LOW)
return types.ThinkingConfig(thinking_level=types.ThinkingLevel.MINIMAL)
return None

_logger.warning("Did not recognise")
return None


def run_prompt(
prompt: str,
video_uri: str | None = None,
Expand Down Expand Up @@ -352,6 +382,10 @@ class Movie(BaseModel):
and makes the output more likely to be factual.
Does not work with structured output.
See the docs (`grounding`_).
do_thinking: bool
Whether Gemini should use a thought process.
This is more expensive but may yield better results.
Do not use for bulk tasks that don't require complex thoughts.
inline_citations: bool
Whether output should include citations inline with the text.
These citations will be links to be used as evidence.
Expand Down Expand Up @@ -394,6 +428,7 @@ async def run_prompt_async(
safety_settings: list[types.SafetySetting] = DEFAULT_SAFETY_SETTINGS,
model_config: ModelConfig | None = None,
use_grounding: bool = False,
do_thinking: bool = False,
inline_citations: bool = False,
labels: dict[str, str] = {},
) -> str:
Expand Down Expand Up @@ -444,6 +479,10 @@ class Movie(BaseModel):
and makes the output more likely to be factual.
Does not work with structured output.
See the docs (`grounding`_).
do_thinking: bool
Whether Gemini should use a thought process.
This is more expensive but may yield better results.
Do not use for bulk tasks that don't require complex thoughts.
inline_citations: bool
Whether output should include citations inline with the text.
These citations will be links to be used as evidence.
Expand Down Expand Up @@ -506,6 +545,7 @@ class Movie(BaseModel):
safety_settings=safety_settings,
**built_gen_config,
labels=merged_labels,
thinking_config=get_thinking_config(model_config.model_name, do_thinking),
),
)

Expand Down
42 changes: 24 additions & 18 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.