Lingara Lingara 文档 学习指南 API 库 应用 构建 嵌入 网页版
语言: 中文简体

NPC 对话

本页译自英文。如两者有出入,以英文页面为准。 阅读英文页面

你的游戏中的角色可以用玩家正在学习的语言与其对话,难度贴合玩家的级别。玩家说的每一句话就是一个回合:玩家工具包发送这句话,角色的回复则分段流式返回。

一个回合携带什么

每个回合都会发送角色所需的全部内容,因为 Lingara 不会在回合之间保留任何对话:

  • npc:谁在说话。它的 name 为 1 到 32 个字符,可选的 persona 是一行描述,1 到 120 个字符,例如“一个卖饺子的小贩”。
  • scene:可选,对话发生的地方,1 到 160 个字符。
  • source_lang 和 target_lang:玩家自己的语言以及他们正在学习的语言。两者必须不同,角色只说 target_lang。
  • level:玩家的级别,从 1 到 9。Lingara 的指南《级别意味着什么》介绍了这套级别体系。
  • line:玩家刚刚说的话,1 到 500 个字符。
  • history:到目前为止的对话,最早的在前,最多 12 行。每一行都有一个 speaker(player 或 npc)以及它的 text,1 到 500 个字符。

每段文本都是单独一行。对话由你的游戏保存:玩家工具包为你打开的每段对话保存一个窗口,把每个完成的回合加入其中,把每条回复截取为前 500 个字符,并丢弃超过 12 行的最早内容。

Lingara 把人设和场景视为来自你的游戏的不可信文本。它们影响角色说话的方式,但绝不影响角色被允许做什么。

回复如何到达

回复以文本片段的形式流式传输,你的游戏在片段到达时依次追加。片段之间可能会出现通知,例如说明回复被截断的通知。回复完整时,回合以 done 结束;失败时以 error 结束。回合不可恢复:如果连接中断,玩家需要再说一遍这句话。

对每个角色一次只说一句话。在角色仍在回复时说出的话会被拒绝而不是排队,因为每个回合都要计费。

谁付费

只有按用量计费客户端的玩家才能进行对话,每个回合都向该客户端计费。玩家的令牌需要 embed:play。

代码示例语言

Bevy

fn spawn_seller(mut commands: Commands) {
    let mei = NpcDialogue::new("Mei", "zh", 2)
        .and_then(|npc| npc.with_persona("a dumpling seller"))
        .and_then(|npc| npc.with_scene("the night market"))
        .expect("within Lingara's bounds");
    commands.spawn(mei);
    commands.spawn(Player);
}

/// One turn at a time: a line said while the NPC is replying is a `Busy` error.
fn talk(keys: Res<ButtonInput<KeyCode>>, npcs: Query<Entity, With<NpcDialogue>>, mut say: MessageWriter<NpcSay>) {
    if keys.just_pressed(KeyCode::Digit1) {
        for entity in &npcs {
            say.write(NpcSay { entity, line: "饺子多少钱?".into() });
        }
    }
}

/// `streaming` grows as the reply arrives; `turns` holds the finished ones.
fn show_reply(transcripts: Query<&NpcTranscript, Changed<NpcTranscript>>) {
    for transcript in &transcripts {
        if let Some(partial) = &transcript.streaming {
            info!("…{partial}");
        }
    }
}

Unity

public DialogueId OpenDialogue(string npc, string sourceLang, string targetLang, byte level) =>
    Client.DialogueOpen(new DialogueConfig { Npc = new Npc { Name = npc }, SourceLang = sourceLang, TargetLang = targetLang, Level = level });

// The reply streams back as NpcTurn events: append each Fragment's text to your label.
public RequestId Say(DialogueId dialogue, string line) => Client.DialogueSay(dialogue, line);

Godot C#

public long OpenDialogue(string npc, string sourceLang, string targetLang, byte level) =>
    Client.DialogueOpen(new DialogueConfig { Npc = new Npc { Name = npc }, SourceLang = sourceLang, TargetLang = targetLang, Level = level }).Value;

// The reply streams back as NpcTurnFragment signals, then one NpcTurnFinished.
public long Say(long dialogue, string line) => Client.DialogueSay(new DialogueId(dialogue), line).Value;

GDScript

# A LingaraNpc child per character: npc_name, target_lang and level in the
# inspector. say() returns false while a turn is in flight (turns are never
# queued, because each one is billed).
func talk_to_mei(line: String) -> void:
	if mei.say(line):
		bubble.text = "…"


func _on_mei_replied(text: String) -> void:
	bubble.text = text

Unreal C++

void ALingaraEmbedSnippetActor::Say(const FString& Line)
{
	ELingaraCallResult Result;
	FLingaraEmbedError Error;
	if (Dialogue == 0)
	{
		FLingaraDialogueConfig Config;
		Config.Npc.Name = TEXT("Mei");
		Config.Npc.Persona = TEXT("a tea seller at the night market");
		Config.SourceLang = TEXT("en");
		Config.TargetLang = TEXT("zh");
		Config.Level = 2;
		Dialogue = Lingara()->DialogueOpen(Config, Result, Error);
	}
	Reply.Reset();
	Lingara()->DialogueSay(Dialogue, Line, Result, Error);
}

// The reply streams back as npc_turn events: append each fragment.
void ALingaraEmbedSnippetActor::HandleNpcTurn(const FLingaraNpcTurn& Turn)
{
	if (Turn.Dialogue == Dialogue && Turn.Update.Type == ELingaraNpcTurnUpdateType::Fragment)
	{
		Reply += Turn.Update.Text;
	}
}

Kotlin

fun talkToMei(line: String) {
    val dialogue = npc ?: lingara.dialogueOpen(DialogueConfig(Npc("Mei", "a tea seller"), "the tea house", "en", "zh", 2, emptyList()))
    npc = dialogue
    lingara.dialogueSay(dialogue, line)
}

private fun onNpcTurn(update: NpcTurnUpdate) {
    when (update) {
        is NpcTurnUpdate.Opened -> bubble = ""
        is NpcTurnUpdate.Fragment -> bubble += update.text
        is NpcTurnUpdate.Finished -> bubble = update.text
        else -> Unit
    }
}

Java

void talkToMei(String line) {
  if (npc < 0) {
    Npc mei = new Npc("Mei", "a tea seller");
    npc =
        lingara.dialogueOpen(new DialogueConfig(mei, "the tea house", "en", "zh", 2, List.of()));
  }
  lingara.dialogueSay(npc, line);
}

void onNpcTurn(EmbedEvent.NpcTurn turn) {
  if (turn.getUpdate() instanceof NpcTurnUpdate.Fragment fragment) {
    bubble.append(fragment.getText());
  } else if (turn.getUpdate() instanceof NpcTurnUpdate.Finished finished) {
    bubble.setLength(0);
    bubble.append(finished.getText());
  }
}

另请参阅