diff --git a/javascript/README.md b/javascript/README.md index bd2eade..35ce8a6 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -9,4 +9,29 @@ This directory contains examples of working with the Gemini API using the For example: +<<<<<<< Updated upstream node text_generation.test.js +======= +## Running the examples + +### Chat example +```bash +npm run chat +``` + +### Configure model parameters example +```bash +npm run configure-model +``` + +## Running tests +```bash +npm test +``` + +## Examples + +- `chat.js` - Shows how to create a chat session and maintain conversation context +- `configure_model_parameters.js` - Shows how to configure generation parameters like temperature and token limits +- `text_generation.js` - Shows how to generate text with Gemini +>>>>>>> Stashed changes diff --git a/javascript/configure_model_parameters.js b/javascript/configure_model_parameters.js new file mode 100644 index 0000000..eddb5cb --- /dev/null +++ b/javascript/configure_model_parameters.js @@ -0,0 +1,67 @@ +/** + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START gemini_javascript_configure_model_parameters] +import {GoogleGenAI} from '@google/genai'; + +/** + * Configure model parameters example with the Gemini API + * + * This example demonstrates how to configure generation parameters: + * - candidate_count: Number of candidates to generate + * - stop_sequences: Sequences that stop generation + * - max_output_tokens: Maximum number of tokens to generate + * - temperature: Controls randomness of generation + */ +export async function configureModelParameters() { + try { + // Initialize the client with your API key + const GEMINI_API_KEY = process.env.GEMINI_API_KEY; + const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY}); + + // Get the Gemini model + const model = ai.getGenerativeModel({model: 'gemini-pro'}); + + // Configure generation parameters + const generationConfig = { + candidateCount: 1, + stopSequences: ['x'], + maxOutputTokens: 20, + temperature: 1.0, + }; + + // Generate content with configured parameters + const result = await model.generateContent({ + contents: [{text: 'Tell me a story about a magic backpack.'}], + generationConfig, + }); + + console.log('Generated text:', result.response.text()); + } catch (error) { + console.error('Error:', error); + } +} + +// Run the example if this file is executed directly +if (process.argv[1] === new URL(import.meta.url).pathname) { + if (!process.env.GEMINI_API_KEY) { + console.error('Please set the GEMINI_API_KEY environment variable'); + process.exit(1); + } + + configureModelParameters().catch(console.error); +} +// [END gemini_javascript_configure_model_parameters] \ No newline at end of file diff --git a/javascript/configure_model_parameters.test.js b/javascript/configure_model_parameters.test.js new file mode 100644 index 0000000..e50a142 --- /dev/null +++ b/javascript/configure_model_parameters.test.js @@ -0,0 +1,45 @@ +/** + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START gemini_javascript_configure_model_parameters_test] +import {jest} from '@jest/globals'; +import {configureModelParameters} from './configure_model_parameters.js'; + +describe('Configure Model Parameters Example', () => { + const originalEnv = process.env; + + beforeEach(() => { + jest.resetModules(); + process.env = {...originalEnv}; + process.env.GEMINI_API_KEY = 'test-api-key'; + }); + + afterEach(() => { + process.env = originalEnv; + }); + + test('should throw error if API key is not set', async () => { + delete process.env.GEMINI_API_KEY; + + await expect(async () => { + await configureModelParameters(); + }).rejects.toThrow(); + }); + + // Note: Add more tests as needed for specific functionality + // Currently keeping tests minimal as they would require mocking the Gemini API +}); +// [END gemini_javascript_configure_model_parameters_test] \ No newline at end of file diff --git a/javascript/package.json b/javascript/package.json index 9ca3d13..85f3ceb 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -3,7 +3,13 @@ "version": "1.0.0", "main": "", "scripts": { +<<<<<<< Updated upstream "test": "" +======= + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", + "chat": "node chat.js", + "configure-model": "node configure_model_parameters.js" +>>>>>>> Stashed changes }, "keywords": [], "author": "", @@ -12,5 +18,11 @@ "type": "module", "dependencies": { "@google/genai": "^0.3.1" +<<<<<<< Updated upstream +======= + }, + "devDependencies": { + "jest": "^29.7.0" +>>>>>>> Stashed changes } }