io.cequence.openaiscala.service.OpenAIChatCompletionServiceFactory.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openai-scala-client_2.12 Show documentation
Show all versions of openai-scala-client_2.12 Show documentation
Scala client for OpenAI API implemented using Play WS lib.
package io.cequence.openaiscala.service
import akka.stream.Materializer
import io.cequence.openaiscala.service.impl.OpenAIChatCompletionServiceImpl
import io.cequence.wsclient.domain.WsRequestContext
import io.cequence.wsclient.service.WSClientEngine
import scala.concurrent.ExecutionContext
object OpenAIChatCompletionServiceFactory
extends IOpenAIChatCompletionServiceFactory[OpenAIChatCompletionService] {
override def apply(
coreUrl: String,
requestContext: WsRequestContext = WsRequestContext()
)(
implicit ec: ExecutionContext,
materializer: Materializer
): OpenAIChatCompletionService =
new OpenAIChatCompletionServiceClassImpl(coreUrl, requestContext)
private final class OpenAIChatCompletionServiceClassImpl(
coreUrl: String,
requestContext: WsRequestContext
)(
implicit val ec: ExecutionContext,
val materializer: Materializer
) extends OpenAIChatCompletionServiceImpl {
// we use play ws client engine
protected val engine: WSClientEngine = ProjectWSClientEngine(coreUrl, requestContext)
}
}
// propose a new name for the trait
trait IOpenAIChatCompletionServiceFactory[F] extends RawWsServiceFactory[F] {
def forAzureAI(
endpoint: String,
region: String,
accessToken: String
)(
implicit ec: ExecutionContext,
materializer: Materializer
): F =
apply(
coreUrl = s"https://${endpoint}.${region}.inference.ai.azure.com/v1/",
requestContext = WsRequestContext(
authHeaders = Seq(("Authorization", s"Bearer $accessToken"))
)
)
}