إذا سبق لك دمج طرازَي Gemini Flash وPro باستخدام Vertex AI في Firebase، يمكنك الانتقال إلى Vertex AI ومواصلة استخدامه كمزوّد واجهة برمجة تطبيقات.
اطّلِع على مستندات Firebase للحصول على دليل نقل بيانات مفصّل.
خطوات البدء:
قبل التفاعل مع واجهة Vertex AI Gemini API مباشرةً من تطبيقك، يمكنك تجربة الطلبات في Vertex AI Studio.
إعداد مشروع على Firebase وربط تطبيقك به
بعد أن تصبح جاهزًا لاستدعاء Vertex AI Gemini API من تطبيقك، اتّبِع التعليمات الواردة في دليل بدء استخدام "الخطوة 1" من Firebase AI Logic لإعداد Firebase وحزمة تطوير البرامج (SDK) في تطبيقك.
إضافة تبعية Gradle
أضِف تبعية Gradle التالية إلى وحدة تطبيقك:
dependencies{// ... other androidx dependencies// Import the BoM for the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.1.0"))// Add the dependency for the Firebase AI Logic library. When using the BoM,// you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai")}
تهيئة النموذج التوليدي
ابدأ بإنشاء مثيل GenerativeModel وتحديد اسم النموذج:
في مستندات Firebase، يمكنك الاطّلاع على مزيد من المعلومات حول النماذج المتاحة للاستخدام مع Gemini Developer API. يمكنك أيضًا الاطّلاع على معلومات حول ضبط مَعلمات النموذج.
إنشاء نص
لإنشاء ردّ نصي، اتّصِل بالرقم generateContent() مع طلبك.
Kotlin
// Note: generateContent() is a suspend function, which integrates well// with existing Kotlin code.scope.launch{valresponse=model.generateContent("Write a story about a magic backpack.")}
Java
Contentprompt=newContent.Builder().addText("Write a story about a magic backpack.").build();ListenableFuture<GenerateContentResponse>response=model.generateContent(prompt);Futures.addCallback(response,newFutureCallback<GenerateContentResponse>(){@OverridepublicvoidonSuccess(GenerateContentResponseresult){StringresultText=result.getText();[...]}@OverridepublicvoidonFailure(Throwablet){t.printStackTrace();}},executor);
كما هو الحال مع Gemini Developer API، يمكنك أيضًا تمرير الصور ومقاطع الصوت والفيديو والملفات مع طلبك النصي (راجِع القسم "التفاعل مع Gemini Developer API من تطبيقك").
لمزيد من المعلومات عن حزمة تطوير البرامج (SDK) لمنصة Firebase AI Logic، يمكنك الاطّلاع على مستندات Firebase.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-17 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-08-17 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Vertex AI Gemini API\n\n| **Note:** if you previously integrated Vertex AI in Firebase (with Gradle `import\n| com.google.firebase:vertex-ai`) you can continue using Vertex AI as an API provider with the Firebase AI Logic SDK.\n\nIf you are new to the Gemini API, the [Gemini Developer API](/ai/gemini-developer-api) is the\nrecommended [API provider](/ai/gemini#api-providers) for Android Developers. But if you have specific\ndata [location requirements](https://cloud.google.com/compute/docs/regions-zones) or you are already embedded in the\nVertex AI or Google Cloud environment, you can use the Vertex AI Gemini API.\n\nMigration from Vertex AI in Firebase\n------------------------------------\n\nIf you originally integrated the Gemini Flash and Pro models using Vertex AI in\nFirebase, you can migrate to and continue using Vertex AI as an API provider.\nRead the Firebase documentation for a detailed [migration guide](https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk).\n\nGetting started\n---------------\n\nBefore you interact with the Vertex AI Gemini API directly from your app, you\ncan experiment with prompts in [Vertex AI Studio](https://console.cloud.google.com/vertex-ai/studio).\n\n### Set up a Firebase project and connect your app to Firebase\n\nOnce you're ready to call the Vertex AI Gemini API from your app, follow the\ninstructions in the \"Step 1\" Firebase AI Logic getting started guide to set up\nFirebase and the SDK in your app.\n\n### Add the Gradle dependency\n\nAdd the following Gradle dependency to your app module: \n\n dependencies {\n // ... other androidx dependencies\n\n // Import the BoM for the Firebase platform\n implementation(platform(\"com.google.firebase:firebase-bom:34.1.0\"))\n\n // Add the dependency for the Firebase AI Logic library. When using the BoM,\n // you don't specify versions in Firebase library dependencies\n implementation(\"com.google.firebase:firebase-ai\")\n }\n\n### Initialize the generative model\n\nStart by instantiating a `GenerativeModel` and specifying the model name: \n\n### Kotlin\n\n val model = Firebase.ai(backend = GenerativeBackend.vertexAI())\n .generativeModel(\"gemini-2.5-flash\")\n\n### Java\n\n GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI())\n .generativeModel(\"gemini-2.5-flash\");\n\n GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);\n\nIn the Firebase documentation, you can learn more about the [available\nmodels](https://firebase.google.com/docs/vertex-ai/gemini-models) for use with the Gemini Developer API. You can also learn\nabout [configuring model parameters](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=android).\n\n### Generate text\n\nTo generate a text response, call `generateContent()` with your prompt. \n\n### Kotlin\n\n // Note: generateContent() is a suspend function, which integrates well\n // with existing Kotlin code.\n scope.launch {\n val response = model.generateContent(\"Write a story about a magic backpack.\")\n }\n\n### Java\n\n Content prompt = new Content.Builder()\n .addText(\"Write a story about a magic backpack.\")\n .build();\n\n ListenableFuture\u003cGenerateContentResponse\u003e response = model.generateContent(prompt);\n Futures.addCallback(response, new FutureCallback\u003cGenerateContentResponse\u003e() {\n @Override\n public void onSuccess(GenerateContentResponse result) {\n String resultText = result.getText();\n [...]\n }\n\n @Override\n public void onFailure(Throwable t) {\n t.printStackTrace();\n }\n }, executor);\n\nSimilar to the Gemini Developer API, you can also pass images, audio, video and\nfiles with your text prompt (see \"Interact [with the Gemini Developer API from\nyour app\"](/ai/gemini/developer-api#interact-gemini)).\n\nTo learn more about Firebase AI Logic SDK, read the [Firebase documentation](https://firebase.google.com/docs/vertex-ai/get-started?platform=android)."]]