AIWrapper
A universal AI wrapper for JavaScript & TypeScript.
Use LLMs from anywhere—servers, browsers and web-apps. AIWrapper works in anything that runs JavaScript.
:warning: It's in early WIP stage and the API may change.
Features
- Generate plain text or JSON objects with a simple API
- Use different LLM providers: OpenAI, Anthropic, Groq, DeepSeek, Ollama and any OpenAI-compatible services
- Output objects based on Zod schemas or JSON Schema
- Swap models quickly or chain different models together
- Use it with JavaScript or TypeScript from anywhere
Installation
npm install aiwrapper
Quick Start
Generate Text
import { Lang } from "aiwrapper";
const lang = Lang.openai({ apiKey: "YOUR KEY" });
const result = await lang.ask("Say hi!");
console.log(result.answer);
Lang (LLM) Examples
Initialize a Model
import { Lang } from "aiwrapper";
const lang = Lang.openai({ apiKey: "YOUR KEY" });
Connect to Custom OpenAI-compatible APIs
import { Lang } from "aiwrapper";
const lang = Lang.openaiLike({
apiKey: "YOUR KEY",
model: "model-name",
baseURL: "https://your-custom-api.example.com/v1",
systemPrompt: "Optional system prompt",
headers: {
"X-Custom-Header": "custom-value",
"Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
},
bodyProperties: {
temperature: 0.7,
presence_penalty: 0.6,
frequency_penalty: 0.1
}
});
const result = await lang.ask("Hello!");
console.log(result.answer);
Stream Results
await lang.ask("Hello, AI!", {
onResult: (msg) => console.log(msg)
});
Use Templates
function getPrompt(product) {
return `You are a naming consultant for new companies. What is a good name for a company that makes ${product}?
Write just the name. Nothing else aside from the name - no extra comments or characters that are not part of the name.`;
}
const prompt = getPrompt("colorful socks");
await lang.ask(prompt, {
onResult: (msg) => console.log(msg)
});
Conversation Management
const result = await lang.ask("Hello, who are you?");
console.log(result.answer);
result.addUserMessage("Tell me more about yourself");
const newResult = await lang.chat(result);
console.log(newResult.answer);
newResult.addUserMessage("What can you help me with?");
const finalResult = await lang.chat(newResult);
console.log(finalResult.answer);
import { LangMessages } from "aiwrapper";
const messages = new LangMessages();
messages.addSystemMessage("You are a helpful assistant.");
messages.addUserMessage("Tell me about TypeScript.");
const chatResult = await lang.chat(messages);
console.log(chatResult.answer);
Getting Objects from LLMs
import { z } from "aiwrapper";
const companyNamesSchema = z.array(z.string());
const result = await lang.askForObject(
"You are a naming consultant for new companies. What are 3 good names for a company that makes colorful socks?",
companyNamesSchema
);
console.log(result.object);
const jsonSchema = {
type: "array",
items: {
type: "string"
}
};
const result2 = await lang.askForObject(
"You are a naming consultant for new companies. What are 3 good names for a company that makes colorful socks?",
jsonSchema
);
console.log(result2.object);
Getting Complex Objects
import { z } from "aiwrapper";
const companySchema = z.object({
name: z.string(),
tagline: z.string(),
marketingStrategy: z.object({
target: z.string(),
channels: z.array(z.string()),
budget: z.number()
})
});
const result = await lang.askForObject(
"Create a company profile for a business that makes colorful socks",
companySchema
);
console.log(result.object);
const jsonSchema = {
type: "object",
properties: {
name: { type: "string" },
tagline: { type: "string" },
marketingStrategy: {
type: "object",
properties: {
target: { type: "string" },
channels: {
type: "array",
items: { type: "string" }
},
budget: { type: "number" }
}
}
},
required: ["name", "tagline", "marketingStrategy"]
};
const result2 = await lang.askForObject(
"Create a company profile for a business that makes colorful socks",
jsonSchema
);
console.log(result2.object);