-
Notifications
You must be signed in to change notification settings - Fork 24
/
AnthropicBedrockCreateMessage.scala
47 lines (39 loc) · 1.88 KB
/
AnthropicBedrockCreateMessage.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package io.cequence.openaiscala.examples.nonopenai
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.TextBlock
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlockBase
import io.cequence.openaiscala.anthropic.domain.Message
import io.cequence.openaiscala.anthropic.domain.Message.{SystemMessage, UserMessage}
import io.cequence.openaiscala.anthropic.domain.response.CreateMessageResponse
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
import io.cequence.openaiscala.domain.NonOpenAIModelId
import io.cequence.openaiscala.examples.ExampleBase
import scala.concurrent.Future
// requires `openai-scala-anthropic-client` as a dependency and 'AWS_BEDROCK_ACCESS_KEY', 'AWS_BEDROCK_SECRET_KEY', 'AWS_BEDROCK_REGION' environment variable to be set
object AnthropicBedrockCreateMessage extends ExampleBase[AnthropicService] {
override protected val service: AnthropicService = AnthropicServiceFactory.forBedrock()
private val messages: Seq[Message] = Seq(
SystemMessage("You are a drunk assistant!"),
UserMessage("What is the weather like in Norway?")
)
private val modelId =
// using 'us.' prefix because of the cross-region inference (enabled only in the us)
"us." + NonOpenAIModelId.bedrock_claude_3_5_sonnet_20241022_v2_0
override protected def run: Future[_] =
service
.createMessage(
messages,
settings = AnthropicCreateMessageSettings(
model = modelId,
max_tokens = 4096,
temperature = Some(1.0)
)
)
.map(printMessageContent)
private def printMessageContent(response: CreateMessageResponse) = {
val text =
response.content.blocks.collect { case ContentBlockBase(TextBlock(text), _) => text }
.mkString(" ")
println(text)
}
}