@@ -7,73 +7,74 @@ namespace DotPrompt.Extensions.OpenAi;
77/// </summary>
88public static class OpenAiExtensions
99{
10- /// <summary>
11- /// Converts a <see cref="PromptFile"/> instance to a collection of <see cref="ChatMessage"/> objects.
12- /// </summary>
1310 /// <param name="promptFile">The <see cref="PromptFile"/> instance containing prompt definitions.</param>
14- /// <param name="values">A dictionary of values to be substituted in the user prompt template.</param>
15- /// <returns>An enumerable collection of <see cref="ChatMessage"/> objects.</returns>
16- public static IEnumerable < ChatMessage > ToOpenAiChatMessages ( this PromptFile promptFile ,
17- IDictionary < string , object > ? values )
11+ extension ( PromptFile promptFile )
1812 {
19- var messages = new List < ChatMessage > ( ) ;
20-
21- // If the prompt file provides any few shot prompts, then include these first
22- if ( promptFile . FewShots . Length != 0 )
13+ /// <summary>
14+ /// Converts a <see cref="PromptFile"/> instance to a collection of <see cref="ChatMessage"/> objects.
15+ /// </summary>
16+ /// <param name="values">A dictionary of values to be substituted in the user prompt template.</param>
17+ /// <returns>An enumerable collection of <see cref="ChatMessage"/> objects.</returns>
18+ public IEnumerable < ChatMessage > ToOpenAiChatMessages ( IDictionary < string , object > ? values )
2319 {
24- foreach ( var fewShot in promptFile . FewShots )
20+ var messages = new List < ChatMessage > ( ) ;
21+
22+ // If the prompt file provides any few shot prompts, then include these first
23+ if ( promptFile . FewShots . Length != 0 )
2524 {
26- messages . Add ( new UserChatMessage ( fewShot . User ) ) ;
27- messages . Add ( new AssistantChatMessage ( fewShot . Response ) ) ;
25+ foreach ( var fewShot in promptFile . FewShots )
26+ {
27+ messages . Add ( new UserChatMessage ( fewShot . User ) ) ;
28+ messages . Add ( new AssistantChatMessage ( fewShot . Response ) ) ;
29+ }
2830 }
29- }
3031
31- if ( ! string . IsNullOrEmpty ( promptFile . Prompts ! . System ) )
32- {
33- messages . Add ( new SystemChatMessage ( promptFile . GetSystemPrompt ( values ) ) ) ;
34- }
32+ if ( ! string . IsNullOrEmpty ( promptFile . Prompts ! . System ) )
33+ {
34+ messages . Add ( new SystemChatMessage ( promptFile . GetSystemPrompt ( values ) ) ) ;
35+ }
3536
36- messages . Add ( new UserChatMessage ( promptFile . GetUserPrompt ( values ) ) ) ;
37+ messages . Add ( new UserChatMessage ( promptFile . GetUserPrompt ( values ) ) ) ;
3738
38- return messages ;
39- }
39+ return messages ;
40+ }
4041
41- /// <summary>
42- /// Converts a <see cref="PromptFile"/> instance to an <see cref="ChatCompletionOptions"/> object.
43- /// </summary>
44- /// <param name="promptFile">The <see cref="PromptFile"/> instance containing configuration and prompt definitions.</param>
45- /// <returns>A <see cref="ChatCompletionOptions"/> object configured based on the <see cref="PromptFile"/> instance.</returns>
46- public static ChatCompletionOptions ToOpenAiChatCompletionOptions ( this PromptFile promptFile )
47- {
48- var chatResponseFormat = promptFile . Config . OutputFormat switch
42+ /// <summary>
43+ /// Converts a <see cref="PromptFile"/> instance to an <see cref="ChatCompletionOptions"/> object.
44+ /// </summary>
45+ /// <returns>A <see cref="ChatCompletionOptions"/> object configured based on the <see cref="PromptFile"/> instance.</returns>
46+ public ChatCompletionOptions ToOpenAiChatCompletionOptions ( )
4947 {
50- OutputFormat . Text => ChatResponseFormat . CreateTextFormat ( ) ,
51- OutputFormat . Json => ChatResponseFormat . CreateJsonObjectFormat ( ) ,
52- OutputFormat . JsonSchema when promptFile . Config . Output ? . Schema is not null =>
53- ChatResponseFormat . CreateJsonSchemaFormat (
54- promptFile . Name ,
55- BinaryData . FromString ( promptFile . Config . Output . ToSchemaDocument ( ) ) ,
56- jsonSchemaIsStrict : true ) ,
57- OutputFormat . JsonSchema when promptFile . Config . Output ? . Schema is null =>
58- throw new DotPromptException ( "A valid schema was not provided to be used with the JsonSchema response type" ) ,
59- _ => throw new DotPromptException ( "The requested output format is not available" )
60- } ;
48+ var chatResponseFormat = promptFile . Config . OutputFormat switch
49+ {
50+ OutputFormat . Text => ChatResponseFormat . CreateTextFormat ( ) ,
51+ OutputFormat . Json => ChatResponseFormat . CreateJsonObjectFormat ( ) ,
52+ OutputFormat . JsonSchema when promptFile . Config . Output ? . Schema is not null =>
53+ ChatResponseFormat . CreateJsonSchemaFormat (
54+ promptFile . Name ,
55+ BinaryData . FromString ( promptFile . Config . Output . ToSchemaDocument ( ) ) ,
56+ jsonSchemaIsStrict : true ) ,
57+ OutputFormat . JsonSchema when promptFile . Config . Output ? . Schema is null =>
58+ throw new DotPromptException ( "A valid schema was not provided to be used with the JsonSchema response type" ) ,
59+ _ => throw new DotPromptException ( "The requested output format is not available" )
60+ } ;
6161
62- var chatCompletionOptions = new ChatCompletionOptions
63- {
64- ResponseFormat = chatResponseFormat
65- } ;
62+ var chatCompletionOptions = new ChatCompletionOptions
63+ {
64+ ResponseFormat = chatResponseFormat
65+ } ;
6666
67- if ( promptFile . Config . Temperature is not null )
68- {
69- chatCompletionOptions . Temperature = promptFile . Config . Temperature ;
70- }
67+ if ( promptFile . Config . Temperature is not null )
68+ {
69+ chatCompletionOptions . Temperature = promptFile . Config . Temperature ;
70+ }
7171
72- if ( promptFile . Config . MaxTokens is not null )
73- {
74- chatCompletionOptions . MaxOutputTokenCount = promptFile . Config . MaxTokens ;
75- }
72+ if ( promptFile . Config . MaxTokens is not null )
73+ {
74+ chatCompletionOptions . MaxOutputTokenCount = promptFile . Config . MaxTokens ;
75+ }
7676
77- return chatCompletionOptions ;
77+ return chatCompletionOptions ;
78+ }
7879 }
7980}
0 commit comments