-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New examples: CreateChatCompletion, GPTVisionWithURL, and GPTVisionWi…
…thLocalFile
- Loading branch information
1 parent
6779db1
commit d2db659
Showing
4 changed files
with
115 additions
and
1 deletion.
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
26 changes: 26 additions & 0 deletions
26
openai-examples/src/main/scala/io/cequence/openaiscala/examples/CreateChatCompletion.scala
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,26 @@ | ||
package io.cequence.openaiscala.examples | ||
|
||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.domain._ | ||
|
||
import scala.concurrent.Future | ||
|
||
object CreateChatCompletion extends Example { | ||
|
||
val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserMessage("What is the weather like in Norway?") | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = ModelId.gpt_4_turbo_preview, | ||
temperature = Some(0), | ||
max_tokens = Some(100) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |
53 changes: 53 additions & 0 deletions
53
openai-examples/src/main/scala/io/cequence/openaiscala/examples/GPTVisionWithLocalFile.scala
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,53 @@ | ||
package io.cequence.openaiscala.examples | ||
|
||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
|
||
import java.awt.image.RenderedImage | ||
import java.io.ByteArrayOutputStream | ||
import java.util.Base64 | ||
import javax.imageio.ImageIO | ||
import scala.concurrent.Future | ||
|
||
object GPTVisionWithLocalFile extends Example { | ||
|
||
// provide a local jpeg here | ||
private val localImagePath = sys.env("EXAMPLE_IMAGE_PATH") | ||
private val bufferedImage = ImageIO.read(new java.io.File(localImagePath)) | ||
private val imageBase64Source = | ||
Base64.getEncoder.encodeToString(imageToBytes(bufferedImage, "jpeg")) | ||
|
||
val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserSeqMessage( | ||
Seq( | ||
TextContent("What is in this picture?"), | ||
ImageURLContent(s"data:image/jpeg;base64,${imageBase64Source}") | ||
) | ||
) | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = ModelId.gpt_4_vision_preview, | ||
temperature = Some(0), | ||
max_tokens = Some(300) | ||
) | ||
) | ||
.map(printMessageContent) | ||
|
||
private def imageToBytes( | ||
image: RenderedImage, | ||
format: String | ||
): Array[Byte] = { | ||
val baos = new ByteArrayOutputStream() | ||
ImageIO.write(image, format, baos) | ||
baos.flush() | ||
val imageInByte = baos.toByteArray | ||
baos.close() | ||
imageInByte | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
openai-examples/src/main/scala/io/cequence/openaiscala/examples/GPTVisionWithURL.scala
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,33 @@ | ||
package io.cequence.openaiscala.examples | ||
|
||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
|
||
import scala.concurrent.Future | ||
|
||
object GPTVisionWithURL extends Example { | ||
|
||
val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserSeqMessage( | ||
Seq( | ||
TextContent("What is in this picture?"), | ||
ImageURLContent( | ||
"https://upload.wikimedia.org/wikipedia/commons/d/df/Hefeweizen_Glass.jpg" | ||
) | ||
) | ||
) | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = ModelId.gpt_4_vision_preview, | ||
temperature = Some(0), | ||
max_tokens = Some(300) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |