Skip to content

Commit 6bc3eb6

Browse files
authored
chore: Add GoogleGenAIChatGenerator examples, set safety_settings (#1901)
* Add examples, set safety_settings * Add print in examples
1 parent be848c1 commit 6bc3eb6

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# To run this example, you will need to
2+
# 1) set `GOOGLE_API_KEY` environment variable
3+
# 2) install the google_genai_haystack integration: pip install google-genai-haystack
4+
# Note: if you change the model, update the model-specific inference parameters.
5+
6+
7+
from haystack.dataclasses import ChatMessage
8+
9+
from haystack_integrations.components.generators.google_genai import GoogleGenAIChatGenerator
10+
11+
generator = GoogleGenAIChatGenerator(
12+
model="gemini-2.0-flash",
13+
# model-specific inference parameters
14+
generation_kwargs={
15+
"temperature": 0.7,
16+
},
17+
)
18+
19+
system_prompt = """
20+
You are a helpful assistant that helps users learn more about Google Cloud services.
21+
Your audience is engineers with a decent technical background.
22+
Be very concise and specific in your answers, keeping them short.
23+
You may use technical terms, jargon, and abbreviations that are common among practitioners.
24+
"""
25+
26+
messages = [
27+
ChatMessage.from_system(system_prompt),
28+
ChatMessage.from_user("Which service should I use to train custom Machine Learning models?"),
29+
]
30+
31+
results = generator.run(messages)
32+
print(results["replies"][0].text)

integrations/google_genai/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ ban-relative-imports = "parents"
142142
[tool.ruff.lint.per-file-ignores]
143143
# Tests can use magic values, assertions, and relative imports
144144
"tests/**/*" = ["PLR2004", "S101", "TID252"]
145+
# Examples can use print statements
146+
"examples/**/*" = ["T201"]
145147

146148
[tool.coverage.run]
147149
source = ["haystack_integrations"]

integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ def run(
509509
if system_instruction:
510510
config_params["system_instruction"] = system_instruction
511511

512+
# Add safety settings if provided
513+
if safety_settings:
514+
config_params["safety_settings"] = safety_settings
515+
512516
# Add tools if provided
513517
if tools:
514518
config_params["tools"] = _convert_tools_to_google_genai_format(tools)
@@ -593,6 +597,10 @@ async def run_async(
593597
if system_instruction:
594598
config_params["system_instruction"] = system_instruction
595599

600+
# Add safety settings if provided
601+
if safety_settings:
602+
config_params["safety_settings"] = safety_settings
603+
596604
# Add tools if provided
597605
if tools:
598606
config_params["tools"] = _convert_tools_to_google_genai_format(tools)

0 commit comments

Comments
 (0)