|
1 | 1 | using System; |
2 | 2 | using System.ClientModel; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | +using System.Threading.Tasks; |
3 | 6 | using Azure.AI.OpenAI; |
| 7 | +using CommunityToolkit.Mvvm.ComponentModel; |
4 | 8 | using OpenAI; |
5 | 9 | using OpenAI.Chat; |
6 | 10 |
|
7 | 11 | namespace SourceGit.AI |
8 | 12 | { |
9 | | - public class Service |
| 13 | + public class Service : ObservableObject |
10 | 14 | { |
11 | | - public string Name { get; set; } = string.Empty; |
12 | | - public string Server { get; set; } = string.Empty; |
13 | | - public string Model { get; set; } = string.Empty; |
14 | | - public string ApiKey { get; set; } = string.Empty; |
15 | | - public bool ReadApiKeyFromEnv { get; set; } = false; |
16 | | - public string AdditionalPrompt { get; set; } = string.Empty; |
| 15 | + public string Name |
| 16 | + { |
| 17 | + get => _name; |
| 18 | + set => SetProperty(ref _name, value); |
| 19 | + } |
| 20 | + |
| 21 | + public string Server |
| 22 | + { |
| 23 | + get; |
| 24 | + set; |
| 25 | + } = string.Empty; |
| 26 | + |
| 27 | + public string ApiKey |
| 28 | + { |
| 29 | + get; |
| 30 | + set; |
| 31 | + } = string.Empty; |
| 32 | + |
| 33 | + public bool ReadApiKeyFromEnv |
| 34 | + { |
| 35 | + get; |
| 36 | + set; |
| 37 | + } = false; |
| 38 | + |
| 39 | + public string AdditionalPrompt |
| 40 | + { |
| 41 | + get; |
| 42 | + set; |
| 43 | + } = string.Empty; |
| 44 | + |
| 45 | + [JsonIgnore] |
| 46 | + public List<string> AvailableModels |
| 47 | + { |
| 48 | + get; |
| 49 | + private set; |
| 50 | + } = []; |
| 51 | + |
| 52 | + public string Model |
| 53 | + { |
| 54 | + get; |
| 55 | + set; |
| 56 | + } = string.Empty; |
| 57 | + |
| 58 | + public async Task<List<string>> FetchAvailableModelsAsync() |
| 59 | + { |
| 60 | + var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey); |
| 61 | + var client = Server.Contains("openai.azure.com/", StringComparison.Ordinal) |
| 62 | + ? new AzureOpenAIClient(new Uri(Server), credential) |
| 63 | + : new OpenAIClient(credential, new() { Endpoint = new Uri(Server) }); |
| 64 | + |
| 65 | + var allModels = client.GetOpenAIModelClient().GetModels(); |
| 66 | + AvailableModels = new List<string>(); |
| 67 | + foreach (var model in allModels.Value) |
| 68 | + AvailableModels.Add(model.Id); |
| 69 | + |
| 70 | + if (AvailableModels.Count > 0) |
| 71 | + { |
| 72 | + if (string.IsNullOrEmpty(Model) || !AvailableModels.Contains(Model)) |
| 73 | + Model = AvailableModels[0]; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + Model = null; |
| 78 | + } |
| 79 | + |
| 80 | + return AvailableModels; |
| 81 | + } |
17 | 82 |
|
18 | 83 | public ChatClient GetChatClient() |
19 | 84 | { |
| 85 | + if (string.IsNullOrEmpty(Model)) |
| 86 | + return null; |
| 87 | + |
20 | 88 | var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey); |
21 | 89 | var client = Server.Contains("openai.azure.com/", StringComparison.Ordinal) |
22 | 90 | ? new AzureOpenAIClient(new Uri(Server), credential) |
23 | 91 | : new OpenAIClient(credential, new() { Endpoint = new Uri(Server) }); |
24 | 92 |
|
25 | 93 | return client.GetChatClient(Model); |
26 | 94 | } |
| 95 | + |
| 96 | + private string _name = string.Empty; |
27 | 97 | } |
28 | 98 | } |
0 commit comments