|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.ai.java |
| 18 | + |
| 19 | +import androidx.concurrent.futures.SuspendToFutureAdapter |
| 20 | +import com.google.common.util.concurrent.ListenableFuture |
| 21 | +import com.google.firebase.ai.TemplateGenerativeModel |
| 22 | +import com.google.firebase.ai.java.ImagenModelFutures.FuturesImpl |
| 23 | +import com.google.firebase.ai.type.FirebaseAIException |
| 24 | +import com.google.firebase.ai.type.GenerateContentResponse |
| 25 | +import com.google.firebase.ai.type.PublicPreviewAPI |
| 26 | +import kotlinx.coroutines.reactive.asPublisher |
| 27 | +import org.reactivestreams.Publisher |
| 28 | + |
| 29 | +/** |
| 30 | + * Wrapper class providing Java compatible methods for [TemplateGenerativeModel]. |
| 31 | + * |
| 32 | + * @see [TemplateGenerativeModel] |
| 33 | + */ |
| 34 | +@OptIn(PublicPreviewAPI::class) |
| 35 | +public abstract class TemplateGenerativeModelFutures internal constructor() { |
| 36 | + |
| 37 | + /** |
| 38 | + * Generates new content using the given templateId with the given inputs. |
| 39 | + * |
| 40 | + * @param templateId The ID of server prompt template. |
| 41 | + * @param inputs the inputs needed to fill in the prompt |
| 42 | + * @return The content generated by the model. |
| 43 | + * @throws [FirebaseAIException] if the request failed. |
| 44 | + * @see [FirebaseAIException] for types of errors. |
| 45 | + */ |
| 46 | + public abstract fun generateContent( |
| 47 | + templateId: String, |
| 48 | + inputs: Map<String, Any> |
| 49 | + ): ListenableFuture<GenerateContentResponse> |
| 50 | + |
| 51 | + /** |
| 52 | + * Generates new content as a stream using the given templateId with the given inputs. |
| 53 | + * |
| 54 | + * @param templateId The ID of server prompt template. |
| 55 | + * @param inputs the inputs needed to fill in the prompt |
| 56 | + * @return A [Publisher] which will emit responses as they are returned by the model. |
| 57 | + * @throws [FirebaseAIException] if the request failed. |
| 58 | + * @see [FirebaseAIException] for types of errors. |
| 59 | + */ |
| 60 | + public abstract fun generateContentStream( |
| 61 | + templateId: String, |
| 62 | + inputs: Map<String, Any> |
| 63 | + ): Publisher<GenerateContentResponse> |
| 64 | + |
| 65 | + /** Returns the [TemplateGenerativeModel] object wrapped by this object. */ |
| 66 | + public abstract fun getGenerativeModel(): TemplateGenerativeModel |
| 67 | + |
| 68 | + private class FuturesImpl(private val model: TemplateGenerativeModel) : |
| 69 | + TemplateGenerativeModelFutures() { |
| 70 | + override fun generateContent( |
| 71 | + templateId: String, |
| 72 | + inputs: Map<String, Any> |
| 73 | + ): ListenableFuture<GenerateContentResponse> { |
| 74 | + return SuspendToFutureAdapter.launchFuture { model.generateContent(templateId, inputs) } |
| 75 | + } |
| 76 | + |
| 77 | + override fun generateContentStream( |
| 78 | + templateId: String, |
| 79 | + inputs: Map<String, Any> |
| 80 | + ): Publisher<GenerateContentResponse> { |
| 81 | + return model.generateContentStream(templateId, inputs).asPublisher() |
| 82 | + } |
| 83 | + |
| 84 | + override fun getGenerativeModel(): TemplateGenerativeModel { |
| 85 | + return model |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public companion object { |
| 90 | + |
| 91 | + /** |
| 92 | + * @return a [TemplateGenerativeModelFutures] created around the provided |
| 93 | + * [TemplateGenerativeModel] |
| 94 | + */ |
| 95 | + @JvmStatic |
| 96 | + public fun from(model: TemplateGenerativeModel): TemplateGenerativeModelFutures = |
| 97 | + FuturesImpl(model) |
| 98 | + } |
| 99 | +} |
0 commit comments