-
Notifications
You must be signed in to change notification settings - Fork 24
/
AnthropicCreateSystemMessage.scala
44 lines (37 loc) · 1.71 KB
/
AnthropicCreateSystemMessage.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
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 `ANTHROPIC_API_KEY` environment variable to be set
object AnthropicCreateSystemMessage extends ExampleBase[AnthropicService] {
override protected val service: AnthropicService = AnthropicServiceFactory()
val systemMessages: Seq[Message] = Seq(
SystemMessage("Talk in pirate speech")
)
val messages: Seq[Message] = Seq(
UserMessage("Who is the most famous football player in the World?")
)
override protected def run: Future[_] =
service
.createMessage(
systemMessages ++ messages,
settings = AnthropicCreateMessageSettings(
model = NonOpenAIModelId.claude_3_haiku_20240307,
max_tokens = 4096
)
)
.map(printMessageContent)
private def printMessageContent(response: CreateMessageResponse) = {
val text =
response.content.blocks.collect { case ContentBlockBase(TextBlock(text), _) => text }
.mkString(" ")
println(text)
}
}