microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.31k stars 3.13k forks source link

Python: Add support for kernel.InvokePromptAsync similar to C# counterpart. #6804

Closed RogerBarreto closed 2 months ago

RogerBarreto commented 2 months ago

Currently we have Kernel examples that are very simple using C#:

Kernel kernel = Kernel.CreateBuilder()
            .AddOpenAIChatCompletion(modelId: “gpt-4o”, apiKey: “…”)
            .Build();

Console.WriteLine(await kernel.InvokePromptAsync("Why is the sky blue?"));

In python there's no way to use it as function_name and plugin_name are required arguments and need to be created manually with a prompttemplateconfig.

Desired but not supported today

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion(ai_model_id="gpt-4o", api_key="..."))

print(asyncio.run(kernel.invoke_prompt("Why is the sky blue")))

Currently to achieve a result all those lines are required in python:

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion(ai_model_id="gpt-4o", api_key="..."))
prompt_template_config = PromptTemplateConfig(template="why is the sky blue")
tldr_function = kernel.add_function(function_name="tldrFunction",plugin_name="tldrPlugin", prompt_template_config = prompt_template_config)
result = asyncio.run(kernel.invoke(tldr_function))
print(result)
moonbox3 commented 2 months ago

Hi @RogerBarreto, we have the invoke_prompt functionality today. You can see it in use in this concept sample.

image

RogerBarreto commented 2 months ago

@moonbox3, Why are function_name and plugin_name are required for that?

In the .Net we don't, we generate a custom function name internally.

Suggest adding some examples in the getting started using this approach for simplicity, couldn't find when I was looking for small and clean examples.

moonbox3 commented 2 months ago

We'll have a look at this. Thank you!