-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGenerativeModelService.cs
71 lines (62 loc) · 2.33 KB
/
GenerativeModelService.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
using Microsoft.Extensions.Options;
namespace Mscc.GenerativeAI.Web
{
public interface IGenerativeModelService
{
GenerativeModel Model { get; }
GenerativeModel CreateInstance();
GenerativeModel CreateInstance(string model);
}
public class GenerativeModelService : IGenerativeModelService
{
private readonly IGenerativeAI _generativeAi;
private readonly GenerativeModel _model;
public GenerativeModelService(IOptions<GenerativeAIOptions> options)
{
var model = options?.Value?.Model ?? GenerativeAI.Model.Gemini15Pro;
if (!string.IsNullOrEmpty(options?.Value.ProjectId))
{
_generativeAi = new VertexAI(options?.Value.ProjectId, options?.Value.Region);
}
else
{
_generativeAi = new GoogleAI(apiKey: options?.Value.Credentials.ApiKey);
}
_model = _generativeAi.GenerativeModel(model: model);
}
public GenerativeModelService(IOptions<GenerativeAIOptions> options, string model) : base()
{
IGenerativeAI genAI;
if (!string.IsNullOrEmpty(options?.Value?.ProjectId))
{
genAI = new VertexAI(options?.Value.ProjectId, options?.Value.Region);
}
else
{
genAI = new GoogleAI(apiKey: options?.Value.Credentials.ApiKey);
}
_model = genAI.GenerativeModel(model: model);
}
/// <summary>
/// Default instance of the model.
/// </summary>
public GenerativeModel Model => _model;
/// <summary>
/// Creates a new instance of the current model.
/// </summary>
/// <returns>A new instance of the current model.</returns>
public GenerativeModel CreateInstance()
{
return _generativeAi.GenerativeModel(model: _model.Name);
}
/// <summary>
/// Creates a new instance of the specified model.
/// </summary>
/// <param name="model">The model name to create.</param>
/// <returns>A new instance of the model.</returns>
public GenerativeModel CreateInstance(string model)
{
return _generativeAi.GenerativeModel(model: model);
}
}
}