-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReadBinaryFiles.kt
58 lines (54 loc) · 1.85 KB
/
ReadBinaryFiles.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.xemantic.claudine.tool
import com.xemantic.ai.anthropic.cache.CacheControl
import com.xemantic.ai.anthropic.content.Image
import com.xemantic.ai.anthropic.content.Text
import com.xemantic.ai.anthropic.content.isImage
import com.xemantic.ai.anthropic.tool.AnthropicTool
import com.xemantic.ai.anthropic.tool.ToolInput
import com.xemantic.ai.tool.schema.meta.Description
import com.xemantic.claudine.files.toBytes
import kotlinx.io.files.Path
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
@OptIn(ExperimentalEncodingApi::class)
@AnthropicTool("ReadBinaryFiles")
@Description(
"Reads binary files from human's machine, so they can be analyzed by the LLM. " +
"Image formats supported by Claude will be provided according to their respective content types. " +
"The contents of other types of files will transferred as Base64 encoded text content.")
data class ReadBinaryFiles(
@Description(
"The list of absolute file paths. " +
"The order of file content in tool result will much the order of file paths."
)
val paths: List<String>,
@Description(
"Indicates whether tool result of reading the files should be cached or not. " +
"Defaults to false if omitted."
)
val cache: Boolean? = false
) : ToolInput() {
init {
use {
paths.forEach { path ->
val bytes = Path(path).toBytes()
when {
bytes.isImage -> {
+Image(path)
}
// The PDF in tool result is still not supported
// bytes.isDocument -> {
// +Document(path)
// }
else -> {
+Text(text = Base64.encode(bytes))
}
}
}
// TODO should cache control be on result level?
cacheControl =
if (cache == true) CacheControl(type = CacheControl.Type.EPHEMERAL)
else null
}
}
}