3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gemini API の実験的機能「URL context」を Node.js で試す

Posted at

はじめに

この記事は、以下の記事で扱っていた Gemini API の新機能や実験的機能などに関する話です。

今回試す内容(Gemini API の URL context)

今回は、実験的機能として提供されている以下の「URL context」を試します。

https://ai.google.dev/gemini-api/docs/url-context
image.png

さっそく試す

さっそく試していきます。今回は、以下の「Code examples with URL context only」という項目のサンプルを使います。

image.png

https://ai.google.dev/gemini-api/docs/url-context#code-examples-url-context
image.png

Python などのサンプルもありますが、JavaScript のサンプルでやってみます。

下準備

この後の内容を進める前の下準備として、「パッケージのインストール」「APIキーの設定」を行います。

パッケージは @google/genai を以下のコマンドでインストールします。

npm i @google/genai

また APIキーの設定については、環境変数 GEMINI_API_KEY に Gemini の APIキーをセットします。

コードと実行結果

以下に、お試し用に使ったコードを示します。

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const urls = [
  "https://qiita.com/youtoy/items/85f35c69bd8e12a2ddbe",
  "https://qiita.com/youtoy/items/433b567bb84672b264c7",
];

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-preview-05-20",
    contents: [
      `${urls[0]}${urls[1]} の内容を比較して、共通点と違いをそれぞれ短く説明してください。`,
    ],
    config: {
      tools: [{ urlContext: {} }],
    },
  });
  console.log(response.text);
  console.log(response.candidates[0].urlContextMetadata);
}

await main();

冒頭に掲載していた Qiitaの記事(過去に書いた複数の記事)の中で、2つの記事を選んでそれらを比較してもらいました。

実行結果

上記を実行した結果は、以下のとおりです。

image.png

2つの記事の内容を読み込んだ上で、それらの内容の共通点・差異を見つけ出し、さらに返答として示してくれていることを確認できました。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?