-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathService.cs
More file actions
114 lines (100 loc) · 3.03 KB
/
Copy pathService.cs
File metadata and controls
114 lines (100 loc) · 3.03 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
using System;
using System.ClientModel;
using System.Collections.Generic;
using Azure.AI.OpenAI;
using CommunityToolkit.Mvvm.ComponentModel;
using OpenAI;
using OpenAI.Chat;
namespace SourceGit.AI
{
public class Service : ObservableObject
{
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public string Server
{
get;
set;
} = string.Empty;
public string ApiKey
{
get;
set;
} = string.Empty;
public bool ReadApiKeyFromEnv
{
get;
set;
} = false;
public List<string> AvailableModels
{
get => _availableModels;
set => SetProperty(ref _availableModels, value);
}
public string Model
{
get => _model;
set => SetProperty(ref _model, value);
}
public string AdditionalPrompt
{
get;
set;
} = string.Empty;
public void AddModel(string model)
{
if (!_availableModels.Contains(model))
{
var newList = new List<string>(_availableModels) { model };
AvailableModels = newList;
}
}
public void RemoveModel(string model)
{
if (_availableModels.Contains(model))
{
var newList = new List<string>(_availableModels);
newList.Remove(model);
AvailableModels = newList;
}
}
public List<string> FetchModelsFromServer()
{
var allModels = GetOpenAIClient().GetOpenAIModelClient().GetModels();
var result = new List<string>();
foreach (var model in allModels.Value)
result.Add(model.Id);
return result;
}
public ChatClient GetChatClient()
{
return !string.IsNullOrEmpty(Model) ? GetOpenAIClient().GetChatClient(Model) : null;
}
public Service Clone()
{
return new Service
{
Name = Name,
Server = Server,
ApiKey = ApiKey,
ReadApiKeyFromEnv = ReadApiKeyFromEnv,
Model = Model,
AdditionalPrompt = AdditionalPrompt,
AvailableModels = new List<string>(AvailableModels),
};
}
private OpenAIClient GetOpenAIClient()
{
var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey);
return Server.Contains("openai.azure.com/", StringComparison.Ordinal)
? new AzureOpenAIClient(new Uri(Server), credential)
: new OpenAIClient(credential, new() { Endpoint = new Uri(Server) });
}
private string _name = string.Empty;
private string _model = string.Empty;
private List<string> _availableModels = [];
}
}