Creating Custom Response

Assistant Tool Usage Guide Part 1.

Before creating your customized AssistantTool, it's essential to define how your application will handle the responses from OpenAI API calls. This involves creating a custom response class, MyCustomResponse, tailored to match the expected structure of the OpenAI API response for your specific use case.

Defining MyCustomResponse

To ensure your tool can effectively process and utilize the data returned by the OpenAI API, define the MyCustomResponse class with properties corresponding to the expected elements in the API's response. Note that currently, the ToolPropertyAttribute supports properties of type string and Enum, catering to the common data types returned by the API.

using System;
using UnityEngine;
using Glitch9.Apis.OpenAI;

public class MyCustomResponse
{
    // Example string property: Define properties according to the expected API response structure.
    // Use the ToolPropertyAttribute to specify details about the API response fields,
    // marking them as required or optional as per your application's needs.
    [ToolProperty("The main text response from the OpenAI API", required: true)]
    public string ResponseText { get; set; }
    
    // Example enum property: If your response includes fields that can be categorized into enums,
    // define an enum and use it as the type for such properties.
    [ToolProperty("Category or type of response, represented as an enum", required: false)]
    public ResponseCategory Category { get; set; }
}

// Example enum to represent a category or type of response.
public enum ResponseCategory
{
    Category1,
    Category2,
    Category3
}

Using ToolPropertyAttribute

The ToolPropertyAttribute is crucial for linking your class properties to the OpenAI API response fields. When defining your custom response class:

  • Apply the ToolPropertyAttribute to each property that corresponds to a data field in the API's response.

  • Utilize the Description parameter to provide a clear explanation of each property for future reference and clarity.

  • The Required flag within the attribute can be set to true for mandatory fields or false for optional ones, aiding in data validation and error handling.

Example #1. Chatbot Response

using Glitch9.Apis.OpenAI;
using Newtonsoft.Json;

/// <summary>
/// Defines the structured response from an AI-powered chatbot, encapsulating the bot's
/// textual response, emotional reaction, and any associated animation or preference insights.
/// </summary>
public class ChatbotResponse
{
    /// <summary>
    /// The chatbot's textual response to the user's query.
    /// This field is essential.
    /// </summary>
    [JsonProperty("response")]
    [ToolProperty("The chatbot's response to the user's input.", true)]
    public string Response { get; set; }

    /// <summary>
    /// Reflects the chatbot's reaction to the input, such as approval or disapproval.
    /// This field is essential.
    /// </summary>
    [JsonProperty("reaction")]
    [ToolProperty("Indicates the chatbot's liking towards the user's input.", true)]
    public ChatbotReaction Reaction { get; set; }

    /// <summary>
    /// Expresses the chatbot's emotional state in response to the input.
    /// This field is optional.
    /// </summary>
    [JsonProperty("emotion")]
    [ToolProperty("Depicts the chatbot's emotional response to the user's input.", false)]
    public ChatbotEmotion Emotion { get; set; }

    /// <summary>
    /// Suggests an animation to accompany the chatbot's response, enriching user interaction.
    /// This field is optional.
    /// </summary>
    [JsonProperty("animation")]
    [ToolProperty("Suggests a suitable animation to pair with the response.", false)]
    public ResponseAnimation Animation { get; set; }
}

/// <summary>
/// Enumerates potential reactions of a chatbot to user input, for instance, expressing agreement or disagreement.
/// </summary>
public enum ChatbotReaction
{
    Positive,
    Negative,
    Neutral
}

/// <summary>
/// Enumerates possible emotional expressions of a chatbot in response to user input, aiding in conveying empathy or sentiment.
/// </summary>
public enum ChatbotEmotion
{
    Happy,
    Sad,
    Surprised,
    Angry
}

/// <summary>
/// Enumerates various animations that can be triggered in response to user interaction, enhancing the engagement and dynamism of the chatbot's presentation.
/// </summary>
public enum ResponseAnimation
{
    None,
    Wave,
    ThumbsUp,
    Laugh
}

Next Steps

After defining your MyCustomResponse class, proceed to customize and utilize your CustomAssistantTool to make API calls and process responses using this structured approach. This will enable your application to handle OpenAI API responses predictably and efficiently, tailored to your specific requirements.

By starting with a well-defined custom response structure, you set a solid foundation for your application's interaction with the OpenAI API, facilitating smoother development and integration of AI-powered features.

Last updated