Skip to content

Commit a367826

Browse files
author
David Motsonashvili
committed
add java types
1 parent 8a9e2e9 commit a367826

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

firebase-ai/api.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ package com.google.firebase.ai.java {
179179
method public com.google.firebase.ai.java.LiveSessionFutures from(com.google.firebase.ai.type.LiveSession session);
180180
}
181181

182+
public abstract class TemplateGenerativeModelFutures {
183+
method public static final com.google.firebase.ai.java.TemplateGenerativeModelFutures from(com.google.firebase.ai.TemplateGenerativeModel model);
184+
method public abstract com.google.common.util.concurrent.ListenableFuture<com.google.firebase.ai.type.GenerateContentResponse> generateContent(String templateId, java.util.Map<java.lang.String,?> inputs);
185+
method public abstract org.reactivestreams.Publisher<com.google.firebase.ai.type.GenerateContentResponse> generateContentStream(String templateId, java.util.Map<java.lang.String,?> inputs);
186+
method public abstract com.google.firebase.ai.TemplateGenerativeModel getGenerativeModel();
187+
field public static final com.google.firebase.ai.java.TemplateGenerativeModelFutures.Companion Companion;
188+
}
189+
190+
public static final class TemplateGenerativeModelFutures.Companion {
191+
method public com.google.firebase.ai.java.TemplateGenerativeModelFutures from(com.google.firebase.ai.TemplateGenerativeModel model);
192+
}
193+
194+
public abstract class TemplateImagenModelFutures {
195+
method public abstract com.google.common.util.concurrent.ListenableFuture<com.google.firebase.ai.type.ImagenGenerationResponse<com.google.firebase.ai.type.ImagenInlineImage>> generateImages(String templateId, java.util.Map<java.lang.String,?> inputs);
196+
method public abstract com.google.firebase.ai.TemplateImagenModel getImageModel();
197+
}
198+
182199
}
183200

184201
package com.google.firebase.ai.type {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.TemplateImagenModel
22+
import com.google.firebase.ai.type.ImagenGenerationResponse
23+
import com.google.firebase.ai.type.ImagenInlineImage
24+
import com.google.firebase.ai.type.PublicPreviewAPI
25+
26+
/**
27+
* Wrapper class providing Java compatible methods for [TemplateImagenModel].
28+
*
29+
* @see [TemplateImagenModel]
30+
*/
31+
@OptIn(PublicPreviewAPI::class)
32+
public abstract class TemplateImagenModelFutures internal constructor() {
33+
34+
/**
35+
* Generates an image, returning the result directly to the caller.
36+
*
37+
* @param templateId The ID of server prompt template.
38+
* @param inputs the inputs needed to fill in the prompt
39+
*/
40+
public abstract fun generateImages(
41+
templateId: String,
42+
inputs: Map<String, Any>
43+
): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>>
44+
45+
/** Returns the [TemplateImagenModel] object wrapped by this object. */
46+
public abstract fun getImageModel(): TemplateImagenModel
47+
48+
private class FuturesImpl(private val model: TemplateImagenModel) : TemplateImagenModelFutures() {
49+
override fun generateImages(
50+
templateId: String,
51+
inputs: Map<String, Any>
52+
): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>> {
53+
return SuspendToFutureAdapter.launchFuture { model.generateImages(templateId, inputs) }
54+
}
55+
56+
override fun getImageModel(): TemplateImagenModel {
57+
return model
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)