From 9ec3d84003c91ce754924c6d0afd4e3d65d6d266 Mon Sep 17 00:00:00 2001 From: Jan Eisenhuth <47415874+Eisenhuth@users.noreply.github.com> Date: Fri, 24 Mar 2023 01:38:15 +0100 Subject: [PATCH] added more options, split very long responses - added option to change the `max_tokens` - added option to show additional info - responses are now broken up into chunks to avoid them getting cut off. they can still get cut off due to the token limit - will probably fix that in the future --- dalamud-chatgpt/ChatGPTPlugin.cs | 31 ++++++++++++++++++++------ dalamud-chatgpt/Configuration.cs | 2 ++ dalamud-chatgpt/dalamud-chatgpt.csproj | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/dalamud-chatgpt/ChatGPTPlugin.cs b/dalamud-chatgpt/ChatGPTPlugin.cs index aefee51..9b5735e 100644 --- a/dalamud-chatgpt/ChatGPTPlugin.cs +++ b/dalamud-chatgpt/ChatGPTPlugin.cs @@ -1,6 +1,8 @@ -using System.Net.Http; +using System.Linq; +using System.Net.Http; using System.Net.Http.Headers; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Dalamud.Game.Command; using Dalamud.Game.Gui; @@ -25,7 +27,9 @@ public class ChatGPTPlugin : IDalamudPlugin [PluginService] private static CommandManager CommandManager { get; set; } = null!; private string configKey; + private int configMaxTokens; private bool configLineBreaks; + private bool configAdditionalInfo; public ChatGPTPlugin([RequiredVersion("1.0")] DalamudPluginInterface dalamudPluginInterface, [RequiredVersion("1.0")] ChatGui chatGui, [RequiredVersion("1.0")] CommandManager commandManager) { @@ -68,7 +72,7 @@ private async Task SendPrompt(string input) var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", configuration.ApiKey); - var requestBody = $"{{\"model\": \"{Configuration.Model}\", \"prompt\": \"{input}\", \"max_tokens\": 256}}"; + var requestBody = $"{{\"model\": \"{Configuration.Model}\", \"prompt\": \"{input}\", \"max_tokens\": {configuration.MaxTokens}}}"; var content = new StringContent(requestBody, Encoding.UTF8, "application/json"); var response = await client.PostAsync(Configuration.Endpoint, content); @@ -81,8 +85,17 @@ private async Task SendPrompt(string input) { if(configLineBreaks) text = text.Replace("\r", "").Replace("\n", ""); - - chatGui.Print($"ChatGPT prompt: {input}{text}"); + + const int chunkSize = 1000; + var regex = new Regex(@".{1," + chunkSize + @"}(\s+|$)"); //jesus take the wheel + var chunks = regex.Matches(text).Select(match => match.Value); + chunks = chunks.ToList(); + + if(configAdditionalInfo) + chatGui.Print($"ChatGPT \nprompt: {input}\nmodel: {Configuration.Model}\nmax_tokens: {configMaxTokens}\nresponse length: {text.Length}\nchunks: {chunks.Count()}"); + + foreach (var chunk in chunks) + chatGui.Print($"ChatGPT: {chunk}"); } } @@ -96,7 +109,9 @@ private void DrawConfiguration() ImGui.Begin($"{Name} Configuration", ref drawConfiguration); ImGui.Separator(); - + ImGui.Checkbox("remove line breaks from responses", ref configLineBreaks); + ImGui.Checkbox("show additional info", ref configAdditionalInfo); + ImGui.InputInt("max_tokens", ref configMaxTokens); ImGui.InputText("API Key", ref configKey, 60, ImGuiInputTextFlags.Password); if (ImGui.Button("Get API Key")) @@ -105,8 +120,6 @@ private void DrawConfiguration() Util.OpenLink(apiKeysUrl); } - ImGui.Checkbox("remove line breaks from responses", ref configLineBreaks); - ImGui.Separator(); @@ -129,13 +142,17 @@ private static void OpenConfig() private void LoadConfiguration() { configKey = configuration.ApiKey; + configMaxTokens = configuration.MaxTokens != 0 ? configuration.MaxTokens : 256; configLineBreaks = configuration.RemoveLineBreaks; + configAdditionalInfo = configuration.ShowAdditionalInfo; } private void SaveConfiguration() { configuration.ApiKey = configKey; + configuration.MaxTokens = configMaxTokens; configuration.RemoveLineBreaks = configLineBreaks; + configuration.ShowAdditionalInfo = configAdditionalInfo; PluginInterface.SavePluginConfig(configuration); } diff --git a/dalamud-chatgpt/Configuration.cs b/dalamud-chatgpt/Configuration.cs index 18bd005..fda00b5 100644 --- a/dalamud-chatgpt/Configuration.cs +++ b/dalamud-chatgpt/Configuration.cs @@ -7,7 +7,9 @@ public class Configuration : IPluginConfiguration { public int Version { get; set; } public bool RemoveLineBreaks { get; set; } = false; + public bool ShowAdditionalInfo { get; set; } public string ApiKey { get; set; } = ""; + public int MaxTokens { get; set; } public const string Endpoint = "https://api.openai.com/v1/completions"; public const string Model = "text-davinci-003"; diff --git a/dalamud-chatgpt/dalamud-chatgpt.csproj b/dalamud-chatgpt/dalamud-chatgpt.csproj index 895a2f5..a329fd5 100644 --- a/dalamud-chatgpt/dalamud-chatgpt.csproj +++ b/dalamud-chatgpt/dalamud-chatgpt.csproj @@ -10,7 +10,7 @@ false $(AppData)\Eisenhuth\DalamudDevPlugins\ChatGPTPlugin\ false - 1.0.3.0 + 1.0.4.0 true true https://github.com/Eisenhuth/dalamud-chatgpt