generated from openrndr/openrndr-template
-
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.
- Loading branch information
Showing
14 changed files
with
409 additions
and
151 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
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 not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
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
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) | ||
} |
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,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) | ||
} |
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,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}") | ||
} | ||
} | ||
} |
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,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) | ||
} |
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 @@ | ||
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) | ||
} | ||
|
||
} |
Oops, something went wrong.