Gemini JavaScript SDK Integration
Use Gemini’s JavaScript SDK to integrate with Helicone to log your Gemini AI usage.
Proxy Integration
Fetch
Create Google Generative AI API Key
Visit the Google Generative AI API Key page. Follow the instructions to create a new API key. Make sure to save the key as you will need it for the next steps.
Set API keys as environment variables
export HELICONE_API_KEY=<your Helicone API key>
export GOOGLE_GENERATIVE_API_KEY=<your Google Generative AI API key>
Install necessary packages
Ensure you have the necessary packages installed in your Javascript project:
npm install node-fetch
Send a request using fetch
const fetch = require('node-fetch');
const url = `https://gateway.helicone.ai/v1beta/models/model-name:generateContent?key=${process.env.GOOGLE_GENERATIVE_API_KEY}`;
const headers = {
'Content-Type': 'application/json',
'Helicone-Auth': `Bearer ${process.env.HELICONE_API_KEY}`,
'Helicone-Target-URL': `https://generativelanguage.googleapis.com`,
};
const body = JSON.stringify({
contents: [{
parts: [{
text: 'Write a story about a magic backpack.'
}]
}]
});
fetch(url, { method: 'POST', headers: headers, body: body })
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Google Generative AI SDK
Create Google Generative AI API Key
Visit the Google Generative AI API Key page. Follow the instructions to create a new API key. Make sure to save the key as you will need it for the next steps.
Set API keys as environment variables
export HELICONE_API_KEY=<your Helicone API key>
export GOOGLE_GENERATIVE_API_KEY=<your Google Generative AI API key>
Install necessary packages
Ensure you have the necessary packages installed in your Javascript project:
npm install @google/generative-ai
Import GoogleGenerativeAI and configure the client
import { GoogleGenerativeAI, RequestOptions } from "@google/generative-ai";
if (!process.env.GOOGLE_GENERATIVE_API_KEY) {
throw new Error("GOOGLE_GENERATIVE_API_KEY environment variable must be set");
}
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GENERATIVE_API_KEY);
const customHeaders = new Headers({
"Helicone-Auth": `Bearer ${process.env.HELICONE_API_KEY}`,
"Helicone-Target-URL": `https://generativelanguage.googleapis.com`,
});
const requestOptions = {
customHeaders: customHeaders,
baseUrl: "https://gateway.helicone.ai",
} as RequestOptions;
Get the generative model and generate content
const model = genAI.getGenerativeModel(
{
model: "model-name",
},
requestOptions
);
async function run() {
const prompt = "Write a story about a magic backpack.";
const result = await model.generateContent(prompt);
const response = result.response;
const text = await response.text();
console.log(text);
}
run();