Skip to content

install scalafix plugin and run scalafix on all source files #12

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 1 commit 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
]
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,10 +3,9 @@ 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.response.{ChatCompletionChunkResponse, FineTuneEvent, TextCompletionResponse}
import io.cequence.openaiscala.domain.settings.{CreateChatCompletionSettings, CreateCompletionSettings}

import scala.concurrent.Future

trait OpenAIServiceStreamedExtra extends OpenAIServiceConsts {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.cequence.openaiscala.service.ws

import akka.NotUsed
import akka.http.scaladsl.common.{EntityStreamingSupport, JsonEntityStreamingSupport}
import akka.http.scaladsl.common.EntityStreamingSupport
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
import akka.stream.Materializer
import akka.stream.scaladsl.Framing.FramingException
import akka.stream.scaladsl.{Flow, Framing, Source}
import akka.util.ByteString
import com.fasterxml.jackson.core.JsonParseException
import io.cequence.openaiscala.{OpenAIScalaClientException, OpenAIScalaClientTimeoutException, OpenAIScalaClientUnknownHostException}
import play.api.libs.json.{JsNull, JsObject, JsString, JsValue, Json}
import play.api.libs.json.{JsObject, JsString, JsValue, Json}
import play.api.libs.ws.JsonBodyWritables._

import java.net.UnknownHostException
Expand All @@ -26,8 +26,7 @@ trait WSStreamRequestHelper {
private val itemPrefix = "data: "
private val endOfStreamToken = "[DONE]"

private implicit val jsonStreamingSupport: JsonEntityStreamingSupport =
EntityStreamingSupport.json()
EntityStreamingSupport.json()

private implicit val jsonMarshaller: Unmarshaller[ByteString, JsValue] =
Unmarshaller.strict[ByteString, JsValue] { byteString =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.typesafe.config.Config

object ConfigImplicits {
implicit class ConfigExt(config: Config) {
def optionalString(configPath: String) =
def optionalString(configPath: String): Option[String] =
if (config.hasPath(configPath)) Some(config.getString(configPath)) else None

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

def optionalBoolean(configPath: String) =
def optionalBoolean(configPath: String): Option[Boolean] =
if (config.hasPath(configPath)) Some(config.getBoolean(configPath)) else None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package io.cequence.openaiscala
import io.cequence.openaiscala.JsonUtil.JsonOps
import io.cequence.openaiscala.domain.ChatRole

import java.{util => ju}
import io.cequence.openaiscala.domain.response._
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, Json, _}

object JsonFormats {
private implicit val dateFormat: Format[ju.Date] = JsonUtil.SecDateFormat
JsonUtil.SecDateFormat

implicit val PermissionFormat: Format[Permission] = Json.format[Permission]
implicit val modelSpecFormat: Format[ModelInfo] = Json.format[ModelInfo]

implicit val usageInfoFormat: Format[UsageInfo] = Json.format[UsageInfo]

private implicit val stringDoubleMapFormat: Format[Map[String, Double]] = JsonUtil.StringDoubleMapFormat
private implicit val stringStringMapFormat: Format[Map[String, String]] = JsonUtil.StringStringMapFormat
JsonUtil.StringDoubleMapFormat
JsonUtil.StringStringMapFormat

implicit val logprobsInfoFormat: Format[LogprobsInfo] = Json.format[LogprobsInfo]
implicit val textCompletionChoiceInfoFormat: Format[TextCompletionChoiceInfo] = Json.format[TextCompletionChoiceInfo]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import java.util.Date
object JsonUtil {

implicit class JsonOps(val json: JsValue) {
def asSafe[T](implicit fjs: Reads[T]) =
def asSafe[T](implicit fjs: Reads[T]): T =
try {
json.validate[T] match {
case JsSuccess(value, path) => value
case JsSuccess(value, _) => value
case JsError(errors) =>
val errorString = errors.map { case (path, pathErrors) => s"JSON at path '${path}' contains the following errors: ${pathErrors.map(_.message).mkString(";")}" }.mkString("\n")
throw new OpenAIScalaClientException(s"Unexpected JSON:\n'${Json.prettyPrint(json)}'. Cannot be parsed due to: $errorString")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.cequence.openaiscala.JsonFormats._
import io.cequence.openaiscala.OpenAIScalaClientException
import io.cequence.openaiscala.domain.settings._
import io.cequence.openaiscala.domain.response._
import io.cequence.openaiscala.ConfigImplicits._
import io.cequence.openaiscala.domain.MessageSpec
import io.cequence.openaiscala.service.ws.{Timeouts, WSRequestHelper}

Expand Down Expand Up @@ -77,7 +76,7 @@ private class OpenAIServiceImpl(
prompt: String,
settings: CreateCompletionSettings,
stream: Boolean
) =
): Seq[(Tag, Option[JsValue])] =
jsonBodyParams(
Tag.prompt -> Some(prompt),
Tag.model -> Some(settings.model),
Expand Down Expand Up @@ -120,7 +119,7 @@ private class OpenAIServiceImpl(
messages: Seq[MessageSpec],
settings: CreateChatCompletionSettings,
stream: Boolean
) = {
): Seq[(Tag, Option[JsValue])] = {
assert(messages.nonEmpty, "At least one message expected.")

val messageJsons = messages.map { case MessageSpec(role, content) =>
Expand Down Expand Up @@ -495,14 +494,14 @@ private class OpenAIServiceImpl(
endPoint: Option[PEP],
endPointParam: Option[String],
params: Seq[(PT, Option[Any])] = Nil
) =
): StandaloneWSRequest#Self =
addHeaders(super.getWSRequestOptional(endPoint, endPointParam, params))

override protected def getWSRequest(
endPoint: Option[PEP],
endPointParam: Option[String],
params: Seq[(PT, Any)] = Nil
) =
): StandaloneWSRequest#Self =
addHeaders(super.getWSRequest(endPoint, endPointParam, params))

private def addHeaders(request: StandaloneWSRequest) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ trait WSHelper {
client
}

def close =
def close: Unit =
client.close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.JsonMappingException
import io.cequence.openaiscala.JsonUtil.toJson
import io.cequence.openaiscala.{OpenAIScalaClientException, OpenAIScalaClientTimeoutException, OpenAIScalaClientUnknownHostException, OpenAIScalaTokenCountExceededException}
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.libs.json.{JsObject, JsValue}
import play.api.libs.ws.{BodyWritable, StandaloneWSRequest}
import play.api.libs.ws.JsonBodyWritables._
import play.api.libs.ws.JsonBodyReadables._
Expand Down Expand Up @@ -180,7 +180,7 @@ trait WSRequestHelper extends WSHelper {
body: T,
endPointForLogging: Option[PEP], // only for logging
acceptableStatusCodes: Seq[Int] = defaultAcceptableStatusCodes
) =
): Future[RichJsResponse] =
execRequestJsonAux(
request, _.post(body),
acceptableStatusCodes,
Expand All @@ -192,7 +192,7 @@ trait WSRequestHelper extends WSHelper {
body: T,
endPointForLogging: Option[PEP], // only for logging
acceptableStatusCodes: Seq[Int] = defaultAcceptableStatusCodes
) =
): Future[RichStringResponse] =
execRequestStringAux(
request, _.post(body),
acceptableStatusCodes,
Expand Down Expand Up @@ -319,10 +319,10 @@ trait WSRequestHelper extends WSHelper {

protected def jsonBodyParams(
params: (PT, Option[Any])*
) =
): Seq[(PT, Option[JsValue])] =
params.map { case (paramName, value) => (paramName, value.map(toJson)) }

protected def handleErrorResponse[T](response: RichResponse[T]) =
protected def handleErrorResponse[T](response: RichResponse[T]): T =
response match {
case Left(data) => data

Expand All @@ -344,13 +344,13 @@ trait WSRequestHelper extends WSHelper {
if (errorCode == 404) None else throw new OpenAIScalaClientException(s"Code ${errorCode} : ${message}")
}

protected def paramsAsString(params: Seq[(PT, Any)]) = {
protected def paramsAsString(params: Seq[(PT, Any)]): String = {
val string = params.map { case (tag, value) => s"$tag=$value" }.mkString("&")

if (string.nonEmpty) s"?$string" else ""
}

protected def paramsOptionalAsString(params: Seq[(PT, Option[Any])]) = {
protected def paramsOptionalAsString(params: Seq[(PT, Option[Any])]): String = {
val string = params.collect { case (tag, Some(value)) => s"$tag=$value" }.mkString("&")

if (string.nonEmpty) s"?$string" else ""
Expand All @@ -359,12 +359,12 @@ trait WSRequestHelper extends WSHelper {
protected def createUrl(
endpoint: Option[PEP],
value: Option[String] = None
) =
): String =
coreUrl + endpoint.map(_.toString).getOrElse("") + value.map("/" + _).getOrElse("")

protected def toOptionalParams(
params: Seq[(PT, Any)]
) =
): Seq[(PT, Some[Any])] =
params.map { case (a, b) => (a, Some(b)) }

// close
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.cequence.openaiscala.domain

abstract class EnumValue(value: String = "") {
override def toString = if (value.nonEmpty) value else getClass.getSimpleName.stripSuffix("$")
override def toString: String = if (value.nonEmpty) value else getClass.getSimpleName.stripSuffix("$")
}
Loading