Skip to content
Merged
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
45 changes: 37 additions & 8 deletions src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
modelSettings: Settings,
providerSettings: provider.ChatSettings);

Usage? usage = null;

var bodyJson = CreateBodyJson(prompt, usedSettings, request.Image);

if (usedSettings.UseStreaming == true)
Expand All @@ -51,7 +53,15 @@
var streamEvent = (PayloadPart)payloadPart;
var chunk = await JsonSerializer.DeserializeAsync<JsonObject>(streamEvent.Bytes, cancellationToken: cancellationToken)
.ConfigureAwait(false);

usage ??= GetUsage(chunk?["message"]?["usage"]);
var type = chunk?["type"]!.GetValue<string>().ToUpperInvariant();

if (type == "MESSAGE_DELTA")
{
usage += GetUsage(chunk?["usage"]);
}

if (type == "CONTENT_BLOCK_DELTA")
{
var delta = chunk?["delta"]?["text"]!.GetValue<string>();
Expand All @@ -62,10 +72,6 @@
});
stringBuilder.Append(delta);
}
if (type == "CONTENT_BLOCK_STOP")
{
break;
}
}

OnDeltaReceived(new ChatResponseDelta
Expand All @@ -82,24 +88,27 @@
else
{
var response = await provider.Api.InvokeModelAsync(Id, bodyJson, cancellationToken).ConfigureAwait(false);
usage = GetUsage(response?["usage"]);

var generatedText = response?["content"]?[0]?["text"]?.GetValue<string>() ?? "";

messages.Add(generatedText.AsAiMessage());
}

var usage = Usage.Empty with
usage ??= Usage.Empty;
usage = usage.Value with
{
Time = watch.Elapsed,
Messages = messages.Count,
};
AddUsage(usage);
provider.AddUsage(usage);
AddUsage(usage.Value);
provider.AddUsage(usage.Value);

var chatResponse = new ChatResponse
{
Messages = messages,
UsedSettings = usedSettings,
Usage = usage,
Usage = usage.Value,
};
OnResponseReceived(chatResponse);

Expand Down Expand Up @@ -162,4 +171,24 @@

return bodyJson;
}

/// <summary>
/// Extracts usage information from the provided JSON node.
/// </summary>
/// <param name="usageNode">The JSON node containing usage information.</param>
/// <returns>A <see cref="Usage"/> object with the extracted usage data.</returns>
private Usage GetUsage(JsonNode? usageNode)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 180 in src/Amazon.Bedrock/src/Chat/AnthropicClaude3ChatModel.cs

View workflow job for this annotation

GitHub Actions / Build and test / Build, test and publish

Member 'GetUsage' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
{
var inputTokens = usageNode?["input_tokens"]?.GetValue<int>() ?? 0;
var outputTokens = usageNode?["output_tokens"]?.GetValue<int>() ?? 0;
var priceInUsd = 0.0;

return Usage.Empty with
{
InputTokens = inputTokens,
OutputTokens = outputTokens,
Messages = 0,
PriceInUsd = priceInUsd,
};
}
}