Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@google/generative-ai

Package Overview
Dependencies
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@google/generative-ai - npm Package Compare versions

Comparing version 0.14.1 to 0.15.0

10

CHANGELOG.md
# @google/generative-ai
## 0.15.0
### Minor Changes
- 05b8631: Add FinishReason.LANGUAGE enum value.
### Patch Changes
- e87d5b0: Fix countTokens to include any params set on the model instance.
## 0.14.1

@@ -4,0 +14,0 @@

1

dist/generative-ai.d.ts

@@ -348,2 +348,3 @@ /**

RECITATION = "RECITATION",
LANGUAGE = "LANGUAGE",
OTHER = "OTHER"

@@ -350,0 +351,0 @@ }

43

dist/index.js

@@ -170,2 +170,4 @@ 'use strict';

FinishReason["RECITATION"] = "RECITATION";
// The candidate content was flagged for using an unsupported language.
FinishReason["LANGUAGE"] = "LANGUAGE";
// Unknown reason.

@@ -285,3 +287,3 @@ FinishReason["OTHER"] = "OTHER";

*/
const PACKAGE_VERSION = "0.14.1";
const PACKAGE_VERSION = "0.15.0";
const PACKAGE_LOG_HEADER = "genai-js";

@@ -546,3 +548,7 @@ var Task;

}
const badFinishReasons = [exports.FinishReason.RECITATION, exports.FinishReason.SAFETY];
const badFinishReasons = [
exports.FinishReason.RECITATION,
exports.FinishReason.SAFETY,
exports.FinishReason.LANGUAGE,
];
function hadBadFinishReason(candidate) {

@@ -902,4 +908,14 @@ return (!!candidate.finishReason &&

}
function formatCountTokensInput(params, model) {
let formattedRequest = {};
function formatCountTokensInput(params, modelParams) {
var _a;
let formattedGenerateContentRequest = {
model: modelParams === null || modelParams === void 0 ? void 0 : modelParams.model,
generationConfig: modelParams === null || modelParams === void 0 ? void 0 : modelParams.generationConfig,
safetySettings: modelParams === null || modelParams === void 0 ? void 0 : modelParams.safetySettings,
tools: modelParams === null || modelParams === void 0 ? void 0 : modelParams.tools,
toolConfig: modelParams === null || modelParams === void 0 ? void 0 : modelParams.toolConfig,
systemInstruction: modelParams === null || modelParams === void 0 ? void 0 : modelParams.systemInstruction,
cachedContent: (_a = modelParams === null || modelParams === void 0 ? void 0 : modelParams.cachedContent) === null || _a === void 0 ? void 0 : _a.name,
contents: [],
};
const containsGenerateContentRequest = params.generateContentRequest != null;

@@ -910,7 +926,6 @@ if (params.contents) {

}
formattedRequest = Object.assign({}, params);
formattedGenerateContentRequest.contents = params.contents;
}
else if (containsGenerateContentRequest) {
formattedRequest = Object.assign({}, params);
formattedRequest.generateContentRequest.model = model;
formattedGenerateContentRequest = Object.assign(Object.assign({}, formattedGenerateContentRequest), params.generateContentRequest);
}

@@ -920,5 +935,5 @@ else {

const content = formatNewContent(params);
formattedRequest.contents = [content];
formattedGenerateContentRequest.contents = [content];
}
return formattedRequest;
return { generateContentRequest: formattedGenerateContentRequest };
}

@@ -1291,3 +1306,11 @@ function formatGenerateContentInput(params) {

async countTokens(request) {
const formattedParams = formatCountTokensInput(request, this.model);
const formattedParams = formatCountTokensInput(request, {
model: this.model,
generationConfig: this.generationConfig,
safetySettings: this.safetySettings,
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
cachedContent: this.cachedContent,
});
return countTokens(this.apiKey, this.model, formattedParams, this.requestOptions);

@@ -1294,0 +1317,0 @@ }

@@ -72,3 +72,3 @@ 'use strict';

*/
const PACKAGE_VERSION = "0.14.1";
const PACKAGE_VERSION = "0.15.0";
const PACKAGE_LOG_HEADER = "genai-js";

@@ -730,2 +730,4 @@ var Task;

FinishReason["RECITATION"] = "RECITATION";
// The candidate content was flagged for using an unsupported language.
FinishReason["LANGUAGE"] = "LANGUAGE";
// Unknown reason.

@@ -732,0 +734,0 @@ FinishReason["OTHER"] = "OTHER";

@@ -17,7 +17,7 @@ /**

*/
import { Content, CountTokensRequest, EmbedContentRequest, GenerateContentRequest, Part, _CountTokensRequestInternal } from "../../types";
import { Content, CountTokensRequest, EmbedContentRequest, GenerateContentRequest, ModelParams, Part, _CountTokensRequestInternal } from "../../types";
export declare function formatSystemInstruction(input?: string | Part | Content): Content | undefined;
export declare function formatNewContent(request: string | Array<string | Part>): Content;
export declare function formatCountTokensInput(params: CountTokensRequest | string | Array<string | Part>, model: string): _CountTokensRequestInternal;
export declare function formatCountTokensInput(params: CountTokensRequest | string | Array<string | Part>, modelParams?: ModelParams): _CountTokensRequestInternal;
export declare function formatGenerateContentInput(params: GenerateContentRequest | string | Array<string | Part>): GenerateContentRequest;
export declare function formatEmbedContentInput(params: EmbedContentRequest | string | Array<string | Part>): EmbedContentRequest;

@@ -74,2 +74,3 @@ /**

RECITATION = "RECITATION",
LANGUAGE = "LANGUAGE",
OTHER = "OTHER"

@@ -76,0 +77,0 @@ }

@@ -17,7 +17,7 @@ /**

*/
import { Content, CountTokensRequest, EmbedContentRequest, GenerateContentRequest, Part, _CountTokensRequestInternal } from "../../types";
import { Content, CountTokensRequest, EmbedContentRequest, GenerateContentRequest, ModelParams, Part, _CountTokensRequestInternal } from "../../types";
export declare function formatSystemInstruction(input?: string | Part | Content): Content | undefined;
export declare function formatNewContent(request: string | Array<string | Part>): Content;
export declare function formatCountTokensInput(params: CountTokensRequest | string | Array<string | Part>, model: string): _CountTokensRequestInternal;
export declare function formatCountTokensInput(params: CountTokensRequest | string | Array<string | Part>, modelParams?: ModelParams): _CountTokensRequestInternal;
export declare function formatGenerateContentInput(params: GenerateContentRequest | string | Array<string | Part>): GenerateContentRequest;
export declare function formatEmbedContentInput(params: EmbedContentRequest | string | Array<string | Part>): EmbedContentRequest;

@@ -74,2 +74,3 @@ /**

RECITATION = "RECITATION",
LANGUAGE = "LANGUAGE",
OTHER = "OTHER"

@@ -76,0 +77,0 @@ }

{
"name": "@google/generative-ai",
"version": "0.14.1",
"version": "0.15.0",
"description": "Google AI JavaScript SDK",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

# 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:
```
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" });
```
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",

@@ -55,61 +64,36 @@ },

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`.
2. cd into the `samples` folder and run `npm install`.
1. Assign your API key to an environment variable: `export API_KEY=MY_API_KEY`.
3. 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`.
4. Open the sample file you're interested in. Example: `text_generation.js`.
In the `runAll()` function, comment out any samples you don't want to run.
**To try out the sample web app, follow these steps:**
5. Run the sample file. Example: `node text_generation.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. 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
See the
[Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbook/) or
[ai.google.dev](https://ai.google.dev) for complete documentation.
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)
## 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).

@@ -243,2 +243,3 @@ /**

model: "my-model",
systemInstruction: "you are a cat",
},

@@ -261,3 +262,5 @@ {

false,
match.any,
match((value: string) => {
return value.includes("hello") && value.includes("you are a cat");
}),
match((value) => {

@@ -264,0 +267,0 @@ return value.apiVersion === "v2000";

@@ -167,3 +167,11 @@ /**

): Promise<CountTokensResponse> {
const formattedParams = formatCountTokensInput(request, this.model);
const formattedParams = formatCountTokensInput(request, {
model: this.model,
generationConfig: this.generationConfig,
safetySettings: this.safetySettings,
tools: this.tools,
toolConfig: this.toolConfig,
systemInstruction: this.systemInstruction,
cachedContent: this.cachedContent,
});
return countTokens(

@@ -170,0 +178,0 @@ this.apiKey,

@@ -20,6 +20,11 @@ /**

import * as sinonChai from "sinon-chai";
import chaiDeepEqualIgnoreUndefined from "chai-deep-equal-ignore-undefined";
import { Content } from "../../types";
import { formatGenerateContentInput } from "./request-helpers";
import {
formatCountTokensInput,
formatGenerateContentInput,
} from "./request-helpers";
use(sinonChai);
use(chaiDeepEqualIgnoreUndefined);

@@ -176,2 +181,100 @@ describe("request formatting methods", () => {

});
describe("formatCountTokensInput", () => {
it("formats a text string into a count request", () => {
const result = formatCountTokensInput("some text content", {
model: "gemini-1.5-flash",
});
expect(result.generateContentRequest).to.deepEqualIgnoreUndefined({
model: "gemini-1.5-flash",
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
});
});
it("formats a text string into a count request, along with model params", () => {
const result = formatCountTokensInput("some text content", {
model: "gemini-1.5-flash",
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: { name: "mycache", contents: [] },
});
expect(result.generateContentRequest).to.deepEqualIgnoreUndefined({
model: "gemini-1.5-flash",
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: "mycache",
});
});
it("formats a 'contents' style count request, along with model params", () => {
const result = formatCountTokensInput(
{
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
},
{
model: "gemini-1.5-flash",
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: { name: "mycache", contents: [] },
},
);
expect(result.generateContentRequest).to.deepEqualIgnoreUndefined({
model: "gemini-1.5-flash",
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: "mycache",
});
});
it("formats a 'generateContentRequest' style count request, along with model params", () => {
const result = formatCountTokensInput(
{
generateContentRequest: {
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
},
},
{
model: "gemini-1.5-flash",
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: { name: "mycache", contents: [] },
},
);
expect(result.generateContentRequest).to.deepEqualIgnoreUndefined({
model: "gemini-1.5-flash",
contents: [
{
role: "user",
parts: [{ text: "some text content" }],
},
],
systemInstruction: "hello",
tools: [{ codeExecution: {} }],
cachedContent: "mycache",
});
});
});
});

@@ -23,4 +23,6 @@ /**

GenerateContentRequest,
ModelParams,
Part,
_CountTokensRequestInternal,
_GenerateContentRequestInternal,
} from "../../types";

@@ -115,5 +117,14 @@ import {

params: CountTokensRequest | string | Array<string | Part>,
model: string,
modelParams?: ModelParams,
): _CountTokensRequestInternal {
let formattedRequest: _CountTokensRequestInternal = {};
let formattedGenerateContentRequest: _GenerateContentRequestInternal = {
model: modelParams?.model,
generationConfig: modelParams?.generationConfig,
safetySettings: modelParams?.safetySettings,
tools: modelParams?.tools,
toolConfig: modelParams?.toolConfig,
systemInstruction: modelParams?.systemInstruction,
cachedContent: modelParams?.cachedContent?.name,
contents: [],
};
const containsGenerateContentRequest =

@@ -127,12 +138,16 @@ (params as CountTokensRequest).generateContentRequest != null;

}
formattedRequest = { ...(params as CountTokensRequest) };
formattedGenerateContentRequest.contents = (
params as CountTokensRequest
).contents;
} else if (containsGenerateContentRequest) {
formattedRequest = { ...(params as CountTokensRequest) };
formattedRequest.generateContentRequest.model = model;
formattedGenerateContentRequest = {
...formattedGenerateContentRequest,
...(params as CountTokensRequest).generateContentRequest,
};
} else {
// Array or string
const content = formatNewContent(params as string | Array<string | Part>);
formattedRequest.contents = [content];
formattedGenerateContentRequest.contents = [content];
}
return formattedRequest;
return { generateContentRequest: formattedGenerateContentRequest };
}

@@ -139,0 +154,0 @@

@@ -166,3 +166,7 @@ /**

const badFinishReasons = [FinishReason.RECITATION, FinishReason.SAFETY];
const badFinishReasons = [
FinishReason.RECITATION,
FinishReason.SAFETY,
FinishReason.LANGUAGE,
];

@@ -169,0 +173,0 @@ function hadBadFinishReason(candidate: GenerateContentCandidate): boolean {

@@ -261,2 +261,4 @@ ## API Report File for "@google/generative-ai"

// (undocumented)
LANGUAGE = "LANGUAGE",
// (undocumented)
MAX_TOKENS = "MAX_TOKENS",

@@ -263,0 +265,0 @@ // (undocumented)

@@ -98,2 +98,4 @@ /**

RECITATION = "RECITATION",
// The candidate content was flagged for using an unsupported language.
LANGUAGE = "LANGUAGE",
// Unknown reason.

@@ -100,0 +102,0 @@ OTHER = "OTHER",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc