Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
67 changes: 67 additions & 0 deletions javascript/configure_model_parameters.js
Original file line number Diff line number Diff line change
@@ -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]
45 changes: 45 additions & 0 deletions javascript/configure_model_parameters.test.js
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 12 additions & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand All @@ -12,5 +18,11 @@
"type": "module",
"dependencies": {
"@google/genai": "^0.3.1"
<<<<<<< Updated upstream
=======
},
"devDependencies": {
"jest": "^29.7.0"
>>>>>>> Stashed changes
}
}