forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProviderHelper.cs
More file actions
46 lines (39 loc) · 1.57 KB
/
Copy pathProviderHelper.cs
File metadata and controls
46 lines (39 loc) · 1.57 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
using OpenAI;
using System.ClientModel;
namespace BotSharp.Plugin.OpenAI.Providers;
public class ProviderHelper
{
public static OpenAIClient GetClient(string provider, string model, string? apiKey, IServiceProvider services)
{
var settingsService = services.GetRequiredService<ILlmProviderService>();
var settings = settingsService.GetSetting(provider, model);
if (settings == null && string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException($"No LLM model settings found for '{provider}.{model}'. Register the model under LlmProviders (appsettings/user secrets) or pass an api key.");
}
var options = !string.IsNullOrEmpty(settings?.Endpoint) ?
new OpenAIClientOptions { Endpoint = new Uri(settings.Endpoint) } : null;
return new OpenAIClient(new ApiKeyCredential(apiKey ?? settings!.ApiKey), options);
}
public static List<RoleDialogModel> GetChatSamples(List<string> lines)
{
var samples = new List<RoleDialogModel>();
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
if (string.IsNullOrEmpty(line.Trim()))
{
continue;
}
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
// comments
if (role == "##")
{
continue;
}
samples.Add(new RoleDialogModel(role, content));
}
return samples;
}
}