Skip to main content

Python SDK for SecondMe API

Project description

SecondMe Python SDK (seme-sdk)

A Python SDK for interacting with the SecondMe API.

Installation

pip install seme-sdk

Or install from source:

git clone https://github.com/secondme/seme-sdk.git
cd seme-sdk
pip install -e .

Quick Start

Using API Key

from seme import SecondMeClient

# Create client with API Key
client = SecondMeClient(api_key="lba_ak_xxxxx...")

# Get user information
user = client.get_user_info()
print(f"Hello, {user.name}!")

# Streaming chat
for chunk in client.chat_stream("Hello, introduce yourself!"):
    print(chunk.delta, end="", flush=True)

# Close the client when done
client.close()

Using OAuth2

from seme import OAuth2Client, SecondMeClient

# Create OAuth2 client
oauth = OAuth2Client(
    client_id="your_client_id",
    client_secret="your_client_secret",
    redirect_uri="https://your-app.com/callback"
)

# Get authorization URL
auth_url = oauth.get_authorization_url(
    scopes=["user.info", "chat", "note.add"]
)
print(f"Direct user to: {auth_url}")

# Exchange authorization code for tokens
tokens = oauth.exchange_code(code="lba_ac_xxxxx...")

# Create client with auto-refresh
client = SecondMeClient.from_oauth(
    oauth_client=oauth,
    token_response=tokens,
    on_token_refresh=lambda t: print(f"Token refreshed!")
)

API Reference

SecondMeClient

The main client for interacting with SecondMe API.

Initialization

# With API Key (long-lived)
client = SecondMeClient(api_key="lba_ak_xxxxx...")

# With Access Token (short-lived, 2 hours)
client = SecondMeClient(access_token="lba_at_xxxxx...")

# With auto-refresh from OAuth2
client = SecondMeClient.from_oauth(oauth_client, token_response)

User API

# Get user information
user = client.get_user_info()
print(user.name, user.email, user.bio)

# Get user interest shades
shades = client.get_user_shades()
for shade in shades:
    print(shade.shade_name, shade.confidence_level)

# Get user soft memory (with pagination)
memories = client.get_user_softmemory(
    keyword="python",  # optional filter
    page_no=1,
    page_size=20
)
for memory in memories.items:
    print(memory.content)

Note API

# Add a text note
note_id = client.add_note(
    content="This is my note content",
    title="My Note"  # optional
)

# Add a note with URLs
note_id = client.add_note(
    urls=["https://example.com/article"],
    memory_type="URL"
)

Chat API

# Streaming chat
for chunk in client.chat_stream("Hello!"):
    print(chunk.delta, end="", flush=True)

# Chat with session continuity
session_id = None
for chunk in client.chat_stream("Hi!", session_id=session_id):
    if chunk.session_id:
        session_id = chunk.session_id
    print(chunk.delta, end="", flush=True)

# Continue conversation
for chunk in client.chat_stream("Tell me more", session_id=session_id):
    print(chunk.delta, end="", flush=True)

# Chat with custom system prompt
for chunk in client.chat_stream(
    "Tell me a joke",
    system_prompt="You are a friendly comedian."
):
    print(chunk.delta, end="", flush=True)

# Get session list
sessions = client.get_session_list()
for session in sessions:
    print(session.session_id, session.last_message)

# Get session messages
messages = client.get_session_messages(session_id="xxx")
for msg in messages:
    print(f"{msg.role}: {msg.content}")

OAuth2Client

For OAuth2 authorization flow.

from seme import OAuth2Client

oauth = OAuth2Client(
    client_id="your_client_id",
    client_secret="your_client_secret",
    redirect_uri="https://your-app.com/callback"
)

# Generate authorization URL
url = oauth.get_authorization_url(
    scopes=["user.info", "chat"],
    state="csrf_token"  # optional
)

# Exchange code for tokens
tokens = oauth.exchange_code(code="lba_ac_xxxxx...")
print(tokens.access_token)
print(tokens.refresh_token)
print(tokens.expires_in)

# Manually refresh token
new_tokens = oauth.refresh_token(tokens.refresh_token)

Available Scopes

Scope Description
user.info Access user basic information
user.info.shades Access user interest shades
user.info.softmemory Access user soft memory
note.add Add notes
chat Chat and session management

Exception Handling

from seme import (
    SecondMeClient,
    SecondMeError,
    AuthenticationError,
    PermissionDeniedError,
    NotFoundError,
    InvalidParameterError,
    RateLimitError,
    ServerError,
    TokenExpiredError,
)

try:
    client = SecondMeClient(api_key="invalid_key")
    user = client.get_user_info()
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
except PermissionDeniedError as e:
    print(f"Permission denied: {e}")
except TokenExpiredError as e:
    print(f"Token expired: {e}")
except SecondMeError as e:
    print(f"API error: {e}")

Data Models

UserInfo

class UserInfo:
    name: str
    email: str
    avatar: str
    bio: str
    self_introduction: str
    voice_id: str
    profile_completeness: float

Shade

class Shade:
    id: str
    shade_name: str
    confidence_level: float
    public_content: str
    private_content: str

SoftMemory

class SoftMemory:
    id: str
    content: str
    created_at: datetime
    memory_type: str

ChatChunk

class ChatChunk:
    content: str      # Accumulated content
    delta: str        # New content in this chunk
    done: bool        # Whether streaming is complete
    session_id: str
    message_id: str

Session

class Session:
    session_id: str
    app_id: str
    last_message: str
    last_update_time: datetime
    message_count: int

Development

Setup

# Clone the repository
git clone https://github.com/secondme/seme-sdk.git
cd seme-sdk

# Install with dev dependencies
pip install -e ".[dev]"

Running Tests

pytest tests/

Code Style

ruff check .
ruff format .

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seme_sdk-0.1.1.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seme_sdk-0.1.1-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file seme_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: seme_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for seme_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 42471fcccba18e10929b45505e0c90156e6b9541e55fc70e20ba6da58bc045bb
MD5 298211678cafedda8d2c22422b84f4cb
BLAKE2b-256 b1651d9073d34afd7283b7a8f40fe06c57bc7ceb7ce5410a6a129459acf085c9

See more details on using hashes here.

File details

Details for the file seme_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: seme_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for seme_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5080a7a19a615deb6b826f86e734d4dda7ef3476007330354d84d063ef672495
MD5 8dd96c610e6a009d5a2a7f42475220b5
BLAKE2b-256 24517817d19a18555c9f553d5eed6612a444844c4d99d6e7036d1708b10eaf23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page