-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathChatCompletionRouterAdapterExample.scala
117 lines (98 loc) · 3.78 KB
/
ChatCompletionRouterAdapterExample.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package io.cequence.openaiscala.examples.adapters
import io.cequence.openaiscala.anthropic.service.AnthropicServiceFactory
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
import io.cequence.openaiscala.domain.{ModelId, NonOpenAIModelId, SystemMessage, UserMessage}
import io.cequence.openaiscala.examples.ExampleBase
import io.cequence.openaiscala.service._
import io.cequence.openaiscala.service.adapter.OpenAIServiceAdapters
import io.cequence.wsclient.domain.WsRequestContext
import scala.concurrent.Future
/**
* Requirements:
* - `OCTOAI_TOKEN` environment variable to be set
* - Ollama service running locally
* - `ANTHROPIC_API_KEY` environment variable to be set
* - `FIREWORKS_API_KEY` environment variable to be set
*/
object ChatCompletionRouterAdapterExample extends ExampleBase[OpenAIService] {
// OctoML
private val octoMLService = OpenAIChatCompletionServiceFactory(
coreUrl = "https://text.octoai.run/v1/",
WsRequestContext(
authHeaders = Seq(("Authorization", s"Bearer ${sys.env("OCTOAI_TOKEN")}"))
)
)
// Ollama
private val ollamaService = OpenAIChatCompletionServiceFactory(
coreUrl = "http://localhost:11434/v1/"
)
// Fireworks AI
private val fireworksModelPrefix = "accounts/fireworks/models/"
private val fireworksService = OpenAIChatCompletionServiceFactory(
coreUrl = "https://api.fireworks.ai/inference/v1/",
WsRequestContext(
authHeaders = Seq(("Authorization", s"Bearer ${sys.env("FIREWORKS_API_KEY")}"))
)
)
// Anthropic
private val anthropicService = AnthropicServiceFactory.asOpenAI()
// OpenAI
private val openAIService = OpenAIServiceFactory()
override val service: OpenAIService =
OpenAIServiceAdapters.forFullService.chatCompletionRouter(
// OpenAI service is default so no need to specify its models here
serviceModels = Map(
octoMLService -> Seq(NonOpenAIModelId.mixtral_8x22b_instruct),
ollamaService -> Seq(NonOpenAIModelId.llama2),
fireworksService -> Seq(
fireworksModelPrefix + NonOpenAIModelId.llama_v3_8b_instruct,
fireworksModelPrefix + NonOpenAIModelId.drbx_instruct
),
anthropicService -> Seq(
NonOpenAIModelId.claude_2_1,
NonOpenAIModelId.claude_3_opus_20240229,
NonOpenAIModelId.claude_3_haiku_20240307
)
),
openAIService
)
private val messages = Seq(
SystemMessage("You are a helpful assistant."),
UserMessage("What is the weather like in Norway?")
)
override protected def run: Future[_] =
for {
// runs on OctoML
_ <- runChatCompletionAux(NonOpenAIModelId.mixtral_8x22b_instruct)
// runs on Ollama
_ <- runChatCompletionAux(NonOpenAIModelId.llama2)
// runs on Fireworks AI
_ <- runChatCompletionAux(fireworksModelPrefix + NonOpenAIModelId.llama_v3_8b_instruct)
// runs on Fireworks AI
_ <- runChatCompletionAux(fireworksModelPrefix + NonOpenAIModelId.drbx_instruct)
// runs on Anthropic
_ <- runChatCompletionAux(NonOpenAIModelId.claude_3_haiku_20240307)
// runs on OpenAI
_ <- runChatCompletionAux(ModelId.gpt_3_5_turbo)
// runs on OpenAI (non-chat-completion function)
_ <- service.listModels.map(_.foreach(println))
} 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(1024),
top_p = Some(0.9),
presence_penalty = Some(0)
)
)
.map { response =>
printMessageContent(response)
println("--------")
}
}
}