Skip to content

Commit

Permalink
Updated to v2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaikech committed Sep 26, 2024
1 parent 5bfe6f9 commit ff357b6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ I created this library based on the following reports.
- [Specifying Output Types for Gemini API with Google Apps Script](https://medium.com/google-cloud/specifying-output-types-for-gemini-api-with-google-apps-script-c2f6a753c8d7)
- [Parsing Invoices using Gemini 1.5 API with Google Apps Script](https://medium.com/google-cloud/parsing-invoices-using-gemini-1-5-api-with-google-apps-script-1f32af1678f2)
- [Taming the Wild Output: Effective Control of Gemini API Response Formats with response_mime_type](https://medium.com/google-cloud/taming-the-wild-output-effective-control-of-gemini-api-response-formats-with-response-mime-type-da273c08be85)
- [Gemini API with JSON schema](https://medium.com/google-cloud/gemini-api-with-json-schema-3dbdabac7d19)
- [Taming the Wild Output: Effective Control of Gemini API Response Formats with response_schema](https://medium.com/google-cloud/taming-the-wild-output-effective-control-of-gemini-api-response-formats-with-response-schema-ae0097b97502)
- [Harnessing Gemini’s Power: A Guide to Generating Content from Structured Data](https://medium.com/google-cloud/harnessing-geminis-power-a-guide-to-generating-content-from-structured-data-45080dac0bbb)

# Features

Expand Down Expand Up @@ -160,7 +163,7 @@ const g = new GeminiWithFiles(object);

The value of `object` is as follows.

```
````
{Object} object API key or access token for using Gemini API.
{String} object.apiKey API key.
{String} object.accessToken Access token.
Expand All @@ -171,19 +174,24 @@ The value of `object` is as follows.
{Array} object.functions If you want to give the custom functions, please use this.
{String} object.response_mime_type In the current stage, only "application/json" can be used.
{String} object.responseMimeType In the current stage, only "application/json" can be used.
{Object} object.systemInstruction Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini
{Boolean} object.exportTotalTokens When this is true, the object `usageMetadata` including `promptTokenCount`, `candidatesTokenCount`, `totalTokenCount` is exported. At that time, the generated content and `usageMetadata` are returned as an object.
{Object} object.response_schema JSON schema for controlling the output format.
{Object} object.responseSchema JSON schema for controlling the output format.
{Number} object.temperature Control the randomness of the output.
{Object} object.systemInstruction Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini.
{Boolean} object.exportTotalTokens When this is true, the total tokens are exported as the result value. At that time, the generated content and the total tokens are returned as an object.
{Boolean} object.exportRawData The default value is false. When this is true, the raw data returned from Gemini API is returned.
{Object} object.toolConfig The default is null. If you want to directly give the object of "toolConfig", please use this.
{Array} object.tools The default value is null. For example, when you want to use "codeExecution", please set `tools: [{ codeExecution: {}}]`.
```
````

- When you want to use `response_mime_type`, please give `jsonSchema` to generateContent method. In the current stage, only `"application/json"` can be used to `response_mime_type`.

- When you want to use `systemInstruction`, please confirm the official document [Ref](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini).

- Gemini 1.5 Flash Latest (`models/gemini-1.5-flash-latest`) is used as the default model. When you want to use Gemini 1.5 Pro Latest (`models/gemini-1.5-pro-latest`), please use it like `const g = GeminiWithFiles.geminiWithFiles({ apiKey, model: "models/gemini-1.5-pro-latest" })`.

- In the current stage, when `response_schema` is used, `response_mime_type: "application/json"` is automatically used.

<a name="setfileIds"></a>

## setFileIds
Expand Down Expand Up @@ -1492,4 +1500,8 @@ I have already proposed the following future requests to the Google issue tracke

1. From this version, `codeExecution` can be used. [Ref](#usecodeexecution)

- v2.0.2 (September 26, 2024)

1. As the option for `generationConfig`, the properties `response_schema` and `temperature` were added.

[TOP](#top)
26 changes: 22 additions & 4 deletions classGeminiWithFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* from multiple images at once.
* This significantly reduces workload and expands possibilities for using Gemini.
*
* GeminiWithFiles v2.0.1
* GeminiWithFiles v2.0.2
* GitHub: https://github.com/tanaikech/GeminiWithFiles
*/
class GeminiWithFiles {
Expand All @@ -22,14 +22,17 @@ class GeminiWithFiles {
* @param {Array} object.functions If you want to give the custom functions, please use this.
* @param {String} object.response_mime_type In the current stage, only "application/json" can be used.
* @param {String} object.responseMimeType In the current stage, only "application/json" can be used.
* @param {Object} object.response_schema JSON schema for controlling the output format.
* @param {Object} object.responseSchema JSON schema for controlling the output format.
* @param {Number} object.temperature Control the randomness of the output.
* @param {Object} object.systemInstruction Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini.
* @param {Boolean} object.exportTotalTokens When this is true, the total tokens are exported as the result value. At that time, the generated content and the total tokens are returned as an object.
* @param {Boolean} object.exportRawData The default value is false. When this is true, the raw data returned from Gemini API is returned.
* @param {Object} object.toolConfig The default is null. If you want to directly give the object of "toolConfig", please use this.
* @param {Array} object.tools The default value is null. For example, when you want to use "codeExecution", please set `tools: [{ codeExecution: {}}]`.
*/
constructor(object = {}) {
const { apiKey, accessToken, model, version, doCountToken, history, functions, response_mime_type, responseMimeType, systemInstruction, exportTotalTokens, exportRawData, toolConfig, tools } = object;
const { apiKey, accessToken, model, version, doCountToken, history, functions, response_mime_type, responseMimeType, response_schema = null, responseSchema = null, temperature = null, systemInstruction, exportTotalTokens, exportRawData, toolConfig, tools } = object;

/** @private */
this.model = model || "models/gemini-1.5-flash-latest"; // After v2.0.0, the model was changed from "models/gemini-1.5-pro-latest" to "models/gemini-1.5-flash-latest".
Expand Down Expand Up @@ -102,8 +105,13 @@ class GeminiWithFiles {
this.functions = {};

if ((response_mime_type && response_mime_type != "") || (responseMimeType && responseMimeType != "")) {
this.response_mime_type = response_mime_type;
this.response_mime_type = response_mime_type || responseMimeType;
}
if ((response_schema && typeof response_schema == "object") || (responseSchema && typeof responseSchema == "object")) {
this.response_schema = response_schema || responseSchema;
}
this.temperature = temperature === null ? null : temperature;

if (functions && functions.params_) {
this.functions = functions;
}
Expand Down Expand Up @@ -407,9 +415,19 @@ class GeminiWithFiles {
do {
retry--;
const payload = { contents, tools: [{ function_declarations }] };

payload.generationConfig = {};
if (this.response_mime_type != "") {
payload.generationConfig = { response_mime_type: this.response_mime_type };
payload.generationConfig.response_mime_type = this.response_mime_type;
}
if (this.response_schema) {
payload.generationConfig.response_schema = this.response_schema;
payload.generationConfig.response_mime_type = "application/json";
}
if (this.temperature !== null) {
payload.generationConfig.temperature = this.temperature;
}

if (this.systemInstruction) {
payload.systemInstruction = this.systemInstruction;
}
Expand Down

0 comments on commit ff357b6

Please sign in to comment.