Skip to content

Commit e3258a9

Browse files
committed
feat: add Chat snippets
1 parent 8133ae7 commit e3258a9

File tree

8 files changed

+211
-8
lines changed

8 files changed

+211
-8
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.google.firebase.example.ailogic.java;
2+
3+
import androidx.lifecycle.ViewModel;
4+
import com.google.common.util.concurrent.FutureCallback;
5+
import com.google.common.util.concurrent.Futures;
6+
import com.google.common.util.concurrent.ListenableFuture;
7+
import com.google.firebase.ai.FirebaseAI;
8+
import com.google.firebase.ai.GenerativeModel;
9+
import com.google.firebase.ai.java.ChatFutures;
10+
import com.google.firebase.ai.java.GenerativeModelFutures;
11+
import com.google.firebase.ai.type.Content;
12+
import com.google.firebase.ai.type.GenerateContentResponse;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.concurrent.Executor;
16+
import org.reactivestreams.Publisher;
17+
import org.reactivestreams.Subscriber;
18+
import org.reactivestreams.Subscription;
19+
20+
public class CommonSnippets extends ViewModel {
21+
private Executor executor;
22+
private GenerativeModel ai = FirebaseAI.getInstance()
23+
.generativeModel("gemini-2.5-flash");
24+
private GenerativeModelFutures model = GenerativeModelFutures.from(ai);
25+
26+
public void chat() {
27+
chatNonStreaming();
28+
chatStreaming();
29+
}
30+
31+
void chatNonStreaming() {
32+
// [START chat_non_streaming]
33+
// (optional) Create previous chat history for context
34+
Content.Builder userContentBuilder = new Content.Builder();
35+
userContentBuilder.setRole("user");
36+
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
37+
Content userContent = userContentBuilder.build();
38+
39+
Content.Builder modelContentBuilder = new Content.Builder();
40+
modelContentBuilder.setRole("model");
41+
modelContentBuilder.addText("Great to meet you. What would you like to know?");
42+
Content modelContent = userContentBuilder.build();
43+
44+
List<Content> history = Arrays.asList(userContent, modelContent);
45+
46+
// Initialize the chat
47+
ChatFutures chat = model.startChat(history);
48+
49+
// Create a new user message
50+
Content.Builder messageBuilder = new Content.Builder();
51+
messageBuilder.setRole("user");
52+
messageBuilder.addText("How many paws are in my house?");
53+
54+
Content message = messageBuilder.build();
55+
56+
// Send the message
57+
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(message);
58+
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
59+
@Override
60+
public void onSuccess(GenerateContentResponse result) {
61+
String resultText = result.getText();
62+
System.out.println(resultText);
63+
}
64+
65+
@Override
66+
public void onFailure(Throwable t) {
67+
t.printStackTrace();
68+
}
69+
}, executor);
70+
// [END chat_non_streaming]
71+
}
72+
73+
void chatStreaming() {
74+
// [START chat_streaming]
75+
// (optional) Create previous chat history for context
76+
Content.Builder userContentBuilder = new Content.Builder();
77+
userContentBuilder.setRole("user");
78+
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
79+
Content userContent = userContentBuilder.build();
80+
81+
Content.Builder modelContentBuilder = new Content.Builder();
82+
modelContentBuilder.setRole("model");
83+
modelContentBuilder.addText("Great to meet you. What would you like to know?");
84+
Content modelContent = userContentBuilder.build();
85+
86+
List<Content> history = Arrays.asList(userContent, modelContent);
87+
88+
// Initialize the chat
89+
ChatFutures chat = model.startChat(history);
90+
91+
// Create a new user message
92+
Content.Builder messageBuilder = new Content.Builder();
93+
messageBuilder.setRole("user");
94+
messageBuilder.addText("How many paws are in my house?");
95+
96+
Content message = messageBuilder.build();
97+
98+
// Send the message
99+
Publisher<GenerateContentResponse> streamingResponse =
100+
chat.sendMessageStream(message);
101+
102+
final String[] fullResponse = {""};
103+
104+
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
105+
106+
@Override
107+
public void onNext(GenerateContentResponse generateContentResponse) {
108+
String chunk = generateContentResponse.getText();
109+
fullResponse[0] += chunk;
110+
}
111+
112+
113+
@Override
114+
public void onComplete() {
115+
System.out.println(fullResponse[0]);
116+
}
117+
118+
// ... other methods omitted for brevity
119+
120+
// [START_EXCLUDE]
121+
@Override
122+
public void onSubscribe(Subscription s) {
123+
124+
}
125+
126+
@Override
127+
public void onError(Throwable t) {
128+
129+
}
130+
// [END_EXCLUDE]
131+
});
132+
// [END chat_streaming]
133+
}
134+
}

firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GeneralViewModel.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.google.firebase.example.ailogic.java;
2+
3+
import androidx.lifecycle.ViewModel;
4+
5+
public class GoogleAISnippets extends ViewModel {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.google.firebase.example.ailogic.java;
2+
3+
import androidx.lifecycle.ViewModel;
4+
5+
public class VertexAISnippets extends ViewModel {
6+
7+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.google.firebase.example.ailogic.kotlin
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import com.google.firebase.ai.GenerativeModel
6+
import com.google.firebase.ai.type.content
7+
import kotlinx.coroutines.flow.collect
8+
import kotlinx.coroutines.launch
9+
10+
class CommonSnippets(
11+
private val generativeModel: GenerativeModel
12+
) : ViewModel() {
13+
14+
fun chat() {
15+
viewModelScope.launch {
16+
chatNonStreaming()
17+
chatStreaming()
18+
}
19+
}
20+
21+
suspend fun chatNonStreaming() {
22+
// [START chat_non_streaming]
23+
// Initialize the chat
24+
val chat = generativeModel.startChat(
25+
history = listOf(
26+
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
27+
content(role = "model") { text("Great to meet you. What would you like to know?") }
28+
)
29+
)
30+
31+
val response = chat.sendMessage("How many paws are in my house?")
32+
print(response.text)
33+
// [END chat_non_streaming]
34+
}
35+
36+
suspend fun chatStreaming() {
37+
// [START chat_streaming]
38+
// Initialize the chat
39+
val chat = generativeModel.startChat(
40+
history = listOf(
41+
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
42+
content(role = "model") { text("Great to meet you. What would you like to know?") }
43+
)
44+
)
45+
46+
chat.sendMessageStream("How many paws are in my house?").collect { chunk ->
47+
print(chunk.text)
48+
}
49+
// [END chat_streaming]
50+
}
51+
}

firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GeneralViewModel.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.google.firebase.example.ailogic.kotlin
2+
3+
import androidx.lifecycle.ViewModel
4+
5+
class GoogleAISnippets : ViewModel() {
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.google.firebase.example.ailogic.kotlin
2+
3+
import androidx.lifecycle.ViewModel
4+
5+
class VertexAISnippets : ViewModel() {
6+
}

0 commit comments

Comments
 (0)