https://github.com/moeru-ai/xsai
🤖💬 extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.
https://github.com/moeru-ai/xsai
ai ollama openai typescript xsai
Last synced: about 1 month ago
JSON representation
🤖💬 extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.
- Host: GitHub
- URL: https://github.com/moeru-ai/xsai
- Owner: moeru-ai
- License: mit
- Created: 2024-11-21T05:25:02.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-03-31T06:35:19.000Z (about 1 month ago)
- Last Synced: 2025-04-01T09:19:28.140Z (about 1 month ago)
- Topics: ai, ollama, openai, typescript, xsai
- Language: TypeScript
- Homepage: https://xsai.js.org
- Size: 1.71 MB
- Stars: 271
- Watchers: 2
- Forks: 16
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome - moeru-ai/xsai - 🤖💬 extra-small AI SDK. (TypeScript)
- awesome - moeru-ai/xsai - 🤖💬 extra-small AI SDK. (TypeScript)
README
# xsAI
extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.
[](https://npmjs.com/package/xsai)
[](https://npm.chart.dev/xsai)
[](https://bundlephobia.com/package/xsai)
[](https://github.com/moeru-ai/xsai/blob/main/LICENSE.md)xsAI is a series of utils to help you use OpenAI or OpenAI-compatible API.
```ts
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](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 pkg-size.dev](https://pkg-size.dev/xsai):
`[email protected]` is 93KB install size and 16KB bundle size (5.8KB 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 19KB install size and 2.9KB bundle size (1.4KB gzipped). ([try install it with pkg-size.dev](https://pkg-size.dev/@xsai/generate-text))
### 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 install xsai# bun
bun install xsai# deno
deno install xsai
```### Getting Started
Read the [documentation](https://xsai.js.org/docs) to get started.
### Examples
###### Generating Text [(see above)](#xsai)
###### Streaming Text
```ts
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
```ts
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
- [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)### Status
xsAI is currently in an early stage of development and may introduce breaking changes at any time.
## License
[MIT](LICENSE.md)
Moeru AI / xsAI is not affiliated with OpenAI.