AI DevKit
Glitch9 Inc.Glitch9 DocsDiscordIssues
  • Introduction
    • AI DevKit 3.0
    • Update Logs
    • Troubleshooting
      • ❗Issues After Updating AIDevKit?
      • ❗The type or namespace name 'Newtonsoft' could not be found
      • ❗Build Error: The name 'UnityMenu' does not exist in the current context
      • ❗Model 'modelName' not found
      • ❗The model `model name` does not exist or you do not have access to it
      • ❗The type or namespace name 'AndroidJavaObject' could not be found
      • ❗The type or namaspace name 'Plastic' does not exist
      • ❗Build Error: The name 'Asset Database' does not exist in the current context
      • ❗'ModelData.Create(Provider, string, UnixTime?, string)': not all code paths return a value
      • ⚠️ Timeout Issues
      • ⚠️ Receiving a “HTTP/1.1 400 Bad Request” Error?
    • FAQ
      • My OpenAI API free trial has ended or is inactive.
  • Quick Start
    • Get API Keys
      • OpenAI API Key Guide
      • Google API Key Guide
      • ElevenLabs API Key Guide
    • Text Generation
    • C# Object Generation
    • Image Generation
    • Sound Effect Generation
    • Text to Speech (TTS)
    • Speech to Text (STT)
    • Voice Changer
    • Audio Isolation
  • Pro Features
    • Generation Menu
      • Code Generators
        • C# Script Generator
        • Unity Component Generator
    • Editor Chat
    • Editor Vision (TTI, ITI)
    • Editor Speech (TTS)
    • Management Tools
      • Prompt History Viewer
      • AI Model Manager
      • TTS Voice Manager
      • OpenAI File Manager
      • OpenAI Assistant Manager
      • ElevenLabs Voice Library
  • Assistants API (OpenAI)
    • How it works
    • Creating custom functions
    • Creating assistants API
  • Advanced API Supports
    • OpenAI API
      • 💬Chat completions
      • 🖼️Image operations
      • 🗣️Text to speech
      • 🎙️Speech to text
        • Recording real-time in Unity
      • 💾Files
      • 🔎Embeddings
      • 🛡️Moderations
      • ⚙️Fine-tuning
    • Google API
      • 📝System instructions
      • 💬Text generation
      • ⚙️Fine-tuning
      • ▶️Fucntion calling
      • 🔎Embeddings
      • 🛡️Safety
      • 💻Code execution
    • ElevenLabs API
  • Legacy Documents
    • AI DevKit 1.0 - 2.0
      • AI DevKit 2.0
      • AI DevKit 1.0
      • Preperation
      • Event Handlers
      • Scriptable Toolkits
        • Chat Streamer
        • Image Generator
        • Voice Transcriber
        • Voice Generator
      • Editor Tools
Powered by GitBook
On this page
  • 1. Without Parameters Schema
  • 2. With Parameters Schema
  • 3. Extending the Function Delegate
  1. Assistants API (OpenAI)

Creating custom functions

Custom functions allow you to extend the capabilities of your Assistant by defining specific actions that the Assistant can perform. Here's how to create custom functions using the FunctionCall class. You can create a FunctionCall instance by specifying the function's name, description, and an optional delegate that will execute the function. There are two methods for creating a FunctionCall instance:

1. Without Parameters Schema

If you need a FunctionCall that returns response in Text (string) format, use the following method to create a FunctionCall instance with the specified name, description, and delegate.

// Creating functionCall without parameters schema
var functionCall = FunctionCall.Create("MyFunction", "This is a sample function", new MyFunctionDelegate());

2. With Parameters Schema

To create a FunctionCall that returns a Run with custom object argument(T), define your custom argument class with properties with JsonSchemaAttribute. Only string and enum types are supported.

using Glitch9.IO.Json.Schema;

// Define your custom return class with JsonSchemaAttributes.
public class GetCurrentTemperatureArgument
{
    // Example string property: Define properties according to the expected API response structure.
    [JsonSchema("location", Description = "The city and state, e.g., San Francisco, CA.", required: true)]
    public string Location { 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.
    [JsonSchema("unit", Description = "The temperature unit to use. Infer this from the user's location.", required: false)]
    public TemperatureUnit Unit { get; set; }
    
    // Properties without JsonSchemaAttribute won't be included in Parameters Schema.
    [JsonProperty("temperature")]
    public string Temperature { get; set; }
}

// Example enum to represent a category or type of response.
// "enum": ["Celsius", "Fahrenheit"],
public enum TemperatureUnit
{
    Celsius,
    Fahrenheit
}

Then use the following method create a FunctionCall instance with the specified name, description, and delegate. The parameters for the function are generated based on the specified type T.

// Creating functionCall with parameters schema
var functionCall = FunctionCall.Create<GetCurrentTemperatureArgument>("get_current_temperature", "Get the current temperature for a specific location.", new MyFunctionDelegate());

3. Extending the Function Delegate

using Cysharp.Threading.Tasks;
using Glitch9.AIDevelopmentKit;

public class GetCurrentTemperatureDelegate<GetCurrentTemperatureArgument> : FunctionDelegate
{ 
    // Example dummy function hard coded to return the same weather
    // In production, this could be your backend API or an external API
    public override async UniTask<GetCurrentTemperatureArgument> ExecuteInternal(GetCurrentTemperatureArgument argument)
    {        
        if (argument.Location.ToLowerCase().Contains("tokyo"))
        {
            argument.Temperature = "10";
            argument.Unit = TemperatureUnit.Celsius;
            return argument;
        }
        else if (argument.Location.ToLowerCase().Contains("san francisco"))
        {
            argument.Temperature = "72";
            argument.Unit = TemperatureUnit.Fahrenheit;
            return argument;
        }
        else if (argument.Location.ToLowerCase().Contains("paris"))
        {
            argument.Temperature = "22";
            argument.Unit = TemperatureUnit.Fahrenheit;
            return argument;
        }
        else
        {
            argument.Temperature = "unknown";
            return argument;
        }
    }
}

If no function delegate is provided to a FunctionCall and the AssistantsAPI returns a Run with RunStatus.RequiresAction, AssistantsAPIv2 will wait until required outputs are submitted with AssistantsAPIv2.SubmitToolOutput.

PreviousHow it worksNextCreating assistants API

Last updated 10 months ago

A function delegate is a class that extends the FunctionDelegate class. This delegate will delegate when OpenAI AssistantsAPI returns a Run with RunStatus.RequiresAction.

submit tool outputs to run