Skip to content

Commit 626e3b1

Browse files
committed
Gemini - refactor PromptFeedback JSON format handling and add new Google Gemini examples.
1 parent 6ff6be2 commit 626e3b1

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

google-gemini-client/src/main/scala/io/cequence/openaiscala/gemini/JsonFormats.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,19 @@ trait JsonFormats {
358358

359359
implicit lazy val topCandidatesFormat: Format[TopCandidates] = Json.format[TopCandidates]
360360

361-
implicit val promptFeedbackFormat: Format[PromptFeedback] = Json.format[PromptFeedback]
361+
implicit val promptFeedbackReads: Reads[PromptFeedback] = (
362+
(__ \ "blockReason").readNullable[BlockReason] and
363+
(__ \ "safetyRatings").readWithDefault[Seq[SafetyRating]](Nil)
364+
)(PromptFeedback.apply _)
365+
366+
implicit val promptFeedbackWrites: Writes[PromptFeedback] = (
367+
(__ \ "blockReason").writeNullable[BlockReason] and
368+
(__ \ "safetyRatings").write[Seq[SafetyRating]]
369+
)(unlift(PromptFeedback.unapply))
370+
371+
implicit val promptFeedbackFormat: Format[PromptFeedback] =
372+
Format(promptFeedbackReads, promptFeedbackWrites)
373+
362374
implicit val generateContentResponseFormat: Format[GenerateContentResponse] =
363375
Json.using[Json.WithDefaultValues].format[GenerateContentResponse]
364376

openai-examples/src/main/scala/io/cequence/openaiscala/examples/anthropic/AnthropicBedrockCreateChatCompletionWithOpenAIAdapter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object AnthropicBedrockCreateChatCompletionWithOpenAIAdapter
2020

2121
private val modelId =
2222
// using 'us.' prefix because of the cross-region inference (enabled only in the us)
23-
"us." + NonOpenAIModelId.bedrock_claude_3_5_haiku_20241022_v1_0
23+
"us." + NonOpenAIModelId.bedrock_claude_sonnet_4_20250514_v1_0
2424

2525
override protected def run: Future[_] =
2626
service
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.cequence.openaiscala.examples.googlegemini
2+
3+
import io.cequence.openaiscala.domain._
4+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
5+
import io.cequence.openaiscala.examples.ExampleBase
6+
import io.cequence.openaiscala.gemini.service.GeminiServiceFactory
7+
import io.cequence.openaiscala.service.OpenAIChatCompletionService
8+
9+
import scala.concurrent.Future
10+
import io.cequence.openaiscala.examples.BufferedImageHelper
11+
12+
/**
13+
* Requires `GOOGLE_API_KEY` environment variable to be set.
14+
*/
15+
object GoogleGeminiCreateChatCompletionImageWithOpenAIAdapter
16+
extends ExampleBase[OpenAIChatCompletionService]
17+
with BufferedImageHelper {
18+
19+
override val service: OpenAIChatCompletionService = GeminiServiceFactory.asOpenAI()
20+
21+
// provide a local jpeg here
22+
private lazy val localImagePath = sys.env("EXAMPLE_IMAGE_PATH")
23+
private val imageSource = imageBase64Source(new java.io.File(localImagePath))
24+
25+
val messages: Seq[BaseMessage] = Seq(
26+
SystemMessage("You are a helpful assistant."),
27+
UserSeqMessage(
28+
Seq(
29+
TextContent("What is in this picture?"),
30+
ImageURLContent(s"data:image/jpeg;base64,${imageSource}")
31+
)
32+
)
33+
)
34+
35+
private val modelId = NonOpenAIModelId.gemini_2_5_pro
36+
37+
override protected def run: Future[_] =
38+
service
39+
.createChatCompletion(
40+
messages = messages,
41+
settings = CreateChatCompletionSettings(
42+
model = modelId
43+
)
44+
)
45+
.map(printMessageContent)
46+
}

openai-examples/src/main/scala/io/cequence/openaiscala/examples/googlegemini/GoogleGeminiGenerateContentCached.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object GoogleGeminiGenerateContentCached extends ExampleBase[GeminiService] {
3232
finally source.close()
3333
}
3434

35-
private val model = NonOpenAIModelId.gemini_2_5_flash_preview_04_17
35+
private val model = NonOpenAIModelId.gemini_2_5_flash
3636

3737
private val knowledgeTextContent: Content =
3838
Content.textPart(

openai-examples/src/main/scala/io/cequence/openaiscala/examples/googlegemini/GoogleGeminiGenerateContentCachedWithOpenAIAdapter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object GoogleGeminiGenerateContentCachedWithOpenAIAdapter
3333
finally source.close()
3434
}
3535

36-
private val model = NonOpenAIModelId.gemini_1_5_flash_002
36+
private val model = NonOpenAIModelId.gemini_2_0_flash_lite
3737

3838
private val systemMessage = SystemMessage(systemPrompt + "\n" + knowledgeContent)
3939

0 commit comments

Comments
 (0)