Login

Google Gemini API Development Guide: How to integrate Google Gemini into your websites and projects?

Author:neo yang Time:2024/01/27 Read: 11107
Last month, Google released Gemini. Immediately afterwards, I developed AI SEO based on Google Gemini - automatic and […]

Last month, Google released Gemini. Immediately afterwards, I developed AI SEO based on Google Gemini - a WordPress plug-in that automatically and batch-generates search engine-friendly articles, an SEO artifact.

Details can be found here:

This month, I developed a chatbot based on Google Gemini.

Here I would like to share my development experience based on Google Gemini API.

I. Overview

1. Development language

If you are developing js, python, go, swift, and android, you can directly refer to the examples in the official Google Gemini API documentation:

https://ai.google.dev/docs?hl=zh-cn

The examples in this article are developed based on PHP.

2. Rest API

In addition to the above development languages, other development languages will use Rest API to interact with Google Gemini.

Therefore, the PHP used in this article is also based on Rest API and Google Gemini interaction.

2. Three request modes

1. Text generation text

This is the most basic.

Model:

Gemini Pro.

Requested URL:

https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=’${API_KEY}

Request method:

POST

Requested data structure:

{ "contents": [ { "parts": [ { "text": "xxxxxx" } ] } ] }

2. Multi-mode (text + picture to generate text)

Use pictures, or text and pictures as prompt words to generate text.

Model:

Gemini Pro Version.

Requested URL:

https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key='${API_KEY}

Request method:

POST

Requested data structure:

{ "contents": [ { "parts": [ { "text": "hello\n Hello! How may I assist you today?\na cat" }, { "inlineData": { "mimeType": "image/jpeg ", "data": "'$(base64 -w0 image0.jpeg)'" } }, { "text": "xxxxxxx" } ] } ], }

3. Multiple rounds of dialogue (chat)

You can only use the Gemini Pro model, which means you can only generate text from text.

Model:

Gemini Pro.

Requested URL:

https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=’${API_KEY}

Request method:

POST

Requested data structure:

{ "contents": [ {"role":"user", "parts":[{ "text": "xxxxxx"}]}, {"role": "model", "parts":[{ "text" : "xxxxxx"}]}, {"role": "user", "parts":[{ "text": "xxxxxx"}]}, ] }

3. Examples

Use PHP and Google Gemini's Rest API to make the call.

1. Obtain Google Gemini API Key

Refer to this article:

2. PHP request code (text generates text)

function gemini_post(){ //gemini's API key $apikey="xxxxxx"; //url $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" . $apikey; / /Request data $data = [ "contents" => [ "parts" => [ "text" => $text ] ] ]; //Post request $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // Settings Request header curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); if (!empty($data)) { // Determine whether it is a POST request curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl) ; $res = json_decode($response); curl_close($curl); return $res; }

3. Multi-mode

The multi-mode request method is the same as text generation, except that there will be pictures in the requested data.

4. Chat

The chat request method is the same as text generation, except that the requested data contains multiple pieces of data, and there must be a "role" field.

4. Best Practices

Server request or client request?

From my experience, if the product involves a large number of prompt words, then it is best to request through the server because it is easier to manage the prompt words.

In addition, considering that some regions cannot request Google Gemini, server-side requests can allow users in these regions to use products based on Google Gemini.

If you don't need to consider these two points, it is more convenient for the client to request.

5. Summary

The capabilities of Google Gemini are not inferior to those of GPT, and it can be used for free. Based on Google Gemini, various AI applications can be developed more conveniently. We are optimistic about the future development of Google Gemini.

6. Appendix

Google Gemini official website:

https://ai.google.dev/

Google Gemini API documentation:

https://ai.google.dev/docs?hl=zh-cn

How to apply for Google Gemini API key:

Product cases developed based on Google Gemini API:



copyright © www.lyustu.com all rights reserved.
Theme: TheMoon V3.0. Author:neo yang