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+ }
0 commit comments