ⵍⴻⴱⵏⵉ ⵏ ⵓⵙⴻⵇⴷⴻⵛ ⵏ AI ⴷⴻⴳ 10 ⵏ ⵜⴻⴷⵇⵉⵇⵉⵏ: ⴰⴹⵔⵉⵙ ⵙ ⵓⵙⵡⵉⵔ

ⴳⴻⵎⵉⵏⵉ
Nodejs
AI
Adlis
ⵍⴻⴱⵏⵉ ⵏ ⵓⵙⴻⵇⴷⴻⵛ ⵏ AI ⴷⴻⴳ 10 ⵏ ⵜⴻⴷⵇⵉⵇⵉⵏ: ⴰⴹⵔⵉⵙ ⵙ ⵓⵙⵡⵉⵔ cover image

ⵙⴻⴳ ⵡⴰⵙⵎⵉ ⴰⵢ ⴷⴼⴼⵖⴻⵏ ChatGPT ⴷ Google Gemini, ⵏⵡⴰⵍⴰ ⴰⵟⴰⵙ ⵏ ⵜⴻⵙⵏⴰⵍⵍⵓⵏⵉⵏ ⴰⵢ ⴷⵢⴻⵜⵜⵡⴰⵙⴱⴻⴷⴷⴻⵏ ⵙ ⵓⵙⵏⴻⵔⵏⵉ ⵏ ⵢⵉⵎⵓⴽⴰⵏⴰ. ⵜⴻⴽⵏⵉⴽⵜⴰ ⵇⵇⴰⵔⴻⵏⴰⵙ ⴰⵔⴱⵉⵜⵔⴰⴳⴻ, ⴷ ⵜⴰⵔⵔⴰⵢⵜ ⴰⵢ ⵢⴻⵜⵜⴹⴰⴼⴰⵔⴻⵏ ⵢⵉⵡⴻⵜ ⵙⴻⴳ ⵍⴻⵛⵖⴰⵍ ⵢⴻⵎⴳⴻⵔⵔⴰⴷⴻⵏ ⴰⵢ ⵢⴻⵣⵎⴻⵔ ⴰⴷ ⵜⴻⵏⵜⵢⴻⴳ AI, ⵜⴻⵙⵏⴻⵔⵏⴰⵢⵉⵜⵜ ⵙ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ ⵏ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ, ⵙⴰⴽⴽⵉⵏ ⵜⴻⵜⵜⴰⴼⴽⵉⵜⵜ ⵉ ⵢⵉⵎⵙⴰⵖⴻⵏ ⴰⵎ ⵜⵖⴰⵡⵙⴰ.

ⵎⴰⴹⵔⵉⵙⴰ ⴰⴷ ⵢⵉⵍⵉ ⴷ ⴰⴹⵔⵉⵙ ⵏ ⵜⵎⵓⵖⵍⵉ ⵙ ⵜⵎⵓⵖⵍⵉ ⵖⴻⴼ ⵡⴰⵎⴻⴽ ⴰⵔⴰ ⵏⴻⵙⵙⴻⵎⵍⵉⵍ ⴰⵎⴹⴰⵏ ⵏ Google Gemini ⴷⴻⴳ ⵓⵙⴻⵇⴷⴻⵛⵏⵏⴻⵖ Node.ⵊⵙ. ⴰⴷ ⵏⵡⴰⵍⵉ ⴰⵎⴻⴽ ⴰⵔⴰ ⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⴰⵎⴰ ⴷ Geminipro ⵉ ⵢⴻⵜⵜⵃⴰⴷⴰⵔ ⴰⴹⵔⵉⵙ ⵏ ⵓⵙⵏⵓⵍⴼⵓ, ⴰⵎⴰ ⴷ Geminiprovision ⵉ ⵢⴻⵜⵜⵃⴰⴷⴰⵔ ⵜⵓⴳⵏⵉⵡⵉⵏ.

ⴰⴷ ⵏⴻⴱⴷⵓ ⴷⵉ ⵜⴰⵣⵡⴰⵔⴰ ⵙ ⵓⵙⴱⴻⴷⴷ ⵏ ⵓⵙⴻⵏⴼⴰⵔ, ⵙⵢⵉⵏ ⴰⴷ ⵜⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⵖⴻⴼ ⵓⵎⴻⴷⵢⴰ ⵏ ⵢⵉⵎⴰⵏⵏⵏⴻⵖ.

ⵉⵣⴻⵎⵍⵉⵍⴻⵢⵢⴻⵏ

ⴰⴷ ⵜⴻⴱⴷⵓⴹ

ⵜⴰⵣⵡⴰⵔⴰ, ⵙⵏⵓⵍⴼⵓⴷ ⴰⵙⴻⵏⴼⴰⵔ ⴰⵎⴰⵢⵏⵓⵜ ⵏ Node.ⵊⵙ ⴷⴰⵅⴻⵍ ⵏ ⵢⵉⵡⴻⵏ ⵏ ⵓⴹⵔⵉⵙ ⵉⵍⴻⵎ:

mkdir gemini-node  
cd gemini-node
npm init -y

ⵜⵓⵔⴰ, ⴰⴷ ⵏⴻⵙⵙⴻⵔⵙ ⵉⴼⴻⵔⴷⵉⵙⴻⵏ ⵉⵍⴰⵇⴻⵏ ⵉ ⵓⴼⴰⵢⵍⵓ ⵏ ⵜⵡⴻⵏⵏⴰⴹⵜ ⴷ Gemini SDK:

npm install dotenv @google/generative-ai

ⴰⵙⴻⴱⴷⴻⴷ ⵏ ⵜⵙⴰⵔⵓⵜ ⵏ API

ⴷⴻⴳ ⵜⵎⴻⵥⴷⵉⵜⵉⴽ ⵏ Google AI, ⵜⵜⴻⴽⵙⴻⴹ ⵖⴻⴼ ⵜⵎⴻⵥⴷⵉⵜ Awid Tasarut ⵏ API ⴰⴽⴽⴻⵏ ⴰⴷ ⴷⵜⴰⴼⴻⴹ Tasarut ⵏ API ⵙⵢⴻⵏ ⴰⴷ ⵜⵜⵜⴻⵙⵙⴻⴽⵛⴻⵎ ⴷⴻⴳ ⵓⴼⴰⵢⵍⵓ ⵏ ⵜⵡⴻⵏⵏⴰⴹⵜⵉⴽ (.env) ⴷⴻⴳ ⵓⴹⵔⵉⵙ ⵏ ⵓⵥⴰⵔ:

API_KEY=YOUR_GEMINI_KEY

ⵙⵏⵓⵍⴼⵓⴷ ⴰⴹⵔⵉⵙ ⴰⵎⴰⵢⵏⵓⵜ ⵍⵉⴱ ⵓ ⵙⵏⵓⵍⴼⵓⴷ ⴰⴼⴰⵢⵍⵓ config.ⵊⵙ ⴷⴰⵅⴻⵍⵉⵙ, ⵙⵢⵉⵏ ⵉ ⵜⵖⴰⵡⵙⴰ ⵉ ⴷⵉⵜⴻⴷⴷⵓⵏ, ⴰⴷ ⵏⴻⵙⵙⴻⴽⵛⴻⵎ API ⴽⴻⵢⵏⵏⴻⵖ:

const  dotenv  =  require("dotenv");
dotenv.config();

const  API_KEY  =  process.env.API_KEY;

module.exports  = { API_KEY };

ⴰⵙⴻⴱⴷⴻⴷ ⵏ SDK ⵏ Gemini

ⴷⴻⴳ ⵜⵖⴰⵡⵙⴰⴰ, ⴰⴷ ⵏⴻⵙⵙⴻⴱⴷⴻⴷ ⵉⵎⵓⴷⴻⵍⵏⵏⴻⵖ ⴰⴽⴽⴻⵏ ⴰⴷ ⵜⴻⵏⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⴷⴻⴳ ⵓⴹⵔⵉⵙⵏⵏⴻⵖ. ⴷⴰⵅⴻⵍ ⵏ ⵓⴹⵔⵉⵙⵏⵏⴻⵖ ⵍⵉⴱ, ⴰⴷ ⴷⵏⴻⵙⵏⵓⵍⴼⵓ 2 ⵏ ⵢⵉⴼⵓⵢⵍⴰ, gemini.ⵊⵙ ⵉ ⵓⵃⴻⵣⵣⴻⴱ ⵏ ⵓⴹⵔⵉⵙ ⴷ geminivision.ⵊⵙ ⵉ ⵓⵃⴻⵣⵣⴻⴱ ⵏ ⵜⵓⴳⵏⵉⵡⵉⵏ.

1- ⴳⵓⴳⵍ ⵊⴻⵎⵉⵏⵉ ⴱⵔⵓ

const { GoogleGenerativeAI } = require("@google/generative-ai");
const { API_KEY } = require("./config");

const googleAI = new GoogleGenerativeAI(API_KEY);
const geminiConfig = {
  temperature: 0.9,
  topP: 1,
  topK: 1,
  maxOutputTokens: 4096,
};

const geminiModel = googleAI.getGenerativeModel({
  model: "gemini-pro",
  geminiConfig,
});

const generateText = async (prompt) => {
  try {
    const result = await geminiModel.generateContent(prompt);
    const response = result.response;
    return response.text();
  } catch (error) {
    console.log("response error", error);
  }
};

module.exports = { generateText };

ⴰⴷ ⵏⴻⵙⵙⴻⵎⵔⴻⵙ ⵜⴰⵖⵓⵍⵜ ⵢⴻⵜⵜⵡⴰⵙⴼⴻⴹⴻⵏ ⴰⴽⴽⴻⵏ ⴰⴷ ⴷⵏⴻⵙⵙⴻⴽⵛⴻⵎ ⴰⴹⵔⵉⵙ ⵙⴻⴳ ⵢⵉⵡⴻⵏ ⵏ ⵓⵙⵎⴻⵍ. ⵏⴻⵣⵎⴻⵔ ⴰⴷ ⵜⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⵉ ⵍⴻⴱⵏⵉ ⵏ ⵓⵏⴰⴳⵔⴰⵡ ⵏ ⵜⵎⴻⵙⵍⵉⵡⵜ, ⴰⵙⵓⵇⴻⵍ ⵏ ⵓⴹⵔⵉⵙ ⴷ ⵡⴰⵟⴰⵙ ⵏ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ ⵏⵏⵉⴹⴻⵏ ⵏ ⵓⵙⴻⵇⴷⴻⵛ.

2- ⵜⴰⵎⵓⵖⵍⵉ ⵏ Gugl ⵏ ⵢⵉⵍⴻⵎⵥⵉⵢⴻⵏ

const { GoogleGenerativeAI } = require("@google/generative-ai");
const { API_KEY } = require("./config");
const fs = require("fs");

const googleAI = new GoogleGenerativeAI(API_KEY);
const geminiConfig = {
  temperature: 0.4,
  topP: 1,
  topK: 32,
  maxOutputTokens: 4096,
};

const geminiModel = googleAI.getGenerativeModel({
  model: "gemini-pro-vision",
  geminiConfig,
});

const interactWithImage = async (filePath) => {
  try {
    const imageFile = fs.readFileSync(filePath);
    const imageBase64 = imageFile.toString("base64");

    const promptConfig = [
      { text: "Generate a caption from this image" },
      {
        inlineData: {
          mimeType: "image/jpeg",
          data: imageBase64,
        },
      },
    ];

    const result = await geminiModel.generateContent({
      contents: [{ role: "user", parts: promptConfig }],
    });

    return result.response.text();
  } catch (error) {
    console.log("response error", error);
  }
};

module.exports = { interactWithImage };

ⴰⴷ ⵏⴻⵙⵙⴻⵎⵔⴻⵙ ⵜⴰⵖⵓⵍⵜ ⵢⴻⵜⵜⵡⴰⵙⴼⴻⴹⴻⵏ ⴰⴽⴽⴻⵏ ⴰⴷ ⴷⵏⴻⵙⵙⴻⴽⵛⴻⵎ ⴰⴹⵔⵉⵙ ⵙⴻⴳ ⵜⵓⴳⵏⴰ.

ⵍⴻⴱⵏⵉ ⵏ ⵓⵙⴻⵏⴼⴰⵔ

ⵙⵏⵓⵍⴼⵓⴷ ⴰⴼⴰⵢⵍⵓ ⴰⵎⴰⵢⵏⵓⵜ ⵉⵏⴷⴻⵅ.ⵊⵙ ⵓ ⴹⴻⴳⴳⴻⵔ ⴰⴽⴰⵔⴰⵎⴰ:

const main = async () => {
  console.log("Hello world!");
};

main();

ⵜⵓⵔⴰ, ⵏⴻⵣⵎⴻⵔ ⴰⴷ ⵏⴻⵙⵙⴻⴽⵛⴻⵎ ⵜⵉⵖⴱⵓⵍⴰⵏⵏⴻⵖ ⵢⴻⵜⵜⵡⴰⵙⴼⴻⴹⴻⵏ ⵢⴻⵔⵏⴰ ⴰⴷ ⵜⴻⵏⵜⵏⴻⵙⵙⴻⵅⴷⴻⵎ ⴰⴽⴽⴻⵏ ⴰⴷ ⴷⵏⴻⵙⵙⴻⴽⵛⴻⵎ ⴰⴹⵔⵉⵙ, ⴰⵎⴰ ⵙⴻⴳ ⵓⵙⵜⴻⵇⵙⵉ ⵏⴻⵖ ⵙⴻⴳ ⵓⵙⵎⴻⵍ ⵏ ⵜⵓⴳⵏⴰ.

ⵜⴰⵖⵓⵍⵜ ⵜⴻⵟⵟⴻⴼ ⴰⴱⵔⵉⴷ ⵏ ⵓⴼⴰⵢⵍⵓ ⴷ ⴰⴼⴻⵔⴷⵉⵙ, ⵉⵀⵉ ⴰⴷ ⵜⴻⵃⵡⴰⴵⴻⴹ ⴰⴷ ⵜⴻⵔⵏⵓⴹ ⴰⴼⴰⵢⵍⵓ ⵏ ⵜⵓⴳⵏⴰ ⴷⴻⴳ ⵓⴹⵔⵉⵙ ⵏ ⵓⵙⴻⵏⴼⴰⵔⵉⴽ ⴰⴽⴽⴻⵏ ⴰⴷ ⵜⵜⴻⵄⵔⴻⴹ.

ⴰⴽⴰⵔⴰⵎⵏⵏⴻⵖ ⴰⴷ ⵢⴻⵜⵜⵡⴰⴱⴻⴷⴷⴻⵍ ⵖⴻⵔ:

const { generateText } = require("./lib/gemini");
const { interactWithImage } = require("./lib/gemini-vision");
const path = require("path");

const main = async () => {
  // Text Generation
  let textFromPrompt = await generateText(
    "tell me about bootcamps in a sentence"
  );
  console.log(textFromPrompt);

  // Caption Generation
  const directoryName = path.join(__dirname, "fish.jpg");
  let captionFromImage = await interactWithImage(directoryName);
  console.log(captionFromImage);
};

main();

ⴷ ⴰⵛⵓ ⵉ ⴷⵉⵜⴻⴷⴷⵓⵏ

ⵜⵓⵔⴰ, ⵜⵣⴻⵎⵔⴻⴹ ⴰⴷ ⵜⴻⵙⵇⴻⴷⵛⴻⴹ ExpressJS ⴰⴽⴽⴻⵏ ⴰⴷ ⵜⴻⴱⵏⵓⴹ API ⴷⴻⴳ ⵓⵎⴹⵉⵇ ⵏ ⵜⴻⵙⵖⵓⵏⵜ. ⵜⵣⴻⵎⵔⴻⴹ ⴷⴰⵖⴻⵏ ⴰⴷ ⵜⴳⴻⴹ ⵜⴰⵣⵔⴰⵡⵜ ⵖⴻⴼ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ ⵢⴻⵍⵀⴰⵏ ⵖⴻⴼ ⵜⵉⵔⴰ ⵏ ⵜⵖⴰⵡⵙⵉⵡⵉⵏ ⵏ ⵓⵙⴻⵇⴷⴻⵛⵉⴽ.

ⵡⵉⴳⵉ ⴷ ⴽⵔⴰ ⵏ ⵢⵉⵎⴻⴷⵢⴰⵜⴻⵏ ⵖⴻⴼ ⵜⴻⵙⵏⴰⵍⵍⵓⵏⵜ ⵉ ⵜⵣⴻⵎⵔⴻⴹ ⴰⴷ ⵜⴻⴱⵏⵓⴹ:

  • Asnulfu ⵏ ⵢⵉⵙⴻⴼⴽⴰ ⵏ SEO Meta ⵙ ⵓⵙⴻⵇⴷⴻⵛ ⵏ API ⵏ Unsplash

  • ⵔⴻⵙⵓⵎⴻ ⵏ ⵓⵙⴻⴽⵢⴻⴷ ⵙ ⵓⵙⵏⴻⵔⵏⵉ ⵏ ⵜⴻⵙⵖⵓⵏⵜ ⵏ ⵣⵢⴰⴷⴰ ⴰⵔⴰ ⴷⵢⴰⵡⵉⵏ ⴰⴳⴱⵓⵔ ⵙⴻⴳ PDF

  • ⴰⵎⴰⵔⵓ ⵏ ⵜⵓⴷⴻⵔⵜ ⵏ Instagram ⵙⴻⴳ ⵢⵉⵡⴻⵜ ⵏ ⵜⵓⴳⵏⴰ

ⵜⴻⵙⵄⵉⴹ ⵍⵃⵉⵔ ⴰⴷ ⵜⴻⴱⵏⵓⴹ ⵜⵉⵥⵔⵉⴳⵉⵏ ⵉⴳⴻⵔⵔⵣⴻⵏ? ⴽⴻⵛⵎⴻⵜ ⵖⴻⵔ bootcampnneɣ ⵏ ⵓⵙⵏⴻⴼⵍⵉ ⵏ ⵡⴻⴱ ⵓ ⵍⴻⵎⴷⴻⵜ ⴰⵎⴻⴽ ⴰⵔⴰ ⵜⴻⴱⵏⵓⵎ ⴰⴼⴰⵔⵉⵙⴻⵏ ⵉⵅⴻⴷⴷⵎⴻⵏ ⵙ ⵍⴻⴽⵎⴰⵍⵏⵙⴻⵏ!


By Omar Mokhfi

Career Services background pattern

ⵉⵎⴻⵥⵍⴰ ⵏ ⵜⵎⵓⵙⵏⵉ

Contact Section background image

ⴰⴷ ⵏⴻⵇⵇⵉⵎ ⴷⴻⴳ ⵓⵙⵉⵡⴰⴹ

Code Labs Academy © 2024 ⵉⵣⴻⵔⴼⴰⵏ ⴰⴽⴽ ⵜⵜⵡⴰⵃⴻⵔⵣⴻⵏ.