|
1 | 1 | package io.cequence.openaiscala.examples
|
2 | 2 |
|
3 | 3 | import io.cequence.openaiscala.domain._
|
4 |
| -import io.cequence.openaiscala.domain.settings.JsonSchemaDef |
5 |
| -import io.cequence.openaiscala.examples.fixtures.TestFixtures |
6 |
| -import io.cequence.openaiscala.service.{JsonSchemaReflectionHelper, OpenAIServiceConsts} |
7 |
| -import play.api.libs.json.Json |
| 4 | +import io.cequence.openaiscala.domain.settings.{CreateChatCompletionSettings, JsonSchemaDef} |
| 5 | +import io.cequence.openaiscala.service.JsonSchemaReflectionHelper |
| 6 | +import play.api.libs.json.{Format, Json} |
| 7 | +import io.cequence.openaiscala.service.OpenAIChatCompletionExtra._ |
8 | 8 |
|
9 | 9 | import scala.concurrent.Future
|
10 | 10 |
|
11 |
| -// experimental |
12 |
| -object CreateChatCompletionJsonForCaseClass |
13 |
| - extends Example |
14 |
| - with TestFixtures |
15 |
| - with JsonSchemaReflectionHelper |
16 |
| - with OpenAIServiceConsts { |
| 11 | +// due to the reflection used in jsonSchemaFor, this example currently works only for Scala 2.12 and 2.13 |
| 12 | +object CreateChatCompletionJsonForCaseClass extends Example with JsonSchemaReflectionHelper { |
17 | 13 |
|
18 |
| - private val messages = Seq( |
19 |
| - SystemMessage(capitalsPrompt), |
20 |
| - UserMessage("List only african countries") |
21 |
| - ) |
22 |
| - |
23 |
| - // Case class(es) |
24 |
| - private case class CapitalsResponse( |
25 |
| - countries: Seq[Country] |
26 |
| - ) |
27 |
| - |
28 |
| - private case class Country( |
| 14 | + // data model |
| 15 | + case class Country( |
29 | 16 | country: String,
|
30 |
| - capital: String |
| 17 | + capital: String, |
| 18 | + populationMil: Double |
31 | 19 | )
|
| 20 | + case class CapitalsResponse(capitals: Seq[Country]) |
32 | 21 |
|
33 |
| - // json schema def |
34 |
| - private val jsonSchemaDef: JsonSchemaDef = JsonSchemaDef( |
| 22 | + // JSON format and schema |
| 23 | + implicit val countryFormat: Format[Country] = Json.format[Country] |
| 24 | + implicit val capitalsResponseFormat: Format[CapitalsResponse] = Json.format[CapitalsResponse] |
| 25 | + |
| 26 | + val jsonSchema: JsonSchemaDef = JsonSchemaDef( |
35 | 27 | name = "capitals_response",
|
36 | 28 | strict = true,
|
37 |
| - // reflective json schema for case class |
38 |
| - structure = jsonSchemaFor[CapitalsResponse]() |
| 29 | + jsonSchemaFor[CapitalsResponse]() |
| 30 | + ) |
| 31 | + |
| 32 | + // messages / prompts |
| 33 | + val messages: Seq[BaseMessage] = Seq( |
| 34 | + SystemMessage("You are an expert geographer"), |
| 35 | + UserMessage("List the most populous African countries in the prescribed JSON format") |
39 | 36 | )
|
40 | 37 |
|
41 |
| - override protected def run: Future[_] = |
| 38 | + override protected def run: Future[_] = { |
| 39 | + // chat completion JSON run |
42 | 40 | service
|
43 |
| - .createChatCompletion( |
44 |
| - messages = messages, |
45 |
| - settings = DefaultSettings.createJsonChatCompletion(jsonSchemaDef) |
| 41 | + .createChatCompletionWithJSON[CapitalsResponse]( |
| 42 | + messages, |
| 43 | + settings = CreateChatCompletionSettings( |
| 44 | + model = ModelId.gpt_4o_2024_08_06, |
| 45 | + temperature = Some(0), |
| 46 | + jsonSchema = Some(jsonSchema) |
| 47 | + ) |
46 | 48 | )
|
47 | 49 | .map { response =>
|
48 |
| - val json = Json.parse(messageContent(response)) |
49 |
| - println(Json.prettyPrint(json)) |
| 50 | + response.capitals.foreach(println) |
50 | 51 | }
|
| 52 | + } |
51 | 53 | }
|
0 commit comments