Skip to content

文本生成

使用 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);

参数说明

参数类型默认值描述
inputstring必填要续写的输入文本
modelTextModel'llama-3-erato-v1'使用的模型
maxLengthnumber40最大生成 token 数 (1-2048)
minLengthnumber1最小生成 token 数 (1-2048)
temperaturenumber1.0随机性 (0.1-100)

完整参数列表请参阅参数说明

模型

模型常量状态描述
llama-3-erato-v1ERATO✅ 推荐最新模型,基于 Llama 3(默认)
kayra-v1KAYRA✅ 可用Kayra 模型
clio-v1CLIO⚠️ 旧版Clio 模型(计划弃用)

注意: 根据官方 API 文档,目前推荐使用 llama-3-erato-v1kayra-v1clio-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);

基于 MIT 许可证发布