ローカル画像でGemini Pro APIをOpenAI APIフォーマットで試してみた【Gemini-OpenAI-Proxy】

Gemini-OpenAI-Proxy
~シリーズ情報~

今回は、Gemini-OpenAI-Proxy を使用して、ローカルに保存された画像を Gemini Pro APIOpenAI API フォーマットで送信し、画像認識のデモを行います。このプロセスは、初心者でも理解しやすいように、ステップバイステップで説明していきます。使用するコードは、Pythonをベースにしています。

Gemini-OpenAI-Proxyとは?

Gemini-OpenAI-Proxy は、OpenAI API プロトコルを Google Gemini Pro プロトコルに変換するプロキシです。これにより、OpenAIで強化された機能を Gemini Pro プロトコルを使用するアプリケーションにシームレスに統合できます。

環境構築方法はこちら

ワンパンでOpenAI APIをGemini Proにつなぐ【Gemini-OpenAI-Proxy】
Gemini-OpenAI-Proxyは、OpenAI APIプロトコルをGoogle Gemini Proプロトコルに変換するためのプロキシです。これにより、Gemini Proプロトコルを使用するアプリケーションにOpenAIが提供する...

こちらの記事もおすすめです

【Dockerでワンパン】PokéLLMonを動かしてみる
PokéLLMonは、ポケモンバトルのゲームにおいて、人間並みのパフォーマンスを実現する最初のLLM(Large Language Model)を体現したエージェントです。この記事では、PokéLLMonを実際に動かすための手順を初心者向け...
AIエージェント達の仮想ソフトウェア開発会社【ChatDev】公式Wikiの日本語版 作ってみた
Quick Start元のWIKIInstall ChatDev(ChatDev のインストール ):設置説明書に記載されているクイックスタートセクション を参照してください。Start building software in one c...

ローカルに保存された単一の画像の実行

ローカルに保存された単一の画像を使用して、画像認識を試すデモを見てみましょう。

ステップ1: 必要なライブラリをインポートする

import base64
import requests
import os
from dotenv import load_dotenv

ステップ2: 環境変数を読み込む

load_dotenv()
YOUR_GOOGLE_AI_STUDIO_API_KEY = os.getenv("GOOGLE_AI_STUDIO_API_KEY")

ステップ3: 画像をBase64形式にエンコードする関数を定義

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

ステップ4: APIリクエストを設定して送信する

image_path = "data/warabi.jpg"
base64_image = encode_image(image_path)

url = "http://localhost:8080/v1/chat/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {YOUR_GOOGLE_AI_STUDIO_API_KEY}"
}
data = {
    "model": "gpt-4-vision-preview",
    "messages": [{
        "role": "user",
        "content": [
            {"type": "text", "text": "What’s in this image?"},
            {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
        ]
    }],
    "temperature": 0.7
}

response = requests.post(url, json=data, headers=headers)
print(response.text)

結果

入力画像

応答

{"id":"chatcmpl-7f17b8f5dc8f4d36aee9dad8935b0d25","object":"chat.completion","created":1707785494,"model":"gemini-pro","choices":[{"index":0,"message":{"role":"assistant","content":" The image contains a plate of mochi, a Japanese rice cake, dusted with kinako, a roasted soybean flour."},"finish_reason":"stop"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0},"system_fingerprint":""}


この画像には、きなこをまぶした餅の皿が含まれています。

複数のローカル画像の実行

複数のローカル画像を使用して、画像認識を試す別のデモを見てみましょう。

ステップ1からステップ3: 上記と同様

複数の画像に対しても、同じ初期ステップを踏みます。

ステップ4: 複数の画像データを含むAPIリクエストを設定して送信

image_paths = [f"data/{filename}" for filename in os.listdir("data") if filename.endswith(".jpg")]

content = [{"type": "text", "text": "What’s in this image?"}]
for image_path in image_paths:
    base64_image = encode_image(image_path)
    content.append({
        "type": "image_url",
        "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
    })

data = {
    "model": "gpt-4-vision-preview",
    "messages": [{"role": "user", "content": content}],
    "temperature": 0.7
}

response = requests.post(url, json=data, headers=headers)
print(response.text)

結果

入力画像



応答

(gemini-openai-proxy) E:\Prj\gemini-openai-proxy>python demo\demo_vision_local_Multiple_img.py
{"id":"chatcmpl-bcb4a636540040c88375b3bb75cf9ff8","object":"chat.completion","created":1707786129,"model":"gemini-pro","choices":[{"index":0,"message":{"role":"assistant","content":" The first image is of a surreal landscape with floating rocks, waterfalls, and a starry sky. The second image is of a steampunk owl with half of its face made of machinery. The third image is of a plate of mochi, a Japanese dessert made of pounded sticky rice."},"finish_reason":"stop"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0},"system_fingerprint":""}


最初の画像は、岩が浮かび、滝が流れ、星空が広がる超現実的な風景。2枚目は、顔の半分が機械でできたスチームパンクのフクロウ。三枚目の画像は、もち米で作られた日本のデザートである餅の皿。

まとめ

この記事を通じて、Gemini-OpenAI-Proxy を使用して、簡単なステップでローカル画像の画像認識を行う方法を学びました。このデモは、OpenAIとGoogleのAPIを組み合わせた強力な画像認識機能を実現するための良い出発点となります。

リポジトリ

GitHub - Sunwood-ai-labs/gemini-openai-proxy: A proxy for converting the OpenAI API protocol to the Google Gemini Pro protocol.
A proxy for converting the OpenAI API protocol to the Google Gemini Pro protocol. - GitHub - Sunwood-ai-labs/gemini-openai-proxy: A proxy for converting the Ope...

コメント

タイトルとURLをコピーしました