8000 GitHub - moeru-ai/xsai: πŸ€–πŸ’¬ extra-small AI SDK.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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 license

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

import { generateText } from '@xsai/generate-text'
import { env } from 'node:process'

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 pkg-size.dev:

Package Installed size Bundled size Gzipped size
xsai@0.2.0 132KB 20KB 7KB
ai@4.3.13 10648KB 240KB 62KB

xsAI reduces the installation size 80x and the bundle size 12x.

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

If you only need the basic generateText, @xsai/generate-text@0.1.2 is only 21KB install size and 3.5KB bundle size (1.6KB gzipped). (try install it with pkg-size.dev)

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

# bun
bun install xsai

# deno
deno install xsai

Getting Started

Read the documentation to get started.

Examples

Generating Text (see above)
Streaming Text
import { streamText } from '@xsai/stream-text'
import { env } from 'node:process'

const { textStream } = await 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 { generateText } from '@xsai/generate-text'
import { tool } from '@xsai/tool'
import { env } from 'node:process'
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

Status

xsAI is currently in an early stage of development and may introduce breaking changes at any time.

License

MIT

Moeru AI / xsAI is not affiliated with OpenAI.

0