-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8f9175
commit 13e3a83
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
plugins { | ||
id("java") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(project(":anthropic-java")) | ||
implementation(project(mapOf("path" to ":anthropic-java-core"))) | ||
} |
49 changes: 49 additions & 0 deletions
49
anthropic-java-example/src/main/java/com/anthropic/example/Main.java
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,49 @@ | ||
package com.anthropic.example; | ||
|
||
import com.anthropic.client.AnthropicClient; | ||
import com.anthropic.client.okhttp.AnthropicOkHttpClient; | ||
import com.anthropic.core.http.StreamResponse; | ||
import com.anthropic.models.*; | ||
|
||
public final class Main { | ||
private Main() {} | ||
|
||
public static void main(String[] args) { | ||
AnthropicClient client = AnthropicOkHttpClient.fromEnv(); | ||
MessageCreateParams.Builder createParamsBuilder = MessageCreateParams.builder() | ||
.model(Model.CLAUDE_3_5_SONNET_LATEST) | ||
.maxTokens(1000) | ||
.addMessage(MessageParam.builder() | ||
.role(MessageParam.Role.USER) | ||
.content(MessageParam.Content.ofString("Tell me a story about building the best SDK!")) | ||
.build()); | ||
|
||
// Having a conversation in a loop | ||
for (int i = 0; i < 4; i++) { | ||
Message message = client.messages().create(createParamsBuilder.build()); | ||
|
||
message.content().stream() | ||
.flatMap(contentBlock -> contentBlock.textBlock().stream()) | ||
.forEach(textBlock -> System.out.println(textBlock.text())); | ||
|
||
System.out.println("\n-----------------------------------\n"); | ||
|
||
createParamsBuilder | ||
.addMessage(message.toParam()) | ||
.addMessage(MessageParam.builder() | ||
.role(MessageParam.Role.USER) | ||
.content(MessageParam.Content.ofString("But why???")) | ||
.build()); | ||
} | ||
|
||
// Streaming example | ||
try (StreamResponse<RawMessageStreamEvent> streamResponse = client.messages().createStreaming(createParamsBuilder.build())) { | ||
streamResponse.stream() | ||
.flatMap(event -> event.rawContentBlockDeltaEvent().stream()) | ||
.flatMap(deltaEvent -> deltaEvent.delta().textDelta().stream()) | ||
.forEach(textDelta -> System.out.print(textDelta.text())); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} |