Google Gemini API Entegrasyonu

Google Gemini, geliştiricilere yüksek performanslı ve geniş bağlam pencereli bir yapay zeka deneyimi sunar. Bu rehberde, popüler yazılım dilleriyle Gemini API'sine nasıl istek atacağınızı inceleyeceğiz.

API Anahtarı Hazırlığı

Entegrasyona başlamadan önce Google AI Studio üzerinden ücretsiz API anahtarınızı oluşturun.

1) PHP (cURL Kullanımı)

Bağımlılık gerektirmeyen, doğrudan sunucu taraflı kullanım örneği.

PHP Integration
$apiKey = "YOUR_API_KEY";
$url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" . $apiKey;

$data = [
    "contents" => [["parts" => [["text" => "Yapay zeka nedir?"]]]]
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);

$response = curl_exec($ch);
echo $response;

2) Python (Official SDK)

pip install -q -U google-generativeai komutu ile kütüphaneyi kurun.

Python SDK
import google.generativeai as genai

genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')

response = model.generate_content("Python ile AI entegrasyonu?")
print(response.text)

3) Node.js

Node.js (GoogleGenerativeAI)
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI("YOUR_API_KEY");

async function run() {
  const model = genAI.getGenerativeModel({ model: "gemini-pro"});
  const result = await model.generateContent("Node.js avantajları?");
  console.log(result.response.text());
}
run();

4) C# (HttpClient)

ASP.NET Core / C#
var client = new HttpClient();
var url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY";
var body = "{\"contents\": [{\"parts\":[{\"text\":\"C# kullanımı\"}]}]}";
var content = new StringContent(body, System.Text.Encoding.UTF8, "application/json");

var response = await client.PostAsync(url, content);
Console.WriteLine(await response.Content.ReadAsStringAsync());

Önemli Not

API anahtarınızı her zaman sunucu tarafında (Backend) tutun. İstemci tarafında (JS) saklanan anahtarlar kolayca çalınabilir ve limitleriniz başkaları tarafından tüketilebilir.