Dynamic model details, get_models()

SambaNova Models Python Module

A lightweight Python client for accessing Sambanova AI model metadata, including model specifications, availability, and pricing. Easily integrate model discovery into your AI applications.

Features

  • :rocket: Real-time model metadata from Sambanova API
  • :money_bag: Access to model pricing (prompt/completion costs)
  • :straight_ruler: Retrieve technical specs (context length, max tokens)
  • :magnifying_glass_tilted_left: Model search by ID
  • :counterclockwise_arrows_button: Automatic caching with manual refresh
  • :hammer_and_wrench: Simple integration with existing projects

Installation

pip install requests

Then place sambanova_models.py in your project directory.

Basic Usage

from sambanova_models import SambanovaModels

# Initialize client
client = SambanovaModels()

# Get all available model IDs
model_ids = client.get_model_ids()
print(f"Available models: {model_ids}")

# Get full details for a specific model
model_details = client.get_model("Meta-Llama-3.1-405B-Instruct")
print(f"Model details: {model_details}")

# Get pricing information
pricing = client.get_pricing("DeepSeek-R1-0528")
print(f"Prompt cost: ${pricing['prompt']}/token")
print(f"Completion cost: ${pricing['completion']}/token")

Applied Usage Examples

Cost Calculation Example

def calculate_chat_cost(client, model_id, prompt_tokens, completion_tokens):
    pricing = client.get_pricing(model_id)
    if not pricing:
        return None
        
    prompt_cost = float(pricing['prompt']) * prompt_tokens
    completion_cost = float(pricing['completion']) * completion_tokens
    return prompt_cost + completion_cost

# Example usage
cost = calculate_chat_cost(client, "Meta-Llama-3.1-405B-Instruct", 1500, 250)
print(f"Chat cost: ${cost:.4f}")

Model Selection by Requirements

def find_models_by_specs(client, min_context_length=0, max_cost=0.001):
    suitable_models = []
    for model in client.get_models():
        context_length = model.get('context_length', 0)
        pricing = model.get('pricing', {})
        prompt_cost = float(pricing.get('prompt', 1))  # High default to exclude
        
        if (context_length >= min_context_length and 
            prompt_cost <= max_cost):
            suitable_models.append(model['id'])
            
    return suitable_models

# Find models with at least 16K context under $0.0001/token
models = find_models_by_specs(client, min_context_length=16000, max_cost=0.0001)
print(f"Models meeting requirements: {models}")

Creative Use Cases

  1. Dynamic Model Selection
    Automatically choose the most cost-effective model for a task:

    def select_best_model(client, required_context):
        best_model = None
        lowest_cost = float('inf')
        
        for model in client.get_models():
            if model['context_length'] >= required_context:
                cost = float(model['pricing']['prompt']) + float(model['pricing']['completion'])
                if cost < lowest_cost:
                    lowest_cost = cost
                    best_model = model['id']
        
        return best_model
    
  2. Budget-Aware AI Agent
    Create an agent that stays within budget:

    class BudgetAwareAgent:
        def __init__(self, client, budget=0.10):
            self.client = client
            self.budget = budget
            self.used_budget = 0.0
            
        def can_use_model(self, model_id, prompt_tokens, expected_completion):
            model = self.client.get_model(model_id)
            if not model:
                return False
                
            cost = (float(model['pricing']['prompt']) * prompt_tokens +
                    float(model['pricing']['completion']) * expected_completion)
                    
            return (self.used_budget + cost) <= self.budget
    
  3. Model Change Detection
    Get notified when model specs change:

    class ModelChangeDetector:
        def __init__(self, client):
            self.client = client
            self.snapshot = self._create_snapshot()
            
        def _create_snapshot(self):
            return {model['id']: model for model in self.client.get_models()}
            
        def check_for_changes(self):
            current = self._create_snapshot()
            changes = []
            
            for model_id, specs in current.items():
                if model_id not in self.snapshot:
                    changes.append(('ADDED', model_id))
                elif specs != self.snapshot[model_id]:
                    changes.append(('MODIFIED', model_id))
                    
            for model_id in set(self.snapshot) - set(current):
                changes.append(('REMOVED', model_id))
                
            self.snapshot = current
            return changes
    

API Reference

SambanovaModels(api_url="https://api.sambanova.ai/v1/models")

Constructor for the client. Optionally specify a custom API endpoint.

Methods

Method Description Returns
refresh() Force refresh model data bool (success)
get_models() Get all models List[Dict]
get_model_ids() Get all model IDs List[str]
get_model(model_id) Get model by ID Dict or None
get_pricing(model_id) Get pricing for model Dict or None
get_context_length(model_id) Get context length int or None
get_max_completion_tokens(model_id) Get max completion tokens int or None

Error Handling

The module handles API errors gracefully:

  • Methods return None when data isn’t available
  • refresh() returns False on connection failures
  • Automatic retry on first failure when accessing data
# Example error handling
model = client.get_model("unknown-model")
if not model:
    print("Model not found or API unavailable")
    
if not client.refresh():
    print("Failed to refresh model data. Check network connection.")

Code for copy / pasta goodness

import requests
from typing import Dict, List, Optional, Any


class SambanovaModels:
    """
    Client for accessing Sambanova AI model metadata

    Usage:
        from sambanova_models import SambanovaModels

        client = SambanovaModels()
        models = client.get_models()
        llama_details = client.get_model("Meta-Llama-3.1-405B-Instruct")
    """

    def __init__(self, api_url: str = "https://api.sambanova.ai/v1/models") -> None:
        self.api_url = api_url
        self._models: Optional[List[Dict[str, Any]]] = None

    def refresh(self) -> bool:
        """Refresh model data from API, returns success status"""
        try:
            response = requests.get(self.api_url, timeout=10)
            response.raise_for_status()
            self._models = response.json().get("data", [])
            return True
        except requests.exceptions.RequestException as e:
            print(f"API Error: {e}")
            return False

    def get_models(self) -> List[Dict[str, Any]]:
        """Get all available models with full metadata"""
        if self._models is None:
            self.refresh()
        return self._models or []

    def get_model_ids(self) -> List[str]:
        """Get list of all available model IDs"""
        return [model["id"] for model in self.get_models()]

    def get_model(self, model_id: str) -> Optional[Dict[str, Any]]:
        """Get full details for a specific model by ID"""
        for model in self.get_models():
            if model["id"] == model_id:
                return model
        return None

    def get_pricing(self, model_id: str) -> Optional[Dict[str, str]]:
        """Get pricing details for a specific model"""
        model = self.get_model(model_id)
        return model.get("pricing") if model else None

    def get_context_length(self, model_id: str) -> Optional[int]:
        """Get context length for a specific model"""
        model = self.get_model(model_id)
        return model.get("context_length") if model else None

    def get_max_completion_tokens(self, model_id: str) -> Optional[int]:
        """Get max completion tokens for a specific model"""
        model = self.get_model(model_id)
        return model.get("max_completion_tokens") if model else None
3 Likes