ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Google ရဲ့ Gemini API မှာလည်း တခြား AI API တွေနဲ့ အခြေခံအတွေးအခေါ် အတူတူပါပဲ။ Prompt ကို JSON အဖြစ် HTTPS နဲ့ ပို့ပြီး ပြန်လာတဲ့ JSON ထဲက ထုတ်ပေးထားတဲ့ စာသားကို ဖတ်ရတာပါ။ Gemini ကို သင်ယူသူတွေအတွက် ထူးခြားစေတာက အခုအချိန်မှာ တံခါးနှစ်ပေါက်ရှိနေတာပါ။ အသစ်ဖြစ်တဲ့ Interactions endpoint ကို Google က project အသစ်တွေအတွက် အကြံပြုပြီး အဟောင်း generateContent endpoint ကိုလည်း ဆက်လက် support လုပ်နေလို့ tutorial ထောင်ချီမှာ ဆက်တွေ့နေရပါတယ်။
စာဖတ်ခန်းအသစ်နဲ့ အဟောင်း ရှိတဲ့ စာကြည့်တိုက်တစ်ခုကို မြင်ကြည့်ပါ။ နှစ်ခုလုံးက စာအုပ်တူတူ ပေးပေမယ့် စင်တွေကို တံဆိပ်ကပ်ပုံ မတူတာမို့ တံဆိပ်အဟောင်းကို သင်ထားတဲ့ ဧည့်သည်က နေရာမှားရှာပြီး စာအုပ်ပျောက်နေတယ်လို့ ထင်သွားပါလိမ့်မယ်။
Problem → Why → Solution
ပြဿနာ: တစ်နှစ်ကြာ tutorial ကို production မှာ သုံးခြင်း
လွန်ခဲ့တဲ့ တစ်နှစ်က tutorial ကို copy ကူးရင် hard-code လုပ်ထားတဲ့ model နာမည်၊ candidates[0].content.parts[0].text လို response path၊ URL ထဲ ရေးထည့်ထားတဲ့ key နဲ့ အောင်မြင်တဲ့ case တစ်ခုကလွဲပြီး ဘာမှ မကိုင်တွယ်တဲ့ code ကို အမွေခံရဖို့ များပါတယ်။
Google ရဲ့ models page ပေါ်က model id တွေက ရွေ့နေပါတယ်။ ဟောင်းတဲ့ id တွေကို ကြေညာထားတဲ့ ရက်စွဲတွေမှာ ရုပ်သိမ်းတာ ဒါမှမဟုတ် တခြား model ဆီ လမ်းကြောင်းလွှဲတာ လုပ်ပြီး မျိုးဆက်အသစ်တွေက id အသစ်နဲ့ ရောက်လာပါတယ်။ Blocked prompt က candidate လုံးဝမပါတဲ့ အဖြေ ပြန်လို့ candidates[0] က TypeError ပစ်ပါတယ်၊ endpoint အသစ်ရဲ့ response မှာလည်း candidates field က အစကတည်းက မရှိပါဘူး။ ပြီးတော့ crash က အကြောင်းရင်းနဲ့ အဝေးကြီးမှာ၊ များသောအားဖြင့် user ကြည့်နေတဲ့ screen ပေါ်မှာ ပေါ်လာပါတယ်။
const MODEL = "gemini-old-tutorial-id"; // copied from a blog post
const url =
"https://generativelanguage.googleapis.com/v1beta/models/" + MODEL +
":generateContent?key=" + API_KEY; // key in the URL: proxies and logs record it
const res = await fetch(url, {
method: "POST",
body: JSON.stringify({ contents: [{ parts: [{ text: "Explain REST" }] }] }),
});
const data = await res.json();
console.log(data.candidates[0].content.parts[0].text); // TypeError when blockedဘာကြောင့် ဘာကိုမှ မစစ်ထားတာလဲ
ဒီ snippet ထဲက ဘာကိုမှ စာချုပ်နဲ့ တိုက်မစစ်ထားပါဘူး။ Id က မရှိတော့နိုင်တယ်၊ key က proxy နဲ့ server log တွေ မှတ်ထားမယ့် URL ထဲမှာရှိတယ်၊ safety block ဒါမှမဟုတ် error body ကို မဖြစ်နိုင်ဘူးလို့ သဘောထားထားတယ်။ လက်ရှိ docs ကို ဖတ်၊ id ကို configuration ထဲထား၊ စာသားကို ကာကွယ်ရေးပါတဲ့ function တစ်ခုထဲကနေ ထုတ်ယူရင် ဒီပြဿနာတွေက အံ့ဩစရာမဟုတ်တော့ဘဲ ကိုင်တွယ်ပြီးသား case တွေ ဖြစ်သွားပါတယ်။
Core concept: မိသားစုတစ်ခုတည်း၊ response shape နှစ်မျိုး
အောက်ပါအရာအားလုံးက Google ရဲ့ Gemini API docs ကနေ ယူထားတာပါ။ Model id လို မကြာခဏ ပြောင်းတဲ့ အချက်တွေမှာတော့ အချိန်ကြာရင် ဟောင်းသွားမယ့် တန်ဖိုးကို မရေးဘဲ ဘယ်မှာကြည့်ရမလဲ ဆိုတာကိုပဲ ပြောပါမယ်။
၁။ API တစ်ခုတည်းကို တံခါးနှစ်ပေါက်
Google docs က project အသစ်တွေအတွက် Interactions endpoint ဖြစ်တဲ့ POST https://generativelanguage.googleapis.com/v1beta/interactions ကို အကြံပြုပါတယ်။ generateContent endpoint ကိုတော့ ဆက်လက် အပြည့်အဝ support လုပ်နေတဲ့ API အဟောင်းလို့ ဖော်ပြပါတယ်။ နှစ်ခုလုံး key header အတူတူနဲ့ authenticate လုပ်လို့ call တစ်ခုချင်းစီ ကူးပြောင်းလို့ ရပါတယ်။
၂။ Auth နဲ့ request body
Key ကို x-goog-api-key header ထဲမှာ Content-Type: application/json နဲ့အတူ ပို့ပါ။ Body မှာ model နဲ့ input လိုပြီး input က စာသားရိုးရိုးဆိုရင် string ပါ။ Optional field တွေမှာ system_instruction၊ generation_config (ဥပမာ max_output_tokens)၊ tools နဲ့ အရင် interaction ကို ဆက်ဖို့ previous_interaction_id တို့ ပါပါတယ်။ system_instruction၊ generation_config နဲ့ tools က ဖန်တီးနေတဲ့ interaction တစ်ခုအတွက်ပဲ သက်ရောက်လို့ turn တိုင်းမှာ ပြန်ထည့်ရပါမယ်။
၃။ Interaction ဆိုတာ step စာရင်းပါ
Reply က id၊ status နဲ့ အချိန်အလိုက် စီထားတဲ့ step စာရင်း ပါတဲ့ Interaction resource ပါ။ type model_output ရှိတဲ့ step မှာ content part တွေပါပြီး text part က { type: "text", text } ပုံစံပါ။ thought၊ function_call၊ google_search_call လို တခြား step type တွေလည်း ပေါ်နိုင်လို့ step တွေကို type နဲ့ ရွေးပါ။ Official SDK တွေမှာ interaction.output_text ပါပြီး response ရဲ့ နောက်ဆုံး text block တွေကို ပြန်ပေးပါတယ်။
၄။ Status နဲ့ usage
status က interaction က completed, failed, incomplete ဒါမှမဟုတ် in_progress စတဲ့ ဘယ်အခြေအနေမှာလဲ ပြောပါတယ်။ usage က total_input_tokens၊ total_output_tokens၊ total_thought_tokens နဲ့ total_tokens ကို ပြပါတယ်။ Thinking token တွေကိုလည်း စုစုပေါင်းထဲ ထည့်ရေတွက်လို့ အဖြေတိုတိုတစ်ခုကလည်း token များများ သုံးနိုင်ပါတယ်။
၅။ Error မှာ ဂဏန်းတင်မက code ပါပါတယ်
Error တိုင်းက snake_case code ပါတဲ့ { error: { code, message } } ပုံစံ JSON ပါ။ rate_limit_exceeded နဲ့ too_many_requests က နှေးအောင်လုပ်ပါလို့ ဆိုလိုတာပါ။ quota_exceeded က တစ်ရက်စာ quota ကုန်သွားတာမို့ မိနစ်ပိုင်းအတွင်း ပြန်ကြိုးစားတာ အဓိပ္ပာယ်မရှိပါဘူး။ authentication က key မပါ ဒါမှမဟုတ် မှားတာ၊ payment_required က prepaid credit ကုန်သွားတာပါ။ Streaming request မှာတော့ error ကို event_type က error ဖြစ်တဲ့ event နဲ့ ပို့ပါတယ်။
Model id ဆိုတာ code မဟုတ်ဘဲ configuration ပါ
Id ကို Google ရဲ့ လက်ရှိ models page ကနေယူပြီး environment variable ထဲ ထားပါ၊ ပြောင်းလာမယ်လို့ ကြိုမျှော်ထားပါ။ ဒီ lesson က <model-id> လို့ တမင်ရေးထားတာပါ၊ ဒီမှာ ဘာတန်ဖိုးရေးရေး မကြာခင် ဟောင်းသွားမှာမို့ပါ။
- Key က x-goog-api-key ထဲမှာ ပါ
- Body မှာ model နဲ့ input ပါ
- model_output step ရဲ့ text ကို ယူ
- Error code က retry/fix ကို ဆုံးဖြတ်
Response shape အကြောင်း အသိအမြင်ကို function တစ်ခုထဲမှာပဲ ထားပါ၊ ဒါမှ နောက်ပိုင်း API revision ပြောင်းရင် screen တိုင်းမဟုတ်ဘဲ တစ်နေရာတည်း ပြင်ရပါမယ်။
အခြေခံ ပုံစံ
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<model-id>",
"input": "<prompt>"
}'Placeholder နှစ်ခုကို လက်ရှိ docs ကနေ ဖြည့်ပါ။ ပုံဥပမာသက်သက်ပါ။ ကိုယ့် key ကို environment variable ထဲထည့်ပြီး run ပါ။ Google ရဲ့ နမူနာတချို့မှာ ရက်စွဲပါတဲ့ API-revision header ထပ်ထည့်ထားတာမို့ လက်ရှိ quickstart ပို့တဲ့အတိုင်း copy ကူးပါ။
Response shape တစ်မျိုးကို extractor တစ်ခု
Script က endpoint နှစ်ခုပါတဲ့ mock server တစ်ခု စတင်ပါတယ်။ တစ်ခုက Interactions shape နဲ့ ဖြေပြီး နောက်တစ်ခုက blocked-prompt အဖြေအပါအဝင် အဟောင်း generateContent shape နဲ့ ဖြေပါတယ်။ Client က key header နဲ့ request တည်ဆောက်၊ shape တစ်မျိုးစီအတွက် function တစ်ခုစီနဲ့ စာသားထုတ်၊ usage ဂဏန်းတွေ print လုပ်ပြီးနောက် 401၊ 400၊ 429 ကို တမင်ဖြစ်စေကာ error code ကို ဘယ်လိုဖတ်ရလဲ ပြပါတယ်။
import http from "node:http";
const KEY = "demo-gemini-key";
const ANSWER = "REST lets programs talk over HTTP.";
// Mock server: one endpoint per response shape, no network and no real key.
let burstCalls = 0;
const server = http.createServer((req, res) => {
let raw = "";
req.on("data", (chunk) => (raw += chunk));
req.on("end", () => {
const send = (status, body) => {
res.writeHead(status, { "content-type": "application/json" });
res.end(JSON.stringify(body));
};
const fail = (status, code, message) => send(status, { error: { code, message } });
if (req.headers["x-goog-api-key"] !== KEY) {
return fail(401, "authentication", "The API key is missing or invalid");
}
const body = JSON.parse(raw);
if (req.url === "/v1beta/interactions") {
if (!body.model || typeof body.input !== "string") {
return fail(400, "invalid_request", "model and input are required");
}
if (body.input === "burst" && burstCalls++ === 0) {
return fail(429, "rate_limit_exceeded", "Too many requests per minute");
}
return send(200, {
id: "interaction_demo",
status: "completed",
steps: [
{ type: "thought" },
{ type: "model_output", content: [{ type: "text", text: ANSWER }] },
],
usage: {
total_input_tokens: 7,
total_output_tokens: 23,
total_thought_tokens: 49,
total_tokens: 79,
},
});
}
if (req.url === "/v1beta/models/mock-model:generateContent") {
if (body.contents?.[0]?.parts?.[0]?.text === "blocked") {
return send(200, { promptFeedback: { blockReason: "SAFETY" } });
}
return send(200, {
candidates: [
{ content: { role: "model", parts: [{ text: ANSWER }] }, finishReason: "STOP" },
],
usageMetadata: {
promptTokenCount: 7,
candidatesTokenCount: 23,
totalTokenCount: 30,
},
});
}
fail(404, "not_found", "unknown path");
});
});
async function post(base, path, key, body) {
const res = await fetch(base + path, {
method: "POST",
headers: { "x-goog-api-key": key, "content-type": "application/json" },
body: JSON.stringify(body),
});
const data = await res.json();
if (!res.ok) {
throw Object.assign(new Error(data.error.message), {
status: res.status,
code: data.error.code,
});
}
return data;
}
// Interactions shape: the text sits in the model_output steps.
function interactionText(interaction) {
return interaction.steps
.filter((step) => step.type === "model_output")
.flatMap((step) => step.content)
.filter((part) => part.type === "text")
.map((part) => part.text)
.join("");
}
// generateContent shape: candidates may be missing when the prompt was blocked.
function legacyText(result) {
const parts = result.candidates?.[0]?.content?.parts;
if (!parts) return { blocked: result.promptFeedback?.blockReason ?? "no candidates" };
return { text: parts.map((part) => part.text ?? "").join("") };
}
await new Promise((resolve) => server.listen(0, resolve));
const base = "http://127.0.0.1:" + server.address().port;
const ask = (input) => ({ model: "mock-model", input });
const interaction = await post(base, "/v1beta/interactions", KEY, ask("Explain REST"));
console.log("interactions:", interactionText(interaction));
console.log("steps:", interaction.steps.map((step) => step.type).join(", "));
const u = interaction.usage;
console.log("usage: in=" + u.total_input_tokens + " out=" + u.total_output_tokens +
" thought=" + u.total_thought_tokens + " total=" + u.total_tokens);
const legacyPath = "/v1beta/models/mock-model:generateContent";
const asLegacy = (text) => ({ contents: [{ parts: [{ text }] }] });
const legacyOk = await post(base, legacyPath, KEY, asLegacy("Explain REST"));
console.log("legacy:", legacyText(legacyOk).text);
const legacyBlocked = await post(base, legacyPath, KEY, asLegacy("blocked"));
console.log("legacy blocked:", legacyText(legacyBlocked).blocked);
const failures = [
["bad key", "wrong-key", ask("hi")],
["no input", KEY, { model: "mock-model" }],
];
for (const [label, key, body] of failures) {
try {
await post(base, "/v1beta/interactions", key, body);
} catch (err) {
console.log(label + " ->", err.status, err.code);
}
}
for (let attempt = 1; attempt <= 3; attempt++) {
try {
const burst = await post(base, "/v1beta/interactions", KEY, ask("burst"));
console.log("burst ->", interactionText(burst));
break;
} catch (err) {
console.log("burst -> retry after", err.status, err.code);
await new Promise((resolve) => setTimeout(resolve, 2 ** attempt * 50));
}
}
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Key in a header
headers: { "x-goog-api-key": key, "content-type": "application/json" },Key က URL ထဲမဟုတ်ဘဲ header ထဲမှာပဲ သွားလို့ access log နဲ့ browser history ထဲ မရောက်ပါဘူး။
- Keep the error code
code: data.error.code,ပစ်လိုက်တဲ့ Error မှာ HTTP status နဲ့ snake_case code နှစ်ခုလုံး ပါပါတယ်။ Retry လုပ်သင့်မလား ဆိုတာကို ဂဏန်းမဟုတ်ဘဲ code က ပြောလို့ပါ။
- Pick steps by type
.filter((step) => step.type === "model_output")Mock ထဲက thought step မှာ text မပါပါဘူး။ model_output step တွေကို ရွေးလိုက်လို့ အဲဒါကို ကျော်ပြီး အဖြေကို မှန်တဲ့နေရာကနေ ဖတ်ပါတယ်။
- Thought tokens
u.total_thought_tokensOutput မှာ အဖြေ token 23 ခု၊ thought token 49 ခုနဲ့ စုစုပေါင်း 79 ကို မြင်ရပါတယ်။ ဒါကြောင့် အဖြေအရှည်တစ်ခုတည်း မဟုတ်ဘဲ စုစုပေါင်းကို log မှတ်သင့်ပါတယ်။
- Blocked prompt, no candidates
if (!parts) return { blocked: result.promptFeedback?.blockReason ?? "no candidates" };generateContent အတွက် optional chain က TypeError ကို ရှောင်ပေးပြီး ဘာမှ မပြန်လာတာ ဘာကြောင့်လဲ ဆိုတာကို ပြန်ပြောပေးပါတယ်။
- Backoff before a retry
await new Promise((resolve) => setTimeout(resolve, 2 ** attempt * 50));rate_limit_exceeded ပြီးရင် loop က 100 ms၊ ပြီးတော့ 200 ms စောင့်ပြီး သုံးကြိမ်ပြည့်ရင် လက်လျှော့ပါတယ်။ Mock က ဒုတိယအကြိမ်ကို ဖြတ်ပေးလိုက်ပါတယ်။
interactions: REST lets programs talk over HTTP.
steps: thought, model_output
usage: in=7 out=23 thought=49 total=79
legacy: REST lets programs talk over HTTP.
legacy blocked: SAFETY
bad key -> 401 authentication
no input -> 400 invalid_request
burst -> retry after 429 rate_limit_exceeded
burst -> REST lets programs talk over HTTP.curl နဲ့ call တူတူနဲ့ အဟောင်း shape
အောက်က ပထမ command က Interactions call ဖြစ်ပြီး ဒုတိယက အဟောင်း generateContent call ပါ။ နှစ်ခုလုံး ပုံဥပမာပါ။ လက်ရှိ docs ကို စစ်ပါ၊ ကိုယ့် key ကို GEMINI_API_KEY ထဲထည့်ပါ၊ <model-id> ကို models page ကနေ id တစ်ခုနဲ့ အစားထိုးပါ။
# Illustrative - check the current docs, and use your own key in GEMINI_API_KEY.
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<model-id>",
"input": "Explain REST in one short paragraph."
}'# Older shape, still seen in tutorials. Illustrative - check the current docs.
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/models/<model-id>:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"contents": [{"parts": [{"text": "Explain REST in one short paragraph."}]}]}'| Interactions | generateContent (အဟောင်း) | |
|---|---|---|
| Input field | input | contents[].parts[].text |
| စာသားရှိရာ | type model_output ရှိတဲ့ steps[] | candidates[0].content.parts[] |
| Token usage | usage.total_*_tokens | usageMetadata.*TokenCount |
| Blocked prompt | safety လို error code | promptFeedback.blockReason |
ကိုင်တွယ်သင့်တဲ့ error code တွေ
Google ရဲ့ error table က snake_case code တစ်ခုချင်းစီကို HTTP status နဲ့ အကြံပြုထားတဲ့ လုပ်ရမယ့်အရာနဲ့ တွဲပေးထားပါတယ်။ ပထမဆုံး တွေ့ရမယ့် အတန်းတွေက အောက်မှာပါ။ safety နဲ့ recitation ဆိုတဲ့ code နှစ်ခုကိုတော့ HTTP status မပါဘဲ generation-blocked code အဖြစ် သီးခြားစာရင်းပေးထားလို့ ခိုင်မာတဲ့ client က error body ရော စာသားမပါတဲ့ reply ရော နှစ်ခုလုံးကို ကိုင်တွယ်ပါတယ်။
| Code (HTTP) | အဓိပ္ပာယ် | Client လုပ်ရမယ့်အရာ |
|---|---|---|
| rate_limit_exceeded (429) | မိနစ်/စက္ကန့်အလိုက် limit ထိ | Backoff နဲ့ retry |
| quota_exceeded (429) | တစ်ရက်စာ quota ကုန် | Reset ကို စောင့် ဒါမှမဟုတ် တိုးခိုင်း |
| service_unavailable (503) | Overload ဖြစ် ဒါမှမဟုတ် ပျက် | Backoff နဲ့ retry |
| invalid_request (400) | Request ပုံစံမှား | Request ကို ပြင် |
| authentication (401) | Key မပါ ဒါမှမဟုတ် မှား | Key ကို ပြင် |
| payment_required (402) | Prepaid credit ကုန် | Credit ထည့်မချင်း ရပ် |
Key လုံခြုံရေးနဲ့ ကုန်ကျစရိတ်
Key ကို ကိုယ့် back end ပေါ်က environment variable ထဲထားပြီး browser က အဲဒီ back end ကို ခေါ်ပါစေ။ Limit နဲ့ ဈေးနှုန်းတွေကို Google က model နဲ့ plan အလိုက် သတ်မှတ်တာမို့ ဂဏန်းတွေကို အလွတ်မကျက်ဘဲ လက်ရှိ page တွေကို ဖတ်ပါ။ generation_config နဲ့ output ကို ကန့်သတ်၊ call တိုင်းမှာ usage ကို log မှတ်ပြီး thought token တွေက စုစုပေါင်းထဲ ပါတာကို သတိရပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project မှာ ဘယ်လိုသုံးမလဲ
Customer feedback တွေကို နေ့စဉ်ညမှာ အကျဉ်းချုပ်ပေးတဲ့ job တစ်ခုကို မြင်ကြည့်ပါ။ Gemini ကို server ဘက် adapter သေးသေးလေးတစ်ခုနဲ့ ထုပ်ပါ။ Model id က environment variable ကလာမယ်၊ function တစ်ခုက ရလာတဲ့ response shape ဘာဖြစ်ဖြစ် { text, usage } ကို ပြောင်းပေးမယ်၊ classifier တစ်ခုက error code တစ်ခုချင်းစီကို retry, wait, fix, stop အဖြစ် ပြောင်းပေးမယ်။ AbortSignal.timeout နဲ့ ဖန်တီးတဲ့ timeout က ကပ်နေတဲ့ request တစ်ခုကြောင့် job တစ်ခုလုံး မရပ်သွားအောင် ကာကွယ်ပေးပါတယ်။
Adapter ကို success၊ blocked output၊ rate_limit_exceeded၊ quota_exceeded၊ authentication နဲ့ timeout တွေကို ပြပေးတဲ့ mock server နဲ့ စမ်းပါ။ ကိုယ်ရွေးထားတဲ့ model ကို Google က ရုပ်သိမ်းတဲ့အခါ ကူးပြောင်းရတာက configuration တစ်ကြောင်းပြောင်းပြီး staging key နဲ့ test အစိမ်းရောင်ရအောင် run ရုံပါပဲ။ သုံးနေတဲ့ id၊ နောက်ဆုံး စစ်ခဲ့တဲ့ရက်နဲ့ key ပိုင်ရှင်ကို runbook မှတ်စုတိုတစ်ခုမှာ ထားပါ။
အမှားများ
မကြာခဏ လုပ်မိတတ်တဲ့ အမှားတွေ
Gemini integration တွေ အများဆုံး ကျတာက အောက်ပါ လေးနည်းပါ။
Tutorial က model id ကို hard-code လုပ်ခြင်း
Id တွေကို ကြေညာထားတဲ့ ရက်စွဲတွေမှာ ရုပ်သိမ်းတာ ဒါမှမဟုတ် လမ်းကြောင်းလွှဲတာ လုပ်ပါတယ်။ Copy ကူးထားတဲ့ id က တစ်နေ့မှာ not_found ပြန်လာနိုင်သလို အပြုအမူကွာတဲ့ model ဆီ တိတ်တိတ်လေး ညွှန်သွားနိုင်ပါတယ်။ Configuration ထဲထားပြီး release လုပ်တဲ့အခါ models page ကို ပြန်စစ်ပါ။
candidates[0] ကို အမြဲရှိမယ်လို့ ယူဆခြင်း
generateContent မှာ blocked prompt က promptFeedback.blockReason ပါပြီး candidate မပါတဲ့ HTTP 200 ပြန်ပါတယ်၊ Interactions shape မှာလည်း candidates field က လုံးဝမရှိပါဘူး။ မရှိတဲ့ path ကို ကိုင်တွယ်တတ်တဲ့ extractor နဲ့ စာသားကို ဖတ်ပါ။
quota_exceeded ကို rate limit လို ပြန်ကြိုးစားခြင်း
နှစ်ခုလုံး 429 အဖြစ် လာပေမယ့် rate_limit_exceeded က စက္ကန့်ပိုင်းနဲ့ ပြေလည်ပြီး quota_exceeded ကတော့ တစ်ရက်စာ quota ကုန်သွားတာပါ။ အဲဒါကို loop လုပ်နေရင် အကြိမ်နဲ့ အချိန်ကိုပဲ ကုန်စေပါတယ်၊ reset ကို စောင့်ပါ ဒါမှမဟုတ် quota ပိုတောင်းပါ။
Key ကို URL ထဲ ဒါမှမဟုတ် browser code ထဲ ထည့်ခြင်း
URL တွေကို server log၊ proxy log နဲ့ browser history ထဲ မှတ်ထားပြီး browser code ထဲက အရာအားလုံးက အများသိဖြစ်ပါတယ်။ Header ကို သုံးပြီး ကိုယ့် back end ကနေ ခေါ်ပါ။
Best Practices
ထိန်းသိမ်းသင့်တဲ့ အလေ့အကျင့်တွေ
- ကိုယ်သုံးမယ့် API revision ရဲ့ quickstart နဲ့ error page ကို ဖတ်ပြီး ဖတ်တဲ့ရက်ကို မှတ်ထားပါ။
- Call တိုင်းမှာ interaction status နဲ့ usage ကို log မှတ်ပါ၊ ဒါမှ ကုန်ကျစရိတ် အံ့ဩစရာတွေကို ကိုယ့် dashboard မှာ အရင်တွေ့ပါမယ်။
- Response shape တစ်ခုချင်းစီနဲ့ error code တစ်ခုချင်းစီကို ကိုယ့် extractor ထဲ ထည့်စမ်းတဲ့ contract test ထည့်ပါ။
Mini challenge
Error code တွေကို အမျိုးအစားခွဲပါ
Gemini error code တွေကို retry, wait, fix, stop ဆိုတဲ့ လုပ်ဆောင်ချက်လေးမျိုး၊ မသိသေးတဲ့ code အတွက် log ဆိုပြီး ခွဲပေးတဲ့ classify(code) ကို ရေးပါ။ ပြီးရင် သက်သေပြပါ။ ကိုယ်တောင်းတဲ့ code နဲ့ ကျတဲ့ mock server ကို port 0 ပေါ်မှာ စတင်၊ code တစ်ခုချင်းစီကို fetch နဲ့ တစ်ကြိမ်စီခေါ်ပြီး HTTP status၊ code နဲ့ ရွေးထားတဲ့ action ကို စာကြောင်းတစ်ကြောင်းစီမှာ print လုပ်ပါ။
- quota_exceeded ကို rate_limit_exceeded နဲ့ ခြားပြီး ကိုင်ပါ
- payment_required ကို ဘယ်တော့မှ retry မလုပ်ပါနဲ့
- မသိတဲ့ code အတွက် throw မလုပ်ဘဲ log ကို return ပြန်ပါ
- HTTP status တစ်ခုတည်းကို မကြည့်ဘဲ JSON body ထဲက error.code ကနေ code ကို ဖတ်ပါ
import http from "node:http";
// TODO 1: decide what a caller should do for each error code and group the codes:
// retry - wait with exponential backoff, then try again
// wait - the daily quota is used up: do not hammer, wait for the reset
// fix - the request or the credentials are wrong: change them first
// stop - money is the problem: never retry
// Codes to place: rate_limit_exceeded, too_many_requests, service_unavailable,
// api_error, quota_exceeded, invalid_request, parameter_unknown, authentication,
// permission_denied, payment_required
function classify(code) {
// TODO 2: return the group name, or "log" for a code you do not know yet
return "log";
}
// TODO 3: start a mock server on port 0. It reads { input } from the JSON body and
// answers with an HTTP error whose body is { error: { code: input, message } }.
// Pick a realistic status for each code (429, 503, 400, 401, 402, 500 ...).
// TODO 4: POST every code with fetch, read error.code from the reply, and print
// one line per code, for example: 429 quota_exceeded -> wait
// TODO 5: close the serverရလဒ်: "429 quota_exceeded -> wait" လို စာကြောင်း ခုနစ်ကြောင်း ထွက်ပါမယ်။ rate နဲ့ 503 code တွေက retry၊ request နဲ့ key ပြဿနာတွေက fix၊ payment_required က stop၊ တီထွင်ထားတဲ့ code က log လို့ ပြပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Group တွေကို array ပါတဲ့ object တစ်ခုထဲထားပြီး Object.entries() နဲ့ loop လုပ်ပါ။ မသိတဲ့ code က loop ကနေ ထွက်သွားပြီး "log" ကို return ပြန်ပါလိမ့်မယ်။
အဖြေ ကြည့်ရန်
import http from "node:http";
// What a caller should do for each documented error code.
const GROUPS = {
retry: ["rate_limit_exceeded", "too_many_requests", "service_unavailable", "api_error"],
wait: ["quota_exceeded"],
fix: ["invalid_request", "parameter_unknown", "authentication", "permission_denied"],
stop: ["payment_required"],
};
function classify(code) {
for (const [action, codes] of Object.entries(GROUPS)) {
if (codes.includes(code)) return action;
}
return "log";
}
// Mock server: fails with whatever code the request asks for.
const STATUS = {
rate_limit_exceeded: 429,
quota_exceeded: 429,
service_unavailable: 503,
invalid_request: 400,
authentication: 401,
payment_required: 402,
brand_new_code: 500,
};
const server = http.createServer((req, res) => {
let raw = "";
req.on("data", (chunk) => (raw += chunk));
req.on("end", () => {
const { input } = JSON.parse(raw);
res.writeHead(STATUS[input], { "content-type": "application/json" });
res.end(JSON.stringify({ error: { code: input, message: "demo failure" } }));
});
});
await new Promise((resolve) => server.listen(0, resolve));
const url = "http://127.0.0.1:" + server.address().port + "/v1beta/interactions";
for (const code of Object.keys(STATUS)) {
const res = await fetch(url, { method: "POST", body: JSON.stringify({ input: code }) });
const { error } = await res.json();
console.log(res.status, error.code, "->", classify(error.code));
}
server.close();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Interactions endpoint က model နဲ့ input ကိုယူ၊ x-goog-api-key နဲ့ authenticate လုပ်ပြီး step တွေ ပြန်ပေးပါတယ်။ အဟောင်း generateContent shape က tutorial အများအပြားမှာ ရှိနေဆဲပါ။
- စာသားကို model_output step တွေကနေ ရွေးပြီး အဟောင်း candidates path ကို blocked prompt ကနေ ကာကွယ်ပါ။
- Thought token တွေက usage ထဲ ပါလို့ အဖြေအရှည်တစ်ခုတည်း မဟုတ်ဘဲ total_tokens ကိုပါ log မှတ်ပါ။
- error.code ကိုဖတ်ပြီး ဆုံးဖြတ်ပါ။ rate limit ကို retry၊ quota_exceeded ကို စောင့်၊ 400 နဲ့ 401 ကို fix၊ payment_required မှာ ရပ်ပါ။
Gemini API Documentation — Google