https://github.com/moeru-ai/xsai
🤖💬 extra-small AI SDK.
https://github.com/moeru-ai/xsai
ai ollama openai typescript xsai
Last synced: 5 months ago
JSON representation
🤖💬 extra-small AI SDK.
- Host: GitHub
- URL: https://github.com/moeru-ai/xsai
- Owner: moeru-ai
- License: mit
- Created: 2024-11-21T05:25:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-12-30T08:41:24.000Z (6 months ago)
- Last Synced: 2026-01-01T02:53:39.424Z (6 months ago)
- Topics: ai, ollama, openai, typescript, xsai
- Language: TypeScript
- Homepage: https://xsai.js.org
- Size: 2.18 MB
- Stars: 546
- Watchers: 3
- Forks: 43
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- awesome - moeru-ai/xsai - 🤖💬 extra-small AI SDK. (TypeScript)
- awesome-ChatGPT-repositories - xsai - 🤖💬 extra-small AI SDK. (Openai)
README
# xsAI
extra-small AI SDK.
[](https://npmjs.com/package/xsai)
[](https://npm.chart.dev/xsai)
[](https://bundlephobia.com/package/xsai)
[](https://packagephobia.com/result?p=xsai)
[](https://github.com/moeru-ai/xsai/blob/main/LICENSE)
xsAI is a series of utils to help you use OpenAI or OpenAI-compatible API.
```ts
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](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), ESM Only, adding additional dependencies only when absolutely necessary.
How xsAI small? you can try install it with [Packagephobia](https://packagephobia.com/result?p=xsai) or [Bundlephobia](https://bundlephobia.com/package/xsai):
> In the following table, we used packagephobia's install size and bundlephobia's minified/gzipped size.
| Package | Install size | Bundled size | Gzipped size |
|---|---|---|---|
| xsai@0.4.0 | 142KB | 22.7KB | 7.1KB |
| ai@6.0.11 | 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/generate-text@0.4.0` 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`.
```sh
# 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](https://xsai.js.org/docs) to get started.
### Examples
###### Generating Text [(see above)](#xsai)
###### Streaming Text
```ts
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
```ts
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
- [moeru-ai/airi](https://github.com/moeru-ai/airi)
- [moeru-ai/arpk](https://github.com/moeru-ai/arpk)
- [lingticio/neuri-js](https://github.com/lingticio/neuri-js)
- [GramSearch/telegram-search](https://github.com/GramSearch/telegram-search)
- [yusixian/moe-copy-ai](https://github.com/yusixian/moe-copy-ai)
- [LemonNekoGH/flow-chat](https://github.com/LemonNekoGH/flow-chat)
## License
[MIT](LICENSE.md)
## Sponsors
