Skip to content

Commit

Permalink
Fix Choices Deserialization
Browse files Browse the repository at this point in the history
Some OpenAPI Compatible APIs like Ollama don't always return `lobprobs`
as part of the `choices` JSON. To handle this we need to add a default
argument to `logprobs` so upickle can handle this case. A private case
class is used to avoid having to change the order of arguments which
would be a breaking change.
  • Loading branch information
kapunga committed Dec 7, 2024
1 parent a8067b4 commit a9ca88f
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sttp.openai.requests.completions

import sttp.openai.json.SnakePickle
import sttp.openai.requests.completions.CompletionsRequestBody.CompletionModel
import sttp.openai.requests.completions.CompletionsResponseData.Choices

object CompletionsResponseData {
case class Choices(
Expand All @@ -11,7 +12,14 @@ object CompletionsResponseData {
finishReason: String
)
object Choices {
implicit val choicesR: SnakePickle.Reader[Choices] = SnakePickle.macroR[Choices]
// Unfortunately, some OpenAPI compatible APIs like Ollama don't always return `logprobs`, so we need to
// give it a default argument of `None`. Unfortunately, changing the argument order so we can do this
// would introduce a breaking change, so we need an intermediate private case class to handle this.
private case class ChoicesJson(text: String, index: Int, finishReason: String, logprobs: Option[String] = None)
private val choicesJsonR: SnakePickle.Reader[ChoicesJson] = SnakePickle.macroR

implicit val choicesR: SnakePickle.Reader[Choices] =
choicesJsonR.map(c => Choices(text = c.text, index = c.index, logprobs = c.logprobs, finishReason = c.finishReason))
}

case class CompletionsResponse(
Expand Down

0 comments on commit a9ca88f

Please sign in to comment.