extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.
xsAI is a series of utils to help you use OpenAI or OpenAI-compatible API.
import { generateText } from '@xsai/generate-text'
import { createOpenAI } from '@xsai/providers'
import { env } from 'node:process'
const openai = createOpenAI({
apiKey: env.OPENAI_API_KEY
})
const { text } = await generateText({
...openai.chat('gpt-4o'),
messages: [
{
content: 'You are a helpful assistant.',
role: 'system',
},
{
content: 'This is a test, so please answer \'YES\' and nothing else.',
role: 'user',
},
],
})
// "YES"
console.log(text)
xsAI uses a variety of methods to make itself smaller.
It's just a wrapper for Fetch API, ESM Only, adding additional dependencies only when absolutely necessary.
How xsAI small? you can try install it with pkg-size.dev:
[email protected]
is 111KB install size and 11KB bundle size (3KB gzipped).
Notably, this contains dependencies introduced to support tool calls and structured output.
If you only need the basic generateText
, @xsai/[email protected]
is only 12KB install size and 1.2KB bundle size (678B gzipped). (try install it with pkg-size.dev)
xsAI doesn't depend on Node.js Built-in Modules, it works well in Browsers, Deno, Bun and even the Edge Runtime.
You can also install only some of the utils of xsAI, such as
@xsai/generate-text
and@xsai/stream-text
.
# npm
npm install xsai
# yarn
yarn add xsai
# pnpm
pnpm install xsai
# bun
bun install xsai
# deno
deno install xsai
Read the documentation to get started.
Generating Text (see above)
import { createOpenAI } from '@xsai/providers'
import { streamText } from '@xsai/stream-text'
import { env } from 'node:process'
const openai = createOpenAI({
apiKey: env.OPENAI_API_KEY,
})
const { textStream } = await streamText({
...openai.chat('gpt-4o'),
messages: [
{
content: 'You are a helpful assistant.',
role: 'system',
},
{
content: 'This is a test, so please answer \'The quick brown fox jumps over the lazy dog.\' and nothing else.',
role: 'user',
},
],
})
const text: string[] = []
for await (const textPart of textStream) {
text.push(textPart)
}
// "The quick brown fox jumps over the lazy dog."
console.log(text)
import { generateText } from '@xsai/generate-text'
import { createOpenAI } from '@xsai/providers'
import { tool } from '@xsai/tool'
import { env } from 'node:process'
import { description, object, pipe, string } from 'valibot'
const openai = createOpenAI({
apiKey: env.OPENAI_API_KEY,
})
const weather = await tool({
description: 'Get the weather in a location',
execute: ({ location }) => JSON.stringify({
location,
temperature: 42,
}),
name: 'weather',
parameters: object({
location: pipe(
string(),
description('The location to get the weather for'),
),
}),
})
const { text } = await generateText({
...openai.chat('gpt-4o'),
maxSteps: 2,
messages: [
{
content: 'You are a helpful assistant.',
role: 'system',
},
{
content: 'What is the weather in San Francisco? do not answer anything else.',
role: 'user',
},
],
toolChoice: 'required',
tools: [weather],
})
// "In San Francisco, it's currently 42Β°F."
console.log(text)
xsAI is currently in an early stage of development and may introduce breaking changes at any time.
Moeru AI / xsAI is not affiliated with OpenAI.