Skip to content

创建自定义角色

接口说明

基于封面图(coverId)和角色属性创建自定义角色。系统会调用 AI 自动生成角色的性格特征、回复风格和开场白,并将角色保存到账号中,后续可通过角色列表接口查询。

创建成功后,即可使用返回的 agentId 发起会话(与官方角色完全相同的流程)。

WARNING

coverId 有效期仅 30 分钟,请在封面图生成后尽快调用此接口完成创建。

基本信息

项目说明
接口路径POST /v1/agent/create
响应类型application/json
认证方式API Key + 请求签名(认证说明

请求体

json
{
  "coverId": "3f6a9b2e-1c4d-4e8f-a7b2-9d0e1f2a3b4c",
  "name": "Mia",
  "voiceId": "female_voice_01",
  "language": "en",
  "gender": "female",
  "age": "18-25",
  "ethnicity": "Asian",
  "bodyType": "Slim",
  "breastSize": "Medium",
  "buttSize": "Medium",
  "hairColor": "black",
  "hairStyle": "Long",
  "personality": "Flirty",
  "occupation": "Photographer",
  "relationship": "Girlfriend",
  "fetish": "Dominant",
  "refImageId": "550e8400-e29b-41d4-a716-446655440000"
}
字段类型必填说明
coverIdstring封面生成接口返回的 coverId,30 分钟有效
namestring角色名称
voiceIdstring语音 ID,来自素材选项 Voice.code
languagestring语言代码:en / zh / ja
genderstringmalefemale
agestring来自素材选项 Age.code
ethnicitystring来自素材选项 Ethnicity.code
bodyTypestring来自素材选项 BodyType.code
breastSizestringfemale 必填来自素材选项 BreastSize.code
buttSizestringfemale 必填来自素材选项 ButtSize.code
hairColorstring来自素材选项 HairColor.code
hairStylestring来自素材选项 HairStyle.code
personalitystring性格特征,来自素材选项 Personality.code
occupationstring职业,来自素材选项 Occupation.code
relationshipstring关系定位,来自素材选项 Relationship.code
fetishstring个性化偏好,来自素材选项 Fetish.code
refImageIdstring参考图 ID,可选

响应格式

成功响应

json
{
  "code": 10200,
  "success": true,
  "message": "OK",
  "data": {
    "agentId": "a36b6b38-e94d-ad19-c170-ad8de3a1a018",
    "name": "Mia",
    "coverUrl": "https://cdn.example.com/saas/custom-agent/cover/uuid.png"
  },
  "traceId": "xEo3sQwB6KRuwFfG2BxNWwlLQhKrvg38",
  "timestamp": "1773298769366"
}
字段类型说明
data.agentIdstring角色 UUID,后续所有接口使用此 ID
data.namestring角色名称
data.coverUrlstring封面图公开访问 URL

错误响应

HTTP 状态码说明
401认证失败
400参数缺失或格式错误
500Dify 服务错误
message说明
coverId is expired or invalid, please regenerate covercoverId 已过期(超过 30 分钟)或无效
breastSize is required for female女性角色未传 breastSize

完整调用流程

javascript
// 第一步:查询素材选项(页面初始化时缓存)
const options = await fetchMaterialOptions(apiKey, apiSecret, userId, {
  namespace: 'custom_agent',
  gender: 'female',
  language: 'en',
})

// 第二步(可选):上传参考图
const refImageId = await uploadRefImage(apiKey, apiSecret, userId, imageFile)

// 第三步(可选):人脸检测
const hasFace = await detectFace(apiKey, apiSecret, userId, imageFile)
if (!hasFace) {
  console.warn('未检测到人脸,可继续但效果可能较差')
}

// 第四步:生成封面图(需要 30~300s)
const { coverId, imageData } = await generateAgentCover(apiKey, apiSecret, userId, {
  gender: 'female',
  age: options.Age[0].code,
  ethnicity: options.Ethnicity[0].code,
  bodyType: options.BodyType[0].code,
  breastSize: options.BreastSize[0].code,
  buttSize: options.ButtSize[0].code,
  hairColor: options.HairColor[0].code,
  hairStyle: options.HairStyle[0].code,
  refImageId,
})

// 展示 imageData 给用户确认封面效果

// 第五步:创建角色
const { agentId } = await createAgent(apiKey, apiSecret, userId, {
  coverId,
  name: 'Mia',
  voiceId: options.Voice[0].code,
  language: 'en',
  gender: 'female',
  age: options.Age[0].code,
  ethnicity: options.Ethnicity[0].code,
  bodyType: options.BodyType[0].code,
  breastSize: options.BreastSize[0].code,
  buttSize: options.ButtSize[0].code,
  hairColor: options.HairColor[0].code,
  hairStyle: options.HairStyle[0].code,
  personality: options.Personality[0].code,
  refImageId,
})

// 第六步:发起对话(与官方角色完全相同)
const { conversationId } = await createConversation(apiKey, apiSecret, userId, agentId)

查询已创建的自定义角色

javascript
// 使用 /v1/agent/list 并传入 type: "custom"
const response = await fetch('/v1/agent/list', {
  method: 'POST',
  headers: await buildAuthHeaders(apiKey, apiSecret, userId, {
    pageNumber: 1, pageSize: 20, language: 'en', type: 'custom'
  }),
  body: JSON.stringify({ pageNumber: 1, pageSize: 20, language: 'en', type: 'custom' }),
})
// 返回当前 API Key 创建的所有自定义角色

Released under the MIT License.