SambaNova for Open WebUI



I made a SambaNova provider manifold for Open WebUI that lists models automatically with web scraping.

Check it out: https://openwebui.com/f/olof/sambanova

💡 Listing models with web scraping

The SambaNova API is largely OpenAI-compatible. However, there is no model list API endpoint at this time.

Previous solutions have solved this by hard-coding the available models, such as this one https://openwebui.com/f/fabiofalopes/sambanova.

🙈 I figured out this alternative approach:

def get_model_ids_uncached(self):
    # Hey, wanna see something crazy?
    # SambaNova offers no model listing API endpoint at the moment.
    # However, we can scrape it from the documentation:
    # https://community.sambanova.ai/t/supported-models/193

    response = requests.get("https://community.sambanova.ai/t/supported-models/193")
    response.raise_for_status()
    soup = BeautifulSoup(response.text, "html.parser")
    code_texts = [code.get_text() for code in soup.find_all("code")]
    return code_texts

It is just like doing [...document.querySelectorAll('code')].map(el => el.textContent) in the browser console:

SambaNova Document Selector Screenshot

📝 Full Code

"""
title: SambaNova
description: https://cloud.sambanova.ai
author: Olof Larsson
author_url: https://olof.tech/sambanova-for-open-webui
version: 1.2.0
license: MIT
"""

import os
import time
import requests
from typing import List, Union, Generator, Iterator, Optional
from pydantic import BaseModel, Field
from bs4 import BeautifulSoup

# Learn more here:
# https://olof.tech/sambanova-for-open-webui


class Pipe:
    class Valves(BaseModel):
        API_KEY: str = Field(
            default=os.getenv("SAMBANOVA_API_KEY", ""),
            description="Get your key here: https://cloud.sambanova.ai",
        )

    def __init__(self):
        self.type = "manifold"
        self.id = "sambanova"
        self.name = "sambanova/"
        self.valves = self.Valves()
        self.base_url = "https://api.sambanova.ai/v1"

    def get_headers(self):
        return {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.valves.API_KEY}",
        }

    def get_model_ids_uncached(self):
        # Hey, wanna see something crazy?
        # SambaNova offers no model listing API endpoint at the moment.
        # However, we can scrape it from the documentation:
        # https://community.sambanova.ai/t/supported-models/193

        response = requests.get("https://community.sambanova.ai/t/supported-models/193")
        response.raise_for_status()
        soup = BeautifulSoup(response.text, "html.parser")
        code_texts = [code.get_text() for code in soup.find_all("code")]
        return code_texts

    def get_model_ids(self):
        # Check if we have a cached result less than 10 minutes old and API key hasn't changed
        current_time = time.time()
        if (
            hasattr(self, "_cached_models")
            and hasattr(self, "_cache_time")
            and hasattr(self, "_cache_api_key")
            and self._cache_api_key == self.valves.API_KEY
        ):
            if current_time - self._cache_time < 600:  # 10 minutes
                return self._cached_models

        # Cache miss, expired, or API key changed - fetch new data
        result = self.get_model_ids_uncached()

        # Update cache with current API key
        self._cached_models = result
        self._cache_time = current_time
        self._cache_api_key = self.valves.API_KEY
        return result

    def pipes(self) -> List[dict]:
        model_ids = self.get_model_ids()
        return [{"id": model_id, "name": model_id} for model_id in model_ids]

    def pipe(
        self, body: dict, __user__: Optional[dict] = None
    ) -> Union[str, Generator, Iterator]:
        body["model"] = body["model"].removeprefix(self.id + ".")
        headers = self.get_headers()

        try:
            r = requests.post(
                url=f"{self.base_url}/chat/completions",
                json=body,
                headers=headers,
                stream=True,
            )

            r.raise_for_status()

            if body["stream"]:
                return r.iter_lines()
            else:
                return r.json()
        except Exception as e:
            return f"Error: {e}"

🔗 Related Posts

I recently made Open WebUI plugins for all of the three big inference hardware companies: