docs: work on README
This commit is contained in:
@@ -14,18 +14,18 @@ namespace AnthropicClient;
|
||||
public interface IAnthropicApiClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a chat message asynchronously.
|
||||
/// Creates a message asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="request">The chat message request to create.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the chat response as an <see cref="AnthropicResult{T}"/>.</returns>
|
||||
Task<AnthropicResult<ChatResponse>> CreateChatMessageAsync(ChatMessageRequest request);
|
||||
/// <param name="request">The message request to create.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns>
|
||||
Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a chat message asynchronously and streams the response.
|
||||
/// Creates a message asynchronously and streams the response.
|
||||
/// </summary>
|
||||
/// <param name="request">The chat message request to create.</param>
|
||||
/// <returns>An asynchronous enumerable that yields the chat response line by line.</returns>
|
||||
IAsyncEnumerable<AnthropicEvent> CreateChatMessageAsync(StreamChatMessageRequest request);
|
||||
/// <param name="request">The message request to create.</param>
|
||||
/// <returns>An asynchronous enumerable that yields the response event by event.</returns>
|
||||
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IAnthropicApiClient"/>
|
||||
@@ -69,7 +69,7 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<AnthropicResult<ChatResponse>> CreateChatMessageAsync(ChatMessageRequest request)
|
||||
public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request)
|
||||
{
|
||||
var response = await SendRequestAsync(request);
|
||||
var anthropicHeaders = new AnthropicHeaders(response.Headers);
|
||||
@@ -78,21 +78,21 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var error = Deserialize<AnthropicError>(responseContent) ?? new AnthropicError();
|
||||
return AnthropicResult<ChatResponse>.Failure(error, anthropicHeaders);
|
||||
return AnthropicResult<MessageResponse>.Failure(error, anthropicHeaders);
|
||||
}
|
||||
|
||||
var chatResponse = Deserialize<ChatResponse>(responseContent) ?? new ChatResponse();
|
||||
var msgResponse = Deserialize<MessageResponse>(responseContent) ?? new MessageResponse();
|
||||
|
||||
if (request.Tools is not null && request.Tools.Count > 0)
|
||||
{
|
||||
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
|
||||
msgResponse.ToolCall = GetToolCall(msgResponse, request.Tools);
|
||||
}
|
||||
|
||||
return AnthropicResult<ChatResponse>.Success(chatResponse, anthropicHeaders);
|
||||
return AnthropicResult<MessageResponse>.Success(msgResponse, anthropicHeaders);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async IAsyncEnumerable<AnthropicEvent> CreateChatMessageAsync(StreamChatMessageRequest request)
|
||||
public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request)
|
||||
{
|
||||
var response = await SendRequestAsync(request);
|
||||
var anthropicHeaders = new AnthropicHeaders(response.Headers);
|
||||
@@ -100,7 +100,7 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
using var responseContent = await response.Content.ReadAsStreamAsync();
|
||||
using var streamReader = new StreamReader(responseContent);
|
||||
|
||||
ChatResponse? chatResponse = null;
|
||||
MessageResponse? msgResponse = null;
|
||||
Content? content = null;
|
||||
var toolInputJsonStringBuilder = new StringBuilder();
|
||||
var currentEvent = new AnthropicEvent();
|
||||
@@ -111,14 +111,14 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
|
||||
// I know...this is not pretty, but here is why...
|
||||
// as events are being yielded I want to also
|
||||
// build up the complete chat response
|
||||
// build up the complete response
|
||||
// so I can yield it as a special event to make tool
|
||||
// calling easier to handle
|
||||
|
||||
// initialize chat response on message start
|
||||
// initialize response on message start
|
||||
if (currentEvent.Type is EventType.MessageStart && currentEvent.Data is MessageStartEventData msgStartData)
|
||||
{
|
||||
chatResponse = msgStartData.Message;
|
||||
msgResponse = msgStartData.Message;
|
||||
}
|
||||
|
||||
// initialize content block on content block start
|
||||
@@ -144,14 +144,14 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
}
|
||||
|
||||
// finalize content block on content block stop
|
||||
// and add it to the chat response
|
||||
// and add it to the response
|
||||
if (currentEvent.Type is EventType.ContentBlockStop)
|
||||
{
|
||||
if (content is not null && chatResponse is not null)
|
||||
if (content is not null && msgResponse is not null)
|
||||
{
|
||||
if (content is TextContent textContent)
|
||||
{
|
||||
chatResponse.Content.Add(textContent);
|
||||
msgResponse.Content.Add(textContent);
|
||||
}
|
||||
|
||||
if (content is ToolUseContent toolUseContent)
|
||||
@@ -164,51 +164,51 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
Input = input!,
|
||||
};
|
||||
|
||||
chatResponse.Content.Add(newToolUseContent);
|
||||
msgResponse.Content.Add(newToolUseContent);
|
||||
}
|
||||
|
||||
content = null;
|
||||
}
|
||||
}
|
||||
|
||||
// update chat response with message delta data
|
||||
// update response with message delta data
|
||||
if (
|
||||
currentEvent.Type is EventType.MessageDelta &&
|
||||
currentEvent.Data is MessageDeltaEventData msgDeltaData &&
|
||||
chatResponse is not null
|
||||
msgResponse is not null
|
||||
)
|
||||
{
|
||||
var existingUsage = chatResponse.Usage;
|
||||
var newUsage = new ChatUsage()
|
||||
var existingUsage = msgResponse.Usage;
|
||||
var newUsage = new Usage()
|
||||
{
|
||||
InputTokens = existingUsage.InputTokens + msgDeltaData.Usage.InputTokens,
|
||||
OutputTokens = existingUsage.OutputTokens + msgDeltaData.Usage.OutputTokens,
|
||||
};
|
||||
|
||||
chatResponse = new ChatResponse()
|
||||
msgResponse = new MessageResponse()
|
||||
{
|
||||
Id = chatResponse.Id,
|
||||
Model = chatResponse.Model,
|
||||
Role = chatResponse.Role,
|
||||
Id = msgResponse.Id,
|
||||
Model = msgResponse.Model,
|
||||
Role = msgResponse.Role,
|
||||
StopReason = msgDeltaData.Delta.StopReason,
|
||||
StopSequence = msgDeltaData.Delta.StopSequence,
|
||||
Type = chatResponse.Type,
|
||||
Type = msgResponse.Type,
|
||||
Usage = newUsage,
|
||||
Content = chatResponse.Content,
|
||||
Content = msgResponse.Content,
|
||||
};
|
||||
|
||||
if (request.Tools is not null && request.Tools.Count > 0)
|
||||
{
|
||||
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
|
||||
msgResponse.ToolCall = GetToolCall(msgResponse, request.Tools);
|
||||
}
|
||||
}
|
||||
|
||||
// yield chat response on message stop
|
||||
if (currentEvent.Type is EventType.MessageStop && chatResponse is not null)
|
||||
// yield response on message stop
|
||||
if (currentEvent.Type is EventType.MessageStop && msgResponse is not null)
|
||||
{
|
||||
var eventData = new MessageCompleteEventData(chatResponse, anthropicHeaders);
|
||||
var eventData = new MessageCompleteEventData(msgResponse, anthropicHeaders);
|
||||
yield return new AnthropicEvent(EventType.MessageComplete, eventData);
|
||||
chatResponse = null;
|
||||
msgResponse = null;
|
||||
}
|
||||
|
||||
if (line is null)
|
||||
@@ -245,7 +245,7 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
} while (true);
|
||||
}
|
||||
|
||||
private ToolCall? GetToolCall(ChatResponse response, List<Tool> tools)
|
||||
private ToolCall? GetToolCall(MessageResponse response, List<Tool> tools)
|
||||
{
|
||||
var toolUse = response.Content.OfType<ToolUseContent>().FirstOrDefault();
|
||||
|
||||
@@ -264,7 +264,7 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
return new ToolCall(tool, toolUse);
|
||||
}
|
||||
|
||||
private async Task<HttpResponseMessage> SendRequestAsync(MessageRequest request)
|
||||
private async Task<HttpResponseMessage> SendRequestAsync(BaseMessageRequest request)
|
||||
{
|
||||
var requestContent = new StringContent(Serialize(request), Encoding.UTF8, JsonContentType);
|
||||
return await _httpClient.PostAsync(MessagesEndpoint, requestContent);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+12
-12
@@ -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
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -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
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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,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.
|
||||
Reference in New Issue
Block a user