forked from google-gemini/generative-ai-android
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaced functionCall with functionCalls (google-gemini#137)
Co-authored-by: David Motsonashvili <davidmotson@google.com> Co-authored-by: Rodrigo Lazo <rlazo@users.noreply.github.com>
- Loading branch information
1 parent
3822173
commit 10054fc
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type":"MINOR","changes":["Add GenerateContentResponse#functionCalls and mark GenerateContentResponse#functionCall deprecated"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type":"MINOR","changes":["Add GenerateContentResponse#functionCalls and mark GenerateContentResponse#functionCall deprecated"]} |
81 changes: 81 additions & 0 deletions
81
generativeai/src/main/java/com/google/ai/client/generativeai/type/GenerateContentResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |