-
-
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.
StructuredOutputTest.kt became a demo
- Loading branch information
Showing
1 changed file
with
3 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,7 @@ | ||
package com.xemantic.anthropic | ||
|
||
import com.xemantic.anthropic.message.* | ||
import com.xemantic.anthropic.tool.AnthropicTool | ||
import com.xemantic.anthropic.tool.UsableTool | ||
import io.kotest.assertions.assertSoftly | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldStartWith | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.Serializable | ||
import kotlin.test.Ignore | ||
import kotlin.test.Test | ||
|
||
/** | ||
* This test tool is based on the | ||
* [article by Dan Nguyen](https://gist.github.com/dannguyen/faaa56cebf30ad51108a9fe4f8db36d8), | ||
* who showed how to extract financial disclosure reports as structured data by using OpenAI API. | ||
* I wanted to try out the same approach with Anthropic API, and it seems like a great test case of this library. | ||
*/ | ||
@AnthropicTool( | ||
name = "DisclosureReport", | ||
description = "Extract the text from this image" | ||
) | ||
class DisclosureReport( | ||
val assets: List<Asset> | ||
) : UsableTool { | ||
override suspend fun use(toolUseId: String) = ToolResult( | ||
toolUseId, "Data provided to client" | ||
) | ||
} | ||
|
||
@Serializable | ||
data class Asset( | ||
val assetName: String, | ||
val owner: String, | ||
val location: String?, | ||
val assetValueLow: Int?, | ||
val assetValueHigh: Int?, | ||
val incomeType: String, | ||
val incomeLow: Int?, | ||
val incomeHigh: Int?, | ||
val txGt1000: Boolean | ||
) | ||
|
||
/** | ||
* This test is located in the jvmTest folder, so it can use File API to read image files. | ||
* In the future it can probably be moved to jvmAndPosix to support all the Kotlin platforms | ||
* having access to the filesystem. | ||
* This test case was moved | ||
* (here)[https://github.com/xemantic/anthropic-sdk-kotlin-demo/blob/main/anthropic-sdk-kotlin-demo-jvm/src/main/kotlin/Demo07OcrToStructuredOutput.kt] | ||
* as a demo. | ||
*/ | ||
class StructuredOutputTest { | ||
|
||
@Test | ||
@Ignore // to be moved to anthropic-sdk-kotlin-demo soon | ||
fun shouldDecodeStructuredOutputFromReportImage() = runTest { | ||
val client = Anthropic { | ||
tool<DisclosureReport>() | ||
} | ||
|
||
val response = client.messages.create { | ||
+Message { | ||
+"Decode structured output from supplied image" | ||
+Image( | ||
path = "test-data/financial-disclosure-report.png", | ||
mediaType = Image.MediaType.IMAGE_PNG | ||
) | ||
} | ||
useTool<DisclosureReport>() | ||
} | ||
|
||
val tool = response.content.filterIsInstance<ToolUse>().first() | ||
val report = tool.input<DisclosureReport>() | ||
|
||
report.assets shouldHaveSize 6 | ||
assertSoftly(report.assets[0]) { | ||
// it seems that sometimes the model is not returning content in square brackets | ||
// so we are going for shouldStartWith | ||
assetName shouldStartWith "11 Zinfandel Lane - Home & Vineyard" | ||
owner shouldBe "JT" | ||
location shouldBe "St. Helena/Napa, CA, US" | ||
assetValueLow shouldBe 5000001 | ||
assetValueHigh shouldBe 25000000 | ||
incomeType shouldBe "Grape Sales" | ||
incomeLow shouldBe 100001 | ||
incomeHigh shouldBe 1000000 | ||
txGt1000 shouldBe false | ||
} | ||
assertSoftly(report.assets[1]) { | ||
assetName shouldStartWith "25 Point Lobos - Commercial Property" | ||
owner shouldBe "SP" | ||
location shouldBe "San Francisco/San Francisco, CA, US" | ||
assetValueLow shouldBe 5000001 | ||
assetValueHigh shouldBe 25000000 | ||
incomeType shouldBe "Rent" | ||
incomeLow shouldBe 100001 | ||
incomeHigh shouldBe 1000000 | ||
txGt1000 shouldBe false | ||
} | ||
} | ||
|
||
} |