@aki-io/aki-io

中文文档
1.0.1发布于 2026/04/06MITTypeScriptES Module零依赖近 30 天下载 1.7万

Official TypeScript/Node.js SDK for the AKI.IO AI Model Hub API

原始使用文档
文档目录21 节
  1. Installation
  2. Quick Start
  3. Configuration
  4. Usage
  5. Simple Request
  6. Chat Context
  7. Progress Callbacks
  8. Async Generator (Streaming)
  9. Convenience Function
  10. Validate API Key
  11. Get Available Endpoints
  12. Get Endpoint Details
  13. Binary Data Handling
  14. Error Handling
  15. API Reference
  16. AkiClient
  17. Utility Functions
  18. Types
  19. Requirements
  20. License
  21. Support

AKI.IO TypeScript/Node.js SDK

A modern TypeScript/Node.js SDK for the AKI.IO AI Model Hub API.

Installation

npm install @aki-io/aki-io
yarn add @aki-io/aki-io
pnpm add @aki-io/aki-io

Quick Start

import { AkiClient } from '@aki-io/aki-io'

const client = new AkiClient({
  endpointName: 'llama3_chat',
  apiKey: 'your-api-key-here'
})

const result = await client.doApiRequest({
  prompt_input: 'Hello, how are you?'
})

console.log(result.text)

Configuration

const client = new AkiClient({
  endpointName: 'llama3_chat',        // Required: API endpoint name
  apiKey: 'your-api-key-here',        // Required: Your API key
  apiServer: 'https://aki.io',        // Optional: API server URL (default: 'https://aki.io')
  outputBinaryFormat: 'base64',       // Optional: 'base64' or 'byteString' (default: 'base64')
  raiseExceptions: false,             // Optional: Throw exceptions on errors (default: false)
  progressInterval: 0.2,              // Optional: Progress poll interval in seconds (default: 0.2)
  returnToolCallDict: false           // Optional: Parse tool calls as JSON (default: false)
})

Usage

Simple Request

import { AkiClient } from '@aki-io/aki-io'

const client = new AkiClient({
  endpointName: 'llama3_chat',
  apiKey: process.env.AKI_API_KEY!
})

const result = await client.doApiRequest({
  prompt_input: 'What is the capital of France?'
})

if (result.success) {
  console.log(result.text)
} else {
  console.error('Error:', result.error)
}

Chat Context

const result = await client.doApiRequest({
  chat_context: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is 2+2?' },
    { role: 'assistant', content: '2+2 equals 4.' },
    { role: 'user', content: 'And what is 3+3?' }
  ]
})

console.log(result.text)

Progress Callbacks

const result = await client.doApiRequest(
  {
    prompt_input: 'Write a long story about a robot.'
  },
  (progressInfo, progressData) => {
    console.log(`Progress: ${progressInfo.progress}%`)
    console.log(`Queue Position: ${progressInfo.queue_position}`)
    
    if (progressData?.text) {
      console.log('Partial text:', progressData.text)
    }
  }
)

console.log('Final result:', result.text)

Async Generator (Streaming)

for await (const update of client.getApiRequestGenerator({ 
  prompt_input: 'Tell me a joke' 
})) {
  if (update.job_state === 'started') {
    console.log('Job started:', update.job_id)
  } else if (update.job_state === 'processing') {
    console.log('Progress:', update.progress_data?.text)
  } else if (update.job_state === 'done') {
    console.log('Final result:', update.result_data?.text)
  }
}

Convenience Function

import { doAkiRequest } from '@aki-io/aki-io'

const result = await doAkiRequest(
  'llama3_chat',
  'your-api-key',
  { prompt_input: 'Hello!' }
)

console.log(result.text)

Validate API Key

const validation = await client.initApiKey()

if (validation.success) {
  console.log('API key is valid')
} else {
  console.error('Invalid API key:', validation.error)
}

Get Available Endpoints

const endpoints = await client.getEndpointList()

console.log('Available endpoints:', endpoints)

Get Endpoint Details

const details = await client.getEndpointDetails('llama3_chat')

console.log('Endpoint details:', details)

Binary Data Handling

import { encodeBinary, decodeBinary } from '@aki-io/aki-io'

const imageBuffer = await fs.readFile('image.png')

const encoded = encodeBinary(imageBuffer, 'png')
console.log(encoded)
// data:image/png;base64,iVBORw0KGgo...

const [format, decoded] = decodeBinary(encoded)
console.log(format)  // 'png'
console.log(decoded) // Buffer containing the image data

Error Handling

import { AkiClient, AkiError, ConnectionError, ValidationError } from '@aki-io/aki-io'

const client = new AkiClient({
  endpointName: 'llama3_chat',
  apiKey: 'your-api-key',
  raiseExceptions: true
})

try {
  const result = await client.doApiRequest({ prompt_input: 'Hello' })
  console.log(result.text)
} catch (error) {
  if (error instanceof ValidationError) {
    console.error('Invalid request:', error.message)
  } else if (error instanceof ConnectionError) {
    console.error('Connection failed:', error.message)
  } else if (error instanceof AkiError) {
    console.error('API error:', error.message)
  }
}

API Reference

AkiClient

Constructor

new AkiClient(config: AkiClientConfig)

Methods

  • doApiRequest(params, progressCallback?) - Make an API request
  • getApiRequestGenerator(params) - Get async generator for streaming
  • initApiKey(apiKey?) - Validate API key
  • getEndpointList() - Get list of available endpoints
  • getEndpointDetails(endpointName?) - Get endpoint details
  • cancelRequest(jobId?) - Cancel a running request
  • setApiKey(apiKey) - Set new API key
  • getVersion() - Get SDK version (static)

Utility Functions

  • encodeBinary(data, format?, type?) - Encode binary data to base64
  • decodeBinary(base64String) - Decode base64 to binary
  • isValidBase64(string) - Check if string is valid base64
  • isValidJson(string) - Check if string is valid JSON

Types

interface AkiClientConfig {
  endpointName: string
  apiKey: string
  apiServer?: string
  outputBinaryFormat?: 'base64' | 'byteString'
  raiseExceptions?: boolean
  progressInterval?: number
  returnToolCallDict?: boolean
}

interface ApiResponse {
  success: boolean
  job_id?: string
  text?: string
  images?: string | string[]
  error?: string
  error_code?: number
  // ... additional fields
}

interface ProgressInfo {
  job_id: string
  progress: number
  queue_position: number
  estimate: number
  job_state?: JobState
  success?: boolean
}

interface ProgressData {
  text?: string
  images?: string | string[]
  num_generated_tokens?: number
  // ... additional fields
}

type ProgressCallback = (progressInfo: ProgressInfo, progressData: ProgressData | null) => void
type JobState = 'started' | 'processing' | 'done' | 'canceled' | 'lapsed'

Requirements

  • Node.js 18.0.0 or higher
  • TypeScript 5.0 or higher (for development)

License

MIT License - see LICENSE file for details.

Support

版本与发布记录

@aki-io/aki-io 共发布 2 个版本,首次发布于 2026/04/06;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.1latest2026/04/067 个月前
1.0.02026/04/067 个月前

关于 @aki-io/aki-io 的常见问题

@aki-io/aki-io 是什么?

Official TypeScript/Node.js SDK for the AKI.IO AI Model Hub API

如何安装 @aki-io/aki-io?

在项目目录运行 npm install @aki-io/aki-io,或使用 pnpm add / yarn add / bun add。

@aki-io/aki-io 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aki-io/aki-io 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aki-io/aki-io 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aki-io/aki-io 可以商用吗?

@aki-io/aki-io 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aki-io/aki-io 的下载量有多少?

根据 npm 官方下载统计,@aki-io/aki-io 在 2026/10/06 – 2026/11/04 这 30 天内被下载 17,035 次,日均约 568 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aki-io/aki-io 还在维护吗?

最新版本 1.0.1 发布于 2026/04/06(7 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/04/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。