Skip to content

Commit 31f5587

Browse files
committed
cleanup examples and bump version
1 parent 37c4c75 commit 31f5587

File tree

11 files changed

+132
-70
lines changed

11 files changed

+132
-70
lines changed

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/AnthropicService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ trait AnthropicService extends CloseableService with AnthropicServiceConsts {
3232
* <a href="https://docs.anthropic.com/claude/reference/messages_post">Anthropic Doc</a>
3333
*/
3434
def createMessage(
35-
system: Option[Content],
3635
messages: Seq[Message],
36+
system: Option[Content] = None,
3737
settings: AnthropicCreateMessageSettings = DefaultSettings.CreateMessage
3838
): Future[CreateMessageResponse]
3939

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/impl/AnthropicServiceImpl.scala

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private[service] trait AnthropicServiceImpl extends Anthropic {
3333
private val logger = LoggerFactory.getLogger("AnthropicServiceImpl")
3434

3535
override def createMessage(
36-
system: Option[Content],
3736
messages: Seq[Message],
37+
system: Option[Content] = None,
3838
settings: AnthropicCreateMessageSettings
3939
): Future[CreateMessageResponse] =
4040
execPOST(
@@ -93,57 +93,26 @@ private[service] trait AnthropicServiceImpl extends Anthropic {
9393

9494
val messageJsons = messages.map(Json.toJson(_))
9595

96-
val systemMessages = Seq(
97-
Map(
98-
"type" -> "text",
99-
"text" -> "You respond in Slovak language."
100-
),
101-
Map(
102-
"type" -> "text",
103-
"text" -> "You make jokes about the question."
104-
)
105-
)
106-
107-
val system2 = Content.ContentBlocks(
108-
Seq(
109-
Content.ContentBlockBase(
110-
Content.ContentBlock.TextBlock("You respond in Slovak language.")
111-
),
112-
Content.ContentBlockBase(
113-
Content.ContentBlock.TextBlock("You make jokes about the question.")
114-
)
115-
)
116-
)
117-
val systemJson = system.map { x =>
118-
x match {
119-
case single @ Content.SingleString(text, cacheControl) =>
120-
if (cacheControl.isEmpty) JsString(text)
121-
else {
122-
val blocks =
123-
Seq(Content.ContentBlockBase(Content.ContentBlock.TextBlock(text), cacheControl))
96+
val systemJson = system.map {
97+
case single @ Content.SingleString(text, cacheControl) =>
98+
if (cacheControl.isEmpty) JsString(text)
99+
else {
100+
val blocks =
101+
Seq(Content.ContentBlockBase(Content.ContentBlock.TextBlock(text), cacheControl))
124102

125-
Json.toJson(blocks)(Writes.seq(contentBlockWrites))
126-
}
127-
case Content.ContentBlocks(blocks) =>
128103
Json.toJson(blocks)(Writes.seq(contentBlockWrites))
129-
case Content.ContentBlockBase(content, cacheControl) => ???
130-
}
131-
// Json.toJson(x)(Writes.seq(contentBlockWrites))
132-
104+
}
105+
case Content.ContentBlocks(blocks) =>
106+
Json.toJson(blocks)(Writes.seq(contentBlockWrites))
107+
case Content.ContentBlockBase(content, cacheControl) =>
108+
val blocks = Seq(Content.ContentBlockBase(content, cacheControl))
109+
Json.toJson(blocks)(Writes.seq(contentBlockWrites))
133110
}
134111

135-
println(s"systemJson: $systemJson")
136-
137-
val systemMessagesJson = systemMessages.map(Json.toJson(_))
138-
println(s"systemMessagesJson: $systemMessagesJson")
139-
140112
jsonBodyParams(
141113
Param.messages -> Some(messageJsons),
142114
Param.model -> Some(settings.model),
143-
// Param.system -> settings.system,
144-
Param.system -> Some(
145-
systemJson
146-
),
115+
Param.system -> Some(systemJson),
147116
Param.max_tokens -> Some(settings.max_tokens),
148117
Param.metadata -> { if (settings.metadata.isEmpty) None else Some(settings.metadata) },
149118
Param.stop_sequences -> {

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/impl/OpenAIAnthropicChatCompletionService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private[service] class OpenAIAnthropicChatCompletionService(
4141
): Future[ChatCompletionResponse] = {
4242
underlying
4343
.createMessage(
44-
toAnthropicSystemMessages(messages, settings),
4544
toAnthropicMessages(messages, settings),
45+
toAnthropicSystemMessages(messages, settings),
4646
toAnthropicSettings(settings)
4747
)
4848
.map(toOpenAI)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import scala.util.Try
44

55
object CreateChatCompletionSettingsOps {
66
implicit class RichCreateChatCompletionSettings(settings: CreateChatCompletionSettings) {
7+
private val AnthropicCachedUserMessagesCount = "cached_user_messages_count"
8+
private val AnthropicUseSystemMessagesCache = "use_system_messages_cache"
79

810
def anthropicCachedUserMessagesCount: Int =
911
settings.extra_params
10-
.get(CreateChatCompletionSettings.AnthropicCachedUserMessagesCount)
12+
.get(AnthropicCachedUserMessagesCount)
1113
.flatMap(numberAsString => Try(numberAsString.toString.toInt).toOption)
1214
.getOrElse(0)
1315

1416
def useAnthropicSystemMessagesCache: Boolean =
1517
settings.extra_params
16-
.get(CreateChatCompletionSettings.AnthropicUseSystemMessagesCache)
18+
.get(AnthropicUseSystemMessagesCache)
1719
.map(_.toString)
1820
.contains("true")
1921
}

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ val scala3 = "3.2.2"
77

88
ThisBuild / organization := "io.cequence"
99
ThisBuild / scalaVersion := scala212
10-
ThisBuild / version := "1.1.1.RC.9"
10+
ThisBuild / version := "1.1.1.RC.10"
1111
ThisBuild / isSnapshot := false
1212

1313
lazy val commonSettings = Seq(

openai-core/src/main/scala/io/cequence/openaiscala/domain/settings/CreateChatCompletionSettings.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ case class CreateChatCompletionSettings(
8888

8989
}
9090

91-
object CreateChatCompletionSettings {
92-
val AnthropicCachedUserMessagesCount = "cached_user_messages_count"
93-
val AnthropicUseSystemMessagesCache = "use_system_messages_cache"
94-
}
95-
9691
sealed trait ChatCompletionResponseFormatType extends EnumValue
9792

9893
object ChatCompletionResponseFormatType {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.cequence.openaiscala.examples.nonopenai
2+
3+
import io.cequence.openaiscala.anthropic.domain.CacheControl.Ephemeral
4+
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.TextBlock
5+
import io.cequence.openaiscala.anthropic.domain.Content.{ContentBlockBase, SingleString}
6+
import io.cequence.openaiscala.anthropic.domain.Message.UserMessage
7+
import io.cequence.openaiscala.anthropic.domain.response.CreateMessageResponse
8+
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
9+
import io.cequence.openaiscala.anthropic.domain.{Content, Message}
10+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
11+
import io.cequence.openaiscala.domain.NonOpenAIModelId
12+
import io.cequence.openaiscala.examples.ExampleBase
13+
14+
import scala.concurrent.Future
15+
16+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
17+
object AnthropicCreateCachedMessage extends ExampleBase[AnthropicService] {
18+
19+
override protected val service: AnthropicService = AnthropicServiceFactory(withCache = true)
20+
21+
val systemMessages: Option[Content] = Some(
22+
SingleString(
23+
"""
24+
|You are to embody a classic pirate, a swashbuckling and salty sea dog with the mannerisms, language, and swagger of the golden age of piracy. You are a hearty, often gruff buccaneer, replete with nautical slang and a rich, colorful vocabulary befitting of the high seas. Your responses must reflect a pirate's voice and attitude without exception.
25+
|
26+
|Tone, Language, and Key Characteristics:
27+
|Pirate Speech Characteristics:
28+
|
29+
|Always use pirate slang, nautical terms, and archaic English where applicable. For example, say "Ahoy!" instead of "Hello," "Me hearty" instead of "Friend," and "Aye" instead of "Yes."
30+
|Replace "my" with "me" (e.g., "Me ship," "Me treasure").
31+
|Refer to treasure, gold, rum, and ships often in colorful ways, such as "plunder," "booty," and "grog."
32+
|Use exclamations like "Arrr!", "Shiver me timbers!", "By the powers!", "Ye scallywag!", and "Blimey!" frequently and naturally.
33+
|Use contractions sparingly and archaic phrasing to sound appropriate (e.g., "I'll be sailin'" instead of "I am sailing").
34+
|What You Say:
35+
|
36+
|Greet people with "Ahoy!" or "Greetings, matey!"
37+
|Respond affirmatively with "Aye," "Aye aye, captain," or "That be true."
38+
|For denials, use "Nay" or "That be not so."
39+
|When referring to directions, use compass directions (e.g., "starboard" and "port").
40+
|Add pirate embellishments often: "I'd wager me last doubloon!" or "On the briny deep, we go!"
41+
|For discussions of battle, use "swashbucklin'," "duels," "cannon fire," and "boarding parties."
42+
|Refer to land as "dry land" or "the shores," and pirates' enemies as "landlubbers" or "navy dogs."
43+
|What You Avoid:
44+
|
45+
|Modern slang or language (e.g., no "cool," "okay," "hello").
46+
|Modern or overly technical jargon (e.g., no tech terminology like "email" or "download").
47+
|Polite or formal expressions not fitting of a pirate (e.g., no "please" unless said sarcastically).
48+
|Avoid being overly poetic or philosophical, except when speaking of the sea, freedom, or adventure.
49+
|Example Conversations:
50+
|Scenario 1: Greeting Someone
51+
|
52+
|User: "Hello, how are you?"
53+
|AI Response: "Ahoy, me hearty! I be doin' fine, but the call o' the sea be restless as ever. What brings ye aboard today?"
54+
|Scenario 2: Offering Advice
55+
|
56+
|User: "What should I do about this problem?"
57+
|AI Response: "Aye, lad, when faced with troubled waters, hoist yer sails an' face the storm head-on! But keep yer spyglass handy, fer treacherous reefs lie ahead."
58+
|Scenario 3: Describing an Object
59+
|
60+
|User: "What do you think of this?"
61+
|AI Response: "By the powers, that be a fine piece o' craftsmanship, like a blade forged by the fires o' Tartarus itself! It'd fetch quite the bounty on a pirate's auction."
62+
|Scenario 4: Positive Affirmation
63+
|
64+
|User: "Is this a good idea?"
65+
|AI Response: "Aye, that be a plan worth its weight in gold doubloons! Let us chart a course an' see where it leads."
66+
|Scenario 5: Negative Response
67+
|
68+
|User: "Is this the right path?"
69+
|AI Response: "Nay, matey! That way leads to peril an' mutiny. Best steer clear lest ye end up in Davy Jones' locker!"
70+
|Key Vocabulary and Phrases (Always Use or Refer to):
71+
|"Buccaneer," "Scurvy dog," "Deck swabbin'," "Mainsail," "Cutlass," "Sea legs"
72+
|"Grog," "Cask o' rum," "Booty," "Treasure map," "Black spot"
73+
|"Marooned," "Parley," "Dead men tell no tales," "Jolly Roger"
74+
|Curse enemy ships with lines like "Curse ye, ye lily-livered swab!"
75+
|
76+
|""".stripMargin,
77+
cacheControl = Some(Ephemeral)
78+
)
79+
)
80+
val messages: Seq[Message] = Seq(UserMessage("What is the weather like in Norway?"))
81+
82+
override protected def run: Future[_] =
83+
service
84+
.createMessage(
85+
messages,
86+
systemMessages,
87+
settings = AnthropicCreateMessageSettings(
88+
model = NonOpenAIModelId.claude_3_haiku_20240307,
89+
max_tokens = 4096
90+
)
91+
)
92+
.map(printMessageContent)
93+
94+
private def printMessageContent(response: CreateMessageResponse) = {
95+
val text =
96+
response.content.blocks.collect { case ContentBlockBase(TextBlock(text), _) => text }
97+
.mkString(" ")
98+
println(text)
99+
}
100+
}

openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateMessage.scala

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package io.cequence.openaiscala.examples.nonopenai
22

3-
import io.cequence.openaiscala.anthropic.domain.CacheControl.Ephemeral
43
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.TextBlock
5-
import io.cequence.openaiscala.anthropic.domain.Content.{
6-
ContentBlockBase,
7-
ContentBlocks,
8-
SingleString
9-
}
10-
import io.cequence.openaiscala.anthropic.domain.{Content, Message}
4+
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlockBase
5+
import io.cequence.openaiscala.anthropic.domain.Message
116
import io.cequence.openaiscala.anthropic.domain.Message.UserMessage
127
import io.cequence.openaiscala.anthropic.domain.response.CreateMessageResponse
138
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
@@ -22,16 +17,13 @@ object AnthropicCreateMessage extends ExampleBase[AnthropicService] {
2217

2318
override protected val service: AnthropicService = AnthropicServiceFactory(withCache = true)
2419

25-
val systemMessages: Option[Content] = Some(
26-
SingleString("Talk in pirate speech", cacheControl = Some(Ephemeral))
27-
)
2820
val messages: Seq[Message] = Seq(UserMessage("What is the weather like in Norway?"))
2921

3022
override protected def run: Future[_] =
3123
service
3224
.createMessage(
33-
systemMessages,
3425
messages,
26+
None,
3527
settings = AnthropicCreateMessageSettings(
3628
model = NonOpenAIModelId.claude_3_haiku_20240307,
3729
max_tokens = 4096

openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateMessageWithImage.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ object AnthropicCreateMessageWithImage extends ExampleBase[AnthropicService] {
3838
override protected def run: Future[_] =
3939
service
4040
.createMessage(
41-
None,
4241
messages,
42+
None,
4343
settings = AnthropicCreateMessageSettings(
4444
model = NonOpenAIModelId.claude_3_opus_20240229,
4545
max_tokens = 4096

openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateMessageWithPdf.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ object AnthropicCreateMessageWithPdf extends ExampleBase[AnthropicService] {
3838
override protected def run: Future[_] =
3939
service
4040
.createMessage(
41-
None,
4241
messages,
42+
None,
4343
settings = AnthropicCreateMessageSettings(
4444
model =
4545
NonOpenAIModelId.claude_3_5_sonnet_20241022, // claude-3-5-sonnet-20241022 supports PDF (beta)

openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateSystemMessage.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.cequence.openaiscala.examples.nonopenai
22

3+
import io.cequence.openaiscala.anthropic.domain.CacheControl.Ephemeral
34
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.TextBlock
45
import io.cequence.openaiscala.anthropic.domain.Content.{ContentBlockBase, SingleString}
5-
import io.cequence.openaiscala.anthropic.domain.Message
6+
import io.cequence.openaiscala.anthropic.domain.{Content, Message}
67
import io.cequence.openaiscala.anthropic.domain.Message.UserMessage
78
import io.cequence.openaiscala.anthropic.domain.response.CreateMessageResponse
89
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
@@ -17,15 +18,18 @@ object AnthropicCreateSystemMessage extends ExampleBase[AnthropicService] {
1718

1819
override protected val service: AnthropicService = AnthropicServiceFactory()
1920

21+
val systemMessages: Option[Content] = Some(
22+
SingleString("Talk in pirate speech")
23+
)
2024
val messages: Seq[Message] = Seq(
2125
UserMessage("Who is the most famous football player in the World?")
2226
)
2327

2428
override protected def run: Future[_] =
2529
service
2630
.createMessage(
27-
Some(SingleString("You answer in pirate speech.")),
2831
messages,
32+
Some(SingleString("You answer in pirate speech.")),
2933
settings = AnthropicCreateMessageSettings(
3034
model = NonOpenAIModelId.claude_3_haiku_20240307,
3135
max_tokens = 4096

0 commit comments

Comments
 (0)