-
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.
- Loading branch information
1 parent
8f160cc
commit 8936454
Showing
7 changed files
with
205 additions
and
47 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
openai-examples/src/main/scala/io/cequence/openaiscala/examples/BufferedImageHelper.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,28 @@ | ||
package io.cequence.openaiscala.examples | ||
|
||
import java.awt.image.RenderedImage | ||
import java.io.ByteArrayOutputStream | ||
import java.util.Base64 | ||
import javax.imageio.ImageIO | ||
|
||
trait BufferedImageHelper { | ||
|
||
protected def imageBase64Source( | ||
file: java.io.File | ||
): String = { | ||
val bufferedImage = ImageIO.read(file) | ||
Base64.getEncoder.encodeToString(imageToBytes(bufferedImage, "jpeg")) | ||
} | ||
|
||
protected 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 | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...c/main/scala/io/cequence/openaiscala/examples/nonopenai/FireworksAIDocumentInlining.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,44 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.OpenAIChatCompletionService | ||
|
||
import scala.concurrent.Future | ||
|
||
/** | ||
* Requires `FIREWORKS_API_KEY` environment variable to be set | ||
* | ||
* Check out the website for more information: | ||
* https://fireworks.ai/blog/document-inlining-launch | ||
*/ | ||
object FireworksAIDocumentInlining extends ExampleBase[OpenAIChatCompletionService] { | ||
|
||
private val fireworksModelPrefix = "accounts/fireworks/models/" | ||
override val service: OpenAIChatCompletionService = ChatCompletionProvider.fireworks | ||
|
||
val messages: Seq[BaseMessage] = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserSeqMessage( | ||
Seq( | ||
TextContent("What are the candidate's BA and MBA GPAs?"), | ||
ImageURLContent( | ||
"https://storage.googleapis.com/fireworks-public/test/sample_resume.pdf#transform=inline" | ||
) | ||
) | ||
) | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages, | ||
settings = CreateChatCompletionSettings( | ||
model = fireworksModelPrefix + NonOpenAIModelId.llama_v3p3_70b_instruct, | ||
temperature = Some(0), | ||
max_tokens = Some(1000) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |
66 changes: 66 additions & 0 deletions
66
...in/scala/io/cequence/openaiscala/examples/nonopenai/FireworksAIDocumentInliningJson.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,66 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.{ | ||
ChatCompletionResponseFormatType, | ||
CreateChatCompletionSettings | ||
} | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.OpenAIChatCompletionService | ||
import play.api.libs.json.Json | ||
import io.cequence.openaiscala.JsonFormats.jsonSchemaFormat | ||
|
||
import scala.concurrent.Future | ||
|
||
/** | ||
* Requires `FIREWORKS_API_KEY` environment variable to be set | ||
* | ||
* Check out the website for more information: | ||
* https://fireworks.ai/blog/document-inlining-launch | ||
*/ | ||
object FireworksAIDocumentInliningJson extends ExampleBase[OpenAIChatCompletionService] { | ||
|
||
private val fireworksModelPrefix = "accounts/fireworks/models/" | ||
override val service: OpenAIChatCompletionService = ChatCompletionProvider.fireworks | ||
|
||
val messages: Seq[BaseMessage] = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserSeqMessage( | ||
Seq( | ||
TextContent( | ||
"Extract the list of professional associations and accomplishments into JSON" | ||
), | ||
ImageURLContent( | ||
"https://storage.googleapis.com/fireworks-public/test/sample_resume.pdf#transform=inline" | ||
) | ||
) | ||
) | ||
) | ||
|
||
private val schema: JsonSchema = JsonSchema.Object( | ||
properties = Seq( | ||
"professional_associations" -> JsonSchema.Array(JsonSchema.String()), | ||
"accomplishment" -> JsonSchema.Array(JsonSchema.String()) | ||
), | ||
required = Seq("professional_associations", "accomplishment") | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages, | ||
settings = CreateChatCompletionSettings( | ||
model = fireworksModelPrefix + NonOpenAIModelId.llama_v3p3_70b_instruct, | ||
temperature = Some(0), | ||
max_tokens = Some(1000), | ||
// response_format_type = Some(ChatCompletionResponseFormatType.json_object), | ||
extra_params = Map( | ||
"response_format" -> Json.obj( | ||
"type" -> ChatCompletionResponseFormatType.json_object.toString, | ||
"schema" -> Json.toJson(schema) | ||
) | ||
) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |
54 changes: 54 additions & 0 deletions
54
...n/scala/io/cequence/openaiscala/examples/nonopenai/FireworksAIDocumentInliningLocal.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,54 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.OpenAIChatCompletionService | ||
|
||
import java.nio.file.{Files, Paths} | ||
import java.util.Base64 | ||
import scala.concurrent.Future | ||
|
||
/** | ||
* Requires `FIREWORKS_API_KEY` and `EXAMPLE_PDF_PATH` environment variables to be set | ||
* | ||
* Check out the website for more information: | ||
* https://fireworks.ai/blog/document-inlining-launch | ||
*/ | ||
object FireworksAIDocumentInliningLocal extends ExampleBase[OpenAIChatCompletionService] { | ||
|
||
private lazy val localPdfPath = sys.env("EXAMPLE_PDF_PATH") | ||
|
||
private val base64Pdf = { | ||
val pdfBytes = Files.readAllBytes(Paths.get(localPdfPath)) | ||
Base64.getEncoder.encodeToString(pdfBytes) | ||
} | ||
|
||
private val fireworksModelPrefix = "accounts/fireworks/models/" | ||
override val service: OpenAIChatCompletionService = ChatCompletionProvider.fireworks | ||
|
||
val messages: Seq[BaseMessage] = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserSeqMessage( | ||
Seq( | ||
TextContent("What are the candidate's BA and MBA GPAs?"), | ||
ImageURLContent( | ||
s"data:application/pdf;base64,${base64Pdf}#transform=inline" | ||
) | ||
) | ||
) | ||
) | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages, | ||
settings = CreateChatCompletionSettings( | ||
model = | ||
fireworksModelPrefix + NonOpenAIModelId.llama_v3p3_70b_instruct, // phi_3_vision_128k_instruct | ||
temperature = Some(0), | ||
max_tokens = Some(1000) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |