microsoft / prompty

Prompty makes it easy to create, manage, debug, and evaluate LLM prompts for your AI applications. Prompty is an asset class and format for LLM prompts designed to enhance observability, understandability, and portability for developers.
https://prompty.ai
MIT License
381 stars 24 forks source link

Invalid prompty template. Header and content could not be parsed. #70

Closed MikeYeager closed 2 weeks ago

MikeYeager commented 3 weeks ago

I created a simple prompty template in VS Code and ran it with no problem. When I try to load the same template file into Semantic Kernel in VS2022, I get this exception: "Invalid prompty template. Header and content could not be parsed."

Here is my template:

---
name: JokePrompt
description: Generate a funny joke
authors:
  - Mike Yeager
model:
  api: chat
  configuration:
    type: azure_openai
  parameters:
    max_tokens: 1000
    temperature: 0.9
    top_p: 0.0
    presence_penalty: 0.0
    frequency_penalty: 0.0
sample:
  subject: square dancing
  style: monty python
---

user:
# WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE SUBJECT BELOW

JOKE MUST BE:
- G RATED
- WORKPLACE/FAMILY SAFE

NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY

BE CREATIVE AND FUNNY. I WANT TO LAUGH.

Incorporate the style suggestion, if provided: '{{style}}'

+++++
{{subject}}
+++++

And here is my SK code:

var builder = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(
        "MyChatTest",
        _endpoint,
        _apiKey);

var kernel = builder.Build();

var promptyFile = Path.Combine(Directory.GetCurrentDirectory(), "Prompty", "joke.prompty");

#pragma warning disable SKEXP0040 Suppress this diagnostic to proceed.
            var promptyFunction = kernel.CreateFunctionFromPrompty(promptyFile);
#pragma warning restore SKEXP0040Suppress this diagnostic to proceed.

var result = await kernel.InvokeAsync(promptyFunction, new() { ["subject"] = "fishing", ["style"] = "knock knock" });
Console.WriteLine(result);

It blows up on kernel.CreateFunctionFromPrompty()

Any ideas? Thanks in advance.

wayliums commented 3 weeks ago

what's your SK version and exact error? Also did you try right click on the prompty file and generate SK code from there?

MikeYeager commented 3 weeks ago

@wayliums

    <PackageReference Include="Microsoft.SemanticKernel" Version="1.18.0-rc" />
    <PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.18.0-rc" />
    <PackageReference Include="Microsoft.SemanticKernel.Prompty" Version="1.18.0-alpha" />

The exact error message is the title of this issue: Invalid prompty template. Header and content could not be parsed.

The stack trace is:

   at Microsoft.SemanticKernel.Prompty.KernelFunctionPrompty.ToPromptTemplateConfig(String promptyTemplate)
   at Microsoft.SemanticKernel.PromptyKernelExtensions.CreateFunctionFromPrompty(Kernel kernel, String promptyTemplate, IPromptTemplateFactory promptTemplateFactory)
   at SKLasVegas.Program.<Prompty>d__7.MoveNext() in C:\Users\MYeager\Dropbox\AI\SK DevIntersections Las Vegas 2024\SampleCode\SKVegas\Program.cs:line 149
   at SKLasVegas.Program.<Main>d__3.MoveNext() in C:\Users\MYeager\Dropbox\AI\SK DevIntersections Las Vegas 2024\SampleCode\SKVegas\Program.cs:line 32

The code generated by the VS Code extension is very similar to the code I'm using and produces the same result.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

var deployment = "";
var endpoint = "";
var key = "aoai_key";
var kernel = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(deployment, endpoint, key)
    .Build();

    // update the input below to match your prompty
    KernelArguments kernelArguments = new()
    {
        { "question", "what's my question?" },
    };

var prompty = kernel.CreateFunctionFromPromptyFile("joke.prompty");
var result = await prompty.InvokeAsync<string>(kernel, kernelArguments);
Console.WriteLine(result);
sethjuarez commented 2 weeks ago

Hmmm - it looks like an implementation problem in SK. In general it should pull all the relevant params and pass it into the API call.

MikeYeager commented 2 weeks ago

Thanks @sethjuarez. I wonder if it's related to this: https://github.com/microsoft/semantic-kernel/issues/8154 I guess I'll wait for a new release of SK and see if it's fixed. If not, I will re-post in the SK repo.