Skip to content

Commit

Permalink
Code Cleanup and some renamings. Readme Update
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhantolga committed Jul 18, 2024
1 parent 7faffde commit 8871510
Show file tree
Hide file tree
Showing 28 changed files with 139 additions and 67 deletions.
3 changes: 0 additions & 3 deletions Anthropic.Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Anthropic.Extensions;
using Anthropic.ObjectModels.RequestModels;
using Anthropic.Playground.TestHelpers;
using Anthropic.Services;
using LaserCatEyes.HttpClientListener;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;


var builder = new ConfigurationBuilder().AddUserSecrets<Program>();

IConfiguration configuration = builder.Build();
Expand All @@ -18,7 +16,6 @@
// It is in Beta version, if you don't want to use it just comment out below line.
serviceCollection.AddLaserCatEyesHttpClientListener();

//if you want to use beta services you have to set UseBeta to true. Otherwise, it will use the stable version of Anthropic apis.
serviceCollection.AddAnthropicService();

//serviceCollection.AddAnthropicService(options =>
Expand Down
3 changes: 2 additions & 1 deletion Anthropic.Playground/TestHelpers/ChatTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static async Task RunChatCompletionTest(IAnthropicService anthropicServic
Console.WriteLine($"Exception occurred: {ex}");
throw;
}
Console.WriteLine("---- 0 ----");

Console.WriteLine("---- 0 ----");
}

/// <summary>
Expand Down Expand Up @@ -103,6 +103,7 @@ public static async Task RunChatCompletionStreamTest(IAnthropicService anthropic
Console.WriteLine($"Exception occurred: {ex}");
throw;
}

Console.WriteLine("---- 0 ----");
}
}
1 change: 1 addition & 0 deletions Anthropic.Playground/TestHelpers/ChatToolUseTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static async Task RunChatCompletionWithToolUseTest(IAnthropicService anth
{
continue;
}

switch (content.Type)
{
case "text":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public class AnthropicOptions
/// </summary>
public static readonly string SettingKey = "AnthropicServiceOptions";

private string? _providerVersion;
private string? _apiVersion;
private string? _baseDomain;

private string? _providerVersion;


public AnthropicProviderType ProviderType { get; set; } = AnthropicProviderType.Anthropic;

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Text.Json.Serialization;
using Anthropic.ObjectModels;
using Anthropic.ObjectModels.SharedModels;

namespace Anthropic.ObjectModels.RequestModels;
namespace Anthropic.ApiModels.RequestModels;

/// <summary>
/// Represents a request to create a message using the Anthropic API.
Expand All @@ -13,7 +14,6 @@ public MessageRequest()
}

/// <summary>
///
/// </summary>
/// <param name="messages"></param>
/// <param name="maxTokens"></param>
Expand All @@ -29,14 +29,14 @@ public MessageRequest(List<Message> messages, int maxTokens, string? model = nul
/// <summary>
/// Gets or sets the maximum number of tokens to generate before stopping.
/// </summary>
/// <remarks> Required </remarks>
/// <remarks> Required </remarks>
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; }

/// <summary>
/// Gets or sets the input messages for the conversation.
/// </summary>
/// <remarks> Required </remarks>
/// <remarks> Required </remarks>
[JsonPropertyName("messages")]
public List<Message> Messages { get; set; }

Expand Down Expand Up @@ -86,7 +86,7 @@ public MessageRequest(List<Message> messages, int maxTokens, string? model = nul
/// <summary>
/// Gets or sets the model that will complete the prompt.
/// </summary>
/// <remarks> Required </remarks>
/// <remarks> Required </remarks>
[JsonPropertyName("model")]
public string Model { get; set; }

Check warning on line 91 in Anthropic/ApiModels/RequestModels/MessageRequest.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'value' of 'void MessageRequest.Model.set' doesn't match implicitly implemented member 'void IModel.Model.set' (possibly because of nullability attributes).

Check warning on line 91 in Anthropic/ApiModels/RequestModels/MessageRequest.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'value' of 'void MessageRequest.Model.set' doesn't match implicitly implemented member 'void IModel.Model.set' (possibly because of nullability attributes).
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
using Anthropic.ObjectModels.SharedModels;

namespace Anthropic.ObjectModels.ResponseModels;
public class TypeBaseResponse:IType

public class TypeBaseResponse : IType
{
[JsonPropertyName("type")]
public string Type { get; set; }
}

public interface IType
{
[JsonPropertyName("type")]
public string Type { get; set; }
}

public interface IStreamResponse:IType
public interface IStreamResponse : IType
{
public string? StreamEvent { get; set; }
}

public static class StreamResponseExtensions
{
public static T As<T>(this IStreamResponse response) where T : class, IStreamResponse
Expand All @@ -42,24 +45,26 @@ public static bool TryAs<T>(this IStreamResponse response, out T result) where T
result = null;
return false;
}

}

public static class StreamExtensions
{
public static bool IsMessageResponse(this IStreamResponse response)
{
return response.Type == "message";
}

public static bool IsPingResponse(this IStreamResponse response)
{
return response.Type == "ping";
}

public static bool IsError(this IStreamResponse response)
{
return response.Type == "error";
}

}

public class BaseResponse : TypeBaseResponse, IStreamResponse
{
/// <summary>
Expand All @@ -74,8 +79,6 @@ public class BaseResponse : TypeBaseResponse, IStreamResponse
[JsonPropertyName("usage")]
public Usage? Usage { get; set; }

[JsonPropertyName("StreamEvent")]
public string? StreamEvent { get; set; }
public bool IsDelta => StreamEvent?.EndsWith("delta") ?? false;

public HttpStatusCode HttpStatusCode { get; set; }
Expand All @@ -85,27 +88,30 @@ public class BaseResponse : TypeBaseResponse, IStreamResponse
public Error? Error { get; set; }

public bool Successful => Error == null;

[JsonPropertyName("StreamEvent")]
public string? StreamEvent { get; set; }
}

public class Error:TypeBaseResponse
public class Error : TypeBaseResponse
{
[JsonPropertyName("message")]
public string Message { get; set; } = string.Empty;
}

/// <summary>
/// Represents Anthropic-specific headers in an HTTP response.
/// Represents Anthropic-specific headers in an HTTP response.
/// </summary>
/// <remarks>
/// Initializes a new instance of the AnthropicHeaders class from HttpResponseHeaders.
/// Initializes a new instance of the AnthropicHeaders class from HttpResponseHeaders.
/// </remarks>
public class AnthropicHeaders
{
/// <summary>
/// Represents Anthropic-specific headers in an HTTP response.
/// Represents Anthropic-specific headers in an HTTP response.
/// </summary>
/// <remarks>
/// Initializes a new instance of the AnthropicHeaders class from HttpResponseHeaders.
/// Initializes a new instance of the AnthropicHeaders class from HttpResponseHeaders.
/// </remarks>
/// <param name="headers">The HTTP response headers.</param>
public AnthropicHeaders(HttpResponseHeaders headers)
Expand All @@ -114,7 +120,7 @@ public AnthropicHeaders(HttpResponseHeaders headers)
}

/// <summary>
/// Gets information about rate limits applied to the request.
/// Gets information about rate limits applied to the request.
/// </summary>
public RateLimitInfo? RateLimit { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@

namespace Anthropic.ObjectModels.ResponseModels;

public class PingResponse: BaseResponse, IStreamResponse
public class PingResponse : BaseResponse, IStreamResponse
{
}

public class MessageResponse : BaseResponse, IStreamResponse
{

public override string? ToString()
{
return Content?.FirstOrDefault()?.Text;
}

[JsonPropertyName("content")]
[JsonConverter(typeof(JsonConverters.ContentConverter))]
public List<ContentBlock>? Content { get; set; }
Expand All @@ -30,6 +25,11 @@ public class MessageResponse : BaseResponse, IStreamResponse
[JsonPropertyName("stop_sequence")]
public string? StopSequence { get; set; }

public override string? ToString()
{
return Content?.FirstOrDefault()?.Text;
}

public Message ToMessage()
{
return new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
namespace Anthropic.ObjectModels.ResponseModels;

/// <summary>
/// Represents rate limit information from Anthropic API headers.
/// Represents rate limit information from Anthropic API headers.
/// </summary>
/// <remarks>
/// Initializes a new instance of the RateLimitInfo class from HttpResponseHeaders.
/// Initializes a new instance of the RateLimitInfo class from HttpResponseHeaders.
/// </remarks>
public class RateLimitInfo
{
/// <summary>
/// Represents rate limit information from Anthropic API headers.
/// Represents rate limit information from Anthropic API headers.
/// </summary>
/// <remarks>
/// Initializes a new instance of the RateLimitInfo class from HttpResponseHeaders.
/// Initializes a new instance of the RateLimitInfo class from HttpResponseHeaders.
/// </remarks>
/// <param name="headers">The HTTP response headers.</param>
public RateLimitInfo(HttpResponseHeaders headers)
Expand All @@ -30,37 +30,37 @@ public RateLimitInfo(HttpResponseHeaders headers)
}

/// <summary>
/// Gets the maximum number of requests allowed within any rate limit period.
/// Gets the maximum number of requests allowed within any rate limit period.
/// </summary>
public int? RequestsLimit { get; }

/// <summary>
/// Gets the number of requests remaining before being rate limited.
/// Gets the number of requests remaining before being rate limited.
/// </summary>
public int? RequestsRemaining { get; }

/// <summary>
/// Gets the time when the request rate limit will reset, provided in RFC 3339 format.
/// Gets the time when the request rate limit will reset, provided in RFC 3339 format.
/// </summary>
public DateTimeOffset? RequestsReset { get; }

/// <summary>
/// Gets the maximum number of tokens allowed within any rate limit period.
/// Gets the maximum number of tokens allowed within any rate limit period.
/// </summary>
public int? TokensLimit { get; }

/// <summary>
/// Gets the number of tokens remaining (rounded to the nearest thousand) before being rate limited.
/// Gets the number of tokens remaining (rounded to the nearest thousand) before being rate limited.
/// </summary>
public int? TokensRemaining { get; }

/// <summary>
/// Gets the time when the token rate limit will reset, provided in RFC 3339 format.
/// Gets the time when the token rate limit will reset, provided in RFC 3339 format.
/// </summary>
public DateTimeOffset? TokensReset { get; }

/// <summary>
/// Gets the number of seconds until you can retry the request.
/// Gets the number of seconds until you can retry the request.
/// </summary>
public int? RetryAfter { get; }

Expand All @@ -70,6 +70,7 @@ public RateLimitInfo(HttpResponseHeaders headers)
{
return value;
}

return null;
}

Expand All @@ -79,6 +80,7 @@ public RateLimitInfo(HttpResponseHeaders headers)
{
return value;
}

return null;
}
}
Loading

0 comments on commit 8871510

Please sign in to comment.