-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCreateChatCompletionWithRouter.scala
67 lines (56 loc) · 2.03 KB
/
CreateChatCompletionWithRouter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.cequence.openaiscala.examples
import io.cequence.openaiscala.domain.{ModelId, SystemMessage, UserMessage}
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
import io.cequence.openaiscala.service._
import io.cequence.openaiscala.service.adapter.OpenAIChatCompletionServiceRouter
import io.cequence.wsclient.domain.WsRequestContext
import scala.concurrent.Future
object CreateChatCompletionWithRouter extends ExampleBase[OpenAIChatCompletionService] {
// OctoML
private val octoMLService = OpenAIChatCompletionServiceFactory(
coreUrl = "https://text.octoai.run/v1/",
requestContext = WsRequestContext(authHeaders =
Seq(("Authorization", s"Bearer ${sys.env("OCTOAI_TOKEN")}"))
)
)
// Ollama
private val ollamaService = OpenAIChatCompletionServiceFactory(
coreUrl = "http://localhost:11434/v1/"
)
// OpenAI
private val openAIService = OpenAIServiceFactory()
override val service = OpenAIChatCompletionServiceRouter(
serviceModels = Map(
octoMLService -> Seq("mixtral-8x7b-instruct"),
ollamaService -> Seq("llama2"),
// it's default so no need to specify all the models
openAIService -> Seq(ModelId.gpt_3_5_turbo)
),
defaultService = openAIService
)
val messages = Seq(
SystemMessage("You are a helpful assistant."),
UserMessage("What is the weather like in Norway?")
)
override protected def run: Future[_] =
for {
_ <- runChatCompletionAux("mixtral-8x7b-instruct")
_ <- runChatCompletionAux("llama2")
_ <- runChatCompletionAux(ModelId.gpt_3_5_turbo)
} yield ()
private def runChatCompletionAux(model: String) = {
println(s"Running chat completion with the model '$model'\n")
service
.createChatCompletion(
messages = messages,
settings = CreateChatCompletionSettings(
model = model,
temperature = Some(0.1),
max_tokens = Some(512),
top_p = Some(0.9),
presence_penalty = Some(0)
)
)
.map(printMessageContent)
}
}