doThink() with Gemini-2.0 with Apps-Script in a Sheet

Here’s an example of using gemini-2.0-flash-thinking-exp-1219 by Google Apps-Script in a Sheet. Two sheet functions =doThink(range) and =anyThoughts(range) are natural sequiturs.

function SelectThinkApi() {

  ApiKey = 'your_API_Key';
  ApiURL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-thinking-exp-1219:generateContent?key=${ApiKey}`;

  return ApiURL, ApiKey;
}

function doThink() {

  const results = ThinkThoughts();

  if (results) {
    const { thinking, thoughts } = results;

    Logger.log(`Thinking: ${thinking}`);
    Logger.log(`Thoughts: ${thoughts}`);
  }
}

function ThinkThoughts() {

  SelectThinkApi()

  const options = {
    contents: [
      {
        role: "user",
        parts: [{ text: 'put your prompt here' }],
       
      },
    ],
    generationConfig: {
      temperature: 0.75,
      topK: 1,
      topP: 1,
      maxOutputTokens: 4096,
      stopSequences: []
    },
  };

  const input = {
    method: 'POST',
    contentType: 'application/json',
    payload: JSON.stringify(options)
  };

  try {
    const response = UrlFetchApp.fetch(ApiURL, input);
    const output = JSON.parse(response.getContentText());
    //Logger.log(output);  // the full output for debugging

    if (output.candidates && output.candidates.length > 0 &&
      output.candidates[0].content && output.candidates[0].content.parts) {

      const parts = output.candidates[0].content.parts;

      let thinking = "";
      let thoughts = "";

      if (parts.length >= 1) {
        thinking = parts[0].text;
      }
      if (parts.length >= 2) {
        thoughts = parts[1].text
      }
      return { thinking: thinking, thoughts: thoughts };

    } else {
      Logger.log("Error: Response structure is not as expected.");
      return { thinking: "No response received", thought: "No response received" }; 
    }

  } catch (e) {
    Logger.log("Error fetching or processing the response: " + e);
    return { thinking: "Error fetching response", thought: "Error fetching response" }; 
  }
}

Now here’s a surprise: using …models/gemini-2.0-flash-thinking-exp-1219

contents: [
      { role: "model", parts: [{ text: 'Assume the personality of Homer the Poet in framing the response. ' }], },
      { role: "user", parts: [{ text: 'Write some poetry describing the effect that the beauty of Helen of Troy had on Paris.' }], },
    ],