@@ -21,7 +21,9 @@ import {
21
21
GenerationConfig ,
22
22
HarmBlockThreshold ,
23
23
HarmCategory ,
24
+ Language ,
24
25
Modality ,
26
+ Outcome ,
25
27
SafetySetting ,
26
28
getGenerativeModel
27
29
} from '../src' ;
@@ -188,6 +190,32 @@ describe('Generate Content', () => {
188
190
} ) ;
189
191
} ) ;
190
192
193
+ it ( 'generateContent: code execution' , async ( ) => {
194
+ const model = getGenerativeModel ( testConfig . ai , {
195
+ model : testConfig . model ,
196
+ generationConfig : commonGenerationConfig ,
197
+ safetySettings : commonSafetySettings ,
198
+ tools : [ { codeExecution : { } } ]
199
+ } ) ;
200
+ const prompt =
201
+ 'What is the sum of the first 50 prime numbers? ' +
202
+ 'Generate and run code for the calculation, and make sure you get all 50.' ;
203
+
204
+ const result = await model . generateContent ( prompt ) ;
205
+ const parts = result . response . candidates ?. [ 0 ] . content . parts ;
206
+ expect (
207
+ parts ?. some ( part => part . executableCode ?. language === Language . PYTHON )
208
+ ) . to . be . true ;
209
+ expect (
210
+ parts ?. some ( part => part . codeExecutionResult ?. outcome === Outcome . OK )
211
+ ) . to . be . true ;
212
+ // Expect these to be truthy (!= null)
213
+ expect ( parts ?. some ( part => part . executableCode ?. code != null ) ) . to . be
214
+ . true ;
215
+ expect ( parts ?. some ( part => part . codeExecutionResult ?. output != null ) ) . to
216
+ . be . true ;
217
+ } ) ;
218
+
191
219
it ( 'generateContentStream: text input, text output' , async ( ) => {
192
220
const model = getGenerativeModel ( testConfig . ai , {
193
221
model : testConfig . model ,
0 commit comments