382 lines
12 KiB
C#
382 lines
12 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace RLHF.Generator.LmStudio;
|
|
|
|
public sealed class LmStudioClient
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly LmStudioOptions _options;
|
|
|
|
public LmStudioClient(HttpClient httpClient, LmStudioOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(httpClient);
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
|
|
ValidateOptions(options);
|
|
|
|
_httpClient = httpClient;
|
|
_options = options;
|
|
}
|
|
|
|
public async Task<string> CompleteAsync(
|
|
string text,
|
|
string? tense,
|
|
bool isFragment,
|
|
string? protectedSubject = null,
|
|
string? protectedMedicalTerm = null,
|
|
IReadOnlyList<string>? allowedMedicalForms = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
throw new ArgumentException("Completion text is required.", nameof(text));
|
|
}
|
|
|
|
if (!SentenceTenses.TryNormalize(tense, out string? normalizedTense))
|
|
{
|
|
throw new ArgumentException(
|
|
"Supported tense values are past, present, and future.",
|
|
nameof(tense));
|
|
}
|
|
|
|
if (protectedSubject is not null && protectedSubject.Trim().Length == 0)
|
|
{
|
|
throw new ArgumentException(
|
|
"Protected subject cannot be empty or whitespace.",
|
|
nameof(protectedSubject));
|
|
}
|
|
|
|
if (protectedMedicalTerm is not null && protectedMedicalTerm.Trim().Length == 0)
|
|
{
|
|
throw new ArgumentException(
|
|
"Protected medical term cannot be empty or whitespace.",
|
|
nameof(protectedMedicalTerm));
|
|
}
|
|
|
|
if (allowedMedicalForms is not null
|
|
&& (allowedMedicalForms.Count == 0
|
|
|| allowedMedicalForms.Any(string.IsNullOrWhiteSpace)))
|
|
{
|
|
throw new ArgumentException(
|
|
"Allowed medical forms cannot be empty or contain empty values.",
|
|
nameof(allowedMedicalForms));
|
|
}
|
|
|
|
string resolvedText = ResolveIndefiniteArticles(text);
|
|
if (isFragment)
|
|
{
|
|
resolvedText = ResolveFragmentAuxiliaries(resolvedText, normalizedTense);
|
|
}
|
|
|
|
bool requiresMedicalFormSelection = allowedMedicalForms?.Count > 1;
|
|
if (!requiresMedicalFormSelection
|
|
&& isFragment
|
|
&& normalizedTense is not null
|
|
&& resolvedText.IndexOfAny(['[', ']']) < 0
|
|
&& !BeginsWithRecognizableVerbPhrase(resolvedText))
|
|
{
|
|
return resolvedText;
|
|
}
|
|
|
|
if (!requiresMedicalFormSelection
|
|
&& isFragment
|
|
&& normalizedTense is null
|
|
&& resolvedText.IndexOfAny(['[', ']']) < 0)
|
|
{
|
|
return resolvedText;
|
|
}
|
|
|
|
List<string> userLines = [$"Is fragment: {isFragment.ToString().ToLowerInvariant()}"];
|
|
if (protectedSubject is not null)
|
|
{
|
|
userLines.Add($"Protected subject: {protectedSubject}");
|
|
}
|
|
|
|
if (protectedMedicalTerm is not null)
|
|
{
|
|
userLines.Add($"Protected medical term: {protectedMedicalTerm}");
|
|
}
|
|
|
|
if (allowedMedicalForms is not null)
|
|
{
|
|
userLines.Add($"Allowed medical forms: {string.Join(" | ", allowedMedicalForms)}");
|
|
}
|
|
|
|
if (normalizedTense is not null)
|
|
{
|
|
userLines.Add($"Requested tense: {normalizedTense}");
|
|
}
|
|
|
|
userLines.Add($"Sentence: {resolvedText}");
|
|
string userContent = string.Join('\n', userLines);
|
|
|
|
var payload = new
|
|
{
|
|
model = _options.ModelName,
|
|
messages = new[]
|
|
{
|
|
new { role = "system", content = _options.SystemPrompt },
|
|
new { role = "user", content = userContent }
|
|
},
|
|
temperature = _options.Temperature,
|
|
top_p = _options.TopP,
|
|
max_tokens = _options.MaxTokens,
|
|
seed = _options.Seed,
|
|
reasoning_effort = "none",
|
|
stream = false
|
|
};
|
|
|
|
using JsonContent requestContent = JsonContent.Create(payload);
|
|
using HttpResponseMessage response = await _httpClient.PostAsync(
|
|
_options.ChatCompletionsUrl,
|
|
requestContent,
|
|
cancellationToken);
|
|
string responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new HttpRequestException(
|
|
$"LM Studio returned HTTP {(int)response.StatusCode} ({response.StatusCode}): {responseBody}",
|
|
null,
|
|
response.StatusCode);
|
|
}
|
|
|
|
LmStudioResponse? completion = JsonSerializer.Deserialize<LmStudioResponse>(responseBody);
|
|
if (completion?.Choices is null || completion.Choices.Count == 0)
|
|
{
|
|
throw new InvalidDataException("LM Studio response contains no choices.");
|
|
}
|
|
|
|
LmStudioMessage? message = completion.Choices[0].Message;
|
|
if (message is null)
|
|
{
|
|
throw new InvalidDataException("LM Studio response choice contains no message.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(message.Content))
|
|
{
|
|
throw new InvalidDataException("LM Studio response message contains no content.");
|
|
}
|
|
|
|
return message.Content.Trim();
|
|
}
|
|
|
|
private static bool BeginsWithRecognizableVerbPhrase(string text)
|
|
{
|
|
int wordOffset = 0;
|
|
while (wordOffset < text.Length && char.IsWhiteSpace(text[wordOffset]))
|
|
{
|
|
wordOffset++;
|
|
}
|
|
|
|
while (wordOffset < text.Length && IsOpeningPunctuation(text[wordOffset]))
|
|
{
|
|
wordOffset++;
|
|
while (wordOffset < text.Length && char.IsWhiteSpace(text[wordOffset]))
|
|
{
|
|
wordOffset++;
|
|
}
|
|
}
|
|
|
|
int wordEnd = wordOffset;
|
|
while (wordEnd < text.Length && char.IsLetter(text[wordEnd]))
|
|
{
|
|
wordEnd++;
|
|
}
|
|
|
|
if (wordEnd == wordOffset)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string firstWord = text[wordOffset..wordEnd];
|
|
return firstWord.EndsWith("ing", StringComparison.OrdinalIgnoreCase)
|
|
|| FragmentVerbWords.Contains(firstWord, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static readonly string[] FragmentVerbWords =
|
|
[
|
|
"am", "is", "are", "was", "were", "be", "been", "being",
|
|
"has", "have", "had", "do", "does", "did", "can", "could",
|
|
"may", "might", "must", "shall", "should", "will", "would"
|
|
];
|
|
|
|
private static string ResolveFragmentAuxiliaries(string text, string? normalizedTense)
|
|
{
|
|
string beReplacement = normalizedTense switch
|
|
{
|
|
SentenceTenses.Past => "was",
|
|
SentenceTenses.Future => "will be",
|
|
_ => "is"
|
|
};
|
|
string haveReplacement = normalizedTense switch
|
|
{
|
|
SentenceTenses.Past => "had",
|
|
SentenceTenses.Future => "will have",
|
|
_ => "has"
|
|
};
|
|
|
|
return text
|
|
.Replace("[is|are|was|were]", beReplacement, StringComparison.Ordinal)
|
|
.Replace("[has|have|had]", haveReplacement, StringComparison.Ordinal);
|
|
}
|
|
|
|
private static string ResolveIndefiniteArticles(string text)
|
|
{
|
|
const string articleExpression = "[a|an]";
|
|
int occurrenceOffset = text.IndexOf(articleExpression, StringComparison.Ordinal);
|
|
if (occurrenceOffset < 0)
|
|
{
|
|
return text;
|
|
}
|
|
|
|
StringBuilder result = new(text.Length);
|
|
int sourceOffset = 0;
|
|
while (occurrenceOffset >= 0)
|
|
{
|
|
int followingOffset = occurrenceOffset + articleExpression.Length;
|
|
result.Append(text, sourceOffset, occurrenceOffset - sourceOffset);
|
|
result.Append(SelectIndefiniteArticle(text, followingOffset));
|
|
sourceOffset = followingOffset;
|
|
occurrenceOffset = text.IndexOf(
|
|
articleExpression,
|
|
sourceOffset,
|
|
StringComparison.Ordinal);
|
|
}
|
|
|
|
result.Append(text, sourceOffset, text.Length - sourceOffset);
|
|
return result.ToString();
|
|
}
|
|
|
|
private static string SelectIndefiniteArticle(string text, int followingOffset)
|
|
{
|
|
while (followingOffset < text.Length && char.IsWhiteSpace(text[followingOffset]))
|
|
{
|
|
followingOffset++;
|
|
}
|
|
|
|
while (followingOffset < text.Length && IsOpeningPunctuation(text[followingOffset]))
|
|
{
|
|
followingOffset++;
|
|
while (followingOffset < text.Length && char.IsWhiteSpace(text[followingOffset]))
|
|
{
|
|
followingOffset++;
|
|
}
|
|
}
|
|
|
|
if (followingOffset >= text.Length || !char.IsLetterOrDigit(text[followingOffset]))
|
|
{
|
|
throw new InvalidDataException(
|
|
"An exact [a|an] expression must be followed by a spoken token.");
|
|
}
|
|
|
|
int tokenEnd = followingOffset;
|
|
while (tokenEnd < text.Length
|
|
&& (char.IsLetterOrDigit(text[tokenEnd]) || text[tokenEnd] is '-' or '\''))
|
|
{
|
|
tokenEnd++;
|
|
}
|
|
|
|
string token = text[followingOffset..tokenEnd];
|
|
if (AppearsToBeginWithLetterName(token))
|
|
{
|
|
return "AEFHILMNORSX".Contains(token[0], StringComparison.Ordinal) ? "an" : "a";
|
|
}
|
|
|
|
if (StartsWithAny(token, ["heir", "honest", "honor", "hour"]))
|
|
{
|
|
return "an";
|
|
}
|
|
|
|
if (StartsWithAny(
|
|
token,
|
|
["one", "once", "euro", "uni", "unique", "unit", "united", "university", "unicorn", "use", "user", "usual", "utility"]))
|
|
{
|
|
return "a";
|
|
}
|
|
|
|
char firstAlphabeticCharacter = token.First(char.IsLetter);
|
|
return "aeiou".Contains(firstAlphabeticCharacter, StringComparison.OrdinalIgnoreCase)
|
|
? "an"
|
|
: "a";
|
|
}
|
|
|
|
private static bool IsOpeningPunctuation(char character) =>
|
|
character is '"' or '\'' or '(' or '[' or '{' or '<';
|
|
|
|
private static bool AppearsToBeginWithLetterName(string token) =>
|
|
char.IsUpper(token[0])
|
|
&& (token.Length == 1
|
|
|| char.IsUpper(token[1])
|
|
|| char.IsDigit(token[1])
|
|
|| token[1] == '-');
|
|
|
|
private static bool StartsWithAny(string token, IReadOnlyList<string> prefixes) =>
|
|
prefixes.Any(prefix => token.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
|
|
|
|
private static void ValidateOptions(LmStudioOptions options)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(options.ChatCompletionsUrl))
|
|
{
|
|
throw new InvalidOperationException("LmStudio ChatCompletionsUrl is required.");
|
|
}
|
|
|
|
if (!Uri.TryCreate(options.ChatCompletionsUrl, UriKind.Absolute, out Uri? uri)
|
|
|| (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"LmStudio ChatCompletionsUrl must be an absolute HTTP or HTTPS URI.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(options.ModelName))
|
|
{
|
|
throw new InvalidOperationException("LmStudio ModelName is required.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(options.SystemPrompt))
|
|
{
|
|
throw new InvalidOperationException("LmStudio SystemPrompt is required.");
|
|
}
|
|
|
|
if (options.Temperature < 0)
|
|
{
|
|
throw new InvalidOperationException("LmStudio Temperature must be greater than or equal to zero.");
|
|
}
|
|
|
|
if (options.TopP <= 0 || options.TopP > 1)
|
|
{
|
|
throw new InvalidOperationException("LmStudio TopP must be greater than zero and at most one.");
|
|
}
|
|
|
|
if (options.MaxTokens <= 0)
|
|
{
|
|
throw new InvalidOperationException("LmStudio MaxTokens must be greater than zero.");
|
|
}
|
|
|
|
if (options.TimeoutSeconds <= 0)
|
|
{
|
|
throw new InvalidOperationException("LmStudio TimeoutSeconds must be greater than zero.");
|
|
}
|
|
}
|
|
|
|
private sealed class LmStudioResponse
|
|
{
|
|
[JsonPropertyName("choices")]
|
|
public List<LmStudioChoice>? Choices { get; init; }
|
|
}
|
|
|
|
private sealed class LmStudioChoice
|
|
{
|
|
[JsonPropertyName("message")]
|
|
public LmStudioMessage? Message { get; init; }
|
|
}
|
|
|
|
private sealed class LmStudioMessage
|
|
{
|
|
[JsonPropertyName("content")]
|
|
public string? Content { get; init; }
|
|
}
|
|
} |