#Part | @google/genai
1 messages · Page 1 of 1 (latest)
@pine slate How did you know that it would be an array of array?
And why didn't we chose ContentUnion[]?
We did, that's how we knew it would be an array of array.
Think of expanding it like this:
- ContentListUnion
- ContentUnion[]
- PartUnion[][]
- Part[][]
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
What was the error?
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
@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.
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?
Like if I have some use case, how to find right set of parameters to give it.
So many clickable links to reach root.
Most of the time you don't need to go that deep. But that data structure is complex.
Okay, I was also confused a bit that which link to click(i.e what data structure to chose). In the above case, how did find it would be array into array?
It is using TypeScript type notation, so that was part of the definition it was showing.
Okay
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.
@pine slate Can you please review the code
@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.
That looks great! Very well structured code, as well.
The cookbook is in a github repository, so it would make sense to fork that and have a PR against it. Make sure you read the CONTRIBUTING file, since Google has some requirements to contribute your code.
And I think it's a great thing to migrate the examples. Very very good of you.
(I keep thinking to do the same for LangChainJS, but can barely find the time.)
I think someone told that from now here they will be putting the examples.
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.
I don't work for Google, so there isn't much that I can confirm.
@radiant magnet Any idea?
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 
https://github.com/google-gemini/api-examples Should I send it here?
@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.
send it to the cookbook https://github.com/google-gemini/cookbook
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
What do you think about it @radiant magnet
these are great questions 🙂 tbh since JS is a new addition we'll be making the rules up as we go along. if you follow the style and patterns from the api-examples repo you should be fine - things like style, API key storage, etc.
anything standalone should be in its own directory, with its package.json etc, so it's runnable
Contribute to haroon10725/google-gemini-js development by creating an account on GitHub.
@radiant magnet Can you please review the code of the two examples which I migrated from Python to JavaScript. Are they matching the standards?
@radiant magnet Did you look at it?