Skip to content

Custom functions (preview)

The purpose of custom functions is to give solution partners a fast and easy way to integrate AI functions into other parts of their solution without the hassle of storing semantic memory or choosing and updating ai models.

Examples of AI function created using custom functions. Could be:

  • Preselecting the best values in forms.
  • Drafting replies to a customer support inquiry.

Semantic function are created beforehand by sending an instruction template to the API.

If any information could be used to improve BAIAs answers this can be stored in custom information sets through the API as well. Examples of custom information sets could be:

  • Sample answers from earlier interactions.
  • Relevant information such as opening times, return policies etc.

Examples of Custom functions

A full example program can be found on GitHub.

A Hello world world example for custom functions

The first step is to create the function.

1
2
3
4
5
   await client.CreateCustomFunctionAsync(new ()
   {
       FunctionId = "hello-world",
       Text = "Write a short greeting for the world."
   });

As soon at the function have been created it can be invoked.

var reply =  await client.CallAndWaitCustomFunctionAsync("hello-world", new ());
Console.WriteLine(reply.Result);

This will generate a small greeting from BAIA.

An example of variables in custom functions

Using the syntax {{$variable}} it is possible to use parameters when creating custom functions.

1
2
3
4
5
   await client.CreateCustomFunctionAsync(new ()
   {
       FunctionId = "hello-you",
       Text = "Write a short greeting for {{$name}}."
   });

The function can now be invoked by given a value for the variable.

1
2
3
4
5
6
7
8
var reply =  await client.CallAndWaitCustomFunctionAsync("hello-world", new ()
{
   Parameters =
   {
     { "name","John Doe"}
   }
});
Console.WriteLine(reply.Result);

This will generate a small greeting from BAIA to John Doe.

Example of a custom information recall

Data stored in custom information sets can be recalled in custom functions using variables.

The custom information set must be populated before the custom function is called.

First the custom information set is created.

await client.CreateCustomInformationSetAsync(new() { Id = "my-facts" });

Then the information set is populated

    await client.SaveCustomInformationSnippetAsync("my-facts", "1", new ()
    {
        Text =
        """
        ## Opening hours

        Monday: 10:00 - 17:00
        Tuesday: 10:00 - 17:00
        Wednesday: 10:00 - 17:00
        Thursday: 10:00 - 20:30
        Friday: 10:00 - 17:00
        Saturday: 10:00 - 14:00
        Sunday: Closed

        """
    });

It can take a few seconds before the snippets are available to BAIA depending on the load on the system.

A custom function is created. This can be done independently of the custom information sets.

await client.CreateCustomFunctionAsync(new ()
{
  FunctionId = "customer-service",
  Text =
    """
    # Instruction

    You are a helpful and playful member of the customer service team. Your task is to answer the customers questions. 
    Please keep your answers short and precise while keeping a polite tone and projecting good mood.
    You should answer the customer in their native language.

    Today is {{$today}}

    # Recipient

    Here are some facts about the customer you are currently communicating with:
    Name: {{$name}}
    Language: {{$language}}

    # Facts from the customer service handbook

    The following are facts from the customer service handbook these may or may not be relevant for the customers question:
    {{$facts}}

    # Previous orders

    If the customer have previously placed orders the latest orders will be listed below:

    {{$orders}}

    # The Question
    Below this line is the question to answer please do only include the answer and nothing else in your reply:
    ----
    {{$question}}

    """
});

It is now possible to call the custom function.

var question = "Hvad tid lukker I i dag?"; //What time do you close today?
var reply = await client.CallAndWaitCustomFunctionAsync("customer-service", new()
{
    Parameters =
    {
        { "name", "Jens Jensen"},
        { "language", "Danish (Denmark)"},
        { "today", DateTimeOffset.Now.ToString("G")},
        { "question", question}
        { "orders", "none"}
    },
    Retrievals =
    [
        new ()
        {
            CustomInformationSetId = "my-facts",
            Handle = "facts",
            Phrase = question
        }
    ]
});
Console.WriteLine(reply.Result);

This will give an helpful answer about opening times in danish.

Example of asynchronous custom function call

Custom function can be used in multiple fashions. They can also return more structured data.

await client.CreateCustomFunctionAsync(new()
{
    FunctionId = "classify",
    Text =
"""
# Instructions
Your task is to classify an email from a person to make sure it reaches the correct customer service representative faster.
You will be presented with the email, and should then classify it as one of the following 5 types:

NEW_BIZZ
The person is not yet a customer, and wants to know more about our offering.

ORDERS
The person have questions to an ongoing order.

COMPLAINTS
The customer have a complaint about a previously delivered order.

PAYMENT
The customer have questions or issues relating to payment.

OTHERS
If none of the other labels apply.

Based on the content of the presented email you should write a rationale for the decision followed on a new line by the chosen label.

# Example

## Email
Dear customer service
The link to track and trace in the order email i received did not work. Can you help me?
Cheers, Jane

## Response
* The sender refers to an order email and a track and trace code.
* Interest in track and trace implies the order is on the way to the sender.
* This must be classified as ORDERS as it is an ongoing order.

ORDERS

## Email to classify:

{{$email}}

"""
});

In this case it would not be meaningful to call the operation in a synchronous fashion.

Instead it should be called as long running operation.

1
2
3
4
5
6
7
var operation = await client.CallCustomFunctionAsync("classify", new()
    {
        Parameters =
        {
            { "email","I ordered a chair from you last month, but now 2 legs fell off. the order number was 1237137223."},
        }
    });

After this the status and result of the task can be collected using the returned operation id.

1
2
3
4
5
6
var result = await client.GetCustomFunctionResultAsync("classify", operation.Id);
var reply = result.Result.Split("\n", StringSplitOptions.TrimEntries);
if (Enum.TryParse<EmailType>(reply[^1], out var emailType))
{
    Console.WriteLine($"Email was classified as: {emailType}");
}