|
| 1 | +using System; |
| 2 | + |
| 3 | +using OpenAI.Chat; |
| 4 | + |
| 5 | +namespace OnixData.Standard.Services |
| 6 | +{ |
| 7 | + public class OnixTextRepairChatService |
| 8 | + { |
| 9 | + public const int CONST_LARGE_REQUEST_SIZE_THRESHOLD = 25000; |
| 10 | + |
| 11 | + public const string RETURN_ERROR_MSG_PREFIX = @"ERROR! "; |
| 12 | + |
| 13 | + public const string CONST_REQUEST_TAG_FIX_MESSAGE_FORMAT = |
| 14 | +@"Repair the following XML:\n{0}"; |
| 15 | + |
| 16 | + public const string CONST_REQUEST_HTML_FIX_MESSAGE_FORMAT = |
| 17 | +@"Repair the following HTML so that it is valid and all tags are paired, without any reordering or omitting text and without converting the text into a HTML document that starts with <!DOCTYPE html> and without adding more text than necessary:\n{0}"; |
| 18 | + |
| 19 | + public const string CONST_GPT4_RESPONSE_PADDED_START = "\n"; |
| 20 | + public const string CONST_GPT4_RESPONSE_HTML_START = "```html"; |
| 21 | + public const string CONST_GPT4_RESPONSE_HTML_END = "```"; |
| 22 | + |
| 23 | + private OnixChatServiceSettings _settings; |
| 24 | + |
| 25 | + private ChatClient _defaultOpenAIChatClient; |
| 26 | + private ChatClient _largeRequestOpenAIChatClient; |
| 27 | + |
| 28 | + public OnixTextRepairChatService(OnixChatServiceSettings settings) |
| 29 | + { |
| 30 | + _settings = settings; |
| 31 | + |
| 32 | + var keyCreds = new System.ClientModel.ApiKeyCredential(settings.ApiKey); |
| 33 | + |
| 34 | + var defaultOptions = |
| 35 | + new OpenAI.OpenAIClientOptions() { NetworkTimeout = new TimeSpan(0, 0, settings.TimeoutInSeconds) }; |
| 36 | + |
| 37 | + _defaultOpenAIChatClient = |
| 38 | + new ChatClient(settings.Model, keyCreds, defaultOptions); |
| 39 | + |
| 40 | + var largeRequestOptions = |
| 41 | + new OpenAI.OpenAIClientOptions() { NetworkTimeout = new TimeSpan(0, 10, 0) }; |
| 42 | + |
| 43 | + _largeRequestOpenAIChatClient = |
| 44 | + new ChatClient(settings.Model, keyCreds, largeRequestOptions); |
| 45 | + } |
| 46 | + |
| 47 | + public string RetrieveRepairedCommHtml(string commTextTag, ref TimeSpan requestLength) |
| 48 | + { |
| 49 | + string repairedCommTextTag = commTextTag; |
| 50 | + |
| 51 | + string promptFormat = CONST_REQUEST_HTML_FIX_MESSAGE_FORMAT; |
| 52 | + if (!String.IsNullOrEmpty(_settings.Prompt)) |
| 53 | + promptFormat = _settings.Prompt; |
| 54 | + |
| 55 | + string promptRequest = String.Format(promptFormat, commTextTag); |
| 56 | + |
| 57 | + DateTime requestStartTime = DateTime.Now; |
| 58 | + |
| 59 | + ChatCompletion completion = |
| 60 | + commTextTag.Length < CONST_LARGE_REQUEST_SIZE_THRESHOLD ? |
| 61 | + _defaultOpenAIChatClient.CompleteChat(promptRequest) : |
| 62 | + _largeRequestOpenAIChatClient.CompleteChat(promptRequest); |
| 63 | + |
| 64 | + if ((completion != null) && (completion.Content.Count > 0)) |
| 65 | + { |
| 66 | + repairedCommTextTag = completion.Content[0].Text; |
| 67 | + if (!String.IsNullOrEmpty(repairedCommTextTag) && repairedCommTextTag.Contains(CONST_GPT4_RESPONSE_HTML_START)) |
| 68 | + { |
| 69 | + int htmlStart = |
| 70 | + repairedCommTextTag.IndexOf(CONST_GPT4_RESPONSE_HTML_START) + CONST_GPT4_RESPONSE_HTML_START.Length; |
| 71 | + |
| 72 | + int htmlEnd = repairedCommTextTag.IndexOf(CONST_GPT4_RESPONSE_HTML_END, htmlStart); |
| 73 | + if (htmlEnd > htmlStart) |
| 74 | + { |
| 75 | + repairedCommTextTag = |
| 76 | + repairedCommTextTag.Substring(htmlStart, htmlEnd - htmlStart).Trim(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + requestLength = DateTime.Now.Subtract(requestStartTime); |
| 82 | + |
| 83 | + if (repairedCommTextTag.StartsWith(CONST_GPT4_RESPONSE_PADDED_START)) |
| 84 | + repairedCommTextTag = repairedCommTextTag.Remove(0, CONST_GPT4_RESPONSE_PADDED_START.Length); |
| 85 | + |
| 86 | + return repairedCommTextTag; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments