-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPromptFileTests.cs
More file actions
522 lines (436 loc) · 19.2 KB
/
Copy pathPromptFileTests.cs
File metadata and controls
522 lines (436 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
using System.Text;
namespace DotPrompt.Tests;
public class PromptFileTests
{
[Fact]
public void FromFile_BasicPrompt_ProducesValidPromptFile()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic.prompt");
var expectedParameters = new Dictionary<string, string>
{
{ "country", "string" },
{ "style?", "string" }
};
var expectedDefaults = new Dictionary<string, object>
{
{ "country", "Malta" }
};
Assert.Equal("basic", promptFile.Name);
Assert.NotNull(promptFile.Model);
Assert.Equal("claude-3-5-sonnet-latest", promptFile.Model);
Assert.NotNull(promptFile.Config);
Assert.Equal(OutputFormat.Text, promptFile.Config.OutputFormat);
Assert.Equal(500, promptFile.Config.MaxTokens);
Assert.NotNull(promptFile.Config.Temperature);
Assert.Equal(0.9, promptFile.Config.Temperature.Value, 1e-2);
Assert.Equal(expectedParameters, promptFile.Config.Input.Parameters);
Assert.Equal(expectedDefaults, promptFile.Config.Input.Default);
Assert.NotNull(promptFile.Prompts);
Assert.StartsWith("You are a helpful AI assistant that enjoys making penguin related puns.",
promptFile.Prompts.System);
Assert.StartsWith("I am looking at going on holiday to {{ country }}", promptFile.Prompts.User);
Assert.Empty(promptFile.FewShots);
}
[Fact]
public void FromFile_WithNameSetting_UsesNameFromConfigurationInsteadOfFile()
{
var promptFile = PromptFile.FromFile("SamplePrompts/with-name-json.prompt");
Assert.Equal("example-with-name", promptFile.Name);
Assert.Equal(OutputFormat.Json, promptFile.Config.OutputFormat);
Assert.Null(promptFile.Config.Temperature);
Assert.Null(promptFile.Config.MaxTokens);
Assert.Null(promptFile.Prompts!.System);
}
[Fact]
public void FromFile_WithFewShotPrompts_CorrectlyExtractsValuesFromFile()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic-fsp.prompt");
Assert.NotEmpty(promptFile.FewShots);
Assert.Equal(3, promptFile.FewShots.Length);
Assert.Equal("What is Bluetooth", promptFile.FewShots[0].User);
Assert.Equal(
"AI is used in virtual assistants like Siri and Alexa, which understand and respond to voice commands.",
promptFile.FewShots[2].Response);
}
[Theory]
[InlineData("SamplePrompts/name-with-invalid-characters.prompt", "dont-use-names-like-this")]
[InlineData("SamplePrompts/multiline-name.prompt", "name-which-is-over-multiple-lines")]
public void FromFile_WithNameContainingInvalidChars_IsCleaned(string promptFilePath, string expectedName)
{
var promptFile = PromptFile.FromFile(promptFilePath);
Assert.Equal(expectedName, promptFile.Name);
}
[Theory]
[InlineData("SamplePrompts/basic.prompt", "You are a helpful AI assistant that enjoys making penguin related puns. You should work as many into your response as possible", false)]
[InlineData("SamplePrompts/with-name.prompt", "", false)]
[InlineData("SamplePrompts/with-name-json.prompt", "Please provide the response in JSON", true)]
[InlineData("SamplePrompts/json-missing-messages.prompt", "You are the voice of the guide, you should be authoritative and informative and appeal to a galactic audience. Please provide the response in JSON", true)]
public void GetSystemPrompt_WhenCalledWithDifferentConfiguration_ShouldRenderCorrectly(string inputFile, string expectedContains, bool exact)
{
var promptFile = PromptFile.FromFile(inputFile);
var systemPrompt = promptFile.GetSystemPrompt(null);
if (exact)
{
Assert.Equal(expectedContains, systemPrompt);
}
else
{
Assert.Contains(expectedContains, systemPrompt);
}
}
[Fact]
public void GetSystemPrompt_WhenPromptContainsTemplateInformation_IsRenderedCorrectly()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic-sp-template.prompt");
var generatedDate = new DateTimeOffset(2006, 1, 2, 3, 4, 5, TimeSpan.Zero);
var systemPrompt = promptFile.GetSystemPrompt(new Dictionary<string, object> { { "country", "Italy" }, { "generated", generatedDate } });
Assert.Contains(
"You are a helpful AI assistant that who has extensive local knowledge of Italy\nYou should append each response with the text `Generated: <date>` where `<date>` is replaced with the current date, for example:\n`Generated: Monday 2 Jan 2006`",
systemPrompt);
}
[Fact]
public void FromFile_InvalidPath_ThrowsException()
{
Action act = () => PromptFile.FromFile("SamplePrompts/does-not-exist.prompt");
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("The specified file does not exist", exception.Message);
}
[Fact]
public void FromFile_InvalidContent_ThrowsException()
{
Action act = () => PromptFile.FromFile("SamplePrompts/invalid-file.prompt");
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Unable to extract prompts from prompt file", exception.Message);
}
[Fact]
public void FromFile_MissingUserPrompt_ThrowsException()
{
Action act = () => PromptFile.FromFile("SamplePrompts/missing-user-prompt.prompt");
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("No user prompt template was provided in the prompt file", exception.Message);
}
[Fact]
public void FromFile_InvalidYamlContent_ThrowsException()
{
Action act = () => PromptFile.FromFile("SamplePrompts/basic-broken.prompt");
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Unable to parse prompt file", exception.Message);
}
[Fact]
public void FromFile_InvalidParameterType_ThrowsException()
{
Action act = () => PromptFile.FromFile("SamplePrompts/invalid-params.prompt");
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains($"The provided data type for 'oops' is not a valid type, should be string, number, bool, datetime, object",
exception.Message);
}
[Fact]
public void FromStream_WithUnreadableStream_ThrowsException()
{
using var ms = new MemoryStream();
var inputStream = new MockStream(ms, false, true);
Action act = () => PromptFile.FromStream("test", inputStream);
var exception = Assert.Throws<ArgumentException>(act);
Assert.Contains("Stream is not in a readable state", exception.Message);
}
[Theory]
[InlineData("clean\r\n\r\nthis name", "clean-this-name")]
[InlineData("do-not-clean", "do-not-clean")]
[InlineData("My COOL nAMe", "my-cool-name")]
[InlineData("this <is .pretty> un*cl()ean", "this-is-pretty-unclean")]
[InlineData(" some extra space ", "some-extra-space")]
public void FromStream_WithNamePart_CleansTheName(string inputName, string expectedName)
{
const string content = "prompts:\n system: System prompt\n user: User prompt";
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
ms.Seek(0, SeekOrigin.Begin);
var promptFile = PromptFile.FromStream(inputName, ms);
Assert.Equal(expectedName, promptFile.Name);
}
[Fact]
public void FromStream_WithInvalidName_ThrowsAnException()
{
const string content = "prompts:\n system: System prompt\n user: User prompt";
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
ms.Seek(0, SeekOrigin.Begin);
var act = () => PromptFile.FromStream("++ -- ()", ms);
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("once cleaned results in an empty string", exception.Message);
}
[Fact]
public void FromStream_WithMissingModelName_IsPersistedAsNullValue()
{
const string content = "name: missing-model\nmodel: \nprompts:\n system: System prompt\n user: User prompt";
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
ms.Seek(0, SeekOrigin.Begin);
var promptFile = PromptFile.FromStream("", ms);
Assert.Equal("missing-model", promptFile.Name);
Assert.Null(promptFile.Model);
}
[Fact]
public void GenerateUserPrompt_UsingDefaults_CorrectlyGeneratesPromptFromTemplate()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic.prompt");
var userPrompt = promptFile.GetUserPrompt(null);
Assert.Equal(
"I am looking at going on holiday to Malta and would like to know more about it, what can you tell me?\n",
userPrompt
);
}
[Fact]
public void GenerateUserPrompt_WithParameterValues_CorrectlyGeneratesPromptFromTemplate()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic.prompt");
var userPrompt = promptFile.GetUserPrompt(new Dictionary<string, object>{ { "country", "Antarctica" } });
Assert.Equal(
"I am looking at going on holiday to Antarctica and would like to know more about it, what can you tell me?\n",
userPrompt
);
}
[Fact]
public void GenerateUserPrompt_WithUnusedValues_CorrectlyGeneratesPromptFromTemplate()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic.prompt");
var userPrompt = promptFile.GetUserPrompt(new Dictionary<string, object>
{
{ "country", "Antarctica" },
{ "not-used", "This isn't used" }
});
Assert.Equal(
"I am looking at going on holiday to Antarctica and would like to know more about it, what can you tell me?\n",
userPrompt
);
}
[Fact]
public void GenerateUserPrompt_WithOptionalValue_CorrectlyGeneratesPromptFromTemplate()
{
var promptFile = PromptFile.FromFile("SamplePrompts/basic.prompt");
var userPrompt = promptFile.GetUserPrompt(new Dictionary<string, object>
{
{ "country", "Antarctica" },
{ "style", "Pirate" }
});
Assert.Equal(
"I am looking at going on holiday to Antarctica and would like to know more about it, what can you tell me?\nCan you answer in the style of a Pirate\n",
userPrompt
);
}
[Fact]
public void GenerateUserPrompt_WithMissingValues_ThrowsAnException()
{
var promptFile = PromptFile.FromFile("SamplePrompts/required-parameters.prompt");
var act = () => promptFile.GetUserPrompt(null);
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Specified values do not contain the required parameter 'name'", exception.Message);
}
[Fact]
public void GenerateUserPrompt_WithMissingValuesFromDictionary_ThrowsAnException()
{
var promptFile = PromptFile.FromFile("SamplePrompts/required-parameters.prompt");
var act = () => promptFile.GetUserPrompt(new Dictionary<string, object>{ { "not-this", "This isn't it" } });
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Specified values do not contain the required parameter 'name'", exception.Message);
}
[Fact]
public void GenerateUserPrompt_WithInvalidTemplate_ThrowsAnException()
{
var promptFile = PromptFile.FromFile("SamplePrompts/invalid-prompt.prompt");
var act = () => promptFile.GetUserPrompt(null);
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Unable to parse the prompt template", exception.Message);
}
[Fact]
public void GenerateUserPrompt_WithAllParameterValues_GeneratesValidPrompt()
{
var promptFile = PromptFile.FromFile("SamplePrompts/param-types.prompt");
var 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>
{
{ "param1", "Arthur Dent" },
{ "param2", 42 },
{ "param3", true },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", new { SEP = true } },
{ "param6", new TestStruct{ Item1 = "Hello", Item2 = 12 } }
});
Assert.Equal(expectedPrompt, userPrompt);
}
[Theory]
[MemberData(nameof(InvalidTypeData))]
public void GenerateUserPrompt_WithInvalidStringParameterValueType_ThrowsException(Dictionary<string, object> parameterValues, string expectedError)
{
var promptFile = PromptFile.FromFile("SamplePrompts/param-types.prompt");
var act = () => promptFile.GetUserPrompt(parameterValues);
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains(expectedError, exception.Message);
}
[Theory]
[MemberData(nameof(NumericData))]
public void GenerateUserPrompt_WithNumericValueTypes_GeneratesValidPrompt(object value, string expectedPrompt)
{
var promptFile = PromptFile.FromFile("SamplePrompts/numeric-types.prompt");
var userPrompt = promptFile.GetUserPrompt(new Dictionary<string, object> { { "param1", value } });
Assert.Equal(expectedPrompt, userPrompt);
}
[Fact]
public void ToStream_WithValidInput_ProducesExpectedContent()
{
var promptFile = new PromptFile
{
Name = "test",
Config = new PromptConfig
{
MaxTokens = 500,
OutputFormat = OutputFormat.Json,
Input = new InputSchema
{
Parameters = new Dictionary<string, string>
{
{ "test?", "string" }
}
}
},
Prompts = new Prompts
{
System = "system prompt",
User = "user prompt"
}
};
var expected = "name: test\nconfig:\n input:\n parameters:\n test?: string\n outputFormat: Json\n maxTokens: 500\nprompts:\n system: system prompt\n user: user prompt\n";
var ms = new MemoryStream();
promptFile.ToStream(ms);
var actual = Encoding.UTF8.GetString(ms.ToArray());
Assert.Equal(expected, actual);
}
[Fact]
public void ToStream_WithNoStreamArgument_ThrowsError()
{
var promptFile = new PromptFile
{
Name = "test"
};
var act = () => promptFile.ToStream(null!);
Assert.Throws<ArgumentNullException>(act);
}
[Fact]
public void ToStream_WithNonWriteableStream_ThrowsError()
{
var promptFile = new PromptFile
{
Name = "test"
};
var ms = new MemoryStream();
var stream = new MockStream(ms, true, false);
var act = () => promptFile.ToStream(stream);
var exception = Assert.Throws<DotPromptException>(act);
Assert.Contains("Unable to use stream as it is not writeable", exception.Message);
}
public static IEnumerable<object[]> NumericData =>
new List<object[]>
{
new object[] { 3d, "Pass" },
new object[] { (double)4, "Pass" },
new object[] { (float)5, "Pass" },
new object[] { (sbyte)6, "Pass" },
new object[] { (byte)7, "Pass" },
new object[] { (short)8, "Pass" },
new object[] { (ushort)9, "Pass" },
new object[] { 10, "Pass" },
new object[] { (uint)11, "Pass" },
new object[] { (long)12, "Pass" },
new object[] { (ulong)13, "Pass" },
new object[] { 2, string.Empty }
};
public static IEnumerable<object[]> InvalidTypeData =>
new List<object[]>
{
new object[]
{
new Dictionary<string, object>
{
{ "param1", 1 },
{ "param2", 42 },
{ "param3", true },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", new { SEP = true } },
{ "param6", new TestStruct { Item1 = "Hello", Item2 = 12 } }
},
"not a valid string"
},
new object[]
{
new Dictionary<string, object>
{
{ "param1", "Arthur Dent" },
{ "param2", "42" },
{ "param3", true },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", new { SEP = true } },
{ "param6", new TestStruct { Item1 = "Hello", Item2 = 12 } }
},
"not a valid numeric type"
},
new object[]
{
new Dictionary<string, object>
{
{ "param1", "Arthur Dent" },
{ "param2", 42 },
{ "param3", "nope" },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", new { SEP = true } },
{ "param6", new TestStruct { Item1 = "Hello", Item2 = 12 } }
},
"not a valid boolean"
},
new object[]
{
new Dictionary<string, object>
{
{ "param1", "Arthur Dent" },
{ "param2", 42 },
{ "param3", true },
{ "param4", "2024-01-02" },
{ "param5", new { SEP = true } },
{ "param6", new TestStruct { Item1 = "Hello", Item2 = 12 } }
},
"not a valid DateTimeOffset"
},
new object[]
{
new Dictionary<string, object>
{
{ "param1", "Arthur Dent" },
{ "param2", 42 },
{ "param3", true },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", 10 },
{ "param6", new TestStruct { Item1 = "Hello", Item2 = 12 } }
},
"not a valid object type"
},
new object[]
{
new Dictionary<string, object>
{
{ "param1", "Arthur Dent" },
{ "param2", 42 },
{ "param3", true },
{ "param4", new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero) },
{ "param5", new { SEP = true } },
{ "param6", 10 }
},
"not a valid object type"
}
};
}
internal struct TestStruct
{
public string Item1 { get; set; }
public int Item2 { get; set; }
public override string ToString()
{
return $"{Item1} : {Item2}";
}
}