-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgemini_b.py
More file actions
58 lines (48 loc) · 1.64 KB
/
Copy pathgemini_b.py
File metadata and controls
58 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# pip install -U google-genai python-dotenv
import os
from pathlib import Path
from dotenv import load_dotenv
from google import genai
from google.genai import types
# Load .env located next to this script
load_dotenv(dotenv_path=Path(__file__).with_name(".env"))
# To run this code you need to install the following dependencies:
# pip install google-genai
# pip install google-generativeai
import base64
import os
from google import genai
from google.genai import types
def generate():
try:
# This is the original line from your exported code
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
model = "gemini-3-flash-preview"
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text="Create a 3 day itinerary for a trip to Japan."),
],
),
]
generate_content_config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="HIGH",
),
)
# Process the successful response here
for chunk in client.models.generate_content_stream(
model=model,
contents=contents,
config=generate_content_config,
):
print(chunk.text, end="")
except Exception as e:
# This block runs ONLY if the API call or connection fails
print(f"An error occurred: {e}")
print("Connection error. Please check your network or API key.")
if __name__ == "__main__":
generate()