Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures Role is returned as Enum #31 #32

Merged
merged 1 commit into from
Apr 25, 2024
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
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 @@ private async Task<HttpResponseMessage> HttpRequestRaw(string url = null, HttpMe
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
}
}
Loading