Skip to content

Scalafmt #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules = [
DisableSyntax,
ExplicitResultTypes,
LeakingImplicitClassVal,
NoAutoTupling,
NoValInForComprehension,
ProcedureSyntax,
RedundantSyntax,
RemoveUnused
]
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 3.7.4
runner.dialect = scala213
30 changes: 25 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@ lazy val guice = (project in file("openai-guice"))
.dependsOn(client)
.aggregate(client_stream)


// POM settings for Sonatype
ThisBuild / homepage := Some(url("https://github.com/cequence-io/openai-scala-client"))
ThisBuild / homepage := Some(
url("https://github.com/cequence-io/openai-scala-client")
)

ThisBuild / sonatypeProfileName := "io.cequence"

ThisBuild / scmInfo := Some(ScmInfo(url("https://github.com/cequence-io/openai-scala-client"), "scm:[email protected]:cequence-io/openai-scala-client.git"))
ThisBuild / scmInfo := Some(
ScmInfo(
url("https://github.com/cequence-io/openai-scala-client"),
"scm:[email protected]:cequence-io/openai-scala-client.git"
)
)

ThisBuild / developers := List(
Developer("bnd", "Peter Banda", "[email protected]", url("https://peterbanda.net"))
Developer(
"bnd",
"Peter Banda",
"[email protected]",
url("https://peterbanda.net")
)
)

ThisBuild / licenses += "MIT" -> url("https://opensource.org/licenses/MIT")
Expand All @@ -44,4 +55,13 @@ ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"

ThisBuild / sonatypeRepository := "https://s01.oss.sonatype.org/service/local"

ThisBuild / publishTo := sonatypePublishToBundle.value
ThisBuild / publishTo := sonatypePublishToBundle.value

inThisBuild(
List(
scalacOptions += "-Ywarn-unused",
scalaVersion := "2.12.15",
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,73 @@ package io.cequence.openaiscala.service
import akka.NotUsed
import akka.stream.scaladsl.Source
import io.cequence.openaiscala.domain.MessageSpec
import io.cequence.openaiscala.domain.response.{ChatCompletionChunkResponse, ChatCompletionResponse, FineTuneEvent, TextCompletionResponse}
import io.cequence.openaiscala.domain.settings.{CreateChatCompletionSettings, CreateCompletionSettings}

import scala.concurrent.Future
import io.cequence.openaiscala.domain.response.{
ChatCompletionChunkResponse,
FineTuneEvent,
TextCompletionResponse
}
import io.cequence.openaiscala.domain.settings.{
CreateChatCompletionSettings,
CreateCompletionSettings
}

trait OpenAIServiceStreamedExtra extends OpenAIServiceConsts {

/**
* Creates a completion for the provided prompt and parameters with streamed results.
*
* @param prompt The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
Note that <|endoftext|> is the document separator that the model sees during training,
so if a prompt is not specified the model will generate as if from the beginning of a new document.
* @param settings
* @return text completion response as a stream (source)
*
* @see <a href="https://beta.openai.com/docs/api-reference/completions/create">OpenAI Doc</a>
*/
/** Creates a completion for the provided prompt and parameters with streamed
* results.
*
* @param prompt
* The prompt(s) to generate completions for, encoded as a string, array of
* strings, array of tokens, or array of token arrays. Note that
* <|endoftext|> is the document separator that the model sees during
* training, so if a prompt is not specified the model will generate as if
* from the beginning of a new document.
* @param settings
* @return
* text completion response as a stream (source)
*
* @see
* <a
* href="https://beta.openai.com/docs/api-reference/completions/create">OpenAI
* Doc</a>
*/
def createCompletionStreamed(
prompt: String,
settings: CreateCompletionSettings = DefaultSettings.CreateCompletion
prompt: String,
settings: CreateCompletionSettings = DefaultSettings.CreateCompletion
): Source[TextCompletionResponse, NotUsed]

/**
* Creates a completion for the chat message(s) with streamed results.
*
* @param messages The messages to generate chat completions.
* @param settings
* @return chat completion response
*
* @see <a href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI Doc</a>
*/
/** Creates a completion for the chat message(s) with streamed results.
*
* @param messages
* The messages to generate chat completions.
* @param settings
* @return
* chat completion response
*
* @see
* <a
* href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI
* Doc</a>
*/
def createChatCompletionStreamed(
messages: Seq[MessageSpec],
settings: CreateChatCompletionSettings = DefaultSettings.CreateChatCompletion
messages: Seq[MessageSpec],
settings: CreateChatCompletionSettings =
DefaultSettings.CreateChatCompletion
): Source[ChatCompletionChunkResponse, NotUsed]

/**
* Get fine-grained status updates for a fine-tune job with streamed results.
*
* @param fineTuneId The ID of the fine-tune job to get events for.
* @return fine tune events or None if not found as a stream (source)
*
* @see <a href="https://beta.openai.com/docs/api-reference/fine-tunes/events">OpenAI Doc</a>
*/
/** Get fine-grained status updates for a fine-tune job with streamed results.
*
* @param fineTuneId
* The ID of the fine-tune job to get events for.
* @return
* fine tune events or None if not found as a stream (source)
*
* @see
* <a
* href="https://beta.openai.com/docs/api-reference/fine-tunes/events">OpenAI
* Doc</a>
*/
def listFineTuneEventsStreamed(
fineTuneId: String
fineTuneId: String
): Source[FineTuneEvent, NotUsed]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,66 @@ import io.cequence.openaiscala.domain.response._
import io.cequence.openaiscala.service.ws.{Timeouts, WSStreamRequestHelper}
import io.cequence.openaiscala.OpenAIScalaClientException
import io.cequence.openaiscala.domain.MessageSpec
import play.api.libs.json.{JsValue, Json}
import play.api.libs.json.JsValue

import scala.concurrent.ExecutionContext

/**
* Private impl. class of [[OpenAIService]].
*
* @param apiKey
* @param orgId
* @param ec
* @param materializer
*
* @since Jan 2023
*/
private trait OpenAIServiceStreamedExtraImpl extends OpenAIServiceStreamedExtra with WSStreamRequestHelper {
/** Private impl. class of [[OpenAIService]].
*
* @param apiKey
* @param orgId
* @param ec
* @param materializer
*
* @since Jan
* 2023
*/
private trait OpenAIServiceStreamedExtraImpl
extends OpenAIServiceStreamedExtra
with WSStreamRequestHelper {
this: OpenAIServiceImpl =>

override def createCompletionStreamed(
prompt: String,
settings: CreateCompletionSettings
prompt: String,
settings: CreateCompletionSettings
): Source[TextCompletionResponse, NotUsed] =
execJsonStreamAux(
Command.completions,
"POST",
bodyParams = createBodyParamsForCompletion(prompt, settings, stream = true)
bodyParams =
createBodyParamsForCompletion(prompt, settings, stream = true)
).map { (json: JsValue) =>
(json \ "error").toOption.map { error =>
throw new OpenAIScalaClientException(error.toString())
}.getOrElse(
json.asSafe[TextCompletionResponse]
)
(json \ "error").toOption
.map { error =>
throw new OpenAIScalaClientException(error.toString())
}
.getOrElse(
json.asSafe[TextCompletionResponse]
)
}

override def createChatCompletionStreamed(
messages: Seq[MessageSpec],
settings: CreateChatCompletionSettings = DefaultSettings.CreateChatCompletion
messages: Seq[MessageSpec],
settings: CreateChatCompletionSettings =
DefaultSettings.CreateChatCompletion
): Source[ChatCompletionChunkResponse, NotUsed] =
execJsonStreamAux(
Command.chat_completions,
"POST",
bodyParams = createBodyParamsForChatCompletion(messages, settings, stream = true)
bodyParams =
createBodyParamsForChatCompletion(messages, settings, stream = true)
).map { (json: JsValue) =>
(json \ "error").toOption.map { error =>
throw new OpenAIScalaClientException(error.toString())
}.getOrElse(
json.asSafe[ChatCompletionChunkResponse]
)
(json \ "error").toOption
.map { error =>
throw new OpenAIScalaClientException(error.toString())
}
.getOrElse(
json.asSafe[ChatCompletionChunkResponse]
)
}

override def listFineTuneEventsStreamed(
fineTuneId: String
fineTuneId: String
): Source[FineTuneEvent, NotUsed] =
execJsonStreamAux(
Command.fine_tunes,
Expand All @@ -70,21 +79,29 @@ private trait OpenAIServiceStreamedExtraImpl extends OpenAIServiceStreamedExtra
Tag.stream -> Some(true)
)
).map { json =>
(json \ "error").toOption.map { error =>
throw new OpenAIScalaClientException(error.toString())
}.getOrElse(
json.asSafe[FineTuneEvent]
)
(json \ "error").toOption
.map { error =>
throw new OpenAIScalaClientException(error.toString())
}
.getOrElse(
json.asSafe[FineTuneEvent]
)
}
}

object OpenAIServiceStreamedFactory extends OpenAIServiceFactoryHelper[OpenAIService with OpenAIServiceStreamedExtra] {
object OpenAIServiceStreamedFactory
extends OpenAIServiceFactoryHelper[
OpenAIService with OpenAIServiceStreamedExtra
] {

override def apply(
apiKey: String,
orgId: Option[String] = None,
timeouts: Option[Timeouts] = None)(
implicit ec: ExecutionContext, materializer: Materializer
apiKey: String,
orgId: Option[String] = None,
timeouts: Option[Timeouts] = None
)(implicit
ec: ExecutionContext,
materializer: Materializer
): OpenAIService with OpenAIServiceStreamedExtra =
new OpenAIServiceImpl(apiKey, orgId, timeouts) with OpenAIServiceStreamedExtraImpl
}
new OpenAIServiceImpl(apiKey, orgId, timeouts)
with OpenAIServiceStreamedExtraImpl
}
Loading