io.cequence.openaiscala.task.binary.ConvertBinaryToDecimalTask.scala Maven / Gradle / Ivy
The newest version!
package io.cequence.openaiscala.task.binary
import io.cequence.openaiscala.task.domain.BinaryTaskCoreSettings
import java.{lang => jl}
class ConvertBinaryToDecimalTask(withAssistantSeedPrompt: Boolean = false) extends BinaryStringTask[BinaryTaskCoreSettings] {
private val answerPrefix = "The decimal representation of the binary number is"
override def seedAssistantPrompt = if (withAssistantSeedPrompt) Some(answerPrefix) else None
private val answerPattern = s"${answerPrefix.toLowerCase} [\\d]*".r
override def generatePrompt(
input: String,
settings: BinaryTaskCoreSettings
) =
s"""In this task you must convert a given binary number to its decimal representation.${generateExamples(settings)}
|
|Now, convert the following binary number to its decimal representation:
|
|${addSpaces(settings.withSpaces)(input)}
|
|At the end of your answer write "$answerPrefix """".stripMargin
override def evalResult(
input: String,
output: String
): Option[Int] = {
val expectedOutput = toDecimal(input.replaceAll(" ", "")).toString
val actualOutput = extractWoSpacesAux(answerPattern, answerPrefix.toLowerCase(), output).map(_.trim)
actualOutput.map(actualOutputAux =>
actualOutputAux == expectedOutput match {
case true => 1 // give one point if match
case false => 0 // otherwise give zero points
}
)
}
override def expectedOutput(
input: String,
settings: BinaryTaskCoreSettings
): Option[String] = {
val expectedOutput = toDecimal(input.replaceAll(" ", ""))
Some(expectedOutput.toString)
}
// aux functions
override protected def addExample(
withSpace: Boolean)(
example: String
) = {
val expectedOutput = toDecimal(example)
s"${addSpaces(withSpace)(example)} -> $expectedOutput"
}
private def toDecimal(string: String) = jl.Long.parseLong(string, 2)
}