Google AI Studio SDK
Pass-Through-Endpunkte für Google AI Studio – rufen Sie den anbieterspezifischen Endpunkt im nativen Format auf (keine Übersetzung).
| Feature | UnterstĂĽtzt | Notizen |
|---|---|---|
| Kostenverfolgung | âś… | unterstĂĽtzt alle Modelle auf dem /generateContent Endpunkt |
| Protokollierung | âś… | funktioniert ĂĽber alle Integrationen hinweg |
| Endbenutzerverfolgung | ❌ | Sagen Sie uns Bescheid, wenn Sie dies benötigen |
| Streaming | âś… |
Ersetzen Sie einfach https://generativelanguage.googleapis.com durch LITELLM_PROXY_BASE_URL/gemini
Beispielverwendung​
- curl
- Google AI Node.js SDK
curl 'http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:countTokens?key=sk-anything' \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
const { GoogleGenerativeAI } = require("@google/generative-ai");
const modelParams = {
model: 'gemini-pro',
};
const requestOptions = {
baseUrl: 'https://:4000/gemini', // http://<proxy-base-url>/gemini
};
const genAI = new GoogleGenerativeAI("sk-1234"); // litellm proxy API key
const model = genAI.getGenerativeModel(modelParams, requestOptions);
async function main() {
try {
const result = await model.generateContent("Explain how AI works");
console.log(result.response.text());
} catch (error) {
console.error('Error:', error);
}
}
// For streaming responses
async function main_streaming() {
try {
const streamingResult = await model.generateContentStream("Explain how AI works");
for await (const chunk of streamingResult.stream) {
console.log('Stream chunk:', JSON.stringify(chunk));
}
const aggregatedResponse = await streamingResult.response;
console.log('Aggregated response:', JSON.stringify(aggregatedResponse));
} catch (error) {
console.error('Error:', error);
}
}
main();
// main_streaming();
UnterstĂĽtzt **ALLE** Google AI Studio Endpunkte (einschlieĂźlich Streaming).
Alle Google AI Studio Endpunkte anzeigen
Schnellstart​
Rufen wir den Gemini /countTokens Endpunkt auf/countTokens endpoint
- FĂĽgen Sie Ihren Gemini API-SchlĂĽssel zur Umgebung hinzu
export GEMINI_API_KEY=""
- LiteLLM Proxy starten
litellm
# RUNNING on http://0.0.0.0:4000
- Testen Sie es!
Rufen wir den Token-Zählungs-Endpunkt von Google AI Studio auf
http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:countTokens?key=anything' \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
Beispiele​
Alles nach http://0.0.0.0:4000/gemini wird als anbieterspezifische Route behandelt und entsprechend verarbeitet.
Wesentliche Änderungen
| UrsprĂĽnglicher Endpunkt | Ersetzen Sie durch |
|---|---|
https://generativelanguage.googleapis.com | http://0.0.0.0:4000/gemini (LITELLM_PROXY_BASE_URL="http://0.0.0.0:4000") |
key=$GOOGLE_API_KEY | key=anything (verwenden Sie key=LITELLM_VIRTUAL_KEY, wenn virtuelle SchlĂĽssel im Proxy eingerichtet sind) |
Beispiel 1: Token zählen​
LiteLLM Proxy Aufruf​
curl http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:countTokens?key=anything \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}],
}],
}'
Direkter Google AI Studio-Aufruf​
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}],
}],
}'
Beispiel 2: Inhalt generieren​
LiteLLM Proxy Aufruf​
curl "http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:generateContent?key=anything" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Write a story about a magic backpack."}]
}]
}' 2> /dev/null
Direkter Google AI Studio-Aufruf​
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Write a story about a magic backpack."}]
}]
}' 2> /dev/null
Beispiel 3: Caching​
curl -X POST "http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash-001:generateContent?key=anything" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "Please summarize this transcript"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
Direkter Google AI Studio-Aufruf​
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "Please summarize this transcript"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
Erweitert​
Voraussetzungen
Verwenden Sie dies, um zu vermeiden, dass Entwickler den rohen Google AI Studio-Schlüssel erhalten, aber ihnen dennoch die Verwendung von Google AI Studio-Endpunkten ermöglichen.
Verwendung mit virtuellen Schlüsseln​
- Umgebung einrichten
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export GEMINI_API_KEY=""
litellm
# RUNNING on http://0.0.0.0:4000
- Virtuellen SchlĂĽssel generieren
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'
Erwartete Antwort
{
...
"key": "sk-1234ewknldferwedojwojw"
}
- Testen Sie es!
http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:countTokens?key=sk-1234ewknldferwedojwojw' \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
Senden Sie tags in den Request-Headern​
Verwenden Sie dies, wenn Sie möchten, dass tags in der LiteLLM-Datenbank und in Logging-Callbacks verfolgt werden.
Ăśbergeben Sie Tags als durch Kommas getrennte Liste in den Request-Headern. Im folgenden Beispiel werden die folgenden Tags verfolgt
tags: ["gemini-js-sdk", "pass-through-endpoint"]
- curl
- Google AI Node.js SDK
curl 'http://0.0.0.0:4000/gemini/v1beta/models/gemini-1.5-flash:generateContent?key=sk-anything' \
-H 'Content-Type: application/json' \
-H 'tags: gemini-js-sdk,pass-through-endpoint' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
const { GoogleGenerativeAI } = require("@google/generative-ai");
const modelParams = {
model: 'gemini-pro',
};
const requestOptions = {
baseUrl: 'https://:4000/gemini', // http://<proxy-base-url>/gemini
customHeaders: {
"tags": "gemini-js-sdk,pass-through-endpoint"
}
};
const genAI = new GoogleGenerativeAI("sk-1234");
const model = genAI.getGenerativeModel(modelParams, requestOptions);
async function main() {
try {
const result = await model.generateContent("Explain how AI works");
console.log(result.response.text());
} catch (error) {
console.error('Error:', error);
}
}
main();