ai.google.dev पर देखें | Colab notebook को आज़माएं | GitHub पर notebook देखें |
एपीआई पासकोड पाएं
शुरू करने के लिए, आपको एक एपीआई पासकोड हासिल करना होगा. इसे एनवायरमेंट वैरिएबल के तौर पर सेट करें:
import os
os.environ["API_KEY"] = "<YOUR API KEY>"
एपीआई क्लाइंट इंस्टॉल करना
नई डायरेक्ट्री में, npm का इस्तेमाल करके Node.js प्रोजेक्ट शुरू करें और
google-auth
लाइब्रेरी:
npm init -y
npm install google-auth-library
Wrote to /content/package.json: { "name": "content", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@google-ai/generativelanguage": "^1.0.1", "google-auth-library": "^9.0.0" }, "devDependencies": {}, "description": "" } + google-auth-library@9.0.0 updated 1 package and audited 74 packages in 1.105s 3 packages are looking for funding run `npm fund` for details found 0 vulnerabilities npm WARN content@1.0.0 No description npm WARN content@1.0.0 No repository field.
इसके बाद, आपको Generative Language क्लाइंट लाइब्रेरी इंस्टॉल करनी होगी:
npm install @google-ai/generativelanguage
+ @google-ai/generativelanguage@1.0.1 updated 1 package and audited 74 packages in 2.126s 3 packages are looking for funding run `npm fund` for details found 0 vulnerabilities npm WARN content@1.0.0 No description npm WARN content@1.0.0 No repository field.
मैसेज जनरेट किए जा रहे हैं
एक नई फ़ाइल index.js
बनाएं और अपनी एपीआई कुंजी देते हुए, नीचे दिया गया कोड जोड़ें
API_KEY
एनवायरमेंट वैरिएबल की मदद से:
%%writefile index.js
const { TextServiceClient } =
require("@google-ai/generativelanguage").v1beta2;
const { GoogleAuth } = require("google-auth-library");
const MODEL_NAME = "models/text-bison-001";
const API_KEY = process.env.API_KEY;
const client = new TextServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
const prompt = "Repeat after me: one, two,";
client
.generateText({
model: MODEL_NAME,
prompt: {
text: prompt,
},
})
.then((result) => {
console.log(JSON.stringify(result, null, 2));
});
Overwriting index.js
इसके बाद स्क्रिप्ट चलाएं:
node index.js
[ { "candidates": [ { "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ], "output": "One, two, three, four." } ], "filters": [], "safetyFeedback": [] }, null, null ]