-
-
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.
Refactoring: new content package, document support (PDF upload), and …
…simplified use of tools. (#15)
- Loading branch information
Showing
31 changed files
with
658 additions
and
318 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
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
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
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,25 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import com.xemantic.anthropic.message.Content | ||
|
||
interface ContentBuilder { | ||
|
||
val content: MutableList<Content> | ||
|
||
operator fun Content.unaryPlus() { | ||
content += this | ||
} | ||
|
||
operator fun String.unaryPlus() { | ||
content += Text(this) | ||
} | ||
|
||
operator fun Number.unaryPlus() { | ||
content += Text(this.toString()) | ||
} | ||
|
||
operator fun Collection<Content>.unaryPlus() { | ||
content += this | ||
} | ||
|
||
} |
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,42 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import com.xemantic.anthropic.cache.CacheControl | ||
import com.xemantic.anthropic.message.Content | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
@SerialName("document") | ||
data class Document( | ||
val source: Source, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
enum class MediaType { | ||
@SerialName("application/pdf") | ||
APPLICATION_PDF | ||
} | ||
|
||
@Serializable | ||
data class Source( | ||
val type: Type = Type.BASE64, | ||
@SerialName("media_type") | ||
val mediaType: MediaType, | ||
val data: String | ||
) { | ||
|
||
enum class Type { | ||
@SerialName("base64") | ||
BASE64 | ||
} | ||
|
||
} | ||
|
||
class Builder { | ||
var data: ByteArray? = null | ||
var mediaType: MediaType? = null | ||
var cacheControl: CacheControl? = null | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/commonMain/kotlin/image/Images.kt → src/commonMain/kotlin/content/Image.kt
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
2 changes: 1 addition & 1 deletion
2
src/commonMain/kotlin/text/Text.kt → src/commonMain/kotlin/content/Text.kt
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
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,103 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import com.xemantic.anthropic.anthropicJson | ||
import com.xemantic.anthropic.cache.CacheControl | ||
import com.xemantic.anthropic.message.Content | ||
import com.xemantic.anthropic.message.toNullIfEmpty | ||
import com.xemantic.anthropic.tool.Tool | ||
import com.xemantic.anthropic.tool.ToolInput | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.Transient | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
@Serializable | ||
@SerialName("tool_use") | ||
data class ToolUse( | ||
val id: String, | ||
val name: String, | ||
val input: JsonObject, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
@Transient | ||
@PublishedApi | ||
internal lateinit var tool: Tool | ||
|
||
@PublishedApi | ||
internal fun decodeInput() = anthropicJson.decodeFromJsonElement( | ||
deserializer = tool.inputSerializer, | ||
element = input | ||
).apply(tool.inputInitializer) | ||
|
||
inline fun <reified T : ToolInput> input(): T = (decodeInput() as T) | ||
|
||
suspend fun use(): ToolResult { | ||
return try { | ||
if (::tool.isInitialized) { | ||
val toolInput = decodeInput() | ||
toolInput.use(toolUseId = id) | ||
} else { | ||
ToolResult(toolUseId = id) { | ||
error("Cannot use unknown tool: $name") | ||
} | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
ToolResult(toolUseId = id) { | ||
error(e.message ?: "Unknown error occurred") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
@Serializable | ||
@SerialName("tool_result") | ||
data class ToolResult( | ||
@SerialName("tool_use_id") | ||
val toolUseId: String, | ||
val content: List<Content>? = null, | ||
@SerialName("is_error") | ||
val isError: Boolean? = false, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
class Builder : ContentBuilder { | ||
|
||
override val content: MutableList<Content> = mutableListOf() | ||
|
||
var isError: Boolean? = null | ||
var cacheControl: CacheControl? = null | ||
|
||
fun error(message: String) { | ||
+message | ||
isError = true | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
inline fun ToolResult( | ||
toolUseId: String, | ||
block: ToolResult.Builder.() -> Unit = {} | ||
): ToolResult { | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val builder = ToolResult.Builder() | ||
block(builder) | ||
return ToolResult( | ||
toolUseId = toolUseId, | ||
content = builder.content.toNullIfEmpty(), | ||
isError = if (builder.isError == null) false else null, | ||
cacheControl = builder.cacheControl | ||
) | ||
} |
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
Oops, something went wrong.