-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anthropic bedrock chat completion examples
- Loading branch information
1 parent
bf9f759
commit 0029d00
Showing
6 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...la/examples/nonopenai/AnthropicBedrockCreateChatCompletionStreamedWithOpenAIAdapter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import akka.stream.scaladsl.Sink | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage} | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIChatCompletionStreamedService | ||
|
||
import scala.concurrent.Future | ||
|
||
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set | ||
object AnthropicBedrockCreateChatCompletionStreamedWithOpenAIAdapter | ||
extends ExampleBase[OpenAIChatCompletionStreamedService] { | ||
|
||
override val service: OpenAIChatCompletionStreamedService = | ||
ChatCompletionProvider.anthropicBedrock | ||
|
||
private val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserMessage("What is the weather like in Norway?") | ||
) | ||
override protected def run: Future[_] = { | ||
service | ||
.createChatCompletionStreamed( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = NonOpenAIModelId.claude_3_5_sonnet_20240620 | ||
) | ||
) | ||
.runWith( | ||
Sink.foreach { response => | ||
print(response.choices.headOption.flatMap(_.delta.content).getOrElse("")) | ||
} | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...penaiscala/examples/nonopenai/AnthropicBedrockCreateChatCompletionWithOpenAIAdapter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage} | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.OpenAIChatCompletionService | ||
|
||
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 AnthropicBedrockCreateChatCompletionWithOpenAIAdapter | ||
extends ExampleBase[OpenAIChatCompletionService] { | ||
|
||
override val service: OpenAIChatCompletionService = ChatCompletionProvider.anthropicBedrock | ||
|
||
private val messages = 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_haiku_20241022_v1_0 | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings(modelId) | ||
) | ||
.map { content => | ||
println(content.choices.headOption.map(_.message.content).getOrElse("N/A")) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/scala/io/cequence/openaiscala/examples/nonopenai/AnthropicBedrockCreateMessage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...la/io/cequence/openaiscala/examples/nonopenai/AnthropicBedrockCreateMessageStreamed.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import akka.stream.scaladsl.Sink | ||
import io.cequence.openaiscala.anthropic.domain.Message | ||
import io.cequence.openaiscala.anthropic.domain.Message.{SystemMessage, UserMessage} | ||
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 AnthropicBedrockCreateMessageStreamed extends ExampleBase[AnthropicService] { | ||
|
||
override protected val service: AnthropicService = AnthropicServiceFactory.forBedrock() | ||
|
||
val messages: Seq[Message] = Seq( | ||
SystemMessage("You are a helpful assistant!"), | ||
UserMessage("Start with the letter S followed by a quick story about Norway and finish with the letter E.") | ||
) | ||
|
||
private val modelId = "us." + NonOpenAIModelId.bedrock_claude_3_5_sonnet_20241022_v2_0 | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createMessageStreamed( | ||
messages, | ||
settings = AnthropicCreateMessageSettings( | ||
model = modelId, | ||
max_tokens = 4096 | ||
) | ||
) | ||
.runWith( | ||
Sink.foreach { response => | ||
print(response.delta.text) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters