Skip to content

moeru-ai/xsai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

xsAI

extra-small AI SDK.

npm version npm downloads bundle size install size license

xsAI is a series of utils to help you use OpenAI or OpenAI-compatible API.

import { env } from 'node:process'

import { generateText } from '@xsai/generate-text'

const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [
    {
      content: 'You are a helpful assistant.',
      role: 'system',
    },
    {
      content: 'This is a test, so please answer \'YES\' and nothing else.',
      role: 'user',
    },
  ],
  model: 'gpt-4o',
})

// "YES"
console.log(text)

Features

extra(x)-small(s)

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 Packagephobia or Bundlephobia:

In the following table, we used packagephobia's install size and bundlephobia's minified/gzipped size.

Package Install size Bundled size Gzipped size
[email protected] 142KB 22.7KB 7.1KB
[email protected] 5740KB 301.5KB 74.3KB

xsAI reduces the install size 40x and the bundled size 13x.

Notably, this contains dependencies introduced to support tool calls and structured output.

If you only need the basic generateText, @xsai/[email protected] is only 22.6KB install size and 4KB bundled size (1.7KB gzipped).

Runtime-agnostic

xsAI doesn't depend on Node.js Built-in Modules, it works well in Browsers, Deno, Bun and even the Edge Runtime.

Usage

Install

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 add xsai

# bun
bun install xsai

# deno
deno install npm:xsai

Getting Started

Read the documentation to get started.

Examples

Generating Text (see above)
Streaming Text
import { env } from 'node:process'

import { streamText } from '@xsai/stream-text'

const { textStream } = streamText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  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',
    },
  ],
  model: 'gpt-4o',
})

const text: string[] = []

for await (const textPart of textStream) {
  text.push(textPart)
}

// "The quick brown fox jumps over the lazy dog."
console.log(text)
Generating Text w/ Tool Calling
import { env } from 'node:process'

import { generateText } from '@xsai/generate-text'
import { tool } from '@xsai/tool'
import { description, object, pipe, string } from 'valibot'

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({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  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',
    },
  ],
  model: 'gpt-4o',
  toolChoice: 'required',
  tools: [weather],
})

// "In San Francisco, it's currently 42Β°F."
console.log(text)

Community Projects

License

MIT

Sponsors

sponsors

About

πŸ€–πŸ’¬ extra-small AI SDK.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors 25