|
1 | 1 | # Microsoft.Extensions.AI Integration |
2 | 2 |
|
3 | | -ModerationAPI provides `AIFunction` tools that can be used with any `IChatClient` for function/tool calling scenarios. This enables AI agents to moderate content as part of their conversation flow. |
| 3 | +!!! tip "Cross-SDK comparison" |
| 4 | + See the [centralized MEAI documentation](https://tryagi.github.io/docs/meai/) for feature matrices and comparisons across all tryAGI SDKs. |
| 5 | + |
| 6 | +The ModerationAPI SDK provides `AIFunction` tool wrappers compatible with [Microsoft.Extensions.AI](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai). These tools can be used with any `IChatClient` to give AI models access to multi-modal content moderation for text and images, review queue statistics, and moderation action management. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +dotnet add package ModerationAPI |
| 12 | +``` |
4 | 13 |
|
5 | 14 | ## Available Tools |
6 | 15 |
|
7 | | -| Tool | Description | |
8 | | -|------|-------------| |
9 | | -| `AsModerateTextTool()` | Moderate text content for toxicity, profanity, PII, spam, violence, hate speech, self-harm, sexual content, and more | |
10 | | -| `AsModerateImageTool()` | Moderate an image by URL for NSFW content, violence, hate symbols, and more | |
11 | | -| `AsGetQueueStatsTool()` | Get detailed statistics about a moderation review queue | |
12 | | -| `AsListActionsTool()` | List all available moderation actions for the organization | |
| 16 | +| Method | Tool Name | Description | |
| 17 | +|--------|-----------|-------------| |
| 18 | +| `AsModerateTextTool()` | `ModerationAPI_ModerateText` | Moderate text for toxicity, profanity, PII, spam, violence, hate speech, and more | |
| 19 | +| `AsModerateImageTool()` | `ModerationAPI_ModerateImage` | Moderate an image by URL for NSFW, violence, hate symbols, and more | |
| 20 | +| `AsGetQueueStatsTool()` | `ModerationAPI_GetQueueStats` | Get review queue statistics including review times and action counts | |
| 21 | +| `AsListActionsTool()` | `ModerationAPI_ListActions` | List all available moderation actions (ban, mute, warn, etc.) | |
13 | 22 |
|
14 | 23 | ## Usage |
15 | 24 |
|
16 | 25 | ```csharp |
17 | 26 | using ModerationAPI; |
18 | | - |
19 | | -var client = new ModerationAPIClient(apiKey); |
20 | | - |
21 | | -// Create tools |
22 | | -var moderateTextTool = client.AsModerateTextTool(); |
23 | | -var moderateImageTool = client.AsModerateImageTool(); |
24 | | -var getQueueStatsTool = client.AsGetQueueStatsTool(); |
25 | | -var listActionsTool = client.AsListActionsTool(); |
26 | | - |
27 | | -// Use with any IChatClient |
28 | | -var tools = new[] { moderateTextTool, moderateImageTool, getQueueStatsTool, listActionsTool }; |
| 27 | +using Microsoft.Extensions.AI; |
| 28 | + |
| 29 | +var moderationClient = new ModerationAPIClient( |
| 30 | + apiKey: Environment.GetEnvironmentVariable("MODERATIONAPI_API_KEY")!); |
| 31 | + |
| 32 | +var options = new ChatOptions |
| 33 | +{ |
| 34 | + Tools = |
| 35 | + [ |
| 36 | + moderationClient.AsModerateTextTool(), |
| 37 | + moderationClient.AsModerateImageTool(), |
| 38 | + moderationClient.AsGetQueueStatsTool(), |
| 39 | + moderationClient.AsListActionsTool(), |
| 40 | + ], |
| 41 | +}; |
| 42 | + |
| 43 | +IChatClient chatClient = /* your chat client */; |
| 44 | + |
| 45 | +var messages = new List<ChatMessage> |
| 46 | +{ |
| 47 | + new(ChatRole.User, "Check this text for policy violations: 'Hello, this is a test message.'"), |
| 48 | +}; |
| 49 | + |
| 50 | +while (true) |
| 51 | +{ |
| 52 | + var response = await chatClient.GetResponseAsync(messages, options); |
| 53 | + messages.AddRange(response.ToChatMessages()); |
| 54 | + |
| 55 | + if (response.FinishReason == ChatFinishReason.ToolCalls) |
| 56 | + { |
| 57 | + var results = await response.CallToolsAsync(options); |
| 58 | + messages.AddRange(results); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + Console.WriteLine(response.Text); |
| 63 | + break; |
| 64 | +} |
29 | 65 | ``` |
30 | | - |
31 | | -## Tool Details |
32 | | - |
33 | | -### AsModerateTextTool |
34 | | - |
35 | | -Submits text content for moderation analysis. Returns: |
36 | | -- Whether the content was flagged |
37 | | -- Flag probability and severity score |
38 | | -- Recommended action (allow, review, or reject) |
39 | | -- Reason codes for the recommendation |
40 | | - |
41 | | -### AsModerateImageTool |
42 | | - |
43 | | -Submits an image URL for moderation analysis. Returns the same evaluation structure as text moderation. |
44 | | - |
45 | | -### AsGetQueueStatsTool |
46 | | - |
47 | | -Retrieves statistics for a moderation review queue including review times, action counts, top reviewers, and trends. |
48 | | - |
49 | | -### AsListActionsTool |
50 | | - |
51 | | -Lists all configured moderation actions (e.g., ban, mute, warn, custom actions) for the authenticated organization. |
|
0 commit comments