|
| 1 | +package io.cequence.openaiscala.examples.responsesapi |
| 2 | + |
| 3 | +import scala.concurrent.Future |
| 4 | +import io.cequence.openaiscala.domain.responsesapi.{Inputs, CreateModelResponseSettings} |
| 5 | +import io.cequence.openaiscala.examples.Example |
| 6 | +import io.cequence.openaiscala.domain.ModelId |
| 7 | +import io.cequence.openaiscala.domain.responsesapi.tools.FunctionTool |
| 8 | +import io.cequence.openaiscala.domain.JsonSchema |
| 9 | +import io.cequence.openaiscala.domain.responsesapi.tools.ToolChoice |
| 10 | + |
| 11 | +object CreateModelResponseWithFunctions extends Example { |
| 12 | + |
| 13 | + override def run: Future[Unit] = |
| 14 | + service |
| 15 | + .createModelResponse( |
| 16 | + Inputs.Text("What is the weather like in Boston today?"), |
| 17 | + settings = CreateModelResponseSettings( |
| 18 | + model = ModelId.gpt_4o_2024_08_06, |
| 19 | + tools = Seq( |
| 20 | + FunctionTool( |
| 21 | + name = "get_current_weather", |
| 22 | + parameters = JsonSchema.Object( |
| 23 | + properties = Map( |
| 24 | + "location" -> JsonSchema.String( |
| 25 | + description = Some("The city and state, e.g. San Francisco, CA") |
| 26 | + ), |
| 27 | + "unit" -> JsonSchema.String( |
| 28 | + enum = Seq("celsius", "fahrenheit") |
| 29 | + ) |
| 30 | + ), |
| 31 | + required = Seq("location", "unit") |
| 32 | + ), |
| 33 | + description = Some("Get the current weather in a given location"), |
| 34 | + strict = true |
| 35 | + ) |
| 36 | + ), |
| 37 | + toolChoice = Some(ToolChoice.Mode.Auto) |
| 38 | + ) |
| 39 | + ) |
| 40 | + .map { response => |
| 41 | + val functionCall = response.outputFunctionCalls.headOption.getOrElse(throw new RuntimeException("No function call output found")) |
| 42 | + |
| 43 | + println( |
| 44 | + s"""Function Call Details: |
| 45 | + |Name: ${functionCall.name} |
| 46 | + |Arguments: ${functionCall.arguments} |
| 47 | + |Call ID: ${functionCall.callId} |
| 48 | + |ID: ${functionCall.id} |
| 49 | + |Status: ${functionCall.status}""".stripMargin |
| 50 | + ) |
| 51 | + |
| 52 | + val toolsUsed = response.tools.map(_.typeString) |
| 53 | + |
| 54 | + println(s"${toolsUsed.size} tools used: ${toolsUsed.mkString(", ")}") |
| 55 | + } |
| 56 | +} |
0 commit comments