-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Claude 3 and Anthropic Messages API with streaming
- Loading branch information
François Bouteruche
committed
May 16, 2024
1 parent
21ebac8
commit c80c446
Showing
11 changed files
with
444 additions
and
16 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Rockhead.Extensions/Anthropic/ClaudeMessagesContentBlockDeltaChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesContentBlockDeltaChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("index")] | ||
public int? Index { get; init; } | ||
|
||
[JsonPropertyName("delta")] | ||
public BlockDeltaChunkContentBlock? Delta { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return Delta?.Text; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return null; | ||
} | ||
|
||
public class BlockDeltaChunkContentBlock | ||
{ | ||
[JsonPropertyName("type")] | ||
public string? Type { get; init; } | ||
|
||
[JsonPropertyName("text")] | ||
public string? Text { get; init; } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Rockhead.Extensions/Anthropic/ClaudeMessagesContentBlockStartChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesContentBlockStartChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("index")] | ||
public int? Index { get; init; } | ||
|
||
[JsonPropertyName("content_block")] | ||
public BlockStartChunkContentBlock? ContentBlock { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return ContentBlock?.Text; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return null; | ||
} | ||
|
||
public class BlockStartChunkContentBlock | ||
{ | ||
[JsonPropertyName("type")] | ||
public string? Type { get; init; } | ||
|
||
[JsonPropertyName("text")] | ||
public string? Text { get; init; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Rockhead.Extensions/Anthropic/ClaudeMessagesContentBlockStopChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesContentBlockStopChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("index")] | ||
public int? Index { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return String.Empty; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return null; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Rockhead.Extensions/Anthropic/ClaudeMessagesMessageDeltaChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using static Rockhead.Extensions.Anthropic.ClaudeMessagesResponse; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesMessageDeltaChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("delta")] | ||
public MessageDelta? Delta { get; set; } | ||
|
||
[JsonPropertyName("usage")] public MessageDeltaChunkUsage? Usage { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return String.Empty; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return Delta?.StopReason; | ||
} | ||
|
||
public class MessageDelta | ||
{ [JsonPropertyName("stop_reason")] public string? StopReason { get; init; } | ||
|
||
[JsonPropertyName("stop_sequence")] public string? StopSequence { get; init; } | ||
} | ||
|
||
public class MessageDeltaChunkUsage | ||
{ | ||
[JsonPropertyName("output_tokens")] public int OutputTokens { get; init; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Rockhead.Extensions/Anthropic/ClaudeMessagesMessageStartChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesMessageStartChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("type")] | ||
public string? Type { get; init; } | ||
|
||
[JsonPropertyName("message")] | ||
public ClaudeMessagesResponse? Message { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return ((ClaudeTextContent?)Message?.Content.FirstOrDefault())?.Text; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return null; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Rockhead.Extensions/Anthropic/ClaudeMessagesMessageStopChunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
public class ClaudeMessagesMessageStopChunk : IClaudeMessagesChunk | ||
{ | ||
[JsonPropertyName("amazon-bedrock-invocationMetrics")] | ||
public AmazonBedrockInvocationMetrics? InvocationMetrics { get; init; } | ||
|
||
public string? GetResponse() | ||
{ | ||
return null; | ||
} | ||
|
||
public string? GetStopReason() | ||
{ | ||
return null; | ||
} | ||
|
||
public class AmazonBedrockInvocationMetrics | ||
{ | ||
[JsonPropertyName("inputTokenCount")] public int? InputTokenCount { get; init; } | ||
|
||
[JsonPropertyName("outputTokenCount")] public int? OutputTokenCount { get; init; } | ||
|
||
[JsonPropertyName("invocationLatency")] public int? InvocationLatency { get; init; } | ||
|
||
[JsonPropertyName("firstByteLatency")] public int? FirstByteLatency { get; init; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rockhead.Extensions.Anthropic; | ||
|
||
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] | ||
[JsonDerivedType(typeof(ClaudeMessagesMessageStartChunk), "message_start")] | ||
[JsonDerivedType(typeof(ClaudeMessagesContentBlockStartChunk), "content_block_start")] | ||
[JsonDerivedType(typeof(ClaudeMessagesContentBlockDeltaChunk), "content_block_delta")] | ||
[JsonDerivedType(typeof(ClaudeMessagesContentBlockStopChunk), "content_block_stop")] | ||
[JsonDerivedType(typeof(ClaudeMessagesMessageDeltaChunk), "message_delta")] | ||
[JsonDerivedType(typeof(ClaudeMessagesMessageStopChunk), "message_stop")] | ||
public interface IClaudeMessagesChunk : IFoundationModelResponse | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters