Packages
snakepit
0.6.2
0.13.0
0.12.0
0.11.1
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
High-performance pooler and session manager for external language integrations. Supports Python, Node.js, Ruby, and more with gRPC streaming, session management, and production-ready process cleanup.
Current section
Files
Jump to
Current section
Files
priv/python/snakepit_bridge/adapters/showcase/handlers/binary_ops.py
"""Binary operations handler for showcase adapter."""
import numpy as np
import time
from typing import Dict, Any, List
from ..tool import Tool
class BinaryOpsHandler:
"""Handler for binary serialization demonstrations."""
def get_tools(self) -> Dict[str, Tool]:
"""Return all tools provided by this handler."""
return {
"create_tensor": Tool(self.create_tensor),
"create_embedding": Tool(self.create_embedding),
"benchmark_encoding": Tool(self.benchmark_encoding),
}
def create_tensor(self, ctx, name: str, shape: List[int],
size_bytes: int = None) -> Dict[str, Any]:
"""Create a tensor and demonstrate encoding detection."""
# Calculate elements needed for target size
if size_bytes:
# Each float64 is 8 bytes
total_elements = size_bytes // 8
# Adjust shape to meet size requirement
if len(shape) == 2:
shape[0] = int(total_elements // shape[1])
# Ensure shape contains only integers
shape = [int(s) for s in shape]
# Create tensor
data = np.random.randn(*shape)
actual_bytes = data.nbytes
# The system automatically chooses encoding
# JSON for < 10KB, binary for >= 10KB
encoding = "binary" if actual_bytes >= 10240 else "json"
# In production, this would use ctx.register_variable()
# For demo, we just report the tensor was created
return {
"name": name,
"shape": shape,
"size_bytes": actual_bytes,
"encoding": encoding,
"elements": data.size
}
def create_embedding(self, ctx, text: str, dimensions: int) -> Dict[str, Any]:
"""Create an embedding (float vector) from text."""
# Simulate text embedding
# In real usage, this would use an actual embedding model
# Create pseudo-random embedding based on text
seed = sum(ord(c) for c in text)
np.random.seed(seed % 2**32)
embedding = np.random.randn(dimensions)
# Normalize to unit vector (common for embeddings)
embedding = embedding / np.linalg.norm(embedding)
# In production, this would use ctx.register_variable()
# For demo, we simulate the storage
size_bytes = embedding.nbytes
encoding = "binary" if size_bytes >= 10240 else "json"
return {
"text": text,
"dimensions": dimensions,
"encoding": encoding,
"size_bytes": size_bytes,
"norm": float(np.linalg.norm(embedding))
}
def benchmark_encoding(self, ctx, start_kb: int = 1, end_kb: int = 100,
step_kb: int = 10) -> Dict[str, Any]:
"""Benchmark JSON vs binary encoding thresholds."""
results = []
for size_kb in range(start_kb, end_kb + 1, step_kb):
size_bytes = size_kb * 1024
elements = size_bytes // 8 # float64
# Create data
data = np.random.randn(elements)
# Measure JSON encoding time
start = time.time()
json_data = data.tolist()
json_time = time.time() - start
# Simulate binary encoding time (much faster)
start = time.time()
binary_data = data.tobytes()
binary_time = time.time() - start
results.append({
"size_kb": size_kb,
"encoding": "binary" if size_bytes >= 10240 else "json",
"json_time_ms": round(json_time * 1000, 2),
"binary_time_ms": round(binary_time * 1000, 2),
"speedup": round(json_time / binary_time, 1)
})
return {"results": results}