Skip to content

Commit d843d2f

Browse files
committed
Fireworks - deepseek r1 example with think token filtration
1 parent d8bd3fd commit d843d2f

File tree

4 files changed

+117
-31
lines changed

4 files changed

+117
-31
lines changed

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import io.cequence.openaiscala.domain._
44
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
55
import io.cequence.openaiscala.examples.ExampleBase
66
import io.cequence.openaiscala.service.OpenAIChatCompletionService
7-
import io.cequence.openaiscala.service.adapter.{MessageConversions, OpenAIServiceAdapters}
87

98
import scala.concurrent.Future
109

@@ -16,18 +15,7 @@ import scala.concurrent.Future
1615
*/
1716
object FireworksAICreateChatCompletion extends ExampleBase[OpenAIChatCompletionService] {
1817

19-
// thinking process ends with </think>
20-
private val omitThinkingOutput = true
21-
22-
override val service: OpenAIChatCompletionService = {
23-
val adapters = OpenAIServiceAdapters.forChatCompletionService
24-
val vanillaService = ChatCompletionProvider.fireworks
25-
26-
if (omitThinkingOutput)
27-
adapters.chatCompletionOutput(MessageConversions.filterOutToThinkEnd)(vanillaService)
28-
else
29-
vanillaService
30-
}
18+
override val service: OpenAIChatCompletionService = ChatCompletionProvider.fireworks
3119

3220
private val fireworksModelPrefix = "accounts/fireworks/models/"
3321

@@ -36,7 +24,7 @@ object FireworksAICreateChatCompletion extends ExampleBase[OpenAIChatCompletionS
3624
UserMessage("What is the weather like in Norway?")
3725
)
3826

39-
private val modelId = NonOpenAIModelId.deepseek_r1 // llama_v3p1_405b_instruct
27+
private val modelId = NonOpenAIModelId.llama_v3p1_405b_instruct
4028

4129
override protected def run: Future[_] =
4230
service

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@ import akka.stream.scaladsl.Sink
44
import io.cequence.openaiscala.domain._
55
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
66
import io.cequence.openaiscala.examples.ExampleBase
7-
import io.cequence.openaiscala.service.OpenAIChatCompletionIOConversionAdapter
87
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIChatCompletionStreamedService
9-
import io.cequence.openaiscala.service.adapter.MessageConversions
108

119
import scala.concurrent.Future
1210

1311
// requires `openai-scala-client-stream` as a dependency and `FIREWORKS_API_KEY` environment variable to be set
1412
object FireworksAICreateChatCompletionStreamed
1513
extends ExampleBase[OpenAIChatCompletionStreamedService] {
1614

17-
// thinking process ends with </think>
18-
private val omitThinkingOutput = true
19-
20-
override val service: OpenAIChatCompletionStreamedService = {
21-
val vanillaService = ChatCompletionProvider.fireworks
22-
23-
if (omitThinkingOutput)
24-
OpenAIChatCompletionIOConversionAdapter(
25-
vanillaService,
26-
outputChunkMessageConversion = Some(MessageConversions.filterOutToThinkEndFlow)
27-
)
28-
else
29-
vanillaService
30-
}
15+
override val service: OpenAIChatCompletionStreamedService = ChatCompletionProvider.fireworks
3116

3217
private val fireworksModelPrefix = "accounts/fireworks/models/"
3318

@@ -36,7 +21,7 @@ object FireworksAICreateChatCompletionStreamed
3621
UserMessage("What is the weather like in Norway?")
3722
)
3823

39-
private val modelId = NonOpenAIModelId.deepseek_r1 // drbx_instruct
24+
private val modelId = NonOpenAIModelId.llama_v3p3_70b_instruct
4025

4126
override protected def run: Future[_] =
4227
service
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.cequence.openaiscala.examples.nonopenai
2+
3+
import akka.stream.scaladsl.Sink
4+
import io.cequence.openaiscala.domain._
5+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
6+
import io.cequence.openaiscala.examples.ExampleBase
7+
import io.cequence.openaiscala.service.OpenAIChatCompletionIOConversionAdapter
8+
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIChatCompletionStreamedService
9+
import io.cequence.openaiscala.service.adapter.MessageConversions
10+
11+
import scala.concurrent.Future
12+
13+
// requires `openai-scala-client-stream` as a dependency and `FIREWORKS_API_KEY` environment variable to be set
14+
object FireworksAICreateChatCompletionStreamedWithDeepseek
15+
extends ExampleBase[OpenAIChatCompletionStreamedService] {
16+
17+
// thinking process ends with </think>
18+
private val omitThinkingOutput = true
19+
20+
override val service: OpenAIChatCompletionStreamedService = {
21+
val vanillaService = ChatCompletionProvider.fireworks
22+
23+
if (omitThinkingOutput)
24+
OpenAIChatCompletionIOConversionAdapter(
25+
vanillaService,
26+
outputChunkMessageConversion = Some(MessageConversions.filterOutToThinkEndFlow)
27+
)
28+
else
29+
vanillaService
30+
}
31+
32+
private val fireworksModelPrefix = "accounts/fireworks/models/"
33+
34+
private val messages = Seq(
35+
SystemMessage("You are a helpful assistant. Be short."),
36+
UserMessage("What is the weather like in Norway?")
37+
)
38+
39+
private val modelId = NonOpenAIModelId.deepseek_r1
40+
41+
override protected def run: Future[_] =
42+
service
43+
.createChatCompletionStreamed(
44+
messages = messages,
45+
settings = CreateChatCompletionSettings(
46+
model = fireworksModelPrefix + modelId,
47+
temperature = Some(0.01),
48+
max_tokens = Some(2048),
49+
top_p = Some(0.9),
50+
presence_penalty = Some(0)
51+
)
52+
)
53+
.runWith(
54+
Sink.foreach { completion =>
55+
val content = completion.choices.headOption.flatMap(_.delta.content)
56+
print(content.getOrElse(""))
57+
}
58+
)
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.cequence.openaiscala.examples.nonopenai
2+
3+
import io.cequence.openaiscala.domain._
4+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
5+
import io.cequence.openaiscala.examples.ExampleBase
6+
import io.cequence.openaiscala.service.OpenAIChatCompletionService
7+
import io.cequence.openaiscala.service.adapter.{MessageConversions, OpenAIServiceAdapters}
8+
9+
import scala.concurrent.Future
10+
11+
/**
12+
* Requires `FIREWORKS_API_KEY` environment variable to be set.
13+
*
14+
* Check out [[ChatCompletionInputAdapterForFireworksAI]] for a more complex example with an
15+
* input adapter
16+
*/
17+
object FireworksAICreateChatCompletionWithDeepseek extends ExampleBase[OpenAIChatCompletionService] {
18+
19+
// thinking process ends with </think>
20+
private val omitThinkingOutput = true
21+
22+
override val service: OpenAIChatCompletionService = {
23+
val adapters = OpenAIServiceAdapters.forChatCompletionService
24+
val vanillaService = ChatCompletionProvider.fireworks
25+
26+
if (omitThinkingOutput)
27+
adapters.chatCompletionOutput(MessageConversions.filterOutToThinkEnd)(vanillaService)
28+
else
29+
vanillaService
30+
}
31+
32+
private val fireworksModelPrefix = "accounts/fireworks/models/"
33+
34+
private val messages = Seq(
35+
SystemMessage("You are a helpful assistant."),
36+
UserMessage("What is the weather like in Norway?")
37+
)
38+
39+
private val modelId = NonOpenAIModelId.deepseek_r1 // llama_v3p1_405b_instruct
40+
41+
override protected def run: Future[_] =
42+
service
43+
.createChatCompletion(
44+
messages = messages,
45+
settings = CreateChatCompletionSettings(
46+
model = fireworksModelPrefix + modelId,
47+
temperature = Some(0.1),
48+
max_tokens = Some(2048),
49+
top_p = Some(0.9),
50+
presence_penalty = Some(0)
51+
)
52+
)
53+
.map(printMessageContent)
54+
}

0 commit comments

Comments
 (0)