Skip to content

Commit a408426

Browse files
committed
install scalafmt sbt plugin and reformat all files
1 parent 61052fa commit a408426

File tree

53 files changed

+1666
-1228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1666
-1228
lines changed

.scalafmt.conf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = 3.7.4
2+
runner.dialect = scala213

openai-client-stream/src/main/scala/io/cequence/openaiscala/service/OpenAIServiceStreamedExtra.scala

+58-36
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,73 @@ package io.cequence.openaiscala.service
33
import akka.NotUsed
44
import akka.stream.scaladsl.Source
55
import io.cequence.openaiscala.domain.MessageSpec
6-
import io.cequence.openaiscala.domain.response.{ChatCompletionChunkResponse, FineTuneEvent, TextCompletionResponse}
7-
import io.cequence.openaiscala.domain.settings.{CreateChatCompletionSettings, CreateCompletionSettings}
8-
6+
import io.cequence.openaiscala.domain.response.{
7+
ChatCompletionChunkResponse,
8+
FineTuneEvent,
9+
TextCompletionResponse
10+
}
11+
import io.cequence.openaiscala.domain.settings.{
12+
CreateChatCompletionSettings,
13+
CreateCompletionSettings
14+
}
915

1016
trait OpenAIServiceStreamedExtra extends OpenAIServiceConsts {
1117

12-
/**
13-
* Creates a completion for the provided prompt and parameters with streamed results.
14-
*
15-
* @param prompt The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
16-
Note that <|endoftext|> is the document separator that the model sees during training,
17-
so if a prompt is not specified the model will generate as if from the beginning of a new document.
18-
* @param settings
19-
* @return text completion response as a stream (source)
20-
*
21-
* @see <a href="https://beta.openai.com/docs/api-reference/completions/create">OpenAI Doc</a>
22-
*/
18+
/** Creates a completion for the provided prompt and parameters with streamed
19+
* results.
20+
*
21+
* @param prompt
22+
* The prompt(s) to generate completions for, encoded as a string, array of
23+
* strings, array of tokens, or array of token arrays. Note that
24+
* <|endoftext|> is the document separator that the model sees during
25+
* training, so if a prompt is not specified the model will generate as if
26+
* from the beginning of a new document.
27+
* @param settings
28+
* @return
29+
* text completion response as a stream (source)
30+
*
31+
* @see
32+
* <a
33+
* href="https://beta.openai.com/docs/api-reference/completions/create">OpenAI
34+
* Doc</a>
35+
*/
2336
def createCompletionStreamed(
24-
prompt: String,
25-
settings: CreateCompletionSettings = DefaultSettings.CreateCompletion
37+
prompt: String,
38+
settings: CreateCompletionSettings = DefaultSettings.CreateCompletion
2639
): Source[TextCompletionResponse, NotUsed]
2740

28-
/**
29-
* Creates a completion for the chat message(s) with streamed results.
30-
*
31-
* @param messages The messages to generate chat completions.
32-
* @param settings
33-
* @return chat completion response
34-
*
35-
* @see <a href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI Doc</a>
36-
*/
41+
/** Creates a completion for the chat message(s) with streamed results.
42+
*
43+
* @param messages
44+
* The messages to generate chat completions.
45+
* @param settings
46+
* @return
47+
* chat completion response
48+
*
49+
* @see
50+
* <a
51+
* href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI
52+
* Doc</a>
53+
*/
3754
def createChatCompletionStreamed(
38-
messages: Seq[MessageSpec],
39-
settings: CreateChatCompletionSettings = DefaultSettings.CreateChatCompletion
55+
messages: Seq[MessageSpec],
56+
settings: CreateChatCompletionSettings =
57+
DefaultSettings.CreateChatCompletion
4058
): Source[ChatCompletionChunkResponse, NotUsed]
4159

42-
/**
43-
* Get fine-grained status updates for a fine-tune job with streamed results.
44-
*
45-
* @param fineTuneId The ID of the fine-tune job to get events for.
46-
* @return fine tune events or None if not found as a stream (source)
47-
*
48-
* @see <a href="https://beta.openai.com/docs/api-reference/fine-tunes/events">OpenAI Doc</a>
49-
*/
60+
/** Get fine-grained status updates for a fine-tune job with streamed results.
61+
*
62+
* @param fineTuneId
63+
* The ID of the fine-tune job to get events for.
64+
* @return
65+
* fine tune events or None if not found as a stream (source)
66+
*
67+
* @see
68+
* <a
69+
* href="https://beta.openai.com/docs/api-reference/fine-tunes/events">OpenAI
70+
* Doc</a>
71+
*/
5072
def listFineTuneEventsStreamed(
51-
fineTuneId: String
73+
fineTuneId: String
5274
): Source[FineTuneEvent, NotUsed]
5375
}

openai-client-stream/src/main/scala/io/cequence/openaiscala/service/OpenAIServiceStreamedImpl.scala

+57-40
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,62 @@ import play.api.libs.json.JsValue
1414

1515
import scala.concurrent.ExecutionContext
1616

17-
/**
18-
* Private impl. class of [[OpenAIService]].
19-
*
20-
* @param apiKey
21-
* @param orgId
22-
* @param ec
23-
* @param materializer
24-
*
25-
* @since Jan 2023
26-
*/
27-
private trait OpenAIServiceStreamedExtraImpl extends OpenAIServiceStreamedExtra with WSStreamRequestHelper {
17+
/** Private impl. class of [[OpenAIService]].
18+
*
19+
* @param apiKey
20+
* @param orgId
21+
* @param ec
22+
* @param materializer
23+
*
24+
* @since Jan
25+
* 2023
26+
*/
27+
private trait OpenAIServiceStreamedExtraImpl
28+
extends OpenAIServiceStreamedExtra
29+
with WSStreamRequestHelper {
2830
this: OpenAIServiceImpl =>
2931

3032
override def createCompletionStreamed(
31-
prompt: String,
32-
settings: CreateCompletionSettings
33+
prompt: String,
34+
settings: CreateCompletionSettings
3335
): Source[TextCompletionResponse, NotUsed] =
3436
execJsonStreamAux(
3537
Command.completions,
3638
"POST",
37-
bodyParams = createBodyParamsForCompletion(prompt, settings, stream = true)
39+
bodyParams =
40+
createBodyParamsForCompletion(prompt, settings, stream = true)
3841
).map { (json: JsValue) =>
39-
(json \ "error").toOption.map { error =>
40-
throw new OpenAIScalaClientException(error.toString())
41-
}.getOrElse(
42-
json.asSafe[TextCompletionResponse]
43-
)
42+
(json \ "error").toOption
43+
.map { error =>
44+
throw new OpenAIScalaClientException(error.toString())
45+
}
46+
.getOrElse(
47+
json.asSafe[TextCompletionResponse]
48+
)
4449
}
4550

4651
override def createChatCompletionStreamed(
47-
messages: Seq[MessageSpec],
48-
settings: CreateChatCompletionSettings = DefaultSettings.CreateChatCompletion
52+
messages: Seq[MessageSpec],
53+
settings: CreateChatCompletionSettings =
54+
DefaultSettings.CreateChatCompletion
4955
): Source[ChatCompletionChunkResponse, NotUsed] =
5056
execJsonStreamAux(
5157
Command.chat_completions,
5258
"POST",
53-
bodyParams = createBodyParamsForChatCompletion(messages, settings, stream = true)
59+
bodyParams =
60+
createBodyParamsForChatCompletion(messages, settings, stream = true)
5461
).map { (json: JsValue) =>
55-
(json \ "error").toOption.map { error =>
56-
throw new OpenAIScalaClientException(error.toString())
57-
}.getOrElse(
58-
json.asSafe[ChatCompletionChunkResponse]
59-
)
62+
(json \ "error").toOption
63+
.map { error =>
64+
throw new OpenAIScalaClientException(error.toString())
65+
}
66+
.getOrElse(
67+
json.asSafe[ChatCompletionChunkResponse]
68+
)
6069
}
6170

6271
override def listFineTuneEventsStreamed(
63-
fineTuneId: String
72+
fineTuneId: String
6473
): Source[FineTuneEvent, NotUsed] =
6574
execJsonStreamAux(
6675
Command.fine_tunes,
@@ -70,21 +79,29 @@ private trait OpenAIServiceStreamedExtraImpl extends OpenAIServiceStreamedExtra
7079
Tag.stream -> Some(true)
7180
)
7281
).map { json =>
73-
(json \ "error").toOption.map { error =>
74-
throw new OpenAIScalaClientException(error.toString())
75-
}.getOrElse(
76-
json.asSafe[FineTuneEvent]
77-
)
82+
(json \ "error").toOption
83+
.map { error =>
84+
throw new OpenAIScalaClientException(error.toString())
85+
}
86+
.getOrElse(
87+
json.asSafe[FineTuneEvent]
88+
)
7889
}
7990
}
8091

81-
object OpenAIServiceStreamedFactory extends OpenAIServiceFactoryHelper[OpenAIService with OpenAIServiceStreamedExtra] {
92+
object OpenAIServiceStreamedFactory
93+
extends OpenAIServiceFactoryHelper[
94+
OpenAIService with OpenAIServiceStreamedExtra
95+
] {
8296

8397
override def apply(
84-
apiKey: String,
85-
orgId: Option[String] = None,
86-
timeouts: Option[Timeouts] = None)(
87-
implicit ec: ExecutionContext, materializer: Materializer
98+
apiKey: String,
99+
orgId: Option[String] = None,
100+
timeouts: Option[Timeouts] = None
101+
)(implicit
102+
ec: ExecutionContext,
103+
materializer: Materializer
88104
): OpenAIService with OpenAIServiceStreamedExtra =
89-
new OpenAIServiceImpl(apiKey, orgId, timeouts) with OpenAIServiceStreamedExtraImpl
90-
}
105+
new OpenAIServiceImpl(apiKey, orgId, timeouts)
106+
with OpenAIServiceStreamedExtraImpl
107+
}

openai-client-stream/src/main/scala/io/cequence/openaiscala/service/ws/WSStreamRequestHelper.scala

+50-29
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ import akka.stream.scaladsl.Framing.FramingException
88
import akka.stream.scaladsl.{Flow, Framing, Source}
99
import akka.util.ByteString
1010
import com.fasterxml.jackson.core.JsonParseException
11-
import io.cequence.openaiscala.{OpenAIScalaClientException, OpenAIScalaClientTimeoutException, OpenAIScalaClientUnknownHostException}
11+
import io.cequence.openaiscala.{
12+
OpenAIScalaClientException,
13+
OpenAIScalaClientTimeoutException,
14+
OpenAIScalaClientUnknownHostException
15+
}
1216
import play.api.libs.json.{JsObject, JsString, JsValue, Json}
1317
import play.api.libs.ws.JsonBodyWritables._
1418

1519
import java.net.UnknownHostException
1620
import java.util.concurrent.TimeoutException
1721

18-
/**
19-
* Stream request support specifically tailored for OpenAI API.
20-
*
21-
* @since Feb 2023
22-
*/
22+
/** Stream request support specifically tailored for OpenAI API.
23+
*
24+
* @since Feb
25+
* 2023
26+
*/
2327
trait WSStreamRequestHelper {
2428
this: WSRequestHelper =>
2529

@@ -31,16 +35,18 @@ trait WSStreamRequestHelper {
3135
private implicit val jsonMarshaller: Unmarshaller[ByteString, JsValue] =
3236
Unmarshaller.strict[ByteString, JsValue] { byteString =>
3337
val data = byteString.utf8String.stripPrefix(itemPrefix)
34-
if (data.equals(endOfStreamToken)) JsString(endOfStreamToken) else Json.parse(data)
38+
if (data.equals(endOfStreamToken)) JsString(endOfStreamToken)
39+
else Json.parse(data)
3540
}
3641

3742
protected def execJsonStreamAux(
38-
endPoint: PEP,
39-
method: String,
40-
endPointParam: Option[String] = None,
41-
params: Seq[(PT, Option[Any])] = Nil,
42-
bodyParams: Seq[(PT, Option[JsValue])] = Nil)(
43-
implicit materializer: Materializer
43+
endPoint: PEP,
44+
method: String,
45+
endPointParam: Option[String] = None,
46+
params: Seq[(PT, Option[Any])] = Nil,
47+
bodyParams: Seq[(PT, Option[JsValue])] = Nil
48+
)(implicit
49+
materializer: Materializer
4450
): Source[JsValue, NotUsed] = {
4551
val source = execStreamRequestAux[JsValue](
4652
endPoint,
@@ -50,8 +56,14 @@ trait WSStreamRequestHelper {
5056
bodyParams,
5157
Framing.delimiter(ByteString("\n\n"), 1000, allowTruncation = true),
5258
{
53-
case e: JsonParseException => throw new OpenAIScalaClientException(s"$serviceName.$endPoint: 'Response is not a JSON. ${e.getMessage}.")
54-
case e: FramingException => throw new OpenAIScalaClientException(s"$serviceName.$endPoint: 'Response is not a JSON. ${e.getMessage}.")
59+
case e: JsonParseException =>
60+
throw new OpenAIScalaClientException(
61+
s"$serviceName.$endPoint: 'Response is not a JSON. ${e.getMessage}."
62+
)
63+
case e: FramingException =>
64+
throw new OpenAIScalaClientException(
65+
s"$serviceName.$endPoint: 'Response is not a JSON. ${e.getMessage}."
66+
)
5567
}
5668
)
5769

@@ -60,32 +72,41 @@ trait WSStreamRequestHelper {
6072
}
6173

6274
protected def execStreamRequestAux[T](
63-
endPoint: PEP,
64-
method: String,
65-
endPointParam: Option[String],
66-
params: Seq[(PT, Option[Any])],
67-
bodyParams: Seq[(PT, Option[JsValue])],
68-
framing: Flow[ByteString, ByteString, NotUsed],
69-
recoverBlock: PartialFunction[Throwable, T])(
70-
implicit um: Unmarshaller[ByteString, T], materializer: Materializer
75+
endPoint: PEP,
76+
method: String,
77+
endPointParam: Option[String],
78+
params: Seq[(PT, Option[Any])],
79+
bodyParams: Seq[(PT, Option[JsValue])],
80+
framing: Flow[ByteString, ByteString, NotUsed],
81+
recoverBlock: PartialFunction[Throwable, T]
82+
)(implicit
83+
um: Unmarshaller[ByteString, T],
84+
materializer: Materializer
7185
): Source[T, NotUsed] = {
7286
val request = getWSRequestOptional(Some(endPoint), endPointParam, params)
7387

7488
val requestWithBody = if (bodyParams.nonEmpty) {
75-
val bodyParamsX = bodyParams.collect { case (fieldName, Some(jsValue)) => (fieldName.toString, jsValue) }
89+
val bodyParamsX = bodyParams.collect { case (fieldName, Some(jsValue)) =>
90+
(fieldName.toString, jsValue)
91+
}
7692
request.withBody(JsObject(bodyParamsX))
7793
} else
7894
request
7995

8096
val source =
8197
requestWithBody.withMethod(method).stream().map { response =>
82-
response
83-
.bodyAsSource
98+
response.bodyAsSource
8499
.via(framing)
85-
.mapAsync(1)(bytes => Unmarshal(bytes).to[T]) // unmarshal one by one
100+
.mapAsync(1)(bytes => Unmarshal(bytes).to[T]) // unmarshal one by one
86101
.recover {
87-
case e: TimeoutException => throw new OpenAIScalaClientTimeoutException(s"$serviceName.$endPoint timed out: ${e.getMessage}.")
88-
case e: UnknownHostException => throw new OpenAIScalaClientUnknownHostException(s"$serviceName.$endPoint cannot resolve a host name: ${e.getMessage}.")
102+
case e: TimeoutException =>
103+
throw new OpenAIScalaClientTimeoutException(
104+
s"$serviceName.$endPoint timed out: ${e.getMessage}."
105+
)
106+
case e: UnknownHostException =>
107+
throw new OpenAIScalaClientUnknownHostException(
108+
s"$serviceName.$endPoint cannot resolve a host name: ${e.getMessage}."
109+
)
89110
}
90111
.recover(recoverBlock) // extra recover
91112
}

openai-client/src/main/scala/io/cequence/openaiscala/ConfigImplicits.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import com.typesafe.config.Config
55
object ConfigImplicits {
66
implicit class ConfigExt(config: Config) {
77
def optionalString(configPath: String): Option[String] =
8-
if (config.hasPath(configPath)) Some(config.getString(configPath)) else None
8+
if (config.hasPath(configPath)) Some(config.getString(configPath))
9+
else None
910

1011
def optionalInt(configPath: String): Option[Int] =
1112
if (config.hasPath(configPath)) Some(config.getInt(configPath)) else None
1213

1314
def optionalBoolean(configPath: String): Option[Boolean] =
14-
if (config.hasPath(configPath)) Some(config.getBoolean(configPath)) else None
15+
if (config.hasPath(configPath)) Some(config.getBoolean(configPath))
16+
else None
1517
}
1618
}

0 commit comments

Comments
 (0)