Merge pull request #24 from StevanFreeborn/stevanfreeborn/feat/add-pdf-support

feat: add pdf support
This commit is contained in:
Stevan Freeborn
2024-11-21 14:07:22 -06:00
committed by GitHub
11 changed files with 487 additions and 19 deletions
@@ -16,6 +16,7 @@ class ContentConverter : JsonConverter<Content>
{
ContentType.Text => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options)!,
ContentType.Image => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options)!,
ContentType.Document => JsonSerializer.Deserialize<DocumentContent>(root.GetRawText(), options)!,
ContentType.ToolUse => JsonSerializer.Deserialize<ToolUseContent>(root.GetRawText(), options)!,
ContentType.ToolResult => JsonSerializer.Deserialize<ToolResultContent>(root.GetRawText(), options)!,
_ => throw new JsonException($"Unknown content type: {type}")
@@ -24,4 +24,9 @@ public static class ContentType
/// Represents the tool result content type.
/// </summary>
public const string ToolResult = "tool_result";
/// <summary>
/// Represents the document content type.
/// </summary>
public const string Document = "document";
}
@@ -0,0 +1,56 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents content from a document that is part of a message.
/// </summary>
public class DocumentContent : Content
{
/// <summary>
/// Gets the source of the document.
/// </summary>
public DocumentSource Source { get; init; } = new();
[JsonConstructor]
internal DocumentContent()
{
}
private void Validate(string mediaType, string data)
{
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
ArgumentValidator.ThrowIfNull(data, nameof(data));
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
public DocumentContent(string mediaType, string data) : base(ContentType.Document)
{
Validate(mediaType, data);
Source = new(mediaType, data);
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <param name="cacheControl">The cache control to be used for the content.</param>
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
/// <exception cref="ArgumentNullException">Thrown when the media type, data, or cache control is null.</exception>
public DocumentContent(string mediaType, string data, CacheControl cacheControl) : base(ContentType.Document, cacheControl)
{
Validate(mediaType, data);
Source = new(mediaType, data);
}
}
@@ -0,0 +1,49 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a document source.
/// </summary>
public class DocumentSource
{
/// <summary>
/// Gets the media type of the document.
/// </summary>
[JsonPropertyName("media_type")]
public string MediaType { get; init; } = string.Empty;
/// <summary>
/// Gets the data of the document.
/// </summary>
public string Data { get; init; } = string.Empty;
/// <summary>
/// Gets the type of encoding of the document data.
/// </summary>
public string Type { get; init; } = "base64";
[JsonConstructor]
internal DocumentSource()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentSource"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <exception cref="ArgumentException">Thrown when the media type is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
/// <returns>A new instance of the <see cref="DocumentSource"/> class.</returns>
public DocumentSource(string mediaType, string data)
{
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
ArgumentValidator.ThrowIfNull(data, nameof(data));
MediaType = mediaType;
Data = data;
}
}