文档

代理流

文本嵌入

分词

管理模型

模型信息

API 参考

使用聊天

SDK 方法,例如 model.respond()model.applyPromptTemplate()model.act() 都将聊天参数作为输入。在 SDK 中表示聊天有几种方式。

选项 1:消息数组

您可以使用消息数组来表示聊天。以下是使用 .respond() 方法的示例。

const prediction = model.respond([
  { role: "system", content: "You are a resident AI philosopher." },
  { role: "user", content: "What is the meaning of life?" },
]);

选项 2:输入单个字符串

如果您的聊天只有一个用户消息,您可以使用单个字符串来表示聊天。以下是使用 .respond 方法的示例。

const prediction = model.respond("What is the meaning of life?");

选项 3:使用 Chat 辅助类

对于更复杂的任务,建议使用 Chat 辅助类。它提供了各种常用的方法来管理聊天。以下是使用 Chat 类的示例。

const chat = Chat.empty();
chat.append("system", "You are a resident AI philosopher.");
chat.append("user", "What is the meaning of life?");

const prediction = model.respond(chat);

您还可以使用 Chat.from 方法快速构建一个 Chat 对象。

const chat = Chat.from([
  { role: "system", content: "You are a resident AI philosopher." },
  { role: "user", content: "What is the meaning of life?" },
]);