-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonsterFactory.cs
82 lines (65 loc) · 2.66 KB
/
MonsterFactory.cs
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
using System.Text.RegularExpressions;
namespace Monster_Collector.Managers;
public partial class MonsterFactory(LLM llm)
{
private readonly LLM Llm = llm;
[GeneratedRegex(@"\s+")]
private static partial Regex MyRegex();
public Monster Create(List<string> ignoreNames, string? customPrompt = null)
{
var monster = new Monster();
if (Llm.IsValid())
{
GenerateNameDescription(monster, ignoreNames, customPrompt);
}
return monster;
}
private string? GenerateNameDescription(Monster monster, List<string> ignoreNames, string? customPrompt = null)
{
string? output = null;
try
{
Console.WriteLine($"Generating name and description for type {monster.Name}");
string prompt = @$"
Generate a unique creative monster name and description for a scary monster in a dungeon game.
The description should be no longer than one sentence. The name should be three or less words and be creative and unique.
Output the result in the format: Name, Description. The Name and Description must be separated by a comma.
The type of monster should be {monster.Name}.
{customPrompt ?? ""}
The name of the monster can not include any of the following names.";
// Format the prompt to remove spaces and newlines.
prompt = prompt.Replace("\r\n", "").Trim();
prompt = MyRegex().Replace(prompt, " ");
// Call the LLM.
output = Llm.GetTextAsync(prompt, ignoreNames != null && ignoreNames.Count > 0 ? string.Join(", ", ignoreNames) : "null").GetAwaiter().GetResult();
// Parse the output for "name, description".
if (output is not null)
{
var parts = output.Split(",", 2);
monster.Name = parts[0].Trim();
monster.Description = parts[1].Trim();
Console.WriteLine($"Created {monster.Name}, {monster.Description}");
}
}
catch (Exception excep)
{
Console.WriteLine(excep.Message);
}
return output;
}
public string? GenerateImage(Monster monster)
{
string? url = "";
try
{
Console.WriteLine($"Generating image for {monster.Name} - {monster.Description}");
var imageResult = llm.GetImage($"{monster.Name} - {monster.Description}").GetAwaiter().GetResult();
Console.WriteLine($"Created {monster.Description}");
}
catch (Exception excep)
{
Console.WriteLine(excep.Message);
}
return url;
}
}