post-thumb

How to fix SKException: Function not available _GLOBAL_FUNCTIONS_.input from SemanticKernel?

When I have created my 1st or 2nd semantic plugin for SemanticKernel I have got an exception: SKException with messageFunction not available _GLOBAL_FUNCTIONS_.input. After a bit of investigation I have discovered the issue. Here you can read how to reproduce that bug and how to fix it.

How to reproduce the issue?

The most important thing to fix any issue is to find a way to reproduce it. So let’s start with it.


    [Fact]
    public async Task throw_SKException_Function_not_available_GLOBAL_FUNCTIONS_input()
    {
        var kernel = SemanticKernelBuilderFactory.Create().Build();

        string promptTemplate = """
                                Give me 3 most interesting cities in a specific country.
                                Return it in JSON format with fields: name, area, interesting attraction.
                                The country is {{input}}
                                """;

        var semanticFunction =
            kernel.CreateSemanticFunction(promptTemplate, "GetInterestingCountries", "CountriesPlugin", 
                "Get a list of interesting cities in a specific county in Json format", new OpenAIRequestSettings()
                {
                    MaxTokens = 250,
                    Temperature = 0.5
                });

        
        var skException =
            await Assert.ThrowsAsync<SKException>(async () => await kernel.RunAsync("Poland", semanticFunction));
       Assert.Contains("Function not available _GLOBAL_FUNCTIONS_.input", skException.Message);
    }

As you can see it throws an exception. You can try it by yourself, because the example is in CodePruner.com GitHub repository .

How to fix it?

The exception is thrown because the variable input is used in wrong way. You can see it is {{input}} in above example. To make it right you should add a dollar sign $ to it. The you should have {{$input}}. Here is working example:


    [Fact]
    public async Task fix_throw_SKException_Function_not_available_GLOBAL_FUNCTIONS_input()
    {
        var kernel = SemanticKernelBuilderFactory.Create().Build();

        string promptTemplate = """
                                Give me 3 most interesting cities in a specific country.
                                Return it in JSON format with fields: name, area, interesting attraction.
                                The country is {{$input}}
                                """;

        var semanticFunction =
            kernel.CreateSemanticFunction(promptTemplate, "GetInterestingCountries", "CountriesPlugin", 
                "Get a list of interesting cities in a specific county in Json format", new OpenAIRequestSettings()
                {
                    MaxTokens = 250,
                    Temperature = 0.5
                });

        
      var resultResponse = await kernel.RunAsync("Slovakia", semanticFunction);
      var result = resultResponse.GetValue<string>();
      Console.WriteLine(result);
      // Example result:
      // {
      //     "cities": [
      //     {
      //         "name": "Bratislava",
      //         "area": "368.45 square kilometers",
      //         "interesting_attraction": "Bratislava Castle"
      //     },
      //     {
      //         "name": "Košice",
      //         "area": "242.77 square kilometers",
      //         "interesting_attraction": "St. Elisabeth Cathedral"
      //     },
      //     {
      //         "name": "Banská Štiavnica",
      //         "area": "50.35 square kilometers",
      //         "interesting_attraction": "Old Castle"
      //     }
      //     ]
      // }
    }

Summary

As you can notice there is a way to invoke a function from prompt when you omit dollar sign $, but it is a story for a different article.

Thank you for being here. I hope I fixed your problem. If you have any questions or comments, please leave them below.

comments powered by Disqus

Are you still here? Subscribe for more content!