1919using System . Collections . ObjectModel ;
2020using System . Linq ;
2121using System . Threading . Tasks ;
22+ using Firebase . VertexAI . Internal ;
2223
2324namespace Firebase . VertexAI {
2425
@@ -123,26 +124,10 @@ public IAsyncEnumerable<GenerateContentResponse> SendMessageStreamAsync(
123124 return SendMessageStreamAsyncInternal ( content ) ;
124125 }
125126
126- private ModelContent GuaranteeRole ( ModelContent content , string role ) {
127- if ( content . Role == role ) {
128- return content ;
129- } else {
130- return new ModelContent ( role , content . Parts ) ;
131- }
132- }
133-
134- private ModelContent GuaranteeUser ( ModelContent content ) {
135- return GuaranteeRole ( content , "user" ) ;
136- }
137-
138- private ModelContent GuaranteeModel ( ModelContent content ) {
139- return GuaranteeRole ( content , "model" ) ;
140- }
141-
142127 private async Task < GenerateContentResponse > SendMessageAsyncInternal (
143128 IEnumerable < ModelContent > requestContent ) {
144129 // Make sure that the requests are set to to role "user".
145- List < ModelContent > fixedRequests = requestContent . Select ( GuaranteeUser ) . ToList ( ) ;
130+ List < ModelContent > fixedRequests = requestContent . Select ( VertexAIExtensions . ConvertToUser ) . ToList ( ) ;
146131 // Set up the context to send in the request
147132 List < ModelContent > fullRequest = new ( chatHistory ) ;
148133 fullRequest . AddRange ( fixedRequests ) ;
@@ -157,7 +142,7 @@ private async Task<GenerateContentResponse> SendMessageAsyncInternal(
157142 ModelContent responseContent = response . Candidates . First ( ) . Content ;
158143
159144 chatHistory . AddRange ( fixedRequests ) ;
160- chatHistory . Add ( GuaranteeModel ( responseContent ) ) ;
145+ chatHistory . Add ( responseContent . ConvertToModel ( ) ) ;
161146 }
162147
163148 return response ;
@@ -166,7 +151,7 @@ private async Task<GenerateContentResponse> SendMessageAsyncInternal(
166151 private async IAsyncEnumerable < GenerateContentResponse > SendMessageStreamAsyncInternal (
167152 IEnumerable < ModelContent > requestContent ) {
168153 // Make sure that the requests are set to to role "user".
169- List < ModelContent > fixedRequests = requestContent . Select ( GuaranteeUser ) . ToList ( ) ;
154+ List < ModelContent > fixedRequests = requestContent . Select ( VertexAIExtensions . ConvertToUser ) . ToList ( ) ;
170155 // Set up the context to send in the request
171156 List < ModelContent > fullRequest = new ( chatHistory ) ;
172157 fullRequest . AddRange ( fixedRequests ) ;
@@ -181,7 +166,7 @@ private async IAsyncEnumerable<GenerateContentResponse> SendMessageStreamAsyncIn
181166 // but we don't want to save the history anymore.
182167 if ( response . Candidates . Any ( ) ) {
183168 ModelContent responseContent = response . Candidates . First ( ) . Content ;
184- responseContents . Add ( GuaranteeModel ( responseContent ) ) ;
169+ responseContents . Add ( responseContent . ConvertToModel ( ) ) ;
185170 } else {
186171 saveHistory = false ;
187172 }
0 commit comments