Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ public class FirebaseSymbolProcessor(
private val logger: KSPLogger,
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver
.getSymbolsWithAnnotation("com.google.firebase.ai.annotations.Generable")
val generableSymbols =
resolver.getSymbolsWithAnnotation(
"com.google.mlkit.genai.structuredoutput.annotations.Generable"
) +
resolver.getSymbolsWithAnnotation("com.google.mlkit.genai.schema.annotations.Generable") +
resolver.getSymbolsWithAnnotation("com.google.firebase.ai.annotations.Generable")

generableSymbols
.filterIsInstance<KSClassDeclaration>()
.distinct()
.map { it to SchemaSymbolProcessorVisitor(logger, codeGenerator) }
.forEach { (klass, visitor) -> visitor.visitClassDeclaration(klass, Unit) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ internal class SchemaSymbolProcessorVisitor(
codeGenerator,
Dependencies(true, containingFile),
)
val companionFile = generateMlKitCompanionFileSpec(classDeclaration)
companionFile.writeTo(
codeGenerator,
Dependencies(true, containingFile),
)
}

fun generateFileSpec(classDeclaration: KSClassDeclaration): FileSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.firebase.testing.processor

import com.google.firebase.ai.annotations.Generable
import com.google.firebase.ai.annotations.Guide
import com.google.mlkit.genai.structuredoutput.annotations.Generable
import com.google.mlkit.genai.structuredoutput.annotations.Guide

/**
* A test kdoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@ internal class GenerativeModelImpl(
android.util.Log.i("MLKIT_IO", "[SDK_BRIDGE] ==========================================")
android.util.Log.i("MLKIT_IO", "[SDK_BRIDGE] Calling ML Kit generateTypedContentRequest")
android.util.Log.i("MLKIT_IO", "[SDK_BRIDGE] Input SDK Class: ${schemaClass.qualifiedName}")
val companionClassName = "${schemaClass.java.name}_MlKitCompanion"
val targetClass =
try {
Class.forName(companionClassName).kotlin
} catch (e: Exception) {
if (schemaClass.java.name.endsWith("_MlKitCompanion")) {
schemaClass
} else {
throw IllegalArgumentException(
"Shadow class $companionClassName not found for structured output. Ensure KSP processor is running and generated the MlKit companion class.",
e
)
}
}
android.util.Log.i(
"MLKIT_IO",
"[SDK_BRIDGE] Resolved ML Kit Class: ${targetClass.qualifiedName}"
)
android.util.Log.i(
"MLKIT_IO",
"[SDK_BRIDGE] includeSchemaInPrompt: true (ML Kit manages prompt formatting)"
Expand All @@ -89,7 +71,7 @@ internal class GenerativeModelImpl(
val mlkitRequest =
com.google.mlkit.genai.prompt.generateTypedContentRequest(
generateContentRequest = request.toMlKit(),
outputClass = targetClass as kotlin.reflect.KClass<Any>,
outputClass = schemaClass as kotlin.reflect.KClass<Any>,
includeSchemaInPrompt = true
)
val typedResponse = mlkitModel.generateContent(mlkitRequest)
Expand All @@ -102,19 +84,15 @@ internal class GenerativeModelImpl(
"[SDK_BRIDGE] ML Kit returned structured instances count: ${mlkitInstances.size}"
)

@Suppress("UNCHECKED_CAST")
val sdkInstances =
mlkitInstances.map { mlkitInstance ->
if (mlkitInstance.javaClass.name.endsWith("_MlKitCompanion")) {
val toSdkMethod = mlkitInstance.javaClass.getMethod("toSdk")
toSdkMethod.invoke(mlkitInstance) as T
} else {
mlkitInstance as T
}
}
@Suppress("UNCHECKED_CAST") val sdkInstances = mlkitInstances as List<T>

com.google.firebase.ai.ondevice.interop.GenerateObjectResponse(sdkInstances)
} catch (e: GenAiException) {
} catch (e: Throwable) {
android.util.Log.e(
"MLKIT_IO",
"[SDK_BRIDGE] generateObject failed with exception: ${e.javaClass.name}: ${e.message}",
e
)
throw getMappingException(e)
}

Expand Down
2 changes: 0 additions & 2 deletions ai-logic/firebase-ai/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ package com.google.firebase.ai {
method @Deprecated public suspend Object? generateImages(String templateId, java.util.Map<java.lang.String,?> inputs, kotlin.coroutines.Continuation<? super com.google.firebase.ai.type.ImagenGenerationResponse<com.google.firebase.ai.type.ImagenInlineImage>>);
}

}

package com.google.firebase.ai.annotations {

@kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.SOURCE) @kotlin.annotation.Target(allowedTargets=kotlin.annotation.AnnotationTarget.CLASS) public @interface Generable {
Expand Down
6 changes: 5 additions & 1 deletion ai-logic/firebase-ai/firebase-ai.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ android {
}

kotlin {
compilerOptions { jvmTarget = JvmTarget.JVM_1_8 }
compilerOptions {
jvmTarget = JvmTarget.JVM_1_8
freeCompilerArgs.add("-Xskip-metadata-version-check")
}
explicitApi()
}

Expand All @@ -84,6 +87,7 @@ dependencies {
implementation(libs.ktor.client.logging)

api(libs.firebase.common)
api(libs.genai.structured.output) { exclude(group = "org.jetbrains.kotlin") }
api("com.google.firebase:firebase-appcheck:19.2.0")
implementation(libs.firebase.components)
implementation(libs.firebase.annotations)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ firebase-annotations = { module = "com.google.firebase:firebase-annotations", ve
firebase-common = { module = "com.google.firebase:firebase-common", version.ref = "firebaseCommon" }
firebase-components = { module = "com.google.firebase:firebase-components", version.ref = "firebaseComponents" }
genai-prompt = { module = "com.google.mlkit:genai-prompt", version.ref = "genaiPrompt" }
genai-schema = { module = "com.google.mlkit:genai-schema", version = "1.0.0-alpha1" }
genai-structured-output = { module = "com.google.mlkit:genai-structured-output", version = "1.0.0-alpha1" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
google-api-client = { module = "com.google.api-client:google-api-client", version.ref = "googleApiClient" }
google-dexmaker = { module = "com.google.dexmaker:dexmaker", version.ref = "dexmakerVersion" }
Expand Down
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencyResolutionManagement {
google()
mavenLocal()
mavenCentral()
maven { url = uri("/Users/milamamat/Downloads/mlkit_genai_structured_output") }
}
}

Expand Down
Loading