-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCreateChatToolCompletion.scala
70 lines (63 loc) · 2.13 KB
/
CreateChatToolCompletion.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io.cequence.openaiscala.examples
import io.cequence.openaiscala.domain.AssistantTool.FunctionTool
import io.cequence.openaiscala.domain._
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
import play.api.libs.json.{JsObject, Json}
import io.cequence.openaiscala.JsonFormats._
import scala.concurrent.Future
object CreateChatToolCompletion extends Example {
val messages = Seq(
SystemMessage("You are a helpful assistant."),
UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?")
)
val tools = Seq(
FunctionTool(
name = "get_current_weather",
description = Some("Get the current weather in a given location"),
parameters = Json
.toJson(
JsonSchema.Object(
properties = Seq(
"location" -> JsonSchema.String(
description = Some("The city and state, e.g. San Francisco, CA")
),
"unit" -> JsonSchema.String(
description = Some("The unit of temperature"),
`enum` = Seq("celsius", "fahrenheit")
)
),
required = Seq("location")
): JsonSchema
)
.as[JsObject]
.value
.toMap
)
)
override protected def run: Future[_] =
service
.createChatToolCompletion(
messages = messages,
tools = tools,
responseToolChoice = None, // means "auto"
settings = CreateChatCompletionSettings(
ModelId.gpt_3_5_turbo_1106,
parallel_tool_calls = Some(true)
)
)
.map { response =>
val chatFunCompletionMessage = response.choices.head.message
val toolCalls = chatFunCompletionMessage.tool_calls.collect {
case (id, x: FunctionCallSpec) => (id, x)
}
println(
"tool call ids : " + toolCalls.map(_._1).mkString(", ")
)
println(
"function/tool call names : " + toolCalls.map(_._2.name).mkString(", ")
)
println(
"function/tool call arguments : " + toolCalls.map(_._2.arguments).mkString(", ")
)
}
}