@302ai/ai-sdk
v0.2.15
Published
The **[302AI provider](https://sdk.vercel.ai/providers/ai-sdk-providers/)** for the [AI SDK](https://sdk.vercel.ai/docs) contains image model support for the [302AI](https://302.ai) platform.
Downloads
1,174
Readme
AI SDK - 302AI Provider
The 302AI provider for the AI SDK contains image model support for the 302AI platform.
Setup
The 302AI provider is available in the @302ai/ai-sdk module. You can install it with
npm i @302ai/ai-sdkProvider Instance
You can import the default provider instance ai302 from @302ai/ai-sdk:
import { ai302 } from '@302ai/ai-sdk';Language Model Example
import { ai302 } from '@302ai/ai-sdk';
import { generateText } from 'ai';
const { text } = await generateText({
model: ai302('gpt-4o'),
prompt: 'Write a JavaScript function that sorts a list:',
});Language Models
Check out the 302AI API docs for more information.
Embedding Model Example
import { ai302 } from '@302ai/ai-sdk';
import { embed } from 'ai';
const { embedding, usage } = await embed({
model: ai302.textEmbeddingModel('BAAI/bge-large-en-v1.5'),
value: 'sunny day at the beach',
});Embedding Models
Check out the 302AI API docs for more information.
text-embedding-3-smalltext-embedding-3-largetext-embedding-ada-002zhipu-embedding-2BAAI/bge-large-en-v1.5BAAI/bge-large-zh-v1.5BAAI/bge-m3Baichuan-Text-Embeddingbce-embedding-base_v1jina-clip-v1jina-clip-v2jina-embeddings-v2-base-enjina-embeddings-v2-base-esjina-embeddings-v2-base-dejina-embeddings-v2-base-zhjina-embeddings-v2-base-codejina-embeddings-v3
Image Model Examples
import { ai302 } from '@302ai/ai-sdk';
import { experimental_generateImage as generateImage } from 'ai';
import fs from 'fs';
const { image } = await generateImage({
model: ai302.image('midjourney/6.1'),
prompt: 'A serene mountain landscape at sunset',
});
const filename = `image-${Date.now()}.png`;
fs.writeFileSync(filename, image.uint8Array);
console.log(`Image saved to ${filename}`);Image Models
Check out the 302AI API docs for more information.
Speech (TTS) Model Example
import { ai302 } from '@302ai/ai-sdk';
import { generateSpeech } from 'ai';
import fs from 'fs';
const { audio } = await generateSpeech({
model: ai302.speech('openai/alloy'),
text: 'Hello, welcome to 302AI!',
});
fs.writeFileSync('speech.mp3', audio.uint8Array);Advanced Usage with Options
// With speed and output format
const { audio } = await generateSpeech({
model: ai302.speech('openai/nova'),
text: 'Hello world!',
speed: 1.2,
providerOptions: {
ai302: {
volume: 0.8,
outputFormat: 'mp3',
},
},
});
// Azure TTS with emotion
const { audio } = await generateSpeech({
model: ai302.speech('azure/zh-CN-XiaoxiaoNeural'),
text: '你好,欢迎使用302AI!',
providerOptions: {
ai302: {
emotion: 'cheerful',
},
},
});
// Async mode for long text
const { audio } = await generateSpeech({
model: ai302.speech('elevenlabs/Rachel'),
text: longText,
providerOptions: {
ai302: {
runAsync: true,
pollInterval: 3000,
timeout: 300,
},
},
});Speech Providers
302AI supports multiple TTS providers through a unified API. The model ID format is provider/voice:
| Provider | Example Model ID | Description |
|----------|------------------|-------------|
| openai | openai/alloy, openai/nova, openai/shimmer | OpenAI TTS voices |
| azure | azure/zh-CN-XiaoxiaoNeural, azure/en-US-JennyNeural | Azure Cognitive Services |
| elevenlabs | elevenlabs/Rachel, elevenlabs/Adam | ElevenLabs voices |
| doubao | doubao/zh_female_qingxin | ByteDance Doubao TTS |
| fish | fish/zh-CN-XiaoxiaoNeural | Fish Audio TTS |
| minimaxi | minimaxi/male-qn-qingse | MiniMax TTS |
| google | google/en-US-Wavenet-A | Google Cloud TTS |
| qwen | qwen/Cherry | Alibaba Qwen TTS |
| meruka | meruka/default | Meruka TTS |
| dubbingx | dubbingx/en-US-male | DubbingX TTS |
Speech Options
| Option | Type | Description |
|--------|------|-------------|
| speed | number | Speech speed (0.25-4.0, default: 1.0) |
| volume | number | Volume level (0-2, default: 1.0) |
| emotion | string | Emotion style (provider-specific) |
| outputFormat | string | Output format: mp3, wav, ogg, etc. |
| runAsync | boolean | Enable async mode for long text |
| webhook | string | Webhook URL for async notifications |
| timeout | number | Request timeout in seconds (default: 180) |
| pollInterval | number | Poll interval in ms for async mode (default: 2000) |
| maxPollAttempts | number | Max poll attempts for async mode (default: 90) |
Check out the 302AI TTS API docs for more information.
Reranking Model Example
Reranking improves search relevance by reordering documents based on their relevance to a query.
import { ai302 } from '@302ai/ai-sdk';
import { rerank } from 'ai';
const documents = [
'Organic skincare for sensitive skin with aloe vera',
'New makeup trends focus on bold colors',
'Bio-Hautpflege für empfindliche Haut mit Aloe Vera',
];
const { ranking, rerankedDocuments } = await rerank({
model: ai302.reranking('jina-reranker-v2-base-multilingual'),
documents,
query: 'skincare products for sensitive skin',
topN: 2,
});
console.log(ranking);
// [
// { index: 0, relevanceScore: 0.95 },
// { index: 2, relevanceScore: 0.78 }
// ]Reranking with Object Documents
const documents = [
{ title: 'Skincare Guide', content: 'Organic products for sensitive skin...' },
{ title: 'Makeup Trends', content: 'Bold colors and techniques...' },
];
const { ranking } = await rerank({
model: ai302.reranking('bge-reranker-v2-m3'),
documents,
query: 'sensitive skin care',
topN: 1,
});Reranking Models
| Provider | Model ID | Description |
|----------|----------|-------------|
| Jina | jina-reranker-v2-base-multilingual | Multilingual reranker |
| Jina | jina-reranker-v1-base-en | English reranker |
| Jina | jina-reranker-v1-turbo-en | Fast English reranker |
| Jina | jina-colbert-v1-en | ColBERT-based reranker |
| BAAI | bge-reranker-v2-m3 | BGE multilingual reranker |
| BCE | bce-reranker-base_v1 | BCE reranker |
| Qwen | Qwen/Qwen3-Reranker-8B | Qwen 8B reranker |
| Qwen | Qwen/Qwen3-Reranker-4B | Qwen 4B reranker |
| Qwen | Qwen/Qwen3-Reranker-0.6B | Qwen 0.6B reranker |
| Voyage | rerank-2.5 | Voyage reranker |
| Voyage | rerank-2.5-lite | Voyage lite reranker |
Check out the 302AI Rerank API docs for more information.
Documentation
Please check out the Vercel AI SDK for more information.
