Show HN: I built an SDK to select the best model for your task

1 day ago 1

Auto-select the best AI model based on your query. This SDK automatically finds the most suitable models from Hugging Face based on semantic similarity to your task description.

npm install autorouter-sdk

AutoRouter requires OpenAI and Pinecone credentials. You'll need:

  • OpenAI API key for generating embeddings
  • Pinecone API key for vector database storage

Set them as environment variables:

export OPENAI_API_KEY='your-openai-key' export PINECONE_API_KEY='your-pinecone-key'

Or use a .env file:

OPENAI_API_KEY=your-openai-key PINECONE_API_KEY=your-pinecone-key PINECONE_INDEX_NAME=autorouter-models

Initial Setup - Index Models

Before using the SDK, you need to index models into your Pinecone database. Run:

npx autorouter-sdk index-models

Or using the CLI directly:

autorouter-sdk index-models \ --openai-key your-openai-key \ --pinecone-key your-pinecone-key \ --index-name autorouter-models

This will:

  1. Load the bundled model registry (12,000+ models)
  2. Generate embeddings for each model
  3. Index them into your Pinecone database

The process takes about 10-15 minutes. You only need to run this once.

import { AutoRouter } from 'autorouter-sdk'; const router = new AutoRouter({ openaiKey: 'your-openai-key', pineconeKey: 'your-pinecone-key', pineconeIndexName: 'autorouter-models' // optional, defaults to 'autorouter-models' }); const models = await router.selectModel('build a chatbot'); console.log(models); // [ // { // id: 'meta-llama/Llama-2-7b-chat-hf', // name: 'Llama-2-7b-chat-hf', // description: 'A conversational AI model...', // task: 'text-generation', // provider: 'huggingface', // license: 'apache-2.0', // downloads: 5000000, // score: 0.95, // endpoint: 'https://api-inference.huggingface.co/models/...' // }, // ... // ]
// With options const models = await router.selectModel('summarize text', { limit: 10, filter: { license: 'apache-2.0' } }); // Get top 5 models const topModels = await router.selectModel('generate images', { limit: 5 });
new AutoRouter(config: AutoRouterConfig)
  • config.openaiKey (string): Your OpenAI API key
  • config.pineconeKey (string): Your Pinecone API key
  • config.pineconeIndexName (string, optional): Pinecone index name (defaults to 'autorouter-models')
selectModel(query, options?)

Returns an array of model recommendations sorted by relevance.

  • query (string): Description of your task (e.g., "build a chatbot", "generate images")
  • options (object, optional):
    • limit (number): Maximum number of results (default: 10)
    • filter (object): Filter options
      • license (string): Filter by license type (e.g., "apache-2.0", "mit")
interface ModelResult { id: string; // Model identifier name: string; // Model name description: string; // Model description task: string; // Task type (e.g., 'text-generation') provider: string; // Provider (e.g., 'huggingface') license: string; // License type downloads?: number; // Download count score: number; // Similarity score (0-1) endpoint?: string; // Inference endpoint URL }
const models = await router.selectModel('generate creative stories');
const models = await router.selectModel('generate anime artwork');
const models = await router.selectModel('sentiment analysis');
const models = await router.selectModel('summarize documents', { filter: { license: 'apache-2.0' } });

Customizing the Model Registry

The package includes a pre-built registry with 7,000+ models. You can customize which models are included by modifying scripts/generate-model-registry.ts and running:

npm run generate-registry

This will regenerate data/model-registry.json based on your changes. Then run the indexing process again:

npx autorouter-sdk index-models

Key parameters in generate-model-registry.ts:

  • modelsPerTask: Number of models to fetch per task type (default: 200)
  • taskCategories: Which task types to include
  • sortBy: How to sort models (default: 'downloads')
autorouter-sdk index-models

Options:

  • --openai-key <key>: OpenAI API key (or use OPENAI_API_KEY env var)
  • --pinecone-key <key>: Pinecone API key (or use PINECONE_API_KEY env var)
  • --index-name <name>: Pinecone index name (default: 'autorouter-models')
  • --registry-path <path>: Path to custom model registry JSON file
try { const models = await router.selectModel('build a chatbot'); } catch (error) { console.error('Failed to select model:', error); }

MIT

Read Entire Article