Skip to content

Commit 26dac98

Browse files
committed
New google gemini examples with pdf files
1 parent b6edb22 commit 26dac98

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/domain/Content.scala

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ object Content {
8787
val `type`: String = "web_search_tool_result"
8888
}
8989

90+
case class WebFetchToolResultBlock(
91+
content: WebFetchToolResultContent,
92+
toolUseId: String
93+
) extends ContentBlock {
94+
val `type`: String = "web_fetch_tool_result"
95+
}
96+
9097
case class McpToolUseBlock(
9198
id: String,
9299
name: String,
@@ -335,6 +342,64 @@ object WebSearchToolResultContent {
335342
}
336343
}
337344

345+
sealed trait WebFetchToolResultContent
346+
347+
object WebFetchToolResultContent {
348+
349+
case class Success(
350+
document: Document,
351+
url: String,
352+
retrievedAt: String
353+
) extends WebFetchToolResultContent
354+
355+
case class Error(
356+
errorCode: WebFetchErrorCode
357+
) extends WebFetchToolResultContent
358+
with HasType {
359+
val `type`: String = "web_fetch_tool_result_error"
360+
}
361+
362+
case class Document(
363+
citations: Citations,
364+
source: Source,
365+
title: String
366+
)
367+
368+
case class Citations(
369+
enabled: Boolean
370+
)
371+
372+
case class Source(
373+
data: String,
374+
mediaType: String,
375+
`type`: String
376+
)
377+
378+
sealed trait WebFetchErrorCode extends EnumValue
379+
380+
object WebFetchErrorCode {
381+
case object invalid_tool_input extends WebFetchErrorCode
382+
case object url_too_long extends WebFetchErrorCode
383+
case object url_not_allowed extends WebFetchErrorCode
384+
case object url_not_accessible extends WebFetchErrorCode
385+
case object unsupported_content_type extends WebFetchErrorCode
386+
case object too_many_requests extends WebFetchErrorCode
387+
case object max_uses_exceeded extends WebFetchErrorCode
388+
case object unavailable extends WebFetchErrorCode
389+
390+
def values: Seq[WebFetchErrorCode] = Seq(
391+
invalid_tool_input,
392+
url_too_long,
393+
url_not_allowed,
394+
url_not_accessible,
395+
unsupported_content_type,
396+
too_many_requests,
397+
max_uses_exceeded,
398+
unavailable
399+
)
400+
}
401+
}
402+
338403
sealed trait McpToolResultContent
339404

340405
case class McpToolResultString(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
import java.util.Base64
12+
import java.nio.file.Files
13+
14+
/**
15+
* Requires `GOOGLE_API_KEY` environment variable to be set.
16+
*/
17+
object GoogleGeminiCreateChatCompletionPDFWithOpenAIAdapter
18+
extends ExampleBase[OpenAIChatCompletionService]
19+
with BufferedImageHelper {
20+
21+
override val service: OpenAIChatCompletionService = GeminiServiceFactory.asOpenAI()
22+
23+
// provide a local jpeg here
24+
private lazy val localImagePath = sys.env("EXAMPLE_PDF_PATH")
25+
26+
private val pdfBase64Source =
27+
Base64.getEncoder.encodeToString(
28+
Files.readAllBytes(new java.io.File(localImagePath).toPath)
29+
)
30+
31+
val messages: Seq[BaseMessage] = Seq(
32+
SystemMessage("You are a helpful assistant."),
33+
UserSeqMessage(
34+
Seq(
35+
TextContent("What is in this picture?"),
36+
ImageURLContent(s"data:application/pdf;base64,${pdfBase64Source}")
37+
)
38+
)
39+
)
40+
41+
private val modelId = NonOpenAIModelId.gemini_2_5_pro
42+
43+
override protected def run: Future[_] =
44+
service
45+
.createChatCompletion(
46+
messages = messages,
47+
settings = CreateChatCompletionSettings(
48+
model = modelId
49+
)
50+
)
51+
.map(printMessageContent)
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import java.util.Base64
12+
import java.nio.file.Files
13+
import io.cequence.openaiscala.domain.settings.JsonSchemaDef
14+
import io.cequence.openaiscala.domain.settings.ChatCompletionResponseFormatType
15+
16+
/**
17+
* Requires `GOOGLE_API_KEY` environment variable to be set.
18+
*/
19+
object GoogleGeminiCreateChatCompletionPdfJSONWithOpenAIAdapter
20+
extends ExampleBase[OpenAIChatCompletionService]
21+
with BufferedImageHelper {
22+
23+
override val service: OpenAIChatCompletionService = GeminiServiceFactory.asOpenAI()
24+
25+
// provide a local jpeg here
26+
private lazy val localImagePath = sys.env("EXAMPLE_PDF_PATH")
27+
28+
private val pdfBase64Source =
29+
Base64.getEncoder.encodeToString(
30+
Files.readAllBytes(new java.io.File(localImagePath).toPath)
31+
)
32+
33+
val messages: Seq[BaseMessage] = Seq(
34+
SystemMessage("You are a helpful assistant."),
35+
UserSeqMessage(
36+
Seq(
37+
TextContent("What is in this pdf?"),
38+
ImageURLContent(s"data:application/pdf;base64,${pdfBase64Source}")
39+
)
40+
)
41+
)
42+
43+
private val jsonSchema =
44+
JsonSchema.Array(
45+
items = JsonSchema.Object(
46+
properties = Map(
47+
"itemName" -> JsonSchema.String(),
48+
"value" -> JsonSchema.String(),
49+
"comment" -> JsonSchema.String()
50+
)
51+
)
52+
)
53+
54+
private val modelId = NonOpenAIModelId.gemini_2_5_pro
55+
56+
override protected def run: Future[_] =
57+
service
58+
.createChatCompletion(
59+
messages = messages,
60+
settings = CreateChatCompletionSettings(
61+
model = modelId,
62+
response_format_type = Some(ChatCompletionResponseFormatType.json_schema),
63+
jsonSchema = Some(
64+
JsonSchemaDef(
65+
name = "extraction_response",
66+
strict = false,
67+
structure = jsonSchema
68+
)
69+
)
70+
)
71+
)
72+
.map(printMessageContent)
73+
}

0 commit comments

Comments
 (0)