Skip to content

Commit

Permalink
Add opt in annotation for v1beta endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
David Motsonashvili committed Feb 1, 2024
1 parent f95f85d commit 34287a8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.ai.client.generativeai

import android.graphics.Bitmap
import com.google.ai.client.generativeai.type.BetaGenAiAPI
import com.google.ai.client.generativeai.type.BlobPart
import com.google.ai.client.generativeai.type.Content
import com.google.ai.client.generativeai.type.FunctionCallPart
Expand Down Expand Up @@ -46,6 +47,7 @@ import kotlinx.coroutines.flow.transform
* @param model the model to use for the interaction
* @property history the previous interactions with the model
*/
@OptIn(BetaGenAiAPI::class)
class Chat(private val model: GenerativeModel, val history: MutableList<Content> = ArrayList()) {
private var lock = Semaphore(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.ai.client.generativeai.internal.api.CountTokensRequest
import com.google.ai.client.generativeai.internal.api.GenerateContentRequest
import com.google.ai.client.generativeai.internal.util.toInternal
import com.google.ai.client.generativeai.internal.util.toPublic
import com.google.ai.client.generativeai.type.BetaGenAiAPI
import com.google.ai.client.generativeai.type.Content
import com.google.ai.client.generativeai.type.CountTokensResponse
import com.google.ai.client.generativeai.type.FinishReason
Expand Down Expand Up @@ -53,6 +54,7 @@ import kotlinx.coroutines.flow.map
* @property safetySettings the safety bounds to use during alongside prompts during content
* generation
*/
@OptIn(BetaGenAiAPI::class)
class GenerativeModel
internal constructor(
val modelName: String,
Expand Down Expand Up @@ -182,6 +184,7 @@ internal constructor(
* @param call A [FunctionCallPart] from the model, containing a function call and parameters
* @return The output of the requested function call
*/
@BetaGenAiAPI
suspend fun executeFunction(call: FunctionCallPart): String {
if (tools == null) {
throw RuntimeException("No registered tools")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.google.ai.client.generativeai.internal.api.shared.HarmCategory
import com.google.ai.client.generativeai.internal.api.shared.Part
import com.google.ai.client.generativeai.internal.api.shared.SafetySetting
import com.google.ai.client.generativeai.internal.api.shared.TextPart
import com.google.ai.client.generativeai.type.BetaGenAiAPI
import com.google.ai.client.generativeai.type.BlockThreshold
import com.google.ai.client.generativeai.type.CitationMetadata
import com.google.ai.client.generativeai.type.FunctionDeclaration
Expand Down Expand Up @@ -108,11 +109,13 @@ internal fun BlockThreshold.toInternal() =
BlockThreshold.UNSPECIFIED -> HarmBlockThreshold.UNSPECIFIED
}

@BetaGenAiAPI
internal fun Tool.toInternal() =
com.google.ai.client.generativeai.internal.api.client.Tool(
functionDeclarations.map { it.toInternal() }
)

@BetaGenAiAPI
internal fun FunctionDeclaration.toInternal():
com.google.ai.client.generativeai.internal.api.client.FunctionDeclaration {
val convertedParams = buildJsonObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.google.ai.client.generativeai.type

@RequiresOptIn(message = "This API is only available on the v1beta endpoint")
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class BetaGenAiAPI
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package com.google.ai.client.generativeai.type
* @property description A description of what the function does and its output.
* @property function the function implementation
*/
@BetaGenAiAPI
class NoParameterFunction(
name: String,
description: String,
Expand All @@ -25,6 +26,7 @@ class NoParameterFunction(
* @property param A description of the first function parameter
* @property function the function implementation
*/
@BetaGenAiAPI
class OneParameterFunction(
name: String,
description: String,
Expand All @@ -44,6 +46,7 @@ class OneParameterFunction(
* @property param2 A description of the second function parameter
* @property function the function implementation
*/
@BetaGenAiAPI
class TwoParameterFunction(
name: String,
description: String,
Expand All @@ -65,6 +68,7 @@ class TwoParameterFunction(
* @property param3 A description of the third function parameter
* @property function the function implementation
*/
@BetaGenAiAPI
class ThreeParameterFunction(
name: String,
description: String,
Expand All @@ -88,6 +92,7 @@ class ThreeParameterFunction(
* @property param4 A description of the fourth function parameter
* @property function the function implementation
*/
@BetaGenAiAPI
class FourParameterFunction(
name: String,
description: String,
Expand All @@ -100,6 +105,7 @@ class FourParameterFunction(
override fun getParameters() = listOf(param1, param2, param3, param4)
}

@BetaGenAiAPI
abstract class FunctionDeclaration(
val name: String,
val description: String,
Expand All @@ -113,6 +119,7 @@ abstract class FunctionDeclaration(
* @property name The name of the function call, this should be clear and descriptive for the model
* @property description A description of what the function does and its output.
*/
@BetaGenAiAPI
class FunctionBuilder(private val name: String, private val description: String) {

fun build(function: suspend () -> String): FunctionDeclaration {
Expand All @@ -124,6 +131,7 @@ class FunctionBuilder(private val name: String, private val description: String)
}
}

@BetaGenAiAPI
class OneFunctionBuilder(
private val name: String,
private val description: String,
Expand All @@ -138,6 +146,7 @@ class OneFunctionBuilder(
}
}

@BetaGenAiAPI
class TwoFunctionBuilder(
private val name: String,
private val description: String,
Expand All @@ -153,6 +162,7 @@ class TwoFunctionBuilder(
}
}

@BetaGenAiAPI
class ThreeFunctionBuilder(
private val name: String,
private val description: String,
Expand All @@ -169,6 +179,7 @@ class ThreeFunctionBuilder(
}
}

@BetaGenAiAPI
class FourFunctionBuilder(
private val name: String,
private val description: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.google.ai.client.generativeai.type
*
* @param functionDeclarations The set of functions that this tool allows the model access to
*/
@BetaGenAiAPI
class Tool(
val functionDeclarations: List<FunctionDeclaration>,
)

0 comments on commit 34287a8

Please sign in to comment.