Skip to content

ClasCat® AI Research

クラスキャット – 生成 AI, AI エージェント, MCP

Menu
  • ホーム
    • ClassCat® AI Research ホーム
    • クラスキャット・ホーム
  • OpenAI API
    • OpenAI Python ライブラリ 1.x : 概要
    • OpenAI ブログ
      • GPT の紹介
      • GPT ストアの紹介
      • ChatGPT Team の紹介
    • OpenAI platform 1.x
      • Get Started : イントロダクション
      • Get Started : クイックスタート (Python)
      • Get Started : クイックスタート (Node.js)
      • Get Started : モデル
      • 機能 : 埋め込み
      • 機能 : 埋め込み (ユースケース)
      • ChatGPT : アクション – イントロダクション
      • ChatGPT : アクション – Getting started
      • ChatGPT : アクション – アクション認証
    • OpenAI ヘルプ : ChatGPT
      • ChatGPTとは何ですか?
      • ChatGPT は真実を語っていますか?
      • GPT の作成
      • GPT FAQ
      • GPT vs アシスタント
      • GPT ビルダー
    • OpenAI ヘルプ : ChatGPT > メモリ
      • FAQ
    • OpenAI ヘルプ : GPT ストア
      • 貴方の GPT をフィーチャーする
    • OpenAI Python ライブラリ 0.27 : 概要
    • OpenAI platform
      • Get Started : イントロダクション
      • Get Started : クイックスタート
      • Get Started : モデル
      • ガイド : GPT モデル
      • ガイド : 画像生成 (DALL·E)
      • ガイド : GPT-3.5 Turbo 対応 微調整
      • ガイド : 微調整 1.イントロダクション
      • ガイド : 微調整 2. データセットの準備 / ケーススタディ
      • ガイド : 埋め込み
      • ガイド : 音声テキスト変換
      • ガイド : モデレーション
      • ChatGPT プラグイン : イントロダクション
    • OpenAI Cookbook
      • 概要
      • API 使用方法 : レート制限の操作
      • API 使用方法 : tiktoken でトークンを数える方法
      • GPT : ChatGPT モデルへの入力をフォーマットする方法
      • GPT : 補完をストリームする方法
      • GPT : 大規模言語モデルを扱う方法
      • 埋め込み : 埋め込みの取得
      • GPT-3 の微調整 : 分類サンプルの微調整
      • DALL-E : DALL·E で 画像を生成して編集する方法
      • DALL·E と Segment Anything で動的マスクを作成する方法
      • Whisper プロンプティング・ガイド
  • Gemini API
    • Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成
    • (2) マルチモーダル入力 / 日本語チャット
    • (3) 埋め込みの使用
    • (4) 高度なユースケース
    • クイックスタート with Node.js
    • クイックスタート with Dart or Flutter (1) 日本語動作確認
    • Gemma
      • 概要 (README)
      • Tutorials : サンプリング
      • Tutorials : KerasNLP による Getting Started
  • Keras 3
    • 新しいマルチバックエンド Keras
    • Keras 3 について
    • Getting Started : エンジニアのための Keras 入門
    • Google Colab 上のインストールと Stable Diffusion デモ
    • コンピュータビジョン – ゼロからの画像分類
    • コンピュータビジョン – 単純な MNIST convnet
    • コンピュータビジョン – EfficientNet を使用した微調整による画像分類
    • コンピュータビジョン – Vision Transformer による画像分類
    • コンピュータビジョン – 最新の MLPモデルによる画像分類
    • コンピュータビジョン – コンパクトな畳込み Transformer
    • Keras Core
      • Keras Core 0.1
        • 新しいマルチバックエンド Keras (README)
        • Keras for TensorFlow, JAX, & PyTorch
        • 開発者ガイド : Getting started with Keras Core
        • 開発者ガイド : 関数型 API
        • 開発者ガイド : シーケンシャル・モデル
        • 開発者ガイド : サブクラス化で新しい層とモデルを作成する
        • 開発者ガイド : 独自のコールバックを書く
      • Keras Core 0.1.1 & 0.1.2 : リリースノート
      • 開発者ガイド
      • Code examples
      • Keras Stable Diffusion
        • 概要
        • 基本的な使い方 (テキスト-to-画像 / 画像-to-画像変換)
        • 混合精度のパフォーマンス
        • インペインティングの簡易アプリケーション
        • (参考) KerasCV – Stable Diffusion を使用した高性能画像生成
  • TensorFlow
    • TF 2 : 初級チュートリアル
    • TF 2 : 上級チュートリアル
    • TF 2 : ガイド
    • TF 1 : チュートリアル
    • TF 1 : ガイド
  • その他
    • 🦜️🔗 LangChain ドキュメント / ユースケース
    • Stable Diffusion WebUI
      • Google Colab で Stable Diffusion WebUI 入門
      • HuggingFace モデル / VAE の導入
      • LoRA の利用
    • Diffusion Models / 拡散モデル
  • クラスキャット
    • 会社案内
    • お問合せ
    • Facebook
    • ClassCat® Blog
Menu

Gemini API : Tutorials : クイックスタート with Python (2) マルチモーダル入力 / 日本語チャット

Posted on 03/13/202403/13/2024 by Sales Information

Gemini API : Tutorials : クイックスタート with Python (2) マルチモーダル入力 / 日本語チャット

翻訳 : クラスキャット セールスインフォメーション
作成日時 : 03/13/2024

* 本ページは、ai.google.dev の以下のページを参考にしてまとめ直し、適宜、補足説明したものです :

  • Gemini API: Quickstart with Python

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

クラスキャット 人工知能 研究開発支援サービス

◆ クラスキャット は人工知能・テレワークに関する各種サービスを提供しています。お気軽にご相談ください :

ClassCat Chatbot

  • 人工知能研究開発支援
    1. 人工知能研修サービス(経営者層向けオンサイト研修)
    2. テクニカルコンサルティングサービス
    3. 実証実験(プロトタイプ構築)
    4. アプリケーションへの実装

  • 人工知能研修サービス

  • PoC(概念実証)を失敗させないための支援
◆ 人工知能とビジネスをテーマに WEB セミナーを定期的に開催しています。スケジュール。
  • お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。

◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。

  • クラスキャット セールス・マーケティング本部 セールス・インフォメーション
  • sales-info@classcat.com  ;  Website: www.classcat.com  ;   ClassCatJP

 

Gemini API : Tutorials : クイックスタート with Python (2) マルチモーダル入力 / 日本語チャット

このドキュメントは以下の記事の続編です :

  • Gemini API : Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成

 

画像とテキスト入力からのテキスト生成

Gemini はマルチモーダル・モデル (gemini-pro-vision) を提供します、これは入力としてテキストと画像の両方を受け取ります。GenerativeModel.generate_content API はマルチモーダルなプロンプトを扱うように設計されていて、テキスト出力を返します。

画像を含めてみましょう :

curl -o image.jpg https://t0.gstatic.com/licensed-image?q=tbn:ANd9GcQ_Kevbk21QBRy-PgB4kQpS79brbmmEG7m3VOTShAn4PecDU5H5UxrJxE3Dw1JiaG17V88QIol19-3TM2wCHw
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  405k  100  405k    0     0  6982k      0 --:--:-- --:--:-- --:--:-- 7106k
import PIL.Image

img = PIL.Image.open('image.jpg')
img

 
gemini-pro-vision model モデルを使用して generate_content で画像をモデルに渡します。

model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(img)

to_markdown(response.text)
These are meal prep containers with chicken, brown rice, broccoli, and bell peppers.

 
プロンプトでテキストと画像の両方を供給するには、文字列と画像を含むリストを渡します :

response = model.generate_content(["Write a short, engaging blog post based on this picture. It should include a description of the meal in the photo and talk about my journey meal prepping.", img], stream=True)
response.resolve()
to_markdown(response.text)
Meal prepping is a great way to save time and money, and it can also help you to eat healthier. This meal is a great example of a healthy and delicious meal that can be easily prepped ahead of time.

This meal features brown rice, roasted vegetables, and chicken teriyaki. The brown rice is a whole grain that is high in fiber and nutrients. The roasted vegetables are a great way to get your daily dose of vitamins and minerals. And the chicken teriyaki is a lean protein source that is also packed with flavor.

This meal is easy to prepare ahead of time. Simply cook the brown rice, roast the vegetables, and cook the chicken teriyaki. Then, divide the meal into individual containers and store them in the refrigerator. When you're ready to eat, simply grab a container and heat it up.

This meal is a great option for busy people who are looking for a healthy and delicious way to eat. It's also a great meal for those who are trying to lose weight or maintain a healthy weight.

If you're looking for a healthy and delicious meal that can be easily prepped ahead of time, this meal is a great option. Give it a try today!

日本語でも試してみましょう :

response2 = model.generate_content(["この写真に基づいて、短い魅了的なブログ記事を書いてください。それは写真の食事の説明を含み、私の旅行の食事準備について話す必要があります。", img])

to_markdown(response2.text)
私はいつも旅行の際に食事の用意をするようにしています。そうすれば、どこに行っても健康的な食事をとることができます。この写真は、私が最近作ったお弁当です。玄米、鶏肉、ブロッコリー、ニンジンが入っています。鶏肉は、醤油、みりん、酒、砂糖で作ったタレに漬け込んで焼きました。ブロッコリーとニンジンは、塩とコショウで炒めました。玄米は、炊飯器で炊きました。このお弁当は、タンパク質、炭水化物、野菜がバランスよく含まれており、とてもヘルシーです。また、持ち運びにも便利です。

旅行の際に食事の用意をすることは、とてもおすすめです。そうすれば、どこに行っても健康的な食事をとることができます。また、お金を節約することもできます。外食は、レストランで食べるよりもずっと安上がりです。

旅行の際に食事の用意をするためのコツをいくつかご紹介します。
  • 事前に計画を立てましょう。何を食べたいか、どのくらいの量を持っていくかを決めておきましょう。
  • 食材は、日持ちするものを選びましょう。生鮮食品は、傷みやすいので、気をつけましょう。
  • 調味料は、小分けにして持っていきましょう。そうすれば、荷物を減らすことができます。
  • 容器は、密閉できるものを選びましょう。そうすれば、食品が傷みにくくなります。
旅行の際に食事の用意をすることは、少しの手間がかかりますが、とてもおすすめです。そうすれば、どこに行っても健康的な食事をとることができます。また、お金を節約することもできます。

 

チャット会話

Gemini は複数のターンにわたりフリー形式の会話をすることを可能にします。ChatSession クラスは会話の状態を管理することによりプロセスを単純化しますので、generate_content とは異なり、会話履歴をリストとしてストアする必要がありません。

チャットを初期化します :

model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
chat
ChatSession(
    model=genai.GenerativeModel(
        model_name='models/gemini-pro',
        generation_config={},
        safety_settings={},
        tools=None,
    ),
    history=[]
)

⭐️ Note : ビジョンモデル gemini-pro-vision はマルチターン・チャット用に最適化されていません。

ChatSession.send_message メソッドは GenerativeModel.generate_content と同じ GenerateContentResponse タイプを返します。それはまたメッセージと応答をチャット履歴に追加します。

response = chat.send_message("In one sentence, explain how a computer works to a young child.")
to_markdown(response.text)
A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!
chat.history
[parts {
   text: "In one sentence, explain how a computer works to a young child."
 }
 role: "user",
 parts {
   text: "A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!"
 }
 role: "model"]

メッセージを送信し続けて会話を続けることができます。チャットをストリームするには stream=True 引数を使用します :

response = chat.send_message("Okay, how about a more detailed explanation to a high schooler?", stream=True)

for chunk in response:
  print(chunk.text)
  print("_"*80)
A computer works by following instructions, called a program, which tells it what to
________________________________________________________________________________
 do. These instructions are written in a special language that the computer can understand, and they are stored in the computer's memory. The computer's processor
________________________________________________________________________________
, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program's logic. The results of these calculations and decisions are then displayed on the computer's screen or stored in memory for later use.

To give you a simple analogy, imagine a computer as a
________________________________________________________________________________
 chef following a recipe. The recipe is like the program, and the chef's actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).

In summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results.
________________________________________________________________________________

glm.Content オブジェクトは各々がテキスト (文字列) か inline_data (glm.Blob) のいずれかを含む glm.Part クラスのリストを含みます、ここで blob はバイナリデータか mime_type を含みます。チャット履歴は ChatSession.history 内の glm.Content オブジェクトのリストとして利用可能です :

for message in chat.history:
  display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))
user: In one sentence, explain how a computer works to a young child.

model: A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!

user: Okay, how about a more detailed explanation to a high schooler?

model: A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer's memory. The computer's processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program's logic. The results of these calculations and decisions are then displayed on the computer's screen or stored in memory for later use.

To give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef's actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).

In summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results.

 
続いて日本語でも試してみましょう :

chat2 = model.start_chat(history=[])

chat2
ChatSession(
    model=genai.GenerativeModel(
        model_name='models/gemini-pro',
        generation_config={},
        safety_settings={},
        tools=None,
    ),
    history=[]
)
response2 = chat2.send_message("小学生高学年の子どもにコンピュータがどのように動作するか1行で説明してください。")
to_markdown(response2.text)
コンピュータは、電子信号を使って情報を処理し、指示を実行する機械です。
chat2.history
[parts {
   text: "\345\260\217\345\255\246\347\224\237\351\253\230\345\255\246\345\271\264\343\201\256\345\255\220\343\201\251\343\202\202\343\201\253\343\202\263\343\203\263\343\203\224\343\203\245\343\203\274\343\202\277\343\201\214\343\201\251\343\201\256\343\202\210\343\201\206\343\201\253\345\213\225\344\275\234\343\201\231\343\202\213\343\201\2131\350\241\214\343\201\247\350\252\254\346\230\216\343\201\227\343\201\246\343\201\217\343\201\240\343\201\225\343\201\204\343\200\202"
 }
 role: "user",
 parts {
   text: "\343\202\263\343\203\263\343\203\224\343\203\245\343\203\274\343\202\277\343\201\257\343\200\201\351\233\273\345\255\220\344\277\241\345\217\267\343\202\222\344\275\277\343\201\243\343\201\246\346\203\205\345\240\261\343\202\222\345\207\246\347\220\206\343\201\227\343\200\201\346\214\207\347\244\272\343\202\222\345\256\237\350\241\214\343\201\231\343\202\213\346\251\237\346\242\260\343\201\247\343\201\231\343\200\202"
 }
 role: "model"]
chat2.history[0].parts[0].text
小学生高学年の子どもにコンピュータがどのように動作するか1行で説明してください。
chat2.history[1].parts[0].text
コンピュータは、電子信号を使って情報を処理し、指示を実行する機械です。
response2 = chat2.send_message("なるほど、高校生へのもう少し詳しい説明についてはどうでしょう?", stream=True)

for chunk in response2:
  print(chunk.text)
  print("_"*80)
コンピュータは、情報を処理するために電子信号を使用する電子機械です。中央
________________________________________________________________________________
処理装置 (CPU) を中心としたコンピュータの主要コンポーネントは、計算、制御、データ処理のタスクを実行します。CPU は
________________________________________________________________________________
情報を格納するメモリと、情報をデバイスやネットワークに送受信する入出力デバイス (I/O) に接続されています。オペレーティングシステムは、コンピュータのハードウェアとソフトウェアリソースを管理し、ユーザーがコンピュータを操作できるようにします。

コンピュータの動作は、以下
________________________________________________________________________________
のような基本的なステップで行われます。

1. **入力:** ユーザーはキーボード、マウス、またはその他の入力デバイスを使用して、コンピュータにデータをまたは指示を入力します。
2. **処理:** CPU は、オペレーティングシステムの指示に従ってデータに演算を実行します。
3. **出力:** 結果は、モニター、プリンター、またはその他の出力デバイスに表示されます。
4. **記憶:** データと結果は、ハードドライブ、ソリッドステートドライブ、またはその他の記憶デバイスに格納されます。
5. **制御:** オペレーティングシステムは、コンピュータのハードウェア
________________________________________________________________________________
とソフトウェアリソースを管理し、システムが正しく動作することを確認します。

コンピュータは、さまざまなタスクに適用でき、複雑な計算、データ処理、コミュニケーション、娯楽など、現代社会に不可欠なツールとなっています。
________________________________________________________________________________
for message in chat2.history:
  display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))
user: 小学生高学年の子どもにコンピュータがどのように動作するか1行で説明してください。

model: コンピュータは、電子信号を使って情報を処理し、指示を実行する機械です。

user: なるほど、高校生へのもう少し詳しい説明についてはどうでしょう?

model: コンピュータは、情報を処理するために電子信号を使用する電子機械です。中央処理装置 (CPU) を中心としたコンピュータの主要コンポーネントは、計算、制御、データ処理のタスクを実行します。CPU は情報を格納するメモリと、情報をデバイスやネットワークに送受信する入出力デバイス (I/O) に接続されています。オペレーティングシステムは、コンピュータのハードウェアとソフトウェアリソースを管理し、ユーザーがコンピュータを操作できるようにします。

コンピュータの動作は、以下のような基本的なステップで行われます。

入力: ユーザーはキーボード、マウス、またはその他の入力デバイスを使用して、コンピュータにデータをまたは指示を入力します。
処理: CPU は、オペレーティングシステムの指示に従ってデータに演算を実行します。
出力: 結果は、モニター、プリンター、またはその他の出力デバイスに表示されます。
記憶: データと結果は、ハードドライブ、ソリッドステートドライブ、またはその他の記憶デバイスに格納されます。
制御: オペレーティングシステムは、コンピュータのハードウェアとソフトウェアリソースを管理し、システムが正しく動作することを確認します。
コンピュータは、さまざまなタスクに適用でき、複雑な計算、データ処理、コミュニケーション、娯楽など、現代社会に不可欠なツールとなっています。

 

以上



クラスキャット

最近の投稿

  • LangGraph Platform : Get started : クイックスタート
  • LangGraph Platform : 概要
  • LangGraph : Prebuilt エージェント : ユーザインターフェイス
  • LangGraph : Prebuilt エージェント : 配備
  • LangGraph : Prebuilt エージェント : マルチエージェント

タグ

AutoGen (13) ClassCat Press Release (20) ClassCat TF/ONNX Hub (11) DGL 0.5 (14) Eager Execution (7) Edward (17) FLUX.1 (16) Gemini (20) HuggingFace Transformers 4.5 (10) HuggingFace Transformers 4.6 (7) HuggingFace Transformers 4.29 (9) Keras 2 Examples (98) Keras 2 Guide (16) Keras 3 (10) Keras Release Note (17) Kubeflow 1.0 (10) LangChain (45) LangGraph (20) MediaPipe 0.8 (11) Model Context Protocol (16) NNI 1.5 (16) OpenAI Agents SDK (8) OpenAI Cookbook (13) OpenAI platform (10) OpenAI platform 1.x (10) OpenAI ヘルプ (8) TensorFlow 2.0 Advanced Tutorials (33) TensorFlow 2.0 Advanced Tutorials (Alpha) (15) TensorFlow 2.0 Advanced Tutorials (Beta) (16) TensorFlow 2.0 Guide (10) TensorFlow 2.0 Guide (Alpha) (16) TensorFlow 2.0 Guide (Beta) (9) TensorFlow 2.0 Release Note (12) TensorFlow 2.0 Tutorials (20) TensorFlow 2.0 Tutorials (Alpha) (14) TensorFlow 2.0 Tutorials (Beta) (12) TensorFlow 2.4 Guide (24) TensorFlow Deploy (8) TensorFlow Get Started (7) TensorFlow Graphics (7) TensorFlow Probability (9) TensorFlow Programmer's Guide (22) TensorFlow Release Note (18) TensorFlow Tutorials (33) TF-Agents 0.4 (11)
2024年3月
月 火 水 木 金 土 日
 123
45678910
11121314151617
18192021222324
25262728293031
« 2月   4月 »
© 2025 ClasCat® AI Research | Powered by Minimalist Blog WordPress Theme