using Microsoft.Win32; using OllamaSharp; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; using System.Windows.Media; using YwxApp.AiChat.Commands; using YwxApp.AiChat.Models; namespace YwxApp.AiChat.ViewModels { /// /// 0、Current class: /// public class SettingViewModel : INotifyPropertyChanged { #region Field | Property | Collection | Command #region Field private string _selectedModel; //select model private string _modelInfo; //model info private SolidColorBrush _labelBackgroundColor; //color style private readonly ShareOllamaObject _ollama; //OllamaAPI object. #endregion #region Property public string OllamaAppPath { get { return _ollama.OllamaAppPath; } set { _ollama.OllamaAppPath = value; OnPropertyChanged(); } } public string RemoteHost { get { return _ollama.RemoteHost; } set { _ollama.RemoteHost = value; OnPropertyChanged(); } } public string RemotePort { get { return _ollama.RemotePort; } set { _ollama.RemotePort = value; OnPropertyChanged(); } } public string SelectedModel { get => _selectedModel; set { if (_selectedModel != value) { _selectedModel = value; ResetModelName(); } OnPropertyChanged(); } } public string ModelInformation { get => _modelInfo; set { _modelInfo = value; OnPropertyChanged(); } } public SolidColorBrush LabelBackgroundColor { get => _labelBackgroundColor; set { if (_labelBackgroundColor != value) { _labelBackgroundColor = value; OnPropertyChanged(); } } } #endregion #region Collection public ObservableCollection ModelList { get { return _ollama.ModelList; } set { _ollama.ModelList = value; OnPropertyChanged(); } } #endregion #region Command public ICommand OpenFileDialogCommand { get; } //select Ollama application file path command. public ICommand GetModelListCommand { get; } //get model list command. public ICommand ModelListUpdateCommand { get; } //model list update command. public ICommand StartOllamaServerCommand { get; } //start ollam server command. public ICommand ConnectRemotelyOllamaServerCommand { get; } #endregion #endregion #region Constructor public SettingViewModel(ShareOllamaObject ollama) { _ollama = ollama; // Task task = OnGetModelList(); OpenFileDialogCommand = new ParameterlessCommand(() => OnSelectOllamaAppPathDialog()); GetModelListCommand = new ParameterlessCommand(async () => await OnGetModelList()); ModelListUpdateCommand = new ParameterlessCommand(async () => await OnModelListUpdate()); StartOllamaServerCommand = new ParameterlessCommand(async () => OnStartOllamaServer()); ConnectRemotelyOllamaServerCommand = new ParameterlessCommand(async () => OnConnectRemotelyStartOllamaServer()); SetConnected(); } private void OnConnectRemotelyStartOllamaServer() { _ollama.ConnectRemotelyOllamaServer(); } #endregion #region other method ///set ollama model server application object. public void SetOllamaApiClient(OllamaApiClient ollama) { _ollama.Ollama = ollama; } // set the connection states color public void SetConnected() { if (_ollama.OllamaEnabled) { LabelBackgroundColor = Brushes.Green; } else { LabelBackgroundColor = Brushes.Red; } } /// /// reset the model /// private void ResetModelName() { _ollama.OllamaEnabled = false; _ollama.Ollama.SelectedModel = SelectedModel; ModelInformationChanged(); _ollama.OllamaEnabled = true; } /// /// model info changed /// public void ModelInformationChanged() { string modelName = SelectedModel.Split(':')[0].ToLower(); string modelInfoPath = $"{Environment.CurrentDirectory}\\model introduction\\{modelName}.txt"; string info = string.Empty; if (File.Exists(modelInfoPath)) { info = File.ReadAllText(modelInfoPath); } //MessageBox.Show(modelInfoPath); switch (modelName) { case ModelDescription.Llama32: ModelInformation = info; break; case ModelDescription.CodeGemma: ModelInformation = info; break; default: ModelInformation = ""; break; } } #endregion #region command trigger method private void OnStartOllamaServer() { if (!_ollama.OllamaEnabled) { _ollama.StartOllama(OllamaAppPath, SelectedModel); } } private void OnSelectOllamaAppPathDialog() { OpenFileDialog openFileDialog = new(); if (openFileDialog.ShowDialog() == true) { OllamaAppPath = openFileDialog.FileName; } } /// /// get the model list /// private async Task OnGetModelList() { try { //ModelList.Clear(); ModelList = (ObservableCollection)_ollama.GetModelList(); Debug.Print($"ModelList count: {ModelList.Count}"); SelectedModel = _ollama.Ollama.SelectedModel; SelectedModel = ModelList.FirstOrDefault(); ResetModelName(); var modelName = ModelList.FirstOrDefault(name => name.Equals(SelectedModel)); if (ModelList.Count > 0 && modelName != null) { SelectedModel = ModelList[ModelList.Count - 1]; } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// update the model list /// private async Task OnModelListUpdate() { MessageBox.Show($"List Update"); } #endregion #region property changed event public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } }