npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-model-picker

v0.3.0

Published

![](./assets/picker-example.png)

Readme

react-model-picker

A simple component and hook that works with the Vercel AI SDK to allow the user to switch between different providers, models, and API keys quickly and easily.

Features

Quick Start

This is all you need to make a model switcher with OpenAI and Google Generative AI providers, just like the image above.

"use client";

import { useState } from "react";
import { generateText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { ModelPicker } from "@/packages/react-model-picker";

export default function MyComponent() {
	const [model, setModel] = useState(null);

	return (
		<>
			<ModelPicker
				providers={[
					{
						name: "OpenAI",
						models: ["gpt-4o", "o4-mini"],
						provider: createOpenAI,
					},
					{
						name: "Google",
						models: ["gemini-2.0-flash", "gemini-2.5-pro"],
						provider: createGoogleGenerativeAI,
					},
				]}
				onModelChange={setModel}
			/>

			<button
				onClick={async () => {
					if (model) {
						console.log(
							await generateText({
								model: model,
								prompt: "What model are you",
							})
						);
					}
				}}
			>
				Generate
			</button>
		</>
	);
}

You can switch out the Vercel AI SDK compatible providers in the object simply by importing a new provider. You can also provide some models to choose from, although the user can also input their own model.

It's as simple as that—now you can use the model as normal.

View a few other advanced options here or use the useModelPicker hook for full customizability.

API Documentation

Provider Format

The provider list is always an array. Each provider looks like this:

{
    name: "OpenAI", // Display name for the provider
    models: ["o3-mini", "o4-mini"], // Default models to use
    provider: createOpenAI // The Vercel AI SDK provider
}

See the Quick Start for an example of importing the models and everything all together.

General Options

Options are always passed in as an object in ModelPicker (options prop) and useModelPicker (second argument after providers).

There are two options currently:

useLocalStorage Option

Boolean. Set to true to save preferred provider, model, and API Key in localStorage to presist between sessions.

This uses useHooks' useLocalStorage under the hood. If you're on something like Next.js, ensure you are in a Client Component, ideally with the useHook useIsClient hook.

modelType Option

Use the ModelType enum imported from react-model-picker.

There are 3 options currently:

  • ModelType.Language: Use for language models. Default.
  • ModelType.Embedding: Use for embedding models
  • ModelType.Image: Use for image generation models.

Under the hood, they simply call .languageModel(), .textEmbeddingModel(), and .imageModel() respectively on the Vercel AI SDK provider.

Note that useModelPicker can take a type parameter for what type of model you're using. For example, you may write useModelPicker<LanguageModel>(/* ... */). The options are LanguageModel, EmbeddingModel<string>, and ImageModel. It defaults to LanguageModel.

Example Configuration

Here's what a potential configuration with ModelPicker could look like:

<ModelPicker
	providers={
		[
			// ... providers
		]
	}
	options={{
		useLocalStorage: true,
		modelType: ModelType.Language,
	}}
	onModelChange={setModel}
/>

Here's that same configuration for useModelPicker:

useModelPicker<LanguageModel>(
	[
		// ... providers
	],
	{
		useLocalStorage: true,
		modelType: ModelType.Language,
	}
);

ModelPicker Component

The ModelPicker hook takes the following props:

| Prop Name | Description | | --------------- | -------------------------------------------------------------------------------- | | providers | Which providers are available to pick from. Learn more above | | options | Options you can pass in. Uses the general options format. | | onModelChange | Called when the model changes. Use it to update which model is used. |

useModelPicker Hook

The useModelPicker hook returns a set of functions to help you manage state. The actual function itself takes two parameters: providers (Learn more above) as well as options (Learn more above).

It also takes a type parameter for what type of model you are using (The options are LanguageModel, EmbeddingModel<string>, and ImageModel.)

Here's what it could look like:

const {
	providerList,
	selectedProvider,
	setSelectedProvider,
	apiKey,
	setApiKey,
	modelIdList,
	modelId,
	setModelId,
	model,
} = useModelPicker<LanguageModel>(
	[
		// ... providers
	],
	{
		// ... options
	}
);

Here's what each of the values returned do:

| Value Name | Description | | --------------------- | ----------------------------------------------------------------------- | | providerList | The original provider list you passed in. | | selectedProvider | Current selected provider, string referencing name of provider. | | setSelectedProvider | Change selected provider. | | apiKey | Current API key entered as string. | | setApiKey | Change current API key. | | modelIdList | List of model IDs available for the current model (what you passed in). | | modelId | Current model ID selected. | | setModelId | Change current model ID selected. | | model | The Vercel AI SDK model object. |

A few notes on useModelPicker:

  • setModelId can set the ID to something not in the modelIdList. You can use this to load custom models.
  • apiKey and modleId automatically are being saved for each provider. They'll be automatically updated as you change providers.

This could be a custom implementation with useModelPicker:

"use client";

import { generateText, LanguageModel } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { createCerebras } from "@ai-sdk/cerebras";
import { createGoogleGenerativeAI } from "@ai-sdk/google";

import { useModelPicker } from "@/packages/react-model-picker";

export default function Home() {
	const {
		providerList,
		selectedProvider,
		setSelectedProvider,
		apiKey,
		setApiKey,
		modelIdList,
		modelId,
		setModelId,
		model,
	} = useModelPicker<LanguageModel>([
		{
			name: "OpenAI",
			models: ["o3-mini", "o4-mini"],
			provider: createOpenAI,
		},
		{
			name: "Cerebras",
			models: ["llama-4-scout-17b-16e-instruct", "llama-3.3-70b"],
			provider: createCerebras,
		},
		{
			name: "Google",
			models: ["gemini-2.0-flash", "gemini-2.0-flash-thinking"],
			provider: createGoogleGenerativeAI,
		},
	]);

	return (
		<div className="max-w-md mx-auto mt-10 p-6 bg-white rounded-lg shadow-md">
			<div className="mb-4">
				<label className="block text-sm font-medium text-gray-700 mb-1">
					Provider
				</label>
				<select
					value={selectedProvider}
					onChange={(e) => setSelectedProvider(e.target.value)}
					className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
				>
					{providerList.map((provider) => (
						<option key={provider.name} value={provider.name}>
							{provider.name}
						</option>
					))}
				</select>
			</div>

			<div className="mb-4">
				<label className="block text-sm font-medium text-gray-700 mb-1">
					API Key
				</label>
				<input
					value={apiKey}
					onChange={(e) => setApiKey(e.target.value)}
					className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
					type="password"
					placeholder="Enter API key"
				/>
			</div>

			<div className="mb-4">
				<label className="block text-sm font-medium text-gray-700 mb-1">
					Model
				</label>
				<select
					value={modelId}
					onChange={(e) => setModelId(e.target.value)}
					className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
				>
					{modelIdList.map((model) => (
						<option key={model} value={model}>
							{model}
						</option>
					))}
				</select>
			</div>

			<button
				onClick={async () => {
					console.log(
						await generateText({
							model: model,
							prompt: "What model are you",
						})
					);
				}}
				className="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition duration-150 ease-in-out"
			>
				Generate
			</button>
		</div>
	);
}