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.
Core Concepts
Section titled “Core Concepts”- 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.
Obtain an API Key
Section titled “Obtain an API Key”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.
Install Sec-Gemini SDK
Section titled “Install Sec-Gemini SDK”pip install sec-gemini
npm install sec-gemini# or:# yarn add sec-gemini
Not applicable.
Initializing
Section titled “Initializing”from sec_gemini import SecGemini, MessageType, MimeTypeapi_key = "your_api_key_here" # replace with the API key from the earlier stepsg = SecGemini(api_key)
// ES Modulesimport SecGemini, { InteractiveSession, Message, MessageTypeEnum } from "sec-gemini";
const apiKey = "your_api_key_here"; // replace with the API key from the earlier stepconst sg = await SecGemini.create(apiKey);
Provide your API key using HTTP Bearer authentication.
Authorization: Bearer your_api_key_here
Creating a new session
Section titled “Creating a new session”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 theInteractiveSession.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}")
const session = await sg.createSession({ ttl: 3600, // 1 hour name: "My Session", description: "Testing Sec-Gemini SDK", language: "en"});
console.log(`Created session with ID: ${session.sessionId}`);
Not supported; all OpenAI requests create a new session.
Sending and receiving messages
Section titled “Sending and receiving messages”Streaming (Preferred)
Section titled “Streaming (Preferred)”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?"))
You provide callback functions to handle incoming messages and the final result.
Parameters:
onmessage
(function): Callback function to handle individual messages (Message
) from the stream.onresult
(function): Callback function to handle the final result (Message
) from the stream.
const streamer = await session.streamer( (message) => { console.log('Message:', message.content); }, (result) => { console.log('Result:', result.content); } );
Once the streamer is active, you can send messages to Sec-Gemini.
await streamer.send("What is the IP address of google.com?");
Not supported.
Interactive query and response
Section titled “Interactive query and response”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.
Not supported; use the streaming approach.
Sec-Gemini provides a minimal chat completion endpoint at https://api.secgemini.google/chat/completions.
Use the OpenAI SDK with https://api.secgemini.google/ or send an HTTP request to https://api.secgemini.google/chat/completions.
Example:
curl --location 'https://api.secgemini.google/chat/completions' \ --header 'Authorization: Bearer YOUR_API_KEY_HERE' \ --header 'Content-Type: application/json' \ --data '{ "model": "sec-gemini-1.1", "messages": [ { "role": "user", "content": "What is the IP address of google.com?" } ]}'
Further Reading
Section titled “Further Reading”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.