Azure AI 콘텐츠 보안 클라이언트 라이브러리 for JavaScript - 버전 1.0.0-beta.1

이 패키지에는 유해 콘텐츠 분석, 에이전트 제어 명세 정책 평가, 콘텐츠 출처 탐지, 텍스트 차단 목록 관리를 위한 동형 SDK가 포함되어 있습니다(Node.js와 브라우저 모두에서 실행됩니다).

키 링크:

이 패키지는 아직 NPM에 공개되지 않았습니다. npm 패키지 페이지와 호스팅된 API 참조는 첫 번째 릴리스 이후에 제공될 예정입니다.

시작하기

현재 지원되는 환경

자세한 내용은 지원 정책 참조하세요.

사전 요구 사항

@azure/ai-content-safety 패키지 설치

첫 번째 베타 릴리스 이후에는 자바스크립트용 Azure AI 콘텐츠 보안 클라이언트 라이브러리를 다음과 같이 npm설치하세요:

npm install @azure/ai-content-safety@next

클라이언트 생성 및 인증

클라이언트를 생성하려면 Azure AI 콘텐츠 보안 리소스와 자격 증명이 필요합니다endpoint. 엔드포인트는 Azure Portal에서 찾을 수 있습니다.

Microsoft Entra ID로 @azure/identity 라이브러리의 자격 증명이나 기존 Microsoft Entra 토큰을 사용해 인증할 수 있습니다.

아래에 표시된 DefaultAzureCredential 공급자 또는 Azure SDK와 함께 제공되는 다른 자격 증명 공급자를 사용하려면 @azure/identity 패키지를 설치하세요.

npm install @azure/identity

또한 새로운 Microsoft Entra 애플리케이션을 등록하고, 서비스 주체에게 적절한 역할을 할당하여 Azure AI 콘텐츠 보안에 접근권을 부여해야 합니다(참고: 역할은 "Owner" 필요한 권한을 부여하지 않습니다).

Microsoft Entra 애플리케이션 생성 방법에 대한 자세한 내용은 이 가이드를 참고하세요.

Node.js 및 노드와 유사한 환경을 사용하여 DefaultAzureCredential 클래스를 사용하여 클라이언트를 인증할 수 있습니다.

import { DefaultAzureCredential } from "@azure/identity";
import {
  ContentSafetyClient,
  ContentProvenanceClient,
  BlocklistClient,
} from "@azure/ai-content-safety";

const credential = new DefaultAzureCredential();
const contentSafetyClient = new ContentSafetyClient("<endpoint>", credential);
const contentProvenanceClient = new ContentProvenanceClient("<endpoint>", credential);
const blocklistClient = new BlocklistClient("<endpoint>", credential);

브라우저 환경의 경우 InteractiveBrowserCredential 패키지의 @azure/identity 사용하여 인증합니다.

import { InteractiveBrowserCredential } from "@azure/identity";
import { ContentSafetyClient } from "@azure/ai-content-safety";

const credential = new InteractiveBrowserCredential({
  tenantId: "<YOUR_TENANT_ID>",
  clientId: "<YOUR_CLIENT_ID>",
});
const client = new ContentSafetyClient("<endpoint>", credential);

JavaScript 번들

브라우저에서 이 클라이언트 라이브러리를 사용하려면 먼저 번들러를 사용해야 합니다. 이 작업을 수행하는 방법에 대한 자세한 내용은 번들링 설명서참조하세요.

주요 컨셉

ContentSafetyClient

ContentSafetyClient 텍스트와 이미지를 분석하고, 보호된 자료와 프롬프트 인젝션을 감지하며, 에이전트 제어 명세 정책에 따라 콘텐츠를 평가합니다.

import { ContentSafetyClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentSafetyClient("<endpoint>", new DefaultAzureCredential());
async function moderateInput(): Promise<void> {
  const result = await client.unifiedModerate({
    policyId: "<policy-id>",
    source: "input",
    content: "Text to evaluate",
  });
  console.log(result.verdict);
}
void moderateInput;

콘텐츠출처클라이언트

ContentProvenanceClientMicrosoft에서 발행한 C2PA 및 워터마크 신호에 대한 장기 탐지 작업을 시작하고 모니터링합니다.

import { ContentProvenanceClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentProvenanceClient("<endpoint>", new DefaultAzureCredential());
async function detectProvenance(): Promise<void> {
  const poller = client.detect({ content: { uri: "<media-blob-uri>" } });
  const result = await poller.pollUntilDone();
  console.log(result.outcome);
}
void detectProvenance;

직렬화된 폴러 상태는 다른 프로세스에서 복원할 수 있습니다:

import { ContentProvenanceClient, restorePoller } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentProvenanceClient("<endpoint>", new DefaultAzureCredential());
async function resumeDetection(serializedState: string): Promise<void> {
  const poller = restorePoller(client, serializedState, client.detect.bind(client));
  const result = await poller.pollUntilDone();
  console.log(result.outcome);
}
void resumeDetection;

블록리스트클라이언트

BlocklistClient 텍스트 차단 목록과 그 항목들을 생성, 업데이트, 목록 및 삭제합니다.

import { BlocklistClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new BlocklistClient("<endpoint>", new DefaultAzureCredential());
async function updateBlocklist(): Promise<void> {
  const result = await client.createOrUpdateTextBlocklist("<blocklist-name>", {
    description: "Blocklist description",
  });
  console.log(result.blocklistName);
}
void updateBlocklist;

블록리스트 항목을 나열할 때 최대 페이지 크기를 요청할 수 있는 방법 byPage({ maxPageSize }) :

import { BlocklistClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new BlocklistClient("<endpoint>", new DefaultAzureCredential());
async function listBlocklistItemPages(): Promise<void> {
  const pages = client.listTextBlocklistItems("<blocklist-name>").byPage({ maxPageSize: 20 });
  for await (const page of pages) {
    for (const item of page) {
      console.log(item.blocklistItemId, item.text);
    }
  }
}
void listBlocklistItemPages;

Troubleshooting

Logging

로깅을 사용하도록 설정하면 오류에 대한 유용한 정보를 파악하는 데 도움이 될 수 있습니다. HTTP 요청 및 응답 로그를 보려면 AZURE_LOG_LEVEL 환경 변수를 info설정합니다. 또는 setLogLevel@azure/logger 호출하여 런타임에 로깅을 사용하도록 설정할 수 있습니다.

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

로그를 사용하도록 설정하는 방법에 대한 자세한 지침은 @azure/로거 패키지 문서를 참조하세요.

Contributing

이 라이브러리에 기여하려면 기여 가이드 읽어 코드를 빌드하고 테스트하는 방법에 대해 자세히 알아보세요.

  • JavaScript용 Microsoft Azure SDK