Skip to content

Getting Started with SDKs

The Sec-Gemini SDKs provide a streamlined way to interact with the Sec-Gemini platform programmatically. The SDK handles all low-level communication with the Sec-Gemini backend service. This guide provides code examples for both Python and TypeScript.

  • Initialization: The SDK is initialized by providing your API key.
  • Sessions: All Sec-Gemini interactions happen within a session (InteractiveSession). You can create new sessions or resume existing ones.
  • Messages: Communication within a session is structured using Message objects, which include information like the message type, role, and content.
  • Streaming: The SDK supports bidirectional streaming of messages, enabling real-time communication.

If you haven’t already, head to secgemini.google and obtain an API key using the “Get API Keys” link on the left side (after signing in, if necessary). You will need it later in this guide.

Terminal window
pip install sec-gemini
from sec_gemini import SecGemini, MessageType, MimeType
api_key = "your_api_key_here" # replace with the API key from the earlier step
sg = SecGemini(api_key)

All interactions with Sec-Gemini occur within a session. Sessions can be customized with optional parameters:

  • ttl (int, optional): Time to live for the session in seconds (default: 86400, minimum: 300). The session will expire this many seconds after creation, unless the TTL is explicitly extended with the InteractiveSession.update() method.
  • name (str, optional): Human-readable name for the session.
  • description (str, optional): Description of the session.
  • log_session (bool, optional): Whether the session can be logged (default: True). This option only has an effect if logging is optional for the API key.
  • model (str | ModelInfoInput, optional): Model to use (‘stable’, ‘experimental’, or specific model info).
  • language (str, optional): ISO language code (default: ‘en’).
session = sg.create_session(
ttl=3600, # 1 hour
name="My Session",
description="Testing Sec-Gemini SDK",
language="en"
)
print(f"Created session with ID: {session.session_id}")

Streams are used for bidirectional, real-time communication within a session.

import asyncio
async def query(prompt):
async for msg in session.stream(prompt):
if msg.message_type == MessageType.INFO and msg.mime_type == MimeType.TEXT:
print(f"INFO: {msg.get_content()}")
if msg.message_type == MessageType.RESULT and msg.mime_type == MimeType.TEXT:
# result
print(f"\nResponse:\n{msg.get_content()}")
break
asyncio.run(query("What is the IP address of google.com?"))

The InteractiveSession.query() method lets you easily send an individual query to Sec-Gemini.

response = session.query("What are the IP addresses for google.com?")
response.text()

The SessionResponse object includes additional information on how Sec-Gemini handled the query.

You’re up and running! You’ve successfully connected to Sec-Gemini and sent your first message.

To learn about what other functionality is available through the SDKs, visit their corresponding reference pages.