Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/typechat/ConstraintsValidator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel.DataAnnotations;

namespace Microsoft.TypeChat;

public interface IConstraintsValidator<T>
Expand Down Expand Up @@ -34,7 +36,7 @@ string ToErrorString(List<ValidationResult> validationResults)
foreach (var result in validationResults)
{
sb.AppendLine("Errors in the following: ");
sb.AppendLine(string.Join(',', result.MemberNames));
sb.AppendLine(string.Join(",", result.MemberNames));
sb.AppendLine(result.ErrorMessage);
}
return sb.ToString();
Expand Down
2 changes: 1 addition & 1 deletion src/typechat/JsonResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public JsonResponse(string responseText)
{
if (HasPrologue || HasEpilogue)
{
return string.Join('\n', Prologue, Epilogue);
return string.Join("\n", Prologue, Epilogue);
}
return null;
}
Expand Down
6 changes: 5 additions & 1 deletion src/typechat/JsonSerializerTypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public JsonSerializerTypeValidator(SchemaText schema, JsonSerializerOptions? opt

public JsonSerializerTypeValidator(TypeSchema schema, JsonSerializerOptions? options = null)
{
ArgumentNullException.ThrowIfNull(schema, nameof(schema));
if (schema == null)
{
throw new ArgumentNullException(nameof(schema));
}

_schema = schema;
if (options != null)
{
Expand Down
20 changes: 16 additions & 4 deletions src/typechat/JsonTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ public JsonTranslator(
IJsonTypeValidator<T> validator,
IJsonTranslatorPrompts? prompts = null)
{
ArgumentNullException.ThrowIfNull(model, nameof(model));
ArgumentNullException.ThrowIfNull(validator, nameof(validator));
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (validator == null)
{
throw new ArgumentNullException(nameof(validator));
}

_model = model;

Expand All @@ -77,7 +83,10 @@ public IJsonTypeValidator<T> Validator
get => _validator;
set
{
ArgumentNullException.ThrowIfNull(value, nameof(Validator));
if (value == null)
{
throw new ArgumentNullException(nameof(Validator));
}
_validator = value;
}
}
Expand Down Expand Up @@ -163,7 +172,10 @@ public async Task<T> TranslateAsync(
CancellationToken cancelToken = default
)
{
ArgumentNullException.ThrowIfNull(request);
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}

requestSettings ??= _translationSettings;
Prompt prompt = CreateRequestPrompt(request, preamble);
Expand Down
5 changes: 4 additions & 1 deletion src/typechat/JsonTranslatorPrompts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public virtual string CreateRepairPrompt(TypeSchema schema, string json, string

public static Prompt RequestPrompt(string typeName, string schema, Prompt request, IList<IPromptSection>? context = null)
{
ArgumentNullException.ThrowIfNull(request, nameof(request));
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
Prompt prompt = new Prompt();

prompt += IntroSection(typeName, schema);
Expand Down
6 changes: 5 additions & 1 deletion src/typechat/ModelInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public class ModelInfo
[JsonConstructor]
public ModelInfo(string name, int maxTokens, double tokenToCharMultiple = 2.5)
{
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Name cannot be null or empty.", nameof(name));
}

_name = name;
_maxTokens = maxTokens;
_tokenToCharMultiple = tokenToCharMultiple;
Expand Down
5 changes: 4 additions & 1 deletion src/typechat/Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public Prompt(IEnumerable<IPromptSection>? preamble, PromptSection? text = null,

public new void Add(IPromptSection section)
{
ArgumentNullException.ThrowIfNull(section, nameof(section));
if (section == null)
{
throw new ArgumentNullException(nameof(section));
}
base.Add(section);
}

Expand Down
10 changes: 8 additions & 2 deletions src/typechat/PromptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public bool Add(string text)
/// <returns>true if added, false if not</returns>
public bool Add(IPromptSection section)
{
ArgumentNullException.ThrowIfNull(section, nameof(section));
if (section == null)
{
throw new ArgumentNullException(nameof(section));
}

string text = section.GetText();
if (string.IsNullOrEmpty(text))
Expand All @@ -89,7 +92,10 @@ public bool Add(IPromptSection section)

public bool AddRange(IEnumerable<IPromptSection> sections)
{
ArgumentNullException.ThrowIfNull(sections, nameof(sections));
if (sections == null)
{
throw new ArgumentNullException(nameof(sections));
}
foreach (var section in sections)
{
if (!Add(section))
Expand Down
17 changes: 14 additions & 3 deletions src/typechat/PromptSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public PromptSection(string text)

public PromptSection(string source, string text)
{
ArgumentException.ThrowIfNullOrEmpty(source, nameof(source));
ArgumentNullException.ThrowIfNull(text, nameof(text));
if (string.IsNullOrEmpty(source))
{
throw new ArgumentException("Source cannot be null or empty.", nameof(source));
}

if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

_source = source;
SetText(text);
}
Expand All @@ -40,7 +48,10 @@ public PromptSection(string source, string text)

public void SetText(string text)
{
ArgumentNullException.ThrowIfNull(text, nameof(text));
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
_text = text;
}

Expand Down
12 changes: 10 additions & 2 deletions src/typechat/SchemaText.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json.Serialization;

namespace Microsoft.TypeChat;

public struct SchemaText
Expand All @@ -20,8 +22,14 @@ public SchemaText(string text)
[JsonConstructor]
public SchemaText(string text, string lang)
{
ArgumentException.ThrowIfNullOrEmpty(text, nameof(text));
ArgumentException.ThrowIfNullOrEmpty(lang, nameof(lang));
if (string.IsNullOrEmpty(text))
{
throw new ArgumentException("Text cannot be null or empty.", nameof(text));
}
if (string.IsNullOrEmpty(lang))
{
throw new ArgumentException("Lanauge cannot be null or empty.", nameof(lang));
}

_lang = lang;
_text = text;
Expand Down
9 changes: 8 additions & 1 deletion src/typechat/TypeChat.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable>
<AssemblyName>TypeChat</AssemblyName>
<RootNamespace>Microsoft.TypeChat</RootNamespace>
<LangVersion>10</LangVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

<Import Project="Package/nuget.props" />

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion src/typechat/TypeSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public class TypeSchema

public TypeSchema(Type type, string schemaText)
{
ArgumentNullException.ThrowIfNull(type, nameof(type));
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
_schema = new SchemaText(schemaText);
_type = type;
}
Expand Down