文本生成
使用 NovelAI 的语言模型生成文本续写。
基本用法
typescript
import { NovelAI } from 'novelai-sdk-unofficial';
const client = new NovelAI({ apiKey: 'your-api-key' });
const text = await client.text.generate({
input: 'Once upon a time, in a kingdom far away,',
model: 'llama-3-erato-v1',
maxLength: 80,
});
console.log(text);参数说明
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
input | string | 必填 | 要续写的输入文本 |
model | TextModel | 'llama-3-erato-v1' | 使用的模型 |
maxLength | number | 40 | 最大生成 token 数 (1-2048) |
minLength | number | 1 | 最小生成 token 数 (1-2048) |
temperature | number | 1.0 | 随机性 (0.1-100) |
完整参数列表请参阅参数说明。
模型
| 模型 | 常量 | 状态 | 描述 |
|---|---|---|---|
llama-3-erato-v1 | ERATO | ✅ 推荐 | 最新模型,基于 Llama 3(默认) |
kayra-v1 | KAYRA | ✅ 可用 | Kayra 模型 |
clio-v1 | CLIO | ⚠️ 旧版 | Clio 模型(计划弃用) |
注意: 根据官方 API 文档,目前推荐使用
llama-3-erato-v1和kayra-v1。clio-v1是旧模型,计划在未来弃用。试用用户只能使用kayra-v1。
typescript
import { NovelAI, ERATO, KAYRA, CLIO } from 'novelai-sdk-unofficial';
// 使用字符串
const text = await client.text.generate({
input: 'Hello',
model: 'llama-3-erato-v1',
});
// 使用常量
const text = await client.text.generate({
input: 'Hello',
model: ERATO,
});温度
控制生成的随机性:
0.1-1.0:更集中、确定性1.0-2.0:平衡2.0+:更有创意、多样化
typescript
// 更可预测
const text = await client.text.generate({
input: 'The answer is',
temperature: 0.5,
});
// 更有创意
const text = await client.text.generate({
input: 'The answer is',
temperature: 1.5,
});生成长度
typescript
const text = await client.text.generate({
input: 'Once upon a time',
minLength: 20, // 至少 20 个 token
maxLength: 100, // 最多 100 个 token
});停止序列
遇到特定文本时停止生成:
typescript
const text = await client.text.generate({
input: 'Character: Hello!\nNarrator:',
stopSequences: ['\nCharacter:', '\n\n'],
});说明:
stopSequences会在请求前进行 token 化,触发额外的 token-count 请求。
禁止序列
防止特定文本出现:
typescript
const text = await client.text.generate({
input: 'Write a story',
banSequences: ['violence', 'explicit'],
});说明:
banSequences同样会触发 token-count 请求。
示例:故事续写
typescript
const story = `The old lighthouse keeper climbed the spiral stairs,
his lantern casting dancing shadows on the stone walls.`;
const continuation = await client.text.generate({
input: story,
model: 'llama-3-erato-v1',
temperature: 1.0,
maxLength: 100,
});
console.log(story + continuation);示例:对话
typescript
const dialogue = `Alice: What do you think about the weather?
Bob:`;
const response = await client.text.generate({
input: dialogue,
stopSequences: ['\nAlice:', '\n\n'],
maxLength: 50,
});
console.log(dialogue + response);