Improve Anthropic Claude streaming handling

- add StreamEven API domain model for reliably parsing stream events.
 - add StreamHelper#mergeToolUseEvents to aggregate partial tool use jsons into a list of ContentBlocks.
 - add StreamHelper#eventToChatCompletionResponse to convert Flux<StreamEvents> into Flux<ChatCompletionResponse>.
 - Rename MediaContent -> ContentBlock, RequestMessage -> AnthropicMessage, ChatCompletion -> ChatCompletionResponse.
 - Improve tests and docs.
This commit is contained in:
Christian Tzolov
2024-07-02 11:06:53 +02:00
parent cb6662a61e
commit d8be884cb1
14 changed files with 1065 additions and 445 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 558 KiB

After

Width:  |  Height:  |  Size: 447 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

View File

@@ -279,6 +279,8 @@ Following class diagram illustrates the `AnthropicApi` chat interfaces and build
image::anthropic-claude3-class-diagram.jpg[AnthropicApi Chat API Diagram, width=1000, align="center"]
image::anthropic-claude3-events-model.jpg[AnthropicApi Event Model, width=1000, align="center"]
Here is a simple snippet how to use the api programmatically:
[source,java]
@@ -286,11 +288,11 @@ Here is a simple snippet how to use the api programmatically:
AnthropicApi anthropicApi =
new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));
RequestMessage chatCompletionMessage = new RequestMessage(
List.of(new MediaContent("Tell me a Joke?")), Role.USER);
AnthropicMessage chatCompletionMessage = new AnthropicMessage(
List.of(new ContentBlock("Tell me a Joke?")), Role.USER);
// Sync request
ResponseEntity<ChatCompletion> response = anthropicApi
ResponseEntity<ChatCompletionResponse> response = anthropicApi
.chatCompletionEntity(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
List.of(chatCompletionMessage), null, 100, 0.8f, false));