A community-maintained easy-to-use Java/Kotlin Claude API
For Kotlin DSL (build.gradle.kts
), add this to your dependencies block:
dependencies {
implementation("com.klaude:klaude:0.0.1")
}
For Maven projects, add this to your pom.xml
file in the <dependencies>
block:
<dependency>
<groupId>com.klaude</groupId>
<artifactId>klaude</artifactId>
<version>0.0.1</version>
</dependency>
See the maven repository for gradle/ant/etc.
This is a basic working example.
fun translateToEnglish() {
val klaudeClient = KlaudeClient.Builder()
.key("YOUR_API_KEY")
.model("claude-2")
.maxTokensToSample(1000)
.build()
val text = "Olá, tudo bem?"
klaudeClient.complete("Translate to English: $text") { result ->
println(result ?: "[empty]")
}
}
fun chat() {
val scan = Scanner(System.`in`)
val klaude = KlaudeClient.Builder()
.model("claude-2")
.key("YOUR_API_KEY")
.maxTokensToSample(500)
.build()
val messages = mutableListOf(KlaudeMessage("You are a helpful assistant", KlaudeMessageType.USER))
println("Start chatting, type 'exit' or 'quit' to finish:\n")
while (true) {
println("Human:")
val input = scan.nextLine().trim()
if (input.isBlank()) {
continue
}
if (input == "exit" || input == "quit") {
break
}
messages.add(KlaudeMessage(input, KlaudeMessageType.USER))
klaude.chat(messages) { reply ->
if (!reply.isNullOrBlank()) {
messages.add(KlaudeMessage(reply.trim(), KlaudeMessageType.ASSISTANT))
println("Assistant:\n${reply.trim()}")
}
}
}
println("DONE")
}
Klaude is an open-sourced software licensed under the MIT License. This is an unofficial library, and is not affiliated with Anthropic.