Skip to content

Commit

Permalink
replaced functionCall with functionCalls (google-gemini#137)
Browse files Browse the repository at this point in the history
Co-authored-by: David Motsonashvili <davidmotson@google.com>
Co-authored-by: Rodrigo Lazo <rlazo@users.noreply.github.com>
  • Loading branch information
3 people authored and PatilShreyas committed Sep 21, 2024
1 parent 3822173 commit 10054fc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changes/common/bath-advertisement-attempt-car.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"MINOR","changes":["Add GenerateContentResponse#functionCalls and mark GenerateContentResponse#functionCall deprecated"]}
1 change: 1 addition & 0 deletions .changes/generativeai/badge-cake-arithmetic-cook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"MINOR","changes":["Add GenerateContentResponse#functionCalls and mark GenerateContentResponse#functionCall deprecated"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generativeai.type

import android.util.Log

/**
* Represents a response from the model.
*
* @property candidates a list of possible responses generated from the model
* @property promptFeedback optional feedback for the given prompt. When streaming, it's only
* populated in the first response.
*/
class GenerateContentResponse(
val candidates: List<Candidate>,
val promptFeedback: PromptFeedback?,
val usageMetadata: UsageMetadata?
) {
/** Convenience field representing the first text part in the response, if it exists. */
val text: String? by lazy { firstPartAs<TextPart>()?.text }

/** Convenience field representing the first function call part in the request, if it exists */
@Deprecated("Use functionCallParts instead", ReplaceWith("functionCallParts"))
val functionCall: FunctionCallPart? by lazy { firstPartAs() }

/** Convenience field to get all the function call parts in the request, if they exist */
val functionCalls: List<FunctionCallPart> by lazy {
candidates.first().content.parts.filterIsInstance<FunctionCallPart>()
}

/**
* Convenience field representing the first function response part in the response, if it exists.
*/
val functionResponse: FunctionResponsePart? by lazy { firstPartAs() }

private inline fun <reified T : Part> firstPartAs(): T? {
if (candidates.isEmpty()) {
warn("No candidates were found, but was asked to get a candidate.")
return null
}

val (parts, otherParts) = candidates.first().content.parts.partition { it is T }
val type = T::class.simpleName ?: "of the part type you asked for"

if (parts.isEmpty()) {
if (otherParts.isNotEmpty()) {
warn(
"We didn't find any $type, but we did find other part types. Did you ask for the right type?"
)
}

return null
}

if (parts.size > 1) {
warn("Multiple $type were found, returning the first one.")
} else if (otherParts.isNotEmpty()) {
warn("Returning the only $type found, but other part types were present as well.")
}

return parts.first() as T
}

private fun warn(message: String) {
Log.w("GenerateContentResponse", message)
}
}

0 comments on commit 10054fc

Please sign in to comment.