-
Notifications
You must be signed in to change notification settings - Fork 13
1015822- Added UG documentation for AI Agent tools #2554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c9432e
1015822- Added Ug documentation for AI Agent tools
Viswajith-SF4658 613387d
Modified the addressed feedbacks for syntax changes and folder renami…
Viswajith-SF4658 a8fd35a
Merge branch 'development' of https://github.com/syncfusion-content/d…
Viswajith-SF4658 39eaadf
Modified the content to resolve CI failures
Viswajith-SF4658 74f652a
Merge branch 'development' of https://github.com/syncfusion-content/d…
Viswajith-SF4658 8f4aa01
Modified the content for feedback addressed and to resolve CI fails
Viswajith-SF4658 998f6ef
Feedback addressed
Viswajith-SF4658 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| --- | ||
| layout: post | ||
| title: Customization | AI Agent Tools | Syncfusion | ||
| description: Learn how to extend and customize the Syncfusion Document SDK Agent Tool library by creating custom agent tool classes and registering them with an AI agent. | ||
| platform: document-processing | ||
| control: AI Agent Tools | ||
| documentation: ug | ||
| --- | ||
|
|
||
| # Customize the Agent Tool Library | ||
|
|
||
| The Syncfusion Document SDK Agent Tool library is designed to be extensible. This guide walks you through creating a custom agent tool class and registering the tools with an AI agent so they are callable alongside the built-in tools. | ||
|
|
||
| --- | ||
|
|
||
| ## Creating a Custom Agent Tool Class | ||
|
|
||
| Follow these steps to expose new document operations to the AI agent. | ||
|
|
||
| **Step 1: Create a Custom Agent Tool by Inheriting AgentToolBase** | ||
|
|
||
| Create a new class that inherits from `AgentToolBase` (in the `Syncfusion.AI.AgentTools.Core` namespace) and accepts a document repository through its constructor: | ||
|
|
||
| ```csharp | ||
| using Syncfusion.AI.AgentTools.Core; | ||
| using Syncfusion.DocIO.DLS; | ||
|
|
||
| public class WordWatermarkAgentTools : AgentToolBase | ||
| { | ||
| private readonly WordDocumentRepository _repository; | ||
|
|
||
| public WordWatermarkAgentTools(WordDocumentRepository repository) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(repository); | ||
| _repository = repository; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Step 2: Add Tool Methods with [Tool]** | ||
|
|
||
| Add `public` instance methods and decorate each one with `[Tool]`, providing a name and a description that the AI agent will use to understand when to call it: | ||
|
|
||
| ```csharp | ||
| [Tool( | ||
| Name = "AddTextWatermark", | ||
| Description = "Adds a text watermark to the specified Word document.")] | ||
| public CallToolResult AddTextWatermark(...) | ||
| { | ||
| // implementation | ||
| } | ||
| ``` | ||
|
|
||
| **Step 3: Annotate Parameters with [ToolParameter]** | ||
|
|
||
| Decorate each method parameter with `[ToolParameter]` to give the AI a natural-language description of what value to pass: | ||
|
|
||
| ```csharp | ||
| public CallToolResult AddTextWatermark( | ||
| [ToolParameter(Description = "The document ID of the Word document.")] | ||
| string documentId, | ||
| [ToolParameter(Description = "The watermark text to display (e.g., 'DRAFT', 'CONFIDENTIAL').")] | ||
| string watermarkText, | ||
| [ToolParameter(Description = "Optional: the font size of the watermark. Defaults to 72.")] | ||
| float fontSize = 72f) | ||
| ``` | ||
|
|
||
| **Step 4: Return CallToolResult** | ||
|
|
||
| All tool methods must return `CallToolResult`. Use the static factory methods to signal success or failure: | ||
|
|
||
| ```csharp | ||
| // Success | ||
| return CallToolResult.Ok("Operation completed successfully."); | ||
|
|
||
| // Failure | ||
| return CallToolResult.Fail("Reason the operation failed."); | ||
| ``` | ||
|
|
||
| ### Example | ||
|
|
||
| ```csharp | ||
| using Syncfusion.AI.AgentTools.Core; | ||
| using Syncfusion.DocIO.DLS; | ||
|
|
||
| public class WordWatermarkAgentTools : AgentToolBase | ||
| { | ||
| private readonly WordDocumentRepository _repository; | ||
|
|
||
| public WordWatermarkAgentTools(WordDocumentRepository repository) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(repository); | ||
| _repository = repository; | ||
| } | ||
|
|
||
| [Tool( | ||
| Name = "AddTextWatermark", | ||
| Description = "Adds a text watermark to the specified Word document.")] | ||
| public CallToolResult AddTextWatermark( | ||
| [ToolParameter(Description = "The document ID of the Word document.")] | ||
| string documentId, | ||
| [ToolParameter(Description = "The watermark text to display (e.g., 'DRAFT', 'CONFIDENTIAL').")] | ||
| string watermarkText) | ||
| { | ||
| try | ||
| { | ||
| WordDocument? doc = _repository.GetDocument(documentId); | ||
| if (doc == null) | ||
| return CallToolResult.Fail($"Document not found: {documentId}"); | ||
|
|
||
| TextWatermark watermark = new TextWatermark(watermarkText, "", 250, 100); | ||
| watermark.Color = Syncfusion.Drawing.Color.LightGray; | ||
| watermark.Layout = WatermarkLayout.Diagonal; | ||
| doc.Watermark = watermark; | ||
|
|
||
| return CallToolResult.Ok( | ||
| $"Watermark '{watermarkText}' applied to document '{documentId}'."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return CallToolResult.Fail(ex.Message); | ||
| } | ||
| } | ||
|
|
||
| [Tool( | ||
| Name = "RemoveWatermark", | ||
| Description = "Removes the watermark from the specified Word document.")] | ||
| public CallToolResult RemoveWatermark( | ||
| [ToolParameter(Description = "The document ID of the Word document.")] | ||
| string documentId) | ||
| { | ||
| try | ||
| { | ||
| WordDocument? doc = _repository.GetDocument(documentId); | ||
| if (doc == null) | ||
| return CallToolResult.Fail($"Document not found: {documentId}"); | ||
|
|
||
| doc.Watermark = null; | ||
| return CallToolResult.Ok($"Watermark removed from document '{documentId}'."); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return CallToolResult.Fail(ex.Message); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Registering Custom Tools with the AI Agent | ||
|
|
||
| Once your custom tool class is created, register it alongside the built-in tools in your host application. | ||
|
|
||
| **Step 1: Instantiate the Custom Tool Class** | ||
|
|
||
| ```csharp | ||
| var wordRepo = new WordDocumentRepository(TimeSpan.FromMinutes(5)); | ||
|
|
||
| // Built-in tools | ||
| var wordDocTools = new WordDocumentAgentTools(wordRepo, outputDirectory); | ||
|
|
||
| // Your custom tool class | ||
| var wordWatermarkTools = new WordWatermarkAgentTools(wordRepo); | ||
| ``` | ||
|
|
||
| **Step 2: Collect All Tools** | ||
|
|
||
| ```csharp | ||
| var allSyncfusionTools = new List<Syncfusion.AI.AgentTools.Core.AITool>(); | ||
| allSyncfusionTools.AddRange(wordDocTools.GetTools()); | ||
| allSyncfusionTools.AddRange(wordWatermarkTools.GetTools()); // <-- custom tools | ||
| ``` | ||
|
|
||
| **Step 3: Convert to Microsoft.Extensions.AI Tools** | ||
|
|
||
| ```csharp | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| var msAiTools = allSyncfusionTools | ||
| .Select(t => AIFunctionFactory.Create(t.Method, t.Instance, new AIFunctionFactoryOptions | ||
| { | ||
| Name = t.Name, | ||
| Description = t.Description | ||
| })) | ||
| .Cast<AITool>() | ||
| .ToList(); | ||
| ``` | ||
|
|
||
| **Step 4: Build the Agent** | ||
|
|
||
| ```csharp | ||
| var agent = openAIClient.AsAIAgent( | ||
| model: openAIModel, | ||
| tools: msAiTools, | ||
| systemPrompt: "You are a helpful document-processing assistant."); | ||
| ``` | ||
|
|
||
| Your custom tool methods are now callable by the AI agent the same way as all built-in tools. | ||
|
|
||
| --- | ||
|
|
||
| ## Example Prompts | ||
|
|
||
| Once the custom watermark tools are registered, you can interact with the AI agent using natural language. The following examples show typical prompts and the tool calls the agent will make in response. | ||
|
|
||
| **Add a watermark:** | ||
|
|
||
| > *"Open the file at C:\Documents\report.docx and add a 'CONFIDENTIAL' watermark to it, then save it."* | ||
|
|
||
| The agent will call `Word_CreateDocument` to load the file, then `Word_AddTextWatermark` with `watermarkText = "CONFIDENTIAL"`, and finally `Word_ExportDocument` to save the result. | ||
|
|
||
| --- | ||
|
|
||
| ## Customizing the System Prompt | ||
|
|
||
| The system prompt shapes how the AI agent uses the tools. Tailor it to your use case: | ||
|
|
||
| ```csharp | ||
| string systemPrompt = """ | ||
| You are an expert document-processing assistant with access to tools for Word operations. | ||
| """; | ||
| ``` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Overview](./overview.md) | ||
| - [Tools Reference](./tools.md) | ||
| - [Getting Started](./getting-started.md) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.