#Part | @google/genai

1 messages · Page 1 of 1 (latest)

white scaffold
#

@pine slate How did you know that it would be an array of array?

#

And why didn't we chose ContentUnion[]?

pine slate
#

We did, that's how we knew it would be an array of array.

Think of expanding it like this:

  • ContentListUnion
  • ContentUnion[]
  • PartUnion[][]
  • Part[][]
white scaffold
#
const GEMINI_API_KEY = "";

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

async function main() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: [[{
      text: "How many sides this shape have?"
    }],
    [{
      fileData: {
        fileUri: "./triangle.jpg"
      }
    }]
  ]
  })
  console.log(response.text)
}

main();
#

@pine slate I tried this, but didn't work

pine slate
white scaffold
#

INVALID_ARGUMENT

pine slate
#

https://ai.google.dev/gemini-api/docs/vision?lang=node#local-images uses the old library, but the concepts (and some of the syntax) is the same.

If you're using a URL, then you need to use a URL provided by the File API or other supported locations. A local file isn't one of them.

That page shows how to make a base64 encoded version of the image and provide it using inlineData

Google AI for Developers

Get started building with Gemini's multimodal capabilities in the Gemini API

white scaffold
#

@pine slate It worked finally. Thanks for the help. But still I am confused with the hierarchy. I want to understand it.

#

How to go into the hierarchy for our usecase.

pine slate
#

What do you mean by "go into the hierarchy"?
And you saw that all those types are clickable to take you to the type definitions, right?

white scaffold
#

So many clickable links to reach root.

pine slate
#

Most of the time you don't need to go that deep. But that data structure is complex.

white scaffold
pine slate
#

It is using TypeScript type notation, so that was part of the definition it was showing.

white scaffold
#

Okay

white scaffold
#
import {GoogleGenAI} from '@google/genai';
const GEMINI_API_KEY = "";
import fs from "fs";

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

function fileToGenerativePart(path, mimeType) {
  return {
    inlineData: {
      data: Buffer.from(fs.readFileSync(path)).toString("base64"),
      mimeType
    },
  };
}

async function main() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: [
      [
        {
          text: "Look at this sequence of three shapes. What shape should come as the fourth shape? Explain your reasoning with detailed descriptions of the first shapes."
        }
      ],
      [
        fileToGenerativePart("./triangle.png", "image/png"),
      ],
      [
        fileToGenerativePart("./square.png", "image/png"),
      ],
      [
        fileToGenerativePart("./pentagon.png", "image/png"),
      ]
    ]
  })
  console.log(response.text);
}

main();

// Output: 

/* 
The sequence of shapes is a triangle, a square, and a pentagon.

*   The first shape is a **triangle**. It has three sides and three angles.
*   The second shape is a **square**. It has four sides and four angles.
*   The third shape is a **pentagon**. It has five sides and five angles.

The number of sides is increasing by one with each new shape in the sequence. Therefore, the fourth shape should have six sides. A shape with six sides is called a **hexagon**.
*/
#

@pine slate Finally understood. Thanks for the help.

white scaffold
#

@pine slate @radiant magnet I was thinking to migrate the existing
Python examples in Gemini cookbook to JS. As it would also give me some hands on experience. Should I migrate them in my own repo, or is there a one where I could open a PR.

pine slate
pine slate
white scaffold
white scaffold
#

Yes I have signed the CLA already

#

@pine slate if you can just confirm me.

#

I think now is the time to migrate, because new version has also been released.

pine slate
white scaffold
#

@radiant magnet Any idea?

radiant magnet
#

feel free to send us a PR for the cookbook with one example to start with (one at a time is always good - it makes review faster and we can set expectations about what's needed with a small scope)

#

and keep in mind that with gsoc we have way more PRs coming in than usual, so things will take time to process. this is new for the cookbook too, so it means we need to set up the standards, CI infra, etc for JS code, which takes more time & care. and we have a backlog of PRs related to open issues that will take priority. despite all of that, it's awesome to see and we are immensely thankful for the contribution gemini_heart

white scaffold
#

@radiant magnet

radiant magnet
#

sorry nope - the team wants to keep that repo specific to things that go in the docs, so will be very much on-topic dedicated samples only.

white scaffold
#

Sure, are there some things to keep in mind while writing code? I was thinking to share code here only for you to review, when its done I can create a PR, because if I create a PR there would be everyone doing the same thing hence creating a lot of backlogs PR's.

#

@radiant magnet

white scaffold
#

What do you think about it @radiant magnet

radiant magnet
white scaffold
#

@radiant magnet Can you please review the code of the two examples which I migrated from Python to JavaScript. Are they matching the standards?

white scaffold
#

@radiant magnet Did you look at it?