Skip to content

Commit

Permalink
Integrate IntelliChatModule into application startup
Browse files Browse the repository at this point in the history
In this commit, the IntelliChatModule was integrated into the application startup sequence in `App.xaml.cs`. In `IntelliChatModule.cs`, JSON serialization and file I/O operations were added, along with a new class `IntelliChatModuleSettings` to store module settings. Two new classes `SupportedIntelliChatLanguage` and `IntelliChatWritingStyle` were added to represent supported languages and writing styles. The `IntelliChatModule` class was expanded to include a static instance of `IntelliChatModuleSettings`, methods for loading and saving settings, and methods for initializing default settings. The `PerformSpellingAndGrammarCheckAsync` and `PerformBeautifySentenceAsync` methods were updated to use the new settings structure. In `ViewModel.cs`, a new public property `IntelliChatModule` was added. The `SupportedIntelliChatLanguage` and `IntelliChatWritingStyle` enums were removed from `IntelliChatModule.cs` as they were replaced by the new classes.
  • Loading branch information
BoiHanny committed Feb 24, 2024
1 parent fae1627 commit f6f65c8
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 48 deletions.
3 changes: 3 additions & 0 deletions vrcosc-magicchatbox/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ protected override async void OnStartup(StartupEventArgs e)
loadingWindow.UpdateProgress("Initializing OpenAI like a rocket launch. 3... 2... 1... Blast off!", 70);
await Task.Run(() => OpenAIModule.Instance.InitializeClient(ViewModel.Instance.OpenAIAccessToken, ViewModel.Instance.OpenAIOrganizationID));

loadingWindow.UpdateProgress("Initializing IntelliSense like a psychic. What's on your mind?", 72);
await Task.Run(() => ViewModel.Instance.IntelliChatModule = new IntelliChatModule());

loadingWindow.UpdateProgress("Warming up the TTS voices. Ready for the vocal Olympics!", 75);
ViewModel.Instance.TikTokTTSVoices = await Task.Run(() => DataController.ReadTkTkTTSVoices());

Expand Down
237 changes: 189 additions & 48 deletions vrcosc-magicchatbox/Classes/Modules/IntelliChatModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,205 @@
using System.Text.Json;
using vrcosc_magicchatbox.ViewModels;
using System.Windows.Threading;
using Newtonsoft.Json;
using System.IO;

namespace vrcosc_magicchatbox.Classes.Modules
{
public class IntelliChatModuleSettings
{
public List<SupportedIntelliChatLanguage> SupportedLanguages { get; set; } = new List<SupportedIntelliChatLanguage>();
public List<IntelliChatWritingStyle> SupportedWritingStyles { get; set; } = new List<IntelliChatWritingStyle>();
public bool IntelliChatPerformModeration { get; set; } = true;
public bool IntelliChatRequesting { get; set; } = false;
public bool IntelliChatWaitingToAccept { get; set; } = false;
public string IntelliChatTxt { get; set; } = string.Empty;
public bool IntelliChatError { get; set; } = false;
public string IntelliChatErrorTxt { get; set; } = string.Empty;

// New fields

public bool AutolanguageSelection { get; set; } = true;
public List<int> SelectedSupportedLanguageIDs { get; set; } = new List<int>();
public int SelectedTranslateLanguageID { get; set; }
public int SelectedWritingStyleID { get; set; }
}




public class SupportedIntelliChatLanguage
{
public int ID { get; set; }
public string LanguageName { get; set; }
public string LanguageDescription { get; set; }
public bool IsBuiltIn { get; set; } = false;
public bool IsActivated { get; set; }
}

public class IntelliChatWritingStyle
{
public int ID { get; set; } // Unique identifier for each writing style
public string StyleName { get; set; }
public string StyleDescription { get; set; }
public bool IsBuiltIn { get; set; } = false;
public bool IsActivated { get; set; }
public double Temperature { get; set; } = 0.7;
}

public class IntelliChatModule
{
private const string IntelliChatSettingsFileName = "IntelliChatSettings.json";
public static IntelliChatModuleSettings Settings = new IntelliChatModuleSettings();
private static bool _isInitialized = false;

public static IntelliChatWritingStyle SelectedWritingStyle
{
get
{
return Settings.SupportedWritingStyles.FirstOrDefault(style => style.ID == Settings.SelectedWritingStyleID);
}
}

public static SupportedIntelliChatLanguage SelectedTranslateLanguage
{
get
{
return Settings.SupportedLanguages.FirstOrDefault(lang => lang.ID == Settings.SelectedTranslateLanguageID);
}
}

public static List<SupportedIntelliChatLanguage> SelectedSupportedLanguages
{
get
{
return Settings.SupportedLanguages.Where(lang => Settings.SelectedSupportedLanguageIDs.Contains(lang.ID)).ToList();
}
}


static IntelliChatModule()
{
Initialize();
}

private static void Initialize()
{
if (_isInitialized) return;

LoadSettings();
_isInitialized = true;
}

private static void InitializeDefaultSettings()
{
InitializeDefaultLanguageSettings();
InitializeDefaultWritingStyleSettings();

SaveSettings();
}

public static void LoadSettings()
{
var filePath = Path.Combine(ViewModel.Instance.DataPath, IntelliChatSettingsFileName);
if (File.Exists(filePath))
{
var jsonData = File.ReadAllText(filePath);
Settings = JsonConvert.DeserializeObject<IntelliChatModuleSettings>(jsonData) ?? new IntelliChatModuleSettings();
}
else
{
InitializeDefaultSettings();
}
}

public static void SaveSettings()
{
var filePath = Path.Combine(ViewModel.Instance.DataPath, IntelliChatSettingsFileName);
var jsonData = JsonConvert.SerializeObject(Settings, Formatting.Indented);
File.WriteAllText(filePath, jsonData);
}



private static void InitializeDefaultLanguageSettings()
{
var defaultLanguages = new List<SupportedIntelliChatLanguage>
{
new SupportedIntelliChatLanguage { LanguageName = "English", IsBuiltIn = true, IsActivated = true },
new SupportedIntelliChatLanguage { LanguageName = "Spanish", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "French", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "German", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Chinese", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Japanese", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Russian", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Portuguese", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Italian", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Dutch", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Arabic", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Turkish", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Korean", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Hindi", IsBuiltIn = true, IsActivated = false },
new SupportedIntelliChatLanguage { LanguageName = "Swedish", IsBuiltIn = true, IsActivated = false },
};

Settings.SupportedLanguages = defaultLanguages;
}

private static void InitializeDefaultWritingStyleSettings()
{
var defaultStyles = new List<IntelliChatWritingStyle>
{
new IntelliChatWritingStyle { StyleName = "Casual", StyleDescription = "A casual, everyday writing style", IsBuiltIn = true, IsActivated = true },
new IntelliChatWritingStyle { StyleName = "Formal", StyleDescription = "A formal, professional writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Friendly", StyleDescription = "A friendly, approachable writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Professional", StyleDescription = "A professional, business-oriented writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Academic", StyleDescription = "An academic, scholarly writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Creative", StyleDescription = "A creative, imaginative writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Humorous", StyleDescription = "A humorous, funny writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "British", StyleDescription = "A British, UK-specific writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Sarcastic", StyleDescription = "A sarcastic, witty writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Romantic", StyleDescription = "A romantic, lovey-dovey writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Action-Packed", StyleDescription = "An action-packed, adrenaline-fueled writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Mysterious", StyleDescription = "A mysterious, suspenseful writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Sci-Fi", StyleDescription = "A futuristic, sci-fi writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Horror", StyleDescription = "A chilling, horror writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Western", StyleDescription = "A wild west, cowboy writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Fantasy", StyleDescription = "A magical, fantasy writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Superhero", StyleDescription = "A heroic, superhero writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Film Noir", StyleDescription = "A dark, film noir writing style", IsBuiltIn = true, IsActivated = false },
new IntelliChatWritingStyle { StyleName = "Comedy", StyleDescription = "A hilarious, comedy writing style", IsBuiltIn = true, IsActivated = false },
};

Settings.SupportedWritingStyles = defaultStyles;

}







public static async Task<string> PerformSpellingAndGrammarCheckAsync(
string text,
List<SupportedIntelliChatLanguage> languages = null)
{
if(!OpenAIModule.Instance.IsInitialized)
if (!OpenAIModule.Instance.IsInitialized)
{
ViewModel.Instance.ActivateSetting("Settings_OpenAI");
return string.Empty;
}
if(string.IsNullOrWhiteSpace(text))
if (string.IsNullOrWhiteSpace(text))
{
ViewModel.Instance.IntelliChatRequesting = false;
return string.Empty;
}

if(ViewModel.Instance.IntelliChatPerformModeration)
if (ViewModel.Instance.IntelliChatPerformModeration)
{
bool moderationResponse = await PerformModerationCheckAsync(text);
if(moderationResponse)
if (moderationResponse)
return string.Empty;
}

Expand All @@ -44,7 +219,7 @@ public static async Task<string> PerformSpellingAndGrammarCheckAsync(
"Please detect and correct and return any spelling and grammar errors in the following text:")
};

if(languages != null && languages.Any() && !ViewModel.Instance.IntelliChatAutoLang)
if (languages != null && languages.Any() && !ViewModel.Instance.IntelliChatAutoLang)
{
messages.Add(new Message(Role.System, $"Consider these languages: {string.Join(", ", languages)}"));
}
Expand All @@ -55,7 +230,7 @@ public static async Task<string> PerformSpellingAndGrammarCheckAsync(
.GetCompletionAsync(new ChatRequest(messages: messages, maxTokens: 120));

// Check the type of response.Content and convert to string accordingly
if(response?.Choices?[0].Message.Content.ValueKind == JsonValueKind.String)
if (response?.Choices?[0].Message.Content.ValueKind == JsonValueKind.String)
{
return response.Choices[0].Message.Content.GetString();
} else
Expand All @@ -66,17 +241,14 @@ public static async Task<string> PerformSpellingAndGrammarCheckAsync(
}


public static async Task<string> PerformBeautifySentenceAsync(
string text,
IntelliChatWritingStyle writingStyle = IntelliChatWritingStyle.Casual,
List<SupportedIntelliChatLanguage> languages = null)
public static async Task<string> PerformBeautifySentenceAsync(string text)
{
if(!OpenAIModule.Instance.IsInitialized)
if (!OpenAIModule.Instance.IsInitialized)
{
ViewModel.Instance.ActivateSetting("Settings_OpenAI");
return string.Empty;
}
if(string.IsNullOrWhiteSpace(text))
if (string.IsNullOrWhiteSpace(text))
{
ViewModel.Instance.IntelliChatRequesting = false;
return string.Empty;
Expand All @@ -91,20 +263,20 @@ public static async Task<string> PerformBeautifySentenceAsync(

var messages = new List<Message>
{
new Message(Role.System, $"Please rewrite the following sentence in a {writingStyle} style:")
new Message(Role.System, $"Please rewrite the following sentence in {SelectedWritingStyle} style:")
};

if(languages != null && languages.Any() && !ViewModel.Instance.IntelliChatAutoLang)
if (!Settings.AutolanguageSelection && Settings.SelectedSupportedLanguageIDs.Count > 0)
{
messages.Add(new Message(Role.System, $"Consider these languages: {string.Join(", ", languages)}"));
messages.Add(new Message(Role.System, $"Consider these languages: {string.Join(", ", SelectedSupportedLanguages)}"));
}

messages.Add(new Message(Role.User, text));

var response = await OpenAIModule.Instance.OpenAIClient.ChatEndpoint
.GetCompletionAsync(new ChatRequest(messages: messages, maxTokens: 120));
.GetCompletionAsync(new ChatRequest(messages: messages, maxTokens: 120,temperature: SelectedWritingStyle.Temperature));

if(response?.Choices?[0].Message.Content.ValueKind == JsonValueKind.String)
if (response?.Choices?[0].Message.Content.ValueKind == JsonValueKind.String)
{
return response.Choices[0].Message.Content.GetString();
} else
Expand Down Expand Up @@ -263,37 +435,6 @@ public static async Task<bool> PerformModerationCheckAsync(string checkString)



}


public enum SupportedIntelliChatLanguage
{
English,
Spanish,
French,
German,
Chinese,
Japanese,
Russian,
Portuguese,
Italian,
Dutch,
Arabic,
Turkish,
Korean,
Hindi,
Swedish,
}

public enum IntelliChatWritingStyle
{
Casual,
Formal,
Friendly,
Professional,
Academic,
Creative,
Humorous,
British,
}
}
2 changes: 2 additions & 0 deletions vrcosc-magicchatbox/ViewModels/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public int MainWindowBlurEffect

private PulsoidModule _heartRateConnector;

public IntelliChatModule IntelliChatModule;


private bool _HeartRateTitle = false;

Expand Down

0 comments on commit f6f65c8

Please sign in to comment.