Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
dotnet-version: 10.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
dotnet-version: 10.0.x
- name: Get latest tag version
id: vars
run: echo "tag=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_OUTPUT
Expand Down
6 changes: 3 additions & 3 deletions DotPrompt.Tests/DotPrompt.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion DotPrompt.Tests/PromptFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void GenerateUserPrompt_WithAllParameterValues_GeneratesValidPrompt()
{
var promptFile = PromptFile.FromFile("SamplePrompts/param-types.prompt");

var expectedPrompt =
const string expectedPrompt =
"Parameter 1: Arthur Dent\nParameter 2: 42\nParameter 3: true\nParameter 4: 2024-01-02 03:04:05Z\nParameter 5: { SEP = True }\nParameter 6: Hello : 12";

var userPrompt = promptFile.GetUserPrompt(new Dictionary<string, object>
Expand Down
8 changes: 4 additions & 4 deletions DotPrompt/DotPrompt.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>DotPrompt</PackageId>
Expand All @@ -21,9 +21,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Fluid.Core" Version="2.25.0" />
<PackageReference Include="JsonSchema.Net" Version="7.4.0" />
<PackageReference Include="OpenAI" Version="2.3.0" />
<PackageReference Include="Fluid.Core" Version="2.31.0" />
<PackageReference Include="JsonSchema.Net" Version="8.0.5" />
<PackageReference Include="OpenAI" Version="2.8.0" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>

Expand Down
109 changes: 55 additions & 54 deletions DotPrompt/Extensions/OpenAi/OpenAiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,74 @@ namespace DotPrompt.Extensions.OpenAi;
/// </summary>
public static class OpenAiExtensions
{
/// <summary>
/// Converts a <see cref="PromptFile"/> instance to a collection of <see cref="ChatMessage"/> objects.
/// </summary>
/// <param name="promptFile">The <see cref="PromptFile"/> instance containing prompt definitions.</param>
/// <param name="values">A dictionary of values to be substituted in the user prompt template.</param>
/// <returns>An enumerable collection of <see cref="ChatMessage"/> objects.</returns>
public static IEnumerable<ChatMessage> ToOpenAiChatMessages(this PromptFile promptFile,
IDictionary<string, object>? values)
extension(PromptFile promptFile)
{
var messages = new List<ChatMessage>();

// If the prompt file provides any few shot prompts, then include these first
if (promptFile.FewShots.Length != 0)
/// <summary>
/// Converts a <see cref="PromptFile"/> instance to a collection of <see cref="ChatMessage"/> objects.
/// </summary>
/// <param name="values">A dictionary of values to be substituted in the user prompt template.</param>
/// <returns>An enumerable collection of <see cref="ChatMessage"/> objects.</returns>
public IEnumerable<ChatMessage> ToOpenAiChatMessages(IDictionary<string, object>? values)
{
foreach (var fewShot in promptFile.FewShots)
var messages = new List<ChatMessage>();

// If the prompt file provides any few shot prompts, then include these first
if (promptFile.FewShots.Length != 0)
{
messages.Add(new UserChatMessage(fewShot.User));
messages.Add(new AssistantChatMessage(fewShot.Response));
foreach (var fewShot in promptFile.FewShots)
{
messages.Add(new UserChatMessage(fewShot.User));
messages.Add(new AssistantChatMessage(fewShot.Response));
}
}
}

if (!string.IsNullOrEmpty(promptFile.Prompts!.System))
{
messages.Add(new SystemChatMessage(promptFile.GetSystemPrompt(values)));
}
if (!string.IsNullOrEmpty(promptFile.Prompts!.System))
{
messages.Add(new SystemChatMessage(promptFile.GetSystemPrompt(values)));
}

messages.Add(new UserChatMessage(promptFile.GetUserPrompt(values)));
messages.Add(new UserChatMessage(promptFile.GetUserPrompt(values)));

return messages;
}
return messages;
}

/// <summary>
/// Converts a <see cref="PromptFile"/> instance to an <see cref="ChatCompletionOptions"/> object.
/// </summary>
/// <param name="promptFile">The <see cref="PromptFile"/> instance containing configuration and prompt definitions.</param>
/// <returns>A <see cref="ChatCompletionOptions"/> object configured based on the <see cref="PromptFile"/> instance.</returns>
public static ChatCompletionOptions ToOpenAiChatCompletionOptions(this PromptFile promptFile)
{
var chatResponseFormat = promptFile.Config.OutputFormat switch
/// <summary>
/// Converts a <see cref="PromptFile"/> instance to an <see cref="ChatCompletionOptions"/> object.
/// </summary>
/// <returns>A <see cref="ChatCompletionOptions"/> object configured based on the <see cref="PromptFile"/> instance.</returns>
public ChatCompletionOptions ToOpenAiChatCompletionOptions()
{
OutputFormat.Text => ChatResponseFormat.CreateTextFormat(),
OutputFormat.Json => ChatResponseFormat.CreateJsonObjectFormat(),
OutputFormat.JsonSchema when promptFile.Config.Output?.Schema is not null =>
ChatResponseFormat.CreateJsonSchemaFormat(
promptFile.Name,
BinaryData.FromString(promptFile.Config.Output.ToSchemaDocument()),
jsonSchemaIsStrict: true),
OutputFormat.JsonSchema when promptFile.Config.Output?.Schema is null =>
throw new DotPromptException("A valid schema was not provided to be used with the JsonSchema response type"),
_ => throw new DotPromptException("The requested output format is not available")
};
var chatResponseFormat = promptFile.Config.OutputFormat switch
{
OutputFormat.Text => ChatResponseFormat.CreateTextFormat(),
OutputFormat.Json => ChatResponseFormat.CreateJsonObjectFormat(),
OutputFormat.JsonSchema when promptFile.Config.Output?.Schema is not null =>
ChatResponseFormat.CreateJsonSchemaFormat(
promptFile.Name,
BinaryData.FromString(promptFile.Config.Output.ToSchemaDocument()),
jsonSchemaIsStrict: true),
OutputFormat.JsonSchema when promptFile.Config.Output?.Schema is null =>
throw new DotPromptException("A valid schema was not provided to be used with the JsonSchema response type"),
_ => throw new DotPromptException("The requested output format is not available")
};

var chatCompletionOptions = new ChatCompletionOptions
{
ResponseFormat = chatResponseFormat
};
var chatCompletionOptions = new ChatCompletionOptions
{
ResponseFormat = chatResponseFormat
};

if (promptFile.Config.Temperature is not null)
{
chatCompletionOptions.Temperature = promptFile.Config.Temperature;
}
if (promptFile.Config.Temperature is not null)
{
chatCompletionOptions.Temperature = promptFile.Config.Temperature;
}

if (promptFile.Config.MaxTokens is not null)
{
chatCompletionOptions.MaxOutputTokenCount = promptFile.Config.MaxTokens;
}
if (promptFile.Config.MaxTokens is not null)
{
chatCompletionOptions.MaxOutputTokenCount = promptFile.Config.MaxTokens;
}

return chatCompletionOptions;
return chatCompletionOptions;
}
}
}
2 changes: 1 addition & 1 deletion DotPrompt/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DotPrompt;
/// </summary>
public class Output
{
private string? _schemaDocument = null;
private string? _schemaDocument;

/// <summary>
/// Gets or sets the format of the output. This determines how the output should be structured.
Expand Down
1 change: 0 additions & 1 deletion DotPrompt/PromptFile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Globalization;
using System.Text.RegularExpressions;
using Fluid;
using Json.Schema;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
Expand Down