docs: work on README

This commit is contained in:
Stevan Freeborn
2024-07-03 16:57:27 -05:00
parent bd8e4c70ab
commit dd380c9253
41 changed files with 847 additions and 523 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ public class AnthropicError
/// <summary>
/// The error object.
/// </summary>
public Error? Error { get; init; } = null;
public Error Error { get; init; } = new ApiError();
[JsonConstructor]
internal AnthropicError()
@@ -0,0 +1,147 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a message request.
/// </summary>
public abstract class BaseMessageRequest
{
/// <summary>
/// Gets the model ID to use for the request.
/// </summary>
public string Model { get; init; } = string.Empty;
/// <summary>
/// Gets the system ID to use for the request.
/// </summary>
public string? System { get; init; } = null;
/// <summary>
/// Gets the messages to send to the model.
/// </summary>
public List<Message> Messages { get; init; } = [];
/// <summary>
/// Gets the maximum number of tokens to generate.
/// </summary>
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; init; } = 1024;
/// <summary>
/// Gets the metadata to include with the request.
/// </summary>
public Dictionary<string, object>? Metadata { get; init; } = null;
/// <summary>
/// Gets the prompt stop sequences.
/// </summary>
[JsonPropertyName("stop_sequences")]
public List<string> StopSequences { get; init; } = [];
/// <summary>
/// Gets the temperature to use for the request.
/// </summary>
public decimal Temperature { get; init; } = 0.0m;
/// <summary>
/// Gets the top-K value to use for the request.
/// </summary>
public int? TopK { get; init; } = null;
/// <summary>
/// Gets the top-P value to use for the request.
/// </summary>
public decimal? TopP { get; init; } = null;
/// <summary>
/// Gets the tool choice mode to use for the request.
/// </summary>
[JsonPropertyName("tool_choice")]
public ToolChoice? ToolChoice { get; init; } = null;
/// <summary>
/// Gets the tools to use for the request.
/// </summary>
public List<Tool>? Tools { get; init; } = null;
/// <summary>
/// Gets a value indicating whether the message should be streamed.
/// </summary>
public bool Stream { get; init; }
[JsonConstructor]
internal BaseMessageRequest() { }
/// <summary>
/// Initializes a new instance of the <see cref="BaseMessageRequest"/> class.
/// </summary>
/// <param name="model">The model ID to use for the request.</param>
/// <param name="messages">The messages to send to the model.</param>
/// <param name="maxTokens">The maximum number of tokens to generate.</param>
/// <param name="system">The system ID to use for the request.</param>
/// <param name="metadata">The metadata to include with the request.</param>
/// <param name="temperature">The temperature to use for the request.</param>
/// <param name="topK">The top-K value to use for the request.</param>
/// <param name="topP">The top-P value to use for the request.</param>
/// <param name="toolChoice">The tool choice mode to use for the request.</param>
/// <param name="tools">The tools to use for the request.</param>
/// <param name="stream">A value indicating whether the message should be streamed.</param>
/// <exception cref="ArgumentException">Thrown when the model ID is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the model or messages is null.</exception>
/// <exception cref="ArgumentException">Thrown when the messages contain no messages.</exception>
/// <exception cref="ArgumentException">Thrown when the max tokens is less than one.</exception>
/// <exception cref="ArgumentException">Thrown when the temperature is less than zero or greater than one.</exception>
/// <returns>A new instance of the <see cref="BaseMessageRequest"/> class.</returns>
protected BaseMessageRequest(
string model,
List<Message> messages,
int maxTokens = 1024,
string? system = null,
Dictionary<string, object>? metadata = null,
decimal temperature = 0.0m,
int? topK = null,
decimal? topP = null,
ToolChoice? toolChoice = null,
List<Tool>? tools = null,
bool stream = false
)
{
ArgumentValidator.ThrowIfNull(model, nameof(model));
ArgumentValidator.ThrowIfNull(messages, nameof(messages));
if (AnthropicModels.IsValidModel(model) is false)
{
throw new ArgumentException($"Invalid model ID: {model}");
}
if (messages.Count < 1)
{
throw new ArgumentException("Messages must contain at least one message");
}
if (maxTokens < 1)
{
throw new ArgumentException($"Invalid max tokens: {maxTokens}");
}
if (temperature < 0.0m || temperature > 1.0m)
{
throw new ArgumentException($"Invalid temperature: {temperature}");
}
Model = model;
Messages = messages;
MaxTokens = maxTokens;
System = system;
Metadata = metadata;
Temperature = temperature;
TopK = topK;
TopP = topP;
ToolChoice = toolChoice;
Tools = tools;
Stream = stream;
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents part of the content of a chat message.
/// Represents part of the content of a message.
/// </summary>
public abstract class Content
{
+1 -1
View File
@@ -5,7 +5,7 @@ using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents image content that is part of a chat message.
/// Represents image content that is part of a message.
/// </summary>
public class ImageContent : Content
{
@@ -5,9 +5,9 @@ using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a chat message.
/// Represents a message.
/// </summary>
public class ChatMessage
public class Message
{
/// <summary>
/// Gets the role of the message.
@@ -20,19 +20,19 @@ public class ChatMessage
public List<Content> Content { get; init; } = [];
[JsonConstructor]
internal ChatMessage()
internal Message()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ChatMessage"/> class.
/// Initializes a new instance of the <see cref="Message"/> class.
/// </summary>
/// <param name="role">The role of the message.</param>
/// <param name="content">The contents of the message.</param>
/// <exception cref="ArgumentException">Thrown when the role is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the role or content is null.</exception>
/// <returns>A new instance of the <see cref="ChatMessage"/> class.</returns>
public ChatMessage(string role, List<Content> content)
/// <returns>A new instance of the <see cref="Message"/> class.</returns>
public Message(string role, List<Content> content)
{
ArgumentValidator.ThrowIfNull(role, nameof(role));
ArgumentValidator.ThrowIfNull(content, nameof(content));
@@ -11,17 +11,17 @@ public class MessageCompleteEventData : EventData
public AnthropicHeaders Headers { get; init; }
/// <summary>
/// Gets the chat response message.
/// Gets the response message.
/// </summary>
public ChatResponse Message { get; init; }
public MessageResponse Message { get; init; }
/// <summary>
/// Initializes a new instance of the <see cref="MessageCompleteEventData"/> class.
/// </summary>
/// <param name="message">The chat response message.</param>
/// <param name="message">The response message.</param>
/// <param name="headers">The anthropic headers.</param>
/// <returns>A new instance of the <see cref="MessageCompleteEventData"/> class.</returns>
public MessageCompleteEventData(ChatResponse message, AnthropicHeaders headers) : base(EventType.MessageComplete)
public MessageCompleteEventData(MessageResponse message, AnthropicHeaders headers) : base(EventType.MessageComplete)
{
Message = message;
Headers = headers;
@@ -13,9 +13,9 @@ public class MessageDeltaEventData : EventData
public MessageDelta Delta { get; init; } = new();
/// <summary>
/// Gets the chat usage.
/// Gets the usage.
/// </summary>
public ChatUsage Usage { get; init; } = new();
public Usage Usage { get; init; } = new();
[JsonConstructor]
internal MessageDeltaEventData() : base(EventType.MessageDelta)
@@ -26,9 +26,9 @@ public class MessageDeltaEventData : EventData
/// Initializes a new instance of the <see cref="MessageDeltaEventData"/> class.
/// </summary>
/// <param name="delta">The message delta.</param>
/// <param name="usage">The chat usage.</param>
/// <param name="usage">The usage.</param>
/// <returns>A new instance of the <see cref="MessageDeltaEventData"/> class.</returns>
public MessageDeltaEventData(MessageDelta delta, ChatUsage usage) : base(EventType.MessageDelta)
public MessageDeltaEventData(MessageDelta delta, Usage usage) : base(EventType.MessageDelta)
{
Delta = delta;
Usage = usage;
+17 -106
View File
@@ -1,79 +1,14 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a message request.
/// </summary>
public abstract class MessageRequest
public class MessageRequest : BaseMessageRequest
{
/// <summary>
/// Gets the model ID to use for the request.
/// </summary>
public string Model { get; init; } = string.Empty;
/// <summary>
/// Gets the system ID to use for the request.
/// </summary>
public string? System { get; init; } = null;
/// <summary>
/// Gets the messages to send to the model.
/// </summary>
public List<ChatMessage> Messages { get; init; } = [];
/// <summary>
/// Gets the maximum number of tokens to generate.
/// </summary>
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; init; } = 1024;
/// <summary>
/// Gets the metadata to include with the request.
/// </summary>
public Dictionary<string, object>? Metadata { get; init; } = null;
/// <summary>
/// Gets the prompt stop sequences.
/// </summary>
[JsonPropertyName("stop_sequences")]
public List<string> StopSequences { get; init; } = [];
/// <summary>
/// Gets the temperature to use for the request.
/// </summary>
public decimal Temperature { get; init; } = 0.0m;
/// <summary>
/// Gets the top-K value to use for the request.
/// </summary>
public int? TopK { get; init; } = null;
/// <summary>
/// Gets the top-P value to use for the request.
/// </summary>
public decimal? TopP { get; init; } = null;
/// <summary>
/// Gets the tool choice mode to use for the request.
/// </summary>
[JsonPropertyName("tool_choice")]
public ToolChoice? ToolChoice { get; init; } = null;
/// <summary>
/// Gets the tools to use for the request.
/// </summary>
public List<Tool>? Tools { get; init; } = null;
/// <summary>
/// Gets a value indicating whether the message should be streamed.
/// </summary>
public bool Stream { get; init; }
[JsonConstructor]
internal MessageRequest() { }
internal MessageRequest() : base() { }
/// <summary>
/// Initializes a new instance of the <see cref="MessageRequest"/> class.
@@ -88,16 +23,15 @@ public abstract class MessageRequest
/// <param name="topP">The top-P value to use for the request.</param>
/// <param name="toolChoice">The tool choice mode to use for the request.</param>
/// <param name="tools">The tools to use for the request.</param>
/// <param name="stream">A value indicating whether the message should be streamed.</param>
/// <exception cref="ArgumentException">Thrown when the model ID is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the model or messages is null.</exception>
/// <exception cref="ArgumentException">Thrown when the messages contain no messages.</exception>
/// <exception cref="ArgumentException">Thrown when the max tokens is less than one.</exception>
/// <exception cref="ArgumentException">Thrown when the temperature is less than zero or greater than one.</exception>
/// <returns>A new instance of the <see cref="MessageRequest"/> class.</returns>
protected MessageRequest(
public MessageRequest(
string model,
List<ChatMessage> messages,
List<Message> messages,
int maxTokens = 1024,
string? system = null,
Dictionary<string, object>? metadata = null,
@@ -105,43 +39,20 @@ public abstract class MessageRequest
int? topK = null,
decimal? topP = null,
ToolChoice? toolChoice = null,
List<Tool>? tools = null,
bool stream = false
List<Tool>? tools = null
) : base(
model,
messages,
maxTokens,
system,
metadata,
temperature,
topK,
topP,
toolChoice,
tools,
false
)
{
ArgumentValidator.ThrowIfNull(model, nameof(model));
ArgumentValidator.ThrowIfNull(messages, nameof(messages));
if (AnthropicModels.IsValidModel(model) is false)
{
throw new ArgumentException($"Invalid model ID: {model}");
}
if (messages.Count < 1)
{
throw new ArgumentException("Messages must contain at least one message");
}
if (maxTokens < 1)
{
throw new ArgumentException($"Invalid max tokens: {maxTokens}");
}
if (temperature < 0.0m || temperature > 1.0m)
{
throw new ArgumentException($"Invalid temperature: {temperature}");
}
Model = model;
Messages = messages;
MaxTokens = maxTokens;
System = system;
Metadata = metadata;
Temperature = temperature;
TopK = topK;
TopP = topP;
ToolChoice = toolChoice;
Tools = tools;
Stream = stream;
}
}
@@ -3,54 +3,54 @@ using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a chat response.
/// Represents a response.
/// </summary>
public class ChatResponse
public class MessageResponse
{
/// <summary>
/// Gets the ID of the chat response.
/// Gets the ID of the response.
/// </summary>
public string Id { get; init; } = string.Empty;
/// <summary>
/// Gets the model used for the chat response.
/// Gets the model used for the response.
/// </summary>
public string Model { get; init; } = string.Empty;
/// <summary>
/// Gets the role of the chat response.
/// Gets the role of the response.
/// </summary>
public string Role { get; init; } = string.Empty;
/// <summary>
/// Gets the stop reason of the chat response.
/// Gets the stop reason of the response.
/// </summary>
[JsonPropertyName("stop_reason")]
public string? StopReason { get; init; }
/// <summary>
/// Gets the stop sequence of the chat response.
/// Gets the stop sequence of the response.
/// </summary>
[JsonPropertyName("stop_sequence")]
public string? StopSequence { get; init; }
/// <summary>
/// Gets the type of the chat response.
/// Gets the type of the response.
/// </summary>
public string Type { get; init; } = string.Empty;
/// <summary>
/// Gets the usage of the chat response.
/// Gets the usage of the response.
/// </summary>
public ChatUsage Usage { get; init; } = new();
public Usage Usage { get; init; } = new();
/// <summary>
/// Gets the contents of the chat response.
/// Gets the contents of the response.
/// </summary>
public List<Content> Content { get; init; } = [];
/// <summary>
/// Gets the tool call of the chat response. If the chat response does not contain a tool call, this property is null.
/// Gets the tool call of the response. If the response does not contain a tool call, this property is null.
/// </summary>
[JsonIgnore]
public ToolCall? ToolCall { get; set; } = null;
@@ -10,7 +10,7 @@ public class MessageStartEventData : EventData
/// <summary>
/// Gets the message.
/// </summary>
public ChatResponse Message { get; init; } = new();
public MessageResponse Message { get; init; } = new();
[JsonConstructor]
internal MessageStartEventData() : base(EventType.MessageStart)
@@ -22,7 +22,7 @@ public class MessageStartEventData : EventData
/// </summary>
/// <param name="message">The message.</param>
/// <returns>A new instance of the <see cref="MessageStartEventData"/> class.</returns>
public MessageStartEventData(ChatResponse message) : base(EventType.MessageStart)
public MessageStartEventData(MessageResponse message) : base(EventType.MessageStart)
{
Message = message;
}
@@ -1,58 +0,0 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a chat message request.
/// </summary>
public class StreamChatMessageRequest : MessageRequest
{
[JsonConstructor]
internal StreamChatMessageRequest() : base() { }
/// <summary>
/// Initializes a new instance of the <see cref="StreamChatMessageRequest"/> class.
/// </summary>
/// <param name="model">The model ID to use for the request.</param>
/// <param name="messages">The messages to send to the model.</param>
/// <param name="maxTokens">The maximum number of tokens to generate.</param>
/// <param name="system">The system ID to use for the request.</param>
/// <param name="metadata">The metadata to include with the request.</param>
/// <param name="temperature">The temperature to use for the request.</param>
/// <param name="topK">The top-K value to use for the request.</param>
/// <param name="topP">The top-P value to use for the request.</param>
/// <param name="toolChoice">The tool choice mode to use for the request.</param>
/// <param name="tools">The tools to use for the request.</param>
/// <exception cref="ArgumentException">Thrown when the model ID is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the model or messages is null.</exception>
/// <exception cref="ArgumentException">Thrown when the messages contain no messages.</exception>
/// <exception cref="ArgumentException">Thrown when the max tokens is less than one.</exception>
/// <exception cref="ArgumentException">Thrown when the temperature is less than zero or greater than one.</exception>
/// <returns>A new instance of the <see cref="StreamChatMessageRequest"/> class.</returns>
public StreamChatMessageRequest(
string model,
List<ChatMessage> messages,
int maxTokens = 1024,
string? system = null,
Dictionary<string, object>? metadata = null,
decimal temperature = 0.0m,
int? topK = null,
decimal? topP = null,
ToolChoice? toolChoice = null,
List<Tool>? tools = null
) : base(
model,
messages,
maxTokens,
system,
metadata,
temperature,
topK,
topP,
toolChoice,
tools,
true
)
{
}
}
@@ -3,15 +3,15 @@ using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a chat message request.
/// Represents a message request.
/// </summary>
public class ChatMessageRequest : MessageRequest
public class StreamMessageRequest : BaseMessageRequest
{
[JsonConstructor]
internal ChatMessageRequest() : base() { }
internal StreamMessageRequest() : base() { }
/// <summary>
/// Initializes a new instance of the <see cref="ChatMessageRequest"/> class.
/// Initializes a new instance of the <see cref="StreamMessageRequest"/> class.
/// </summary>
/// <param name="model">The model ID to use for the request.</param>
/// <param name="messages">The messages to send to the model.</param>
@@ -28,10 +28,10 @@ public class ChatMessageRequest : MessageRequest
/// <exception cref="ArgumentException">Thrown when the messages contain no messages.</exception>
/// <exception cref="ArgumentException">Thrown when the max tokens is less than one.</exception>
/// <exception cref="ArgumentException">Thrown when the temperature is less than zero or greater than one.</exception>
/// <returns>A new instance of the <see cref="ChatMessageRequest"/> class.</returns>
public ChatMessageRequest(
/// <returns>A new instance of the <see cref="StreamMessageRequest"/> class.</returns>
public StreamMessageRequest(
string model,
List<ChatMessage> messages,
List<Message> messages,
int maxTokens = 1024,
string? system = null,
Dictionary<string, object>? metadata = null,
@@ -51,7 +51,7 @@ public class ChatMessageRequest : MessageRequest
topP,
toolChoice,
tools,
false
true
)
{
}
+1 -1
View File
@@ -5,7 +5,7 @@ using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents text content that is part of a chat message.
/// Represents text content that is part of a message.
/// </summary>
public class TextContent : Content
{
+1 -1
View File
@@ -29,7 +29,7 @@ public interface ITool
}
/// <summary>
/// Represents a tool that can be used in the chat.
/// Represents a tool that can be used.
/// </summary>
public class Tool
{
-1
View File
@@ -1,6 +1,5 @@
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using AnthropicClient.Json;
@@ -5,7 +5,7 @@ using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents tool result content that is part of a chat message.
/// Represents tool result content that is part of a message.
/// </summary>
public class ToolResultContent : Content
{
@@ -3,9 +3,9 @@ using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents the usage of a chat response.
/// Represents the usage of a response.
/// </summary>
public class ChatUsage
public class Usage
{
/// <summary>
/// Gets the number of input tokens used.