-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add "vision" feature (#258)
- Loading branch information
Showing
14 changed files
with
687 additions
and
19 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
55 changes: 55 additions & 0 deletions
55
openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestChatVision.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.chat.* | ||
import com.aallam.openai.api.chat.internal.ToolType | ||
import com.aallam.openai.api.model.ModelId | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import kotlin.test.* | ||
|
||
class TestChatVision : TestOpenAI() { | ||
|
||
@Test | ||
fun textimage() = test { | ||
val request = chatCompletionRequest { | ||
model = ModelId("gpt-4-vision-preview") | ||
messages { | ||
user { | ||
content { | ||
text("What’s in this image?") | ||
image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg") | ||
} | ||
} | ||
} | ||
maxTokens = 300 | ||
} | ||
val response = openAI.chatCompletion(request) | ||
val content = response.choices.first().message.content.orEmpty() | ||
assertNotNull(content) | ||
} | ||
|
||
@Test | ||
fun multiImage() = test { | ||
val request = chatCompletionRequest { | ||
model = ModelId("gpt-4-vision-preview") | ||
messages { | ||
user { | ||
content { | ||
text("What are in these images? Is there any difference between them?") | ||
image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg") | ||
image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg") | ||
} | ||
} | ||
} | ||
maxTokens = 300 | ||
} | ||
val response = openAI.chatCompletion(request) | ||
val content = response.choices.first().message.content.orEmpty() | ||
assertNotNull(content) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
openai-client/src/jvmTest/kotlin/com/aallam/openai/client/TestChatVisionJVM.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.chat.* | ||
import com.aallam.openai.api.chat.internal.ToolType | ||
import com.aallam.openai.api.model.ModelId | ||
import com.aallam.openai.client.TestOpenAI | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import okio.FileSystem | ||
import okio.Okio | ||
import okio.Path.Companion.toPath | ||
import kotlin.test.* | ||
|
||
class TestChatVisionJVM : TestOpenAI() { | ||
|
||
@Test | ||
fun encoded() = test { | ||
val byteString = FileSystem.RESOURCES.read("nature.jpeg".toPath()) { | ||
readByteString() | ||
} | ||
val encoded = byteString.base64() | ||
val request = chatCompletionRequest { | ||
model = ModelId("gpt-4-vision-preview") | ||
messages { | ||
user { | ||
content { | ||
text("What’s in this image?") | ||
image("data:image/jpeg;base64,$encoded") | ||
} | ||
} | ||
} | ||
maxTokens = 300 | ||
} | ||
val response = openAI.chatCompletion(request) | ||
val content = response.choices.first().message.content.orEmpty() | ||
assertNotNull(content) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.