Skip to content

Commit

Permalink
Update function calling kotlin sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rlazo committed Jul 12, 2024
1 parent b04745e commit ef62034
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ suspend fun functionCalling() {
var response = chat.sendMessage(prompt)

// Check if the model responded with a function call
if (response.functionCalls.isNotEmpty() &&
response.functionCalls.first().name == "multiply") {
val functionCall = response.functionCalls.first()

val a = functionCall.args["a"]?.toDouble() ?: throw InvalidStateException("Missing param 'a'")
val b = functionCall.args["b"]?.toDouble() ?: throw InvalidStateException("Missing param 'b'")
val result = JSONObject().put("result", multiply(a, b))
response.functionCalls.first { it.name == "multiply" }.apply {
val a: String by args
val b: String by args

val result = JSONObject(mapOf("result" to multiply(a.toDouble(), b.toDouble())))
response = chat.sendMessage(
content(role = "function") {
part(FunctionResponsePart(functionCall.name, result))
part(FunctionResponsePart("multiply", result))
}
)
}
Expand Down

0 comments on commit ef62034

Please sign in to comment.