Skip to content

Commit

Permalink
New examples: CreateChatCompletion, GPTVisionWithURL, and GPTVisionWi…
Browse files Browse the repository at this point in the history
…thLocalFile
  • Loading branch information
peterbanda committed Nov 27, 2023
1 parent 6779db1 commit d2db659
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ case class FileInfo(
created_at: ju.Date,
updated_at: Option[ju.Date],
filename: String,
// The intended purpose of the file.
// Supported values are fine-tune, fine-tune-results, assistants, and assistants_output.
purpose: String,
status: String, // uploaded, processed, pending, error, deleting or deleted
status_details: Option[String],
statistics: Option[FileStatistics]
statistics: Option[FileStatistics] // provided by Azure
)

case class FileStatistics(
Expand Down
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)
}
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
}
}
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)
}

0 comments on commit d2db659

Please sign in to comment.