Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

6.7 KB
Raw Blame
import React, { useMemo, useState } from "react";
import { BookOpen, Plus, Sparkles } from "lucide-react";

import { useModelCostMap } from "@/app/(dashboard)/hooks/models/useModelCostMap";
import { useProviderFields } from "@/app/(dashboard)/hooks/providers/useProviderFields";
import { useProxyDecisionProviders } from "@/app/(dashboard)/hooks/providers/useProxyDecisionProviders";
import { Logo } from "@/components/molecules/logo/Logo";
import { getProviderLogoAndName } from "@/components/provider_info_helpers";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import {
  DECISIONS_DOCS_URL,
  DECISIONS_PROVIDERS_DOCS_URL,
  DECISIONS_SELF_HOSTED_DOCS_URL,
  DecisionGardenModel,
  DecisionRoute,
  buildDecisionGarden,
} from "@/lib/decisionModels";
import { uiHref } from "@/utils/uiHref";

const PREVIEW_COUNT = 8;

const plural = (count: number, one: string, many: string): string => (count === 1 ? one : many);

const ROUTE_LINKS: Readonly<Record<DecisionRoute, { readonly href: string; readonly label: string }>> = {
  decisions: { href: DECISIONS_DOCS_URL, label: "/v1/decisions docs" },
  "self-hosted": { href: DECISIONS_SELF_HOSTED_DOCS_URL, label: "Self-hosted, own route" },
  "needs-upgrade": { href: DECISIONS_PROVIDERS_DOCS_URL, label: "Needs a newer LiteLLM" },
};

const DEPLOYMENT_NAMED_PROVIDERS: ReadonlySet<string> = new Set(["azure", "azure_ai"]);

const OWN_DEPLOYMENT_LINK = { href: DECISIONS_PROVIDERS_DOCS_URL, label: "Deploy under your own name" } as const;

const needsOwnDeploymentName = (row: DecisionGardenModel): boolean =>
  row.route === "decisions" && DEPLOYMENT_NAMED_PROVIDERS.has(row.provider);

const cardLink = (row: DecisionGardenModel) =>
  needsOwnDeploymentName(row) ? OWN_DEPLOYMENT_LINK : ROUTE_LINKS[row.route];

export const addModelHref = (row: DecisionGardenModel): string => {
  const params = new URLSearchParams({ tab: "add", provider: row.provider, litellm_model: row.model });
  return `${uiHref("models-and-endpoints")}?${params.toString()}`;
};

interface DecisionModelCardProps {
  readonly row: DecisionGardenModel;
  readonly canAddModels: boolean;
}

function DecisionModelCard({ row, canAddModels }: DecisionModelCardProps) {
  const { displayName } = getProviderLogoAndName(row.provider);
  const link = cardLink(row);
  return (
    <div
      className="flex flex-col gap-3 rounded-lg border bg-background p-4"
      data-testid={`decision-model-card-${row.model}`}
    >
      <div className="flex items-center gap-2">
        <Logo provider={row.provider} className="size-5" />
        <span className="truncate text-sm font-medium text-foreground">{displayName}</span>
      </div>
      <code className="truncate text-xs text-muted-foreground" title={row.model}>
        {row.model}
      </code>
      <div className="mt-auto flex items-center justify-between gap-2">
        <a
          href={link.href}
          target="_blank"
          rel="noopener noreferrer"
          className="text-xs text-muted-foreground underline"
        >
          {link.label}
        </a>
        {canAddModels && (
          <Button size="xs" variant="outline" nativeButton={false} role="link" render={<a href={addModelHref(row)} />}>
            <Plus />
            Add model
          </Button>
        )}
      </div>
    </div>
  );
}

interface DecisionModelGardenProps {
  readonly accessToken: string | null;
  readonly canAddModels: boolean;
}

const DecisionModelGarden: React.FC<DecisionModelGardenProps> = ({ accessToken, canAddModels }) => {
  const { data: costMap } = useModelCostMap(true, true);
  const { data: providerFields } = useProviderFields();
  const { data: proxyProviders, isLoading: proxyProvidersLoading } = useProxyDecisionProviders(accessToken);
  const rows = useMemo(() => buildDecisionGarden(costMap, proxyProviders ?? null), [costMap, proxyProviders]);
  const addableProviders = useMemo(
    () => new Set((providerFields ?? []).map((info) => info.litellm_provider)),
    [providerFields],
  );
  const [showAll, setShowAll] = useState(false);
  const isAddable = (row: DecisionGardenModel): boolean =>
    row.route === "decisions" && !needsOwnDeploymentName(row) && addableProviders.has(row.provider);

  if (proxyProvidersLoading || rows.length === 0) {
    return null;
  }

  const routedRows = rows.filter((row) => row.route === "decisions");
  const routedProviderCount = new Set(routedRows.map((row) => row.provider)).size;
  const selfHostedCount = rows.filter((row) => row.route === "self-hosted").length;
  const needsUpgradeCount = rows.filter((row) => row.route === "needs-upgrade").length;
  const visibleRows = showAll ? rows : rows.slice(0, PREVIEW_COUNT);

  return (
    <Card className="mt-6 px-6" data-testid="decision-model-garden">
      <div className="mb-4 flex items-center gap-4">
        <div className="flex size-10 shrink-0 items-center justify-center rounded-full border bg-background">
          <Sparkles className="size-4 text-muted-foreground" />
        </div>
        <div className="min-w-0 flex-1">
          <h3 className="m-0 text-base font-semibold text-foreground">Decision models LiteLLM supports</h3>
          <p className="m-0 mt-0.5 text-sm text-muted-foreground">
            {routedRows.length} decision {plural(routedRows.length, "model", "models")} across {routedProviderCount}{" "}
            {plural(routedProviderCount, "provider", "providers")} {plural(routedRows.length, "answers", "answer")} on{" "}
            <code>/v1/decisions</code>.
            {selfHostedCount > 0 &&
              ` ${selfHostedCount} more ${plural(selfHostedCount, "is self-hosted and answers on its own route", "are self-hosted and answer on their own routes")}.`}
            {needsUpgradeCount > 0 &&
              ` ${needsUpgradeCount} more ${plural(needsUpgradeCount, "needs", "need")} a newer LiteLLM version.`}
          </p>
        </div>
        <Button
          variant="outline"
          className="shrink-0"
          nativeButton={false}
          role="link"
          render={<a href={DECISIONS_DOCS_URL} target="_blank" rel="noopener noreferrer" />}
        >
          <BookOpen />
          Decisions docs
        </Button>
      </div>
      <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
        {visibleRows.map((row) => (
          <DecisionModelCard key={row.model} row={row} canAddModels={canAddModels && isAddable(row)} />
        ))}
      </div>
      {rows.length > PREVIEW_COUNT && (
        <div className="mt-4 flex justify-center">
          <Button variant="ghost" type="button" onClick={() => setShowAll((value) => !value)}>
            {showAll ? "Show fewer" : `Show all ${rows.length}`}
          </Button>
        </div>
      )}
    </Card>
  );
};

export default DecisionModelGarden;