Skip to content

Commit 2d00fe4

Browse files
committed
Anthropic examples + reshuffling
1 parent b8c3756 commit 2d00fe4

14 files changed

+55
-21
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],
3536
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/AnthropicServiceFactory.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ object AnthropicServiceFactory extends AnthropicServiceConsts {
3838
*/
3939
def asOpenAI(
4040
apiKey: String = getAPIKeyFromEnv(),
41-
timeouts: Option[Timeouts] = None
41+
timeouts: Option[Timeouts] = None,
42+
withCache: Boolean = false
4243
)(
4344
implicit ec: ExecutionContext,
4445
materializer: Materializer
4546
): OpenAIChatCompletionStreamedService =
4647
new OpenAIAnthropicChatCompletionService(
47-
AnthropicServiceFactory(apiKey, timeouts)
48+
AnthropicServiceFactory(apiKey, timeouts, withPdf = false, withCache)
4849
)
4950

5051
/**

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

Lines changed: 1 addition & 1 deletion
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],
3637
messages: Seq[Message],
37-
system: Option[Content] = None,
3838
settings: AnthropicCreateMessageSettings
3939
): Future[CreateMessageResponse] =
4040
execPOST(

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
@@ -40,8 +40,8 @@ private[service] class OpenAIAnthropicChatCompletionService(
4040
): Future[ChatCompletionResponse] = {
4141
underlying
4242
.createMessage(
43-
toAnthropicMessages(messages, settings),
4443
toAnthropicSystemMessages(messages, settings),
44+
toAnthropicMessages(messages, settings),
4545
toAnthropicSettings(settings)
4646
)
4747
.map(toOpenAI)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object AnthropicCreateCachedMessage extends ExampleBase[AnthropicService] {
1818

1919
override protected val service: AnthropicService = AnthropicServiceFactory(withCache = true)
2020

21-
val systemMessages: Option[Content] = Some(
21+
val systemMessage: Content =
2222
SingleString(
2323
"""
2424
|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.
@@ -76,14 +76,14 @@ object AnthropicCreateCachedMessage extends ExampleBase[AnthropicService] {
7676
|""".stripMargin,
7777
cacheControl = Some(Ephemeral)
7878
)
79-
)
79+
8080
val messages: Seq[Message] = Seq(UserMessage("What is the weather like in Norway?"))
8181

8282
override protected def run: Future[_] =
8383
service
8484
.createMessage(
85+
Some(systemMessage),
8586
messages,
86-
systemMessages,
8787
settings = AnthropicCreateMessageSettings(
8888
model = NonOpenAIModelId.claude_3_haiku_20240307,
8989
max_tokens = 4096
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.cequence.openaiscala.examples.nonopenai
2+
3+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
4+
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage}
5+
import io.cequence.openaiscala.examples.ExampleBase
6+
import io.cequence.openaiscala.service.OpenAIChatCompletionService
7+
8+
import scala.concurrent.Future
9+
10+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
11+
object AnthropicCreateChatCompletionCachedWithOpenAIAdapter
12+
extends ExampleBase[OpenAIChatCompletionService] {
13+
14+
override val service: OpenAIChatCompletionService = ChatCompletionProvider.anthropic(withCache = true)
15+
16+
private val messages = Seq(
17+
SystemMessage("You are a helpful assistant."),
18+
UserMessage("What is the weather like in Norway?")
19+
)
20+
21+
override protected def run: Future[_] =
22+
service
23+
.createChatCompletion(
24+
messages = messages,
25+
settings = CreateChatCompletionSettings(NonOpenAIModelId.claude_3_5_sonnet_20241022)
26+
)
27+
.map { content =>
28+
println(content.choices.headOption.map(_.message.content).getOrElse("N/A"))
29+
}
30+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object AnthropicCreateChatCompletionStreamedWithOpenAIAdapter
1818

1919
private val logger = LoggerFactory.getLogger(this.getClass)
2020

21-
override val service: OpenAIChatCompletionStreamedService = ChatCompletionProvider.anthropic
21+
override val service: OpenAIChatCompletionStreamedService = ChatCompletionProvider.anthropic()
2222

2323
private val messages = Seq(
2424
SystemMessage("You are a helpful assistant."),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.concurrent.Future
1111
object AnthropicCreateChatCompletionWithOpenAIAdapter
1212
extends ExampleBase[OpenAIChatCompletionService] {
1313

14-
override val service: OpenAIChatCompletionService = ChatCompletionProvider.anthropic
14+
override val service: OpenAIChatCompletionService = ChatCompletionProvider.anthropic()
1515

1616
private val messages = Seq(
1717
SystemMessage("You are a helpful assistant."),

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

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

33
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock.TextBlock
4-
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlockBase
5-
import io.cequence.openaiscala.anthropic.domain.Message
4+
import io.cequence.openaiscala.anthropic.domain.Content.{ContentBlockBase, SingleString}
5+
import io.cequence.openaiscala.anthropic.domain.{Content, Message}
66
import io.cequence.openaiscala.anthropic.domain.Message.UserMessage
77
import io.cequence.openaiscala.anthropic.domain.response.CreateMessageResponse
88
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
@@ -17,13 +17,14 @@ object AnthropicCreateMessage extends ExampleBase[AnthropicService] {
1717

1818
override protected val service: AnthropicService = AnthropicServiceFactory(withCache = true)
1919

20+
val systemMessage: Content = SingleString("You are a helpful assistant.")
2021
val messages: Seq[Message] = Seq(UserMessage("What is the weather like in Norway?"))
2122

2223
override protected def run: Future[_] =
2324
service
2425
.createMessage(
26+
Some(systemMessage),
2527
messages,
26-
None,
2728
settings = AnthropicCreateMessageSettings(
2829
model = NonOpenAIModelId.claude_3_haiku_20240307,
2930
max_tokens = 4096

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

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

33
import akka.stream.scaladsl.Sink
4-
import io.cequence.openaiscala.anthropic.domain.Message
4+
import io.cequence.openaiscala.anthropic.domain.Content.SingleString
5+
import io.cequence.openaiscala.anthropic.domain.{Content, Message}
56
import io.cequence.openaiscala.anthropic.domain.Message.UserMessage
67
import io.cequence.openaiscala.anthropic.domain.settings.AnthropicCreateMessageSettings
78
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
@@ -15,12 +16,13 @@ object AnthropicCreateMessageStreamed extends ExampleBase[AnthropicService] {
1516

1617
override protected val service: AnthropicService = AnthropicServiceFactory()
1718

19+
val systemMessage: Content = SingleString("You are a helpful assistant.")
1820
val messages: Seq[Message] = Seq(UserMessage("What is the weather like in Norway?"))
1921

2022
override protected def run: Future[_] =
2123
service
2224
.createMessageStreamed(
23-
None,
25+
Some(systemMessage),
2426
messages,
2527
settings = AnthropicCreateMessageSettings(
2628
model = NonOpenAIModelId.claude_3_haiku_20240307,

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+
system = None,
4142
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ object AnthropicCreateMessageWithPdf extends ExampleBase[AnthropicService] {
3636
override protected def run: Future[_] =
3737
service
3838
.createMessage(
39+
system = None,
3940
messages,
4041
settings = AnthropicCreateMessageSettings(
4142
model =

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ object AnthropicCreateSystemMessage extends ExampleBase[AnthropicService] {
1717

1818
override protected val service: AnthropicService = AnthropicServiceFactory()
1919

20-
val systemMessages: Option[Content] = Some(
21-
SingleString("Talk in pirate speech")
22-
)
20+
val systemMessage: Content = SingleString("Talk in pirate speech")
2321
val messages: Seq[Message] = Seq(
2422
UserMessage("Who is the most famous football player in the World?")
2523
)
2624

2725
override protected def run: Future[_] =
2826
service
2927
.createMessage(
28+
Some(systemMessage),
3029
messages,
31-
Some(SingleString("You answer in pirate speech.")),
3230
settings = AnthropicCreateMessageSettings(
3331
model = NonOpenAIModelId.claude_3_haiku_20240307,
3432
max_tokens = 4096

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ object ChatCompletionProvider {
8383
* Requires `ANTHROPIC_API_KEY`
8484
*/
8585
def anthropic(
86+
withCache: Boolean = false)(
8687
implicit ec: ExecutionContext,
8788
m: Materializer
8889
): OpenAIChatCompletionStreamedService =
89-
AnthropicServiceFactory.asOpenAI()
90+
AnthropicServiceFactory.asOpenAI(withCache = withCache)
9091

9192
private def provide(
9293
settings: ProviderSettings

0 commit comments

Comments
 (0)