-
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.
- Loading branch information
1 parent
a8d3770
commit 475caac
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
...c/main/scala/io/cequence/openaiscala/examples/nonopenai/MistralCreateChatCompletion.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 io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.{OpenAIChatCompletionService, OpenAIChatCompletionServiceFactory} | ||
import io.cequence.wsclient.domain.WsRequestContext | ||
|
||
import scala.concurrent.Future | ||
|
||
// requires `MISTRAL_API_KEY` environment variable to be set | ||
object MistralCreateChatCompletion extends ExampleBase[OpenAIChatCompletionService] { | ||
|
||
override val service: OpenAIChatCompletionService = OpenAIChatCompletionServiceFactory( | ||
coreUrl = "https://api.mistral.ai/v1/", | ||
WsRequestContext(authHeaders = | ||
Seq(("Authorization", s"Bearer ${sys.env("MISTRAL_API_KEY")}")) | ||
) | ||
) | ||
|
||
private val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserMessage("What is the weather like in Norway?") | ||
) | ||
|
||
private val modelId = NonOpenAIModelId.open_mistral_nemo | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletion( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = modelId, | ||
temperature = Some(0.1), | ||
max_tokens = Some(512) | ||
) | ||
) | ||
.map(printMessageContent) | ||
} |
50 changes: 50 additions & 0 deletions
50
...cala/io/cequence/openaiscala/examples/nonopenai/MistralCreateChatCompletionStreamed.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,50 @@ | ||
package io.cequence.openaiscala.examples.nonopenai | ||
|
||
import akka.stream.scaladsl.Sink | ||
import io.cequence.openaiscala.domain._ | ||
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings | ||
import io.cequence.openaiscala.examples.ExampleBase | ||
import io.cequence.openaiscala.service.{ | ||
OpenAIChatCompletionStreamedServiceExtra, | ||
OpenAIChatCompletionStreamedServiceFactory | ||
} | ||
import io.cequence.wsclient.domain.WsRequestContext | ||
|
||
import scala.concurrent.Future | ||
|
||
// requires `openai-scala-client-stream` as a dependency and `MISTRAL_API_KEY` environment variable to be set | ||
object MistralCreateChatCompletionStreamed | ||
extends ExampleBase[OpenAIChatCompletionStreamedServiceExtra] { | ||
|
||
override val service: OpenAIChatCompletionStreamedServiceExtra = | ||
OpenAIChatCompletionStreamedServiceFactory( | ||
coreUrl = "https://api.mistral.ai/v1/", | ||
WsRequestContext(authHeaders = | ||
Seq(("Authorization", s"Bearer ${sys.env("MISTRAL_API_KEY")}")) | ||
) | ||
) | ||
|
||
private val messages = Seq( | ||
SystemMessage("You are a helpful assistant."), | ||
UserMessage("What is the weather like in Norway?") | ||
) | ||
|
||
private val modelId = NonOpenAIModelId.mistral_large_latest | ||
|
||
override protected def run: Future[_] = | ||
service | ||
.createChatCompletionStreamed( | ||
messages = messages, | ||
settings = CreateChatCompletionSettings( | ||
model = modelId, | ||
temperature = Some(0.1), | ||
max_tokens = Some(512) | ||
) | ||
) | ||
.runWith( | ||
Sink.foreach { completion => | ||
val content = completion.choices.headOption.flatMap(_.delta.content) | ||
print(content.getOrElse("")) | ||
} | ||
) | ||
} |