Set person information

更新时间:
复制 MD 格式

Use the Content Moderation .NET SDK to add a name and remarks to a custom person in your retrieval library. After you set this information, it appears in the results returned by custom retrieval queries.

Setting person information is optional. If you only need to search by face similarity and don't need metadata in the results, you can skip this step.

Prerequisites

Before you begin, ensure that you have:

  • Installed the .NET dependencies using the required version. See Install .NET dependencies. Using an unsupported version causes subsequent operation calls to fail.

  • A personId for the custom person to update. This is the ID returned when you added the person to your library.

Note: Use the AI Guardrails API endpoints when calling the SDK. For endpoint details, see Endpoints.

Set person information

The following example sets the name and note fields for a custom person. personId is required; name and note are optional fields that appear in custom retrieval results to help you identify or classify the person.

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load credentials from environment variables.
            // Store your AccessKey ID and secret in environment variables
            // rather than hardcoding them in source code.
            DefaultProfile profile = DefaultProfile.GetProfile(
                "cn-shanghai",
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

            // Reuse the client instance across requests to improve performance
            // and avoid repeated connection overhead.
            DefaultAcsClient client = new DefaultAcsClient(profile);

            SetPersonRequest request = new SetPersonRequest();
            request.AcceptFormat = FormatType.JSON;
            request.ContentType = FormatType.JSON;
            request.Method = MethodType.POST;
            request.Encoding = "UTF-8";

            Dictionary<string, object> httpBody = new Dictionary<string, object>();
            // Required: the ID of the custom person to update.
            httpBody.Add("personId", "<person-id>");
            // Optional: a display name for the person. Returned in retrieval results.
            httpBody.Add("name", "<person-name>");
            // Optional: free-text remarks for the person (e.g., department, role).
            // Returned in retrieval results alongside the person ID and name.
            httpBody.Add("note", "<remarks>");

            request.SetContent(
                System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
                "utf-8",
                FormatType.JSON);

            try
            {
                SetPersonResponse response = client.GetAcsResponse(request);

                if (response.HttpResponse.Status != 200)
                {
                    Console.WriteLine("Request failed. Status: {0}", response.HttpResponse.Status);
                }
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Request failed: {0}", ex.Message);
            }
        }
    }
}

Replace the following placeholders before running the code:

PlaceholderDescription
<person-id>The ID of the custom person to update
<person-name>A display name for the person
<remarks>Free-text notes, such as department or role

For the full list of request parameters, see API reference for setting person information.

What's next

After setting person information, run a custom retrieval query to confirm that the name and note fields appear in the returned results.