Examples
Common Napcar integration patterns — detection, streaming UI, structured extraction, and embeddings.
Feature-detect and fall back
import { createClient, isNapcar } from "@napcar/sdk";
if (!isNapcar()) {
showInstallBanner(); // degrade gracefully — never crash
}
const client = createClient();Local in Napcar, your cloud provider elsewhere
One call site, two backends. In Napcar the prompt runs on the local model; in
any other browser it routes to the cloud provider you configure. The
call_llm("…") pattern is the one-shot prompt() helper:
import { prompt } from "@napcar/sdk";
const text = await prompt("Summarize this page", {
// Used only when NOT in Napcar (no local binding). Ignored in Napcar.
cloud: { provider: "openai", apiKey: OPENAI_API_KEY },
});Or keep an explicit client and reuse sessions — same routing:
import { createClient } from "@napcar/sdk";
const client = createClient({
cloud: { provider: "groq", apiKey: GROQ_API_KEY }, // openai | openrouter | groq | together | mistral | deepseek | ollama | custom
});
const session = await client.requestSession({ task: "chat" });
const { text, model } = await session.generate({ input: "Hello" });
console.log("local:", client.isNative, "model:", model);A custom provider points at any OpenAI-compatible endpoint:
createClient({
cloud: { provider: "custom", baseUrl: "https://api.example/v1", model: "my-model", apiKey },
});Streaming into the UI
const session = await client.requestSession({ task: "chat", localOnly: true });
const output = document.querySelector("#out");
for await (const chunk of session.generateStreaming({ input: prompt })) {
output.textContent += chunk;
}Structured extraction
const { text } = await session.generate({
input: invoiceText,
responseFormat: {
type: "json_schema",
schema: {
type: "object",
properties: { vendor: { type: "string" }, total: { type: "number" } },
required: ["vendor", "total"],
},
},
});
const invoice = JSON.parse(text);Embeddings for local search
const { embeddings } = await client.embed({ input: docs });
// build a local vector index — nothing leaves the machineOpenAI-compatible client
const res = await fetch("https://local.napcar.ai/v1/chat/completions", {
method: "POST",
headers: { "Content-Type": "application/json", "X-Napcar-LLM-Handoff": "optional" },
body: JSON.stringify({ model: "napcar-default", messages, stream: true }),
});