-
Notifications
You must be signed in to change notification settings - Fork 414
/
Copy pathExtensionMethods.cs
61 lines (52 loc) · 1.93 KB
/
ExtensionMethods.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
using LLama.Sampling;
using Microsoft.SemanticKernel.ChatCompletion;
using AuthorRole = LLama.Common.AuthorRole;
namespace LLamaSharp.SemanticKernel;
public static class ExtensionMethods
{
public static LLama.Common.ChatHistory ToLLamaSharpChatHistory(this ChatHistory chatHistory, bool ignoreCase = true)
{
if (chatHistory is null)
{
throw new ArgumentNullException(nameof(chatHistory));
}
var history = new LLama.Common.ChatHistory();
foreach (var chat in chatHistory)
{
if (!Enum.TryParse<AuthorRole>(chat.Role.Label, ignoreCase, out var role))
role = AuthorRole.Unknown;
history.AddMessage(role, chat.Content ?? "");
}
return history;
}
/// <summary>
/// Convert LLamaSharpPromptExecutionSettings to LLamaSharp InferenceParams
/// </summary>
/// <param name="requestSettings"></param>
/// <returns></returns>
internal static LLama.Common.InferenceParams ToLLamaSharpInferenceParams(this LLamaSharpPromptExecutionSettings requestSettings)
{
if (requestSettings is null)
{
throw new ArgumentNullException(nameof(requestSettings));
}
var antiPrompts = new List<string>(requestSettings.StopSequences)
{
$"{AuthorRole.User}:",
$"{AuthorRole.Assistant}:",
$"{AuthorRole.System}:"
};
return new LLama.Common.InferenceParams
{
AntiPrompts = antiPrompts,
MaxTokens = requestSettings.MaxTokens ?? -1,
SamplingPipeline = new DefaultSamplingPipeline()
{
Temperature = (float)requestSettings.Temperature,
TopP = (float)requestSettings.TopP,
PresencePenalty = (float)requestSettings.PresencePenalty,
FrequencyPenalty = (float)requestSettings.FrequencyPenalty,
}
};
}
}