Skip to content

Commit 17d0227

Browse files
committed
Merge remote-tracking branch 'origin/master' into feature/3654-prompt-caching
# Conflicts: # anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/impl/OpenAIAnthropicChatCompletionService.scala # anthropic-client/src/test/scala/io/cequence/openaiscala/anthropic/service/impl/AnthropicServiceSpec.scala # openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateCachedMessage.scala # openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateMessageStreamed.scala # openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicCreateSystemMessage.scala
2 parents 43d9744 + a9f92d6 commit 17d0227

11 files changed

+62
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ 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],
3637
settings: AnthropicCreateMessageSettings = DefaultSettings.CreateMessage
3738
): Future[CreateMessageResponse]

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private[service] trait AnthropicServiceImpl extends Anthropic {
3535
private val logger = LoggerFactory.getLogger("AnthropicServiceImpl")
3636

3737
override def createMessage(
38+
system: Option[Content],
3839
messages: Seq[Message],
3940
settings: AnthropicCreateMessageSettings
4041
): Future[CreateMessageResponse] =

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ object CreateChatCompletionSettingsOps {
77
private val AnthropicCachedUserMessagesCount = "cached_user_messages_count"
88
private val AnthropicUseSystemMessagesCache = "use_system_messages_cache"
99

10+
def setAnthropicCachedUserMessagesCount(count: Int): CreateChatCompletionSettings =
11+
settings.copy(
12+
extra_params = settings.extra_params + (AnthropicCachedUserMessagesCount -> count)
13+
)
14+
15+
def setUseAnthropicSystemMessagesCache(useCache: Boolean): CreateChatCompletionSettings =
16+
settings.copy(
17+
extra_params = settings.extra_params + (AnthropicUseSystemMessagesCache -> useCache)
18+
)
19+
1020
def anthropicCachedUserMessagesCount: Int =
1121
settings.extra_params
1222
.get(AnthropicCachedUserMessagesCount)
13-
.flatMap(numberAsString => Try(numberAsString.toString.toInt).toOption)
23+
.flatMap {
24+
case value: Int => Some(value)
25+
case value: Any => Try(value.toString.toInt).toOption
26+
}
1427
.getOrElse(0)
1528

1629
def useAnthropicSystemMessagesCache: Boolean =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 =
15+
ChatCompletionProvider.anthropic(withCache = true)
16+
17+
private val messages = Seq(
18+
SystemMessage("You are a helpful assistant."),
19+
UserMessage("What is the weather like in Norway?")
20+
)
21+
22+
override protected def run: Future[_] =
23+
service
24+
.createChatCompletion(
25+
messages = messages,
26+
settings = CreateChatCompletionSettings(NonOpenAIModelId.claude_3_5_sonnet_20241022)
27+
)
28+
.map { content =>
29+
println(content.choices.headOption.map(_.message.content).getOrElse("N/A"))
30+
}
31+
}

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

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

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

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

2324
private val messages = Seq(
2425
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 & 2 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,11 +17,13 @@ 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,
2628
settings = AnthropicCreateMessageSettings(
2729
model = NonOpenAIModelId.claude_3_haiku_20240307,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ object AnthropicCreateMessageWithImage extends ExampleBase[AnthropicService] {
3838
override protected def run: Future[_] =
3939
service
4040
.createMessage(
41+
system = None,
4142
messages,
4243
settings = AnthropicCreateMessageSettings(
4344
model = NonOpenAIModelId.claude_3_opus_20240229,

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
@@ -37,6 +37,7 @@ object AnthropicCreateMessageWithPdf extends ExampleBase[AnthropicService] {
3737
override protected def run: Future[_] =
3838
service
3939
.createMessage(
40+
system = None,
4041
messages,
4142
settings = AnthropicCreateMessageSettings(
4243
model =

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ object ChatCompletionProvider {
8383
* Requires `ANTHROPIC_API_KEY`
8484
*/
8585
def anthropic(
86+
withCache: Boolean = false
87+
)(
8688
implicit ec: ExecutionContext,
8789
m: Materializer
8890
): OpenAIChatCompletionStreamedService =
89-
AnthropicServiceFactory.asOpenAI()
91+
AnthropicServiceFactory.asOpenAI(withCache = withCache)
9092

9193
private def provide(
9294
settings: ProviderSettings

0 commit comments

Comments
 (0)