semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Not able to use semantic kernel for simple prompt

Open ktjosh opened this issue 7 months ago • 1 comments

Hello I am using semantic kernel for a single prompt I want to use. As its a single prompt I do not want to create a plugin with skprompts file. Below is the sample code I have. When I run this code I receive Keynotfound exception saying .

As there is lack of examples I am not sure if we want to import plugins first after creation.

One or more errors occurred. (The plugin collection does not contain a plugin and/or function with the specified names. Plugin name - '', function name - 'topic'.)

Here is the sample code

            Kernel kernel = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(deploymentName, openAiEndpoint, new DefaultAzureCredential()).Build();


            var prompt = "Tell me a joke about {{topic}}";
            var jokeFunction = kernel.CreateFunctionFromPrompt(prompt, functionName: "JokeFunction");

            var jokePlugin = kernel.CreatePluginFromFunctions("JokePlugin", new List<KernelFunction>() { jokeFunction });


            KernelArguments args = new KernelArguments
            {
                {"topic" , "cats" }
            };

            var result = kernel.InvokeAsync<string>(pluginName: "JokePlugin", functionName: "JokeFunction", args).Result;

ktjosh avatar May 12 '25 18:05 ktjosh

@ktjosh Thanks for creating this issue. The code can be simplified a bit, try the following:

// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatCompletion(
        modelId: TestConfiguration.OpenAI.ChatModelId,
        apiKey: TestConfiguration.OpenAI.ApiKey)
    .Build();

// Example 2. Invoke the kernel with a templated prompt and display the result
KernelArguments arguments = new() { { "topic", "sea" } };
Console.WriteLine(await kernel.InvokePromptAsync("What color is the {{$topic}}?", arguments));

https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStarted/Step1_Create_Kernel.cs

markwallace-microsoft avatar May 15 '25 06:05 markwallace-microsoft

Thanks @markwallace-microsoft for responding, The prompt I was trying for is a bit more complex, but I can find a way to put it the way you did it to avoid creating a new skprompt file. Thanks

ktjosh avatar May 22 '25 22:05 ktjosh