Skip to content

Commit

Permalink
CreateChatCompletionWithJSON - parseJson customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbanda committed Jan 6, 2025
1 parent fe468b1 commit 552b056
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.cequence.openaiscala.domain.settings.{
}
import io.cequence.openaiscala.domain.{BaseMessage, ChatRole, ModelId, UserMessage}
import org.slf4j.{Logger, LoggerFactory}
import play.api.libs.json.{Format, Json}
import play.api.libs.json.{Format, JsValue, Json}

import scala.concurrent.{ExecutionContext, Future}

Expand Down Expand Up @@ -61,7 +61,8 @@ object OpenAIChatCompletionExtra {
failoverModels: Seq[String] = Nil,
maxRetries: Option[Int] = Some(defaultMaxRetries),
retryOnAnyError: Boolean = false,
taskNameForLogging: Option[String] = None
taskNameForLogging: Option[String] = None,
parseJson: String => JsValue = defaultParseJsonOrThrow
)(
implicit ec: ExecutionContext,
scheduler: Scheduler
Expand Down Expand Up @@ -93,7 +94,7 @@ object OpenAIChatCompletionExtra {
val content = response.choices.head.message.content
val contentTrimmed = content.stripPrefix("```json").stripSuffix("```").trim
val contentJson = contentTrimmed.dropWhile(_ != '{')
val json = parseJsonOrThrow(contentJson)
val json = parseJson(contentJson)

logger.debug(
s"${taskNameForLoggingFinal.capitalize} finished in " + (new java.util.Date().getTime - start.getTime) + " ms."
Expand All @@ -103,7 +104,7 @@ object OpenAIChatCompletionExtra {
}
}

private def parseJsonOrThrow(
private def defaultParseJsonOrThrow(
jsonString: String
) = try {
Json.parse(jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object CreateChatCompletionVisionWithURL extends Example {
.createChatCompletion(
messages,
settings = CreateChatCompletionSettings(
model = ModelId.gpt_4_vision_preview,
model = ModelId.gpt_4o,
temperature = Some(0),
max_tokens = Some(300)
)
Expand Down

0 comments on commit 552b056

Please sign in to comment.