Skip to content

Commit

Permalink
Merge pull request #32 from tghamm/feature/3.2.1
Browse files Browse the repository at this point in the history
Ensures Role is returned as Enum #31
  • Loading branch information
tghamm committed Apr 25, 2024
2 parents 0aebb16 + 758e52b commit acfaa76
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Anthropic.SDK/Anthropic.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<PackageTags>Claude, AI, ML, API, Anthropic</PackageTags>
<Title>Claude API</Title>
<PackageReleaseNotes>
Simpler use of Messages and Responses, cleaner function calls, and more robust error handling.
RoleType for MessageResponse and StreamMessage.
</PackageReleaseNotes>
<PackageId>Anthropic.SDK</PackageId>
<Version>3.2.0</Version>
<AssemblyVersion>3.2.0.0</AssemblyVersion>
<FileVersion>3.2.0.0</FileVersion>
<Version>3.2.1</Version>
<AssemblyVersion>3.2.1.0</AssemblyVersion>
<FileVersion>3.2.1.0</FileVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
Expand Down
2 changes: 1 addition & 1 deletion Anthropic.SDK/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected async Task<T> HttpRequestMessages<T>(string url = null, HttpMethod ver
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new ContentConverter(), new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
Converters = { new ContentConverter() }
};
string jsonContent = JsonSerializer.Serialize(postData, options);
var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");
Expand Down
34 changes: 34 additions & 0 deletions Anthropic.SDK/Extensions/RoleTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Anthropic.SDK.Messaging;

namespace Anthropic.SDK.Extensions
{
public class RoleTypeConverter : JsonConverter<RoleType>
{
public override RoleType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string value = reader.GetString();
return value switch
{
"user" => RoleType.User,
"assistant" => RoleType.Assistant,
_ => throw new JsonException($"Unknown role type: {value}")
};
}

public override void Write(Utf8JsonWriter writer, RoleType value, JsonSerializerOptions options)
{
string roleString = value switch
{
RoleType.User => "user",
RoleType.Assistant => "assistant",
_ => throw new InvalidOperationException("Invalid role type")
};
writer.WriteStringValue(roleString);
}
}
}
2 changes: 2 additions & 0 deletions Anthropic.SDK/Messaging/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.Json.Serialization;
using Anthropic.SDK.Common;
using Anthropic.SDK.Extensions;

namespace Anthropic.SDK.Messaging
{
Expand Down Expand Up @@ -39,6 +40,7 @@ public Message(Function toolCall, string functionResult, bool isError = false)
/// Accepts <see cref="RoleType.User"/> or <see cref="RoleType.Assistant"/>
/// </summary>
[JsonPropertyName("role")]
[JsonConverter(typeof(RoleTypeConverter))]
public RoleType Role { get; set; }

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions Anthropic.SDK/Messaging/MessageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using Anthropic.SDK.Extensions;

namespace Anthropic.SDK.Messaging
{
Expand All @@ -19,7 +20,8 @@ public class MessageResponse
public string Model { get; set; }

[JsonPropertyName("role")]
public string Role { get; set; }
[JsonConverter(typeof(RoleTypeConverter))]
public RoleType Role { get; set; }

[JsonPropertyName("stop_reason")]
public string StopReason { get; set; }
Expand Down Expand Up @@ -58,7 +60,8 @@ public class StreamMessage
public string Type { get; set; }

[JsonPropertyName("role")]
public string Role { get; set; }
[JsonConverter(typeof(RoleTypeConverter))]
public RoleType Role { get; set; }

[JsonPropertyName("content")]
public List<object> Content { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions Anthropic.SDK/Messaging/RoleType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Anthropic.SDK.Messaging
{
public enum RoleType
{
[EnumMember(Value = "user")]
User,
[EnumMember(Value = "assistant")]
Assistant
}
}

0 comments on commit acfaa76

Please sign in to comment.