Skip to content

Commit

Permalink
demos before the workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Oct 25, 2024
1 parent d10c8c5 commit aee0693
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 151 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ repositories {

dependencies {

implementation("com.xemantic.anthropic:anthropic-sdk-kotlin:0.5.0")
implementation("com.xemantic.anthropic:anthropic-sdk-kotlin:0.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.3")
implementation("io.ktor:ktor-client-java:3.0.0")
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added data/images/happy-birthday-chords-two-hands.webp
Binary file not shown.
Binary file added data/images/open-calls-creatives.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 0 additions & 150 deletions src/main/kotlin/ClaludeAiArtist.kt

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/kotlin/Demo01HelloWorld.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import com.xemantic.anthropic.Anthropic
import com.xemantic.anthropic.message.Message
import kotlinx.coroutines.runBlocking

fun main() {
val anthropic = Anthropic()
val response = runBlocking {
anthropic.messages.create {
+Message {
+"Hello World!"
}
}
}
println(response)
}
32 changes: 32 additions & 0 deletions src/main/kotlin/Demo02Conversation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import com.xemantic.anthropic.Anthropic
import com.xemantic.anthropic.message.Message
import com.xemantic.anthropic.message.plusAssign
import kotlinx.coroutines.runBlocking

/**
* This example shows how conversation cumulates in the
* token window.
*/
fun main() = runBlocking {
val anthropic = Anthropic()
val conversation = mutableListOf<Message>()
conversation += Message {
+"Is it true, that to know we can die is to be dead already?"
}
val response1 = anthropic.messages.create {
messages = conversation
}
println(response1)
conversation += response1
conversation += Message {
+"Why do you think I asked you this question?"
}
val response2 = anthropic.messages.create {
messages = conversation
}
println(response2)
conversation += response2

println("The whole past conversation is included in the token window")
println(conversation)
}
37 changes: 37 additions & 0 deletions src/main/kotlin/Demo03ConversationLoop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import com.xemantic.anthropic.Anthropic
import com.xemantic.anthropic.message.Message
import com.xemantic.anthropic.message.Text
import com.xemantic.anthropic.message.plusAssign
import kotlinx.coroutines.runBlocking

val systemPrompt = """
Act as an art critic. I am aspiring artists. Please be very
critical regarding ideas of my conceptual artwork.
"""

/**
* This example extends cumulated conversation into
* endless loop (only limited by the size of the token window).
* The system prompt is framing the conversation.
*/
fun main() {
val anthropic = Anthropic()
val conversation = mutableListOf<Message>()
while (true) {
print("[user]> ")
val line = readln()
if (line == "exit") break
conversation += Message { +line }
println("...Thinking...")
val response = runBlocking {
anthropic.messages.create {
messages = conversation
system(systemPrompt)
}
}
conversation += response
response.content.filterIsInstance<Text>().forEach {
println("[assistant]> ${it.text}")
}
}
}
31 changes: 31 additions & 0 deletions src/main/kotlin/Demo04ToolsInTheHandsOfAi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.xemantic.anthropic.Anthropic
import com.xemantic.anthropic.message.Message
import com.xemantic.anthropic.message.ToolResult
import com.xemantic.anthropic.message.ToolUse
import com.xemantic.anthropic.schema.Description
import com.xemantic.anthropic.tool.AnthropicTool
import com.xemantic.anthropic.tool.UsableTool
import kotlinx.coroutines.runBlocking

@AnthropicTool("Fibonacci")
@Description("Calculates Fibonacci number n")
data class FibonacciTool(val n: Int) : UsableTool {
override suspend fun use(toolUseId: String) = ToolResult(
toolUseId, text = "${fibonacci(n)}"
)
private tailrec fun fibonacci(
n: Int, a: Int = 0, b: Int = 1
): Int = when (n) {
0 -> a; 1 -> b; else -> fibonacci(n - 1, b, a + b)
}
}
fun main() = runBlocking {
val client = Anthropic { tool<FibonacciTool>() }
val response = client.messages.create {
+Message { +"What's Fibonacci number 42" }
useTools()
}
val toolUse = response.content.filterIsInstance<ToolUse>().first()
val toolResult = toolUse.use()
println(toolResult)
}
49 changes: 49 additions & 0 deletions src/main/kotlin/Demo05OpenCallsExtractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import com.xemantic.anthropic.Anthropic
import com.xemantic.anthropic.message.*
import com.xemantic.anthropic.schema.Description
import com.xemantic.anthropic.tool.AnthropicTool
import com.xemantic.anthropic.tool.UsableTool
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable

@AnthropicTool("OpenCallsReceiver")
@Description("Receives entries from the image")
data class OpenCallsReceiver(
val calls: List<Entry>
) : UsableTool {
override suspend fun use(toolUseId: String) = ToolResult(
toolUseId, "Data provided to client"
)
}

@Serializable
data class Entry(
val deadline: String,
val title: String,
)

fun main() = runBlocking {

val client = Anthropic {
tool<OpenCallsReceiver>()
}

val response = client.messages.create {
+Message {
+"Decode open calls from supplied image"
+Image(
path = "data/images/open-calls-creatives.jpg",
mediaType = Image.MediaType.IMAGE_JPEG
)
}
useTool<OpenCallsReceiver>()
}

val tool = response.content.filterIsInstance<ToolUse>().first()
val receiver = tool.input<OpenCallsReceiver>()

receiver.calls.forEach {
println(it)
}

}
Loading

0 comments on commit aee0693

Please sign in to comment.