@google/generative-ai
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -89,2 +89,18 @@ /** | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* Content type for both prompts and response candidates. | ||
@@ -94,3 +110,3 @@ * @public | ||
export declare interface Content { | ||
role: Role; | ||
role: string; | ||
parts: Part[]; | ||
@@ -148,7 +164,18 @@ } | ||
/** | ||
* Returns the text string from the response, if available. | ||
* Returns the text string assembled from all `Part`s of the first candidate | ||
* of the response, if available. | ||
* Throws if the prompt or candidate was blocked. | ||
*/ | ||
text: () => string; | ||
/** | ||
* Deprecated: use `functionCalls()` instead. | ||
* @deprecated | ||
*/ | ||
functionCall: () => FunctionCall | undefined; | ||
/** | ||
* Returns function calls found in any `Part`s of the first candidate | ||
* of the response, if available. | ||
* Throws if the prompt or candidate was blocked. | ||
*/ | ||
functionCalls: () => FunctionCall[] | undefined; | ||
} | ||
@@ -578,27 +605,9 @@ | ||
apiVersion?: string; | ||
/** | ||
* Base endpoint url. Defaults to "https://generativelanguage.googleapis.com" | ||
*/ | ||
baseUrl?: string; | ||
} | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* Role is the producer of the content. | ||
* @public | ||
*/ | ||
export declare type Role = (typeof POSSIBLE_ROLES)[number]; | ||
/** | ||
* A safety rating associated with a {@link GenerateContentCandidate} | ||
@@ -605,0 +614,0 @@ * @public |
@@ -197,3 +197,3 @@ 'use strict'; | ||
*/ | ||
const BASE_URL = "https://generativelanguage.googleapis.com"; | ||
const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"; | ||
const DEFAULT_API_VERSION = "v1"; | ||
@@ -204,3 +204,3 @@ /** | ||
*/ | ||
const PACKAGE_VERSION = "0.3.1"; | ||
const PACKAGE_VERSION = "0.4.0"; | ||
const PACKAGE_LOG_HEADER = "genai-js"; | ||
@@ -224,5 +224,6 @@ var Task; | ||
toString() { | ||
var _a; | ||
var _a, _b; | ||
const apiVersion = ((_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.apiVersion) || DEFAULT_API_VERSION; | ||
let url = `${BASE_URL}/${apiVersion}/${this.model}:${this.task}`; | ||
const baseUrl = ((_b = this.requestOptions) === null || _b === void 0 ? void 0 : _b.baseUrl) || DEFAULT_BASE_URL; | ||
let url = `${baseUrl}/${apiVersion}/${this.model}:${this.task}`; | ||
if (this.stream) { | ||
@@ -324,2 +325,5 @@ url += "?alt=sse"; | ||
}; | ||
/** | ||
* TODO: remove at next major version | ||
*/ | ||
response.functionCall = () => { | ||
@@ -329,3 +333,3 @@ if (response.candidates && response.candidates.length > 0) { | ||
console.warn(`This response had ${response.candidates.length} ` + | ||
`candidates. Returning function call from the first candidate only. ` + | ||
`candidates. Returning function calls from the first candidate only. ` + | ||
`Access response.candidates directly to use the other candidates.`); | ||
@@ -336,3 +340,5 @@ } | ||
} | ||
return getFunctionCall(response); | ||
console.warn(`response.functionCall() is deprecated. ` + | ||
`Use response.functionCalls() instead.`); | ||
return getFunctionCalls(response)[0]; | ||
} | ||
@@ -344,2 +350,19 @@ else if (response.promptFeedback) { | ||
}; | ||
response.functionCalls = () => { | ||
if (response.candidates && response.candidates.length > 0) { | ||
if (response.candidates.length > 1) { | ||
console.warn(`This response had ${response.candidates.length} ` + | ||
`candidates. Returning function calls from the first candidate only. ` + | ||
`Access response.candidates directly to use the other candidates.`); | ||
} | ||
if (hadBadFinishReason(response.candidates[0])) { | ||
throw new GoogleGenerativeAIResponseError(`${formatBlockErrorMessage(response)}`, response); | ||
} | ||
return getFunctionCalls(response); | ||
} | ||
else if (response.promptFeedback) { | ||
throw new GoogleGenerativeAIResponseError(`Function call not available. ${formatBlockErrorMessage(response)}`, response); | ||
} | ||
return undefined; | ||
}; | ||
return response; | ||
@@ -364,5 +387,18 @@ } | ||
*/ | ||
function getFunctionCall(response) { | ||
function getFunctionCalls(response) { | ||
var _a, _b, _c, _d; | ||
return (_d = (_c = (_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.functionCall; | ||
const functionCalls = []; | ||
if ((_b = (_a = response.candidates) === null || _a === void 0 ? void 0 : _a[0].content) === null || _b === void 0 ? void 0 : _b.parts) { | ||
for (const part of (_d = (_c = response.candidates) === null || _c === void 0 ? void 0 : _c[0].content) === null || _d === void 0 ? void 0 : _d.parts) { | ||
if (part.functionCall) { | ||
functionCalls.push(part.functionCall); | ||
} | ||
} | ||
} | ||
if (functionCalls.length > 0) { | ||
return functionCalls; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
@@ -952,3 +988,3 @@ const badFinishReasons = [exports.FinishReason.RECITATION, exports.FinishReason.SAFETY]; | ||
async function countTokens(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.COUNT_TOKENS, apiKey, false, {}); | ||
const url = new RequestUrl(model, Task.COUNT_TOKENS, apiKey, false, requestOptions); | ||
const response = await makeRequest(url, JSON.stringify(Object.assign(Object.assign({}, params), { model })), requestOptions); | ||
@@ -975,3 +1011,3 @@ return response.json(); | ||
async function embedContent(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.EMBED_CONTENT, apiKey, false, {}); | ||
const url = new RequestUrl(model, Task.EMBED_CONTENT, apiKey, false, requestOptions); | ||
const response = await makeRequest(url, JSON.stringify(params), requestOptions); | ||
@@ -981,3 +1017,3 @@ return response.json(); | ||
async function batchEmbedContents(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.BATCH_EMBED_CONTENTS, apiKey, false, {}); | ||
const url = new RequestUrl(model, Task.BATCH_EMBED_CONTENTS, apiKey, false, requestOptions); | ||
const requestsWithModel = params.requests.map((request) => { | ||
@@ -984,0 +1020,0 @@ return Object.assign(Object.assign({}, request), { model }); |
@@ -18,2 +18,3 @@ /** | ||
import { RequestOptions } from "../../types"; | ||
export declare const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"; | ||
export declare const DEFAULT_API_VERSION = "v1"; | ||
@@ -20,0 +21,0 @@ export declare enum Task { |
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
export declare function getFunctionCall(response: GenerateContentResponse): FunctionCall; | ||
export declare function getFunctionCalls(response: GenerateContentResponse): FunctionCall[]; | ||
export declare function formatBlockErrorMessage(response: GenerateContentResponse): string; |
@@ -17,3 +17,2 @@ /** | ||
*/ | ||
import { Role } from "./enums"; | ||
/** | ||
@@ -24,3 +23,3 @@ * Content type for both prompts and response candidates. | ||
export interface Content { | ||
role: Role; | ||
role: string; | ||
parts: Part[]; | ||
@@ -27,0 +26,0 @@ } |
@@ -18,7 +18,2 @@ /** | ||
/** | ||
* Role is the producer of the content. | ||
* @public | ||
*/ | ||
export type Role = (typeof POSSIBLE_ROLES)[number]; | ||
/** | ||
* Possible roles. | ||
@@ -25,0 +20,0 @@ * @public |
@@ -108,2 +108,6 @@ /** | ||
apiVersion?: string; | ||
/** | ||
* Base endpoint url. Defaults to "https://generativelanguage.googleapis.com" | ||
*/ | ||
baseUrl?: string; | ||
} | ||
@@ -110,0 +114,0 @@ /** |
@@ -46,7 +46,18 @@ /** | ||
/** | ||
* Returns the text string from the response, if available. | ||
* Returns the text string assembled from all `Part`s of the first candidate | ||
* of the response, if available. | ||
* Throws if the prompt or candidate was blocked. | ||
*/ | ||
text: () => string; | ||
/** | ||
* Deprecated: use `functionCalls()` instead. | ||
* @deprecated | ||
*/ | ||
functionCall: () => FunctionCall | undefined; | ||
/** | ||
* Returns function calls found in any `Part`s of the first candidate | ||
* of the response, if available. | ||
* Throws if the prompt or candidate was blocked. | ||
*/ | ||
functionCalls: () => FunctionCall[] | undefined; | ||
} | ||
@@ -53,0 +64,0 @@ /** |
{ | ||
"name": "@google/generative-ai", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Google AI JavaScript SDK", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150947
3839