Open2

Gemini 2.0 API メモ

yuheitomiyuheitomi

先日リリースされた推論モデル (gemini-2.0-flash-thinking-exp-1219) について、推論パート (thought part) と回答パート (answer) それぞれを分けて取得する方法。

response.candidates[0].content.parts[0] に推論パート、parts[1] に回答が入ってくる。

import { GoogleGenerativeAI } from "@google/generative-ai";

if (process.env.GOOGLE_GENERATIVE_AI_API_KEY === undefined) {
  throw new Error("GOOGLE_GENERATIVE_AI_API_KEY is not set");
}

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GENERATIVE_AI_API_KEY);
const modelName = "gemini-2.0-flash-thinking-exp-1219";
const model = genAI.getGenerativeModel({ model: modelName });

const generateTest = async () => {
  const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash-exp" });
  const result = await model.generateContent("Find a prime number between 1 and 100");

  const thinkingPart = result.response.candidates?.at(0)?.content.parts[0].text;
  const outputPart = result.response.candidates?.at(0)?.content.parts[1].text;

  console.log(`## Thoughts:\n${thinkingPart}`);
  console.log("\n--------------------\n");
  console.log(`## Answer:\n${outputPart}`);
};

generateTest();