Creating Custom Tool

Assistant Tool Usage Guide Part 2.

Extending AssistantTool

To leverage the full capabilities of AssistantTool in your OpenAI API interactions, you might need to override certain methods to tailor its functionality to your specific needs. This section will guide you through the process of extending AssistantTool, including how to override its methods and integrate it into your application with a sample code snippet.

Step 1: Creating a Derived Class

To start, create a new class that derives from AssistantToolwith your custom response class type created in Part 1. This class will implement the abstract methods and can override existing virtual methods to customize behavior.

using Glitch9.Apis.OpenAI;
using Cysharp.Threading.Tasks;

public class CustomAssistantTool : AssistantTool<MyCustomResponse>
{
    public CustomAssistantTool(ToolOptions toolOptions, RunOptions runOptions = null)
        : base(toolOptions, runOptions)
    {
    }
}

Step 2: Override Methods (Optional)

Overview of Overridable Methods

  1. OnInitializeAsync: Prepares the tool by setting up the assistant and thread. Override this method to add any pre-initialization logic or to change how the assistant and thread are configured.

  2. FindMatchingAssistant: This method is crucial for integrating your tool with OpenAI's infrastructure, ensuring your application can correctly identify and utilize an existing assistant or create a new one as needed.

  3. CreateFunction: This method in is designed for automatic FunctionObject creation using ToolPropertyAttribute. However, for scenarios requiring manual adjustments or specific configurations not covered by automatic generation, you can override this method. This allows for detailed customization of the function's parameters and behavior, ensuring your tool precisely meets your application's needs.

  4. Dispose: Cleans up resources and removes event listeners. Override for custom cleanup logic or to manage additional resources.

Sample Implementation

Here’s an example showing how to override some of these methods in a derived class to customize the tool's behavior:

using Cysharp.Threading.Tasks;
using Glitch9.Apis.OpenAI;
using System;
using System.Threading.Tasks;

public class CustomAssistantTool : AssistantTool<MyCustomResponse>
{
    public CustomAssistantTool(ToolOptions toolOptions, RunOptions runOptions = null)
        : base(toolOptions, runOptions)
    {
    }

    protected override async UniTask OnInitializeAsync()
    {
        // Add custom initialization logic here
        await base.OnInitializeAsync(); // Ensure to call base implementation if necessary
    }  

    protected override AssistantObject FindMatchingAssistant(AssistantObject[] assistants)
    {
        // Implement custom logic to select the appropriate assistant
        return base.FindMatchingAssistant(assistants);
    } 

    public override void Dispose()
    {
        // Custom cleanup logic here
        base.Dispose();
    }
}

Last updated