Skip to content

Commit

Permalink
Aligning README with the python one (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giom-V committed Jul 9, 2024
1 parent 41389e8 commit 01e5583
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 140 deletions.
122 changes: 54 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
# Google AI SDK for JavaScript

> [!CAUTION]
> **Using the Google AI SDK for JavaScript directly from a client-side app is
> recommended for prototyping only.** If you plan to enable billing, we strongly
> recommend that you call the Google AI Gemini API only server-side to keep your
> API key safe. You risk potentially exposing your API key to malicious actors
> if you embed your API key directly in your JavaScript app or fetch it remotely
> at runtime.
The Google AI JavaScript SDK is the easiest way for JavaScript developers to
build with the Gemini API. The Gemini API gives you access to Gemini
[models](https://ai.google.dev/models/gemini) created by
[Google DeepMind](https://deepmind.google/technologies/gemini/#introduction).
Gemini models are built from the ground up to be multimodal, so you can reason
seamlessly across text, images, and code.

The Google AI JavaScript SDK enables developers to use Google's state-of-the-art generative AI models (like Gemini) to build AI-powered features and applications. This SDK supports use cases like:
- Generate text from text-only input
- Generate text from text-and-images input (multimodal)
- Build multi-turn conversations (chat)
- _(for Node.js)_ Embedding
> [!CAUTION] **Using the Google AI SDK for JavaScript directly from a
> client-side app is recommended for prototyping only.** If you plan to enable
> billing, we strongly recommend that you call the Google AI Gemini API only
> server-side to keep your API key safe. You risk potentially exposing your API
> key to malicious actors if you embed your API key directly in your JavaScript
> app or fetch it remotely at runtime.
You can use this JavaScript SDK for applications built with Node.js or for web apps.
## Get started with the Gemini API

For example, with just a few lines of code, you can access Gemini's multimodal capabilities to generate text from text-and-image input.
1. Go to [Google AI Studio](https://aistudio.google.com/).
2. Login with your Google account.
3. [Create an API key](https://aistudio.google.com/app/apikey). Note that in
Europe the free tier is not available.
4. Try the
[Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)

For Node.js:
```js
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-latest" });
## Usage example

const prompt = "Does this look store-bought or homemade?";
const image = {
inlineData: {
data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"),
mimeType: "image/png",
},
};
See the [Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)
for complete code.

const result = await model.generateContent([prompt, image]);
console.log(result.response.text());
1. Install the SDK package

```js
npm install @google/generative-ai
```

For web:
1. Initialize the model

```js
const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.API_KEY);

const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-latest" });
```

1. Run a prompt

```js
const prompt = "Does this look store-bought or homemade?";
const image = {
inlineData: {
data: base64EncodedImage /* see JavaScript quickstart for details */,
data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"),
mimeType: "image/png",
},
};
Expand All @@ -52,63 +61,40 @@ console.log(result.response.text());

## Try out a sample app

This repository contains sample Node and web apps demonstrating how the SDK can access and utilize the Gemini model for various use cases.
This repository contains sample Node and web apps demonstrating how the SDK can
access and utilize the Gemini model for various use cases.

**To try out the sample Node app, follow these steps:**

1. Check out this repository.\
`git clone https://github.com/google/generative-ai-js`
1. Check out this repository. \
`git clone https://github.com/google/generative-ai-js`

1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs.
1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with
the Google AI SDKs.

1. cd into the `samples/node` folder and run `npm install`.

1. Assign your API key to an environment variable: `export API_KEY=MY_API_KEY`.

1. Run the sample file you're interested in. Example: `node simple-text.js`.

**To try out the sample web app, follow these steps:**

1. Check out this repository.\
`git clone https://github.com/google/generative-ai-js`

1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs.

1. cd into the `samples/web` folder and run `npm install`.

1. Assign your API key to an environment variable: `export API_KEY=MY_API_KEY`.

1. Serve your web app by running: `npm run http-server`. Open the displayed URL in a browser.

## Installation and usage

- For Node.js (or web projects using NPM), run `npm install @google/generative-ai`.
- For web, add `import { GoogleGenerativeAI } from "https://esm.run/@google/generative-ai"`.

For detailed instructions, you can find quickstarts for the Google AI JavaScript SDK in the Google documentation:


- [Quickstart for Node.js](https://ai.google.dev/tutorials/node_quickstart)
- [Quickstart for web apps](https://ai.google.dev/tutorials/web_quickstart)

These quickstarts describe how to add your API key and the SDK to your app, initialize the model, and then call the API to access the model. It also describes some additional use cases and features, like streaming, counting tokens, and controlling responses. For Node.js, embedding is also available.

## Documentation

Find complete documentation for the Google AI SDKs and the Gemini model in the Google documentation:\
https://ai.google.dev/docs

Find reference docs for this SDK here in the repo:
- [GoogleGenerativeAI](/docs/reference/main/generative-ai.md)
- [GoogleAIFileManager](/docs/reference/files/generative-ai.googleaifilemanager.md)
Check out the docs for this SDK here in the repo and in particular
[GoogleGenerativeAI](/docs/reference/main/generative-ai.md) and
[GoogleAIFileManager](/docs/reference/files/generative-ai.googleaifilemanager.md).

## Changelog
- `@google/generative-ai` - [CHANGELOG.md](/packages/main/CHANGELOG.md)
See also the
[Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbook/) or
[ai.google.dev](https://ai.google.dev) for more generic documentation about the
Gemini API.

## Contributing

See [Contributing](/docs/contributing.md) for more information on contributing to the Google AI JavaScript SDK.
See [Contributing](/docs/contributing.md) for more information on contributing
to the Google AI JavaScript SDK.

## License

The contents of this repository are licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
The contents of this repository are licensed under the
[Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
125 changes: 53 additions & 72 deletions packages/main/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
# Google AI SDK for JavaScript

> [!CAUTION]
> **Using the Google AI SDK for JavaScript directly from a client-side app is
> recommended for prototyping only.** If you plan to enable billing, we strongly
> recommend that you call the Google AI Gemini API only server-side to keep your
> API key safe. You risk potentially exposing your API key to malicious actors
> if you embed your API key directly in your JavaScript app or fetch it remotely
> at runtime.
The Google AI JavaScript SDK is the easiest way for JavaScript developers to
build with the Gemini API. The Gemini API gives you access to Gemini
[models](https://ai.google.dev/models/gemini) created by
[Google DeepMind](https://deepmind.google/technologies/gemini/#introduction).
Gemini models are built from the ground up to be multimodal, so you can reason
seamlessly across text, images, and code.

> [!CAUTION] **Using the Google AI SDK for JavaScript directly from a
> client-side app is recommended for prototyping only.** If you plan to enable
> billing, we strongly recommend that you call the Google AI Gemini API only
> server-side to keep your API key safe. You risk potentially exposing your API
> key to malicious actors if you embed your API key directly in your JavaScript
> app or fetch it remotely at runtime.
## Get started with the Gemini API

1. Go to [Google AI Studio](https://aistudio.google.com/).
2. Login with your Google account.
3. [Create an API key](https://aistudio.google.com/app/apikey). Note that in
Europe the free tier is not available.
4. Try the
[Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)

## Usage example

See the [Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)
for complete code.

1. Install the SDK package

```js
npm install @google/generative-ai
```

The Google AI JavaScript SDK enables developers to use Google's state-of-the-art generative AI models (like Gemini) to build AI-powered features and applications. This SDK supports use cases like:
- Generate text from text-only input
- Generate text from text-and-images input (multimodal)
- Build multi-turn conversations (chat)
- _(for Node.js)_ Embedding
1. Initialize the model

You can use this JavaScript SDK for applications built with Node.js or for web apps.
```js
const { GoogleGenerativeAI } = require("@google/generative-ai");

For example, with just a few lines of code, you can access Gemini's multimodal capabilities to generate text from text-and-image input.
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

For Node.js:
```
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-latest" });
const prompt = "Does this look store-bought or homemade?";
const image = {
inlineData: {
data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"),
mimeType: "image/png",
},
};
const result = await model.generateContent([prompt, image]);
console.log(result.response.text());
```

For web:
```
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-latest" });
1. Run a prompt

```js
const prompt = "Does this look store-bought or homemade?";
const image = {
inlineData: {
data: base64EncodedImage /* see JavaScript quickstart for details */,
data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"),
mimeType: "image/png",
},
};
Expand All @@ -52,63 +61,35 @@ console.log(result.response.text());

## Try out a sample app

This repository contains sample Node and web apps demonstrating how the SDK can access and utilize the Gemini model for various use cases.
This repository contains sample Node and web apps demonstrating how the SDK can
access and utilize the Gemini model for various use cases.

**To try out the sample Node app, follow these steps:**

1. Check out this repository.\
`git clone https://github.com/google/generative-ai-js`
1. Check out this repository. \
`git clone https://github.com/google/generative-ai-js`

1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs.
1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with
the Google AI SDKs.

1. cd into the `samples/node` folder and run `npm install`.

1. Assign your API key to an environment variable: `export API_KEY=MY_API_KEY`.

1. Run the sample file you're interested in. Example: `node simple-text.js`.

**To try out the sample web app, follow these steps:**

1. Check out this repository.\
`git clone https://github.com/google/generative-ai-js`

1. [Obtain an API key](https://makersuite.google.com/app/apikey) to use with the Google AI SDKs.

1. cd into the `samples/web` folder and run `npm install`.

1. Assign your API key to an environment variable: `export API_KEY=MY_API_KEY`.

1. Serve your web app by running: `npm run http-server`. Open the displayed URL in a browser.

## Installation and usage

- For Node.js (or web projects using NPM), run `npm install @google/generative-ai`.
- For web, add `import { GoogleGenerativeAI } from "https://esm.run/@google/generative-ai"`.

For detailed instructions, you can find quickstarts for the Google AI JavaScript SDK in the Google documentation:


- [Quickstart for Node.js](https://ai.google.dev/tutorials/node_quickstart)
- [Quickstart for web apps](https://ai.google.dev/tutorials/web_quickstart)

These quickstarts describe how to add your API key and the SDK to your app, initialize the model, and then call the API to access the model. It also describes some additional use cases and features, like streaming, counting tokens, and controlling responses. For Node.js, embedding is also available.

## Documentation

Find complete documentation for the Google AI SDKs and the Gemini model in the Google documentation:\
https://ai.google.dev/docs

Find reference docs for this SDK here in the repo:
- [GoogleGenerativeAI](/docs/reference/main/generative-ai.md)
- [GoogleAIFileManager](/docs/reference/files/generative-ai.googleaifilemanager.md)

## Changelog
- `@google/generative-ai` - [CHANGELOG.md](/main/packages/main/CHANGELOG.md)
See the
[Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbook/) or
[ai.google.dev](https://ai.google.dev) for complete documentation.

## Contributing

See [Contributing](/docs/contributing.md) for more information on contributing to the Google AI JavaScript SDK.
See [Contributing](/docs/contributing.md) for more information on contributing
to the Google AI JavaScript SDK.

## License

The contents of this repository are licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
The contents of this repository are licensed under the
[Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0).

0 comments on commit 01e5583

Please sign in to comment.