Skip to content

Commit

Permalink
Merge pull request #75 from cequence-io/feature/special-character-par…
Browse files Browse the repository at this point in the history
…sing

correctly parse byte arrays
  • Loading branch information
peterbanda authored Jul 16, 2024
2 parents 3b96f16 + 9122381 commit 5e921ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,38 @@ object JsonFormats {
Format(reads, writes)
}

implicit val byteArrayReads: Reads[Seq[Byte]] = new Reads[Seq[Byte]] {

/**
* Parses a JSON representation of a `Seq[Byte]` into a `JsResult[Seq[Byte]]`. This method
* expects the JSON to be an array of numbers, where each number represents a valid byte
* value (between -128 and 127, inclusive). If the JSON structure is correct and all
* numbers are valid byte values, it returns a `JsSuccess` containing the sequence of
* bytes. Otherwise, it returns a `JsError` detailing the parsing issue encountered.
*
* @param json
* The `JsValue` to be parsed, expected to be a `JsArray` of `JsNumber`.
* @return
* A `JsResult[Seq[Byte]]` which is either a `JsSuccess` containing the parsed sequence
* of bytes, or a `JsError` with parsing error details.
*/
def reads(json: JsValue): JsResult[Seq[Byte]] = json match {
case JsArray(elements) =>
try {
JsSuccess(elements.map {
case JsNumber(n) if n.isValidInt => n.toIntExact.toByte
case _ => throw new RuntimeException("Invalid byte value")
}.toIndexedSeq)
} catch {
case e: Exception => JsError("Error parsing byte array: " + e.getMessage)
}
case _ => JsError("Expected JSON array for byte array")
}
}

implicit lazy val logprobInfoFormat: Format[LogprobInfo] =
Json.format[LogprobInfo]

implicit lazy val logprobsFormat: Format[Logprobs] =
Json.format[Logprobs]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.cequence.openaiscala.examples

import io.cequence.openaiscala.domain._
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings

import scala.concurrent.Future

object CreateChatCompletionWithLogprobsWithSpecialChars extends Example {

val messages = Seq(UserMessage("Just return the string °C and nothing else"))

override protected def run: Future[_] =
service
.createChatCompletion(
messages = messages,
settings = CreateChatCompletionSettings(
model = ModelId.gpt_4o,
temperature = Some(0),
max_tokens = Some(100),
logprobs = Some(true),
top_logprobs = Some(3)
)
)
.map { response =>
printMessageContent(response)
val logprobs = response.choices.head.logprobs.map(_.content).getOrElse(Nil)
logprobs.foreach { logprob =>
println(
s"Logprob: ${logprob.token} -> ${logprob.logprob}, top: ${logprob.top_logprobs.map(_.token).mkString(", ")}"
)
}
}
}

0 comments on commit 5e921ff

Please sign in to comment.