@google/generative-ai
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -45,6 +45,7 @@ /** | ||
params?: StartChatParams; | ||
requestOptions?: RequestOptions; | ||
private _apiKey; | ||
private _history; | ||
private _sendPromise; | ||
constructor(apiKey: string, model: string, params?: StartChatParams); | ||
constructor(apiKey: string, model: string, params?: StartChatParams, requestOptions?: RequestOptions); | ||
/** | ||
@@ -269,3 +270,4 @@ * Gets the chat history so far. Blocked prompts are not added to history. | ||
safetySettings: SafetySetting[]; | ||
constructor(apiKey: string, modelParams: ModelParams); | ||
requestOptions: RequestOptions; | ||
constructor(apiKey: string, modelParams: ModelParams, requestOptions?: RequestOptions); | ||
/** | ||
@@ -312,7 +314,7 @@ * Makes a single non-streaming call to the model | ||
*/ | ||
getGenerativeModel(modelParams: ModelParams): GenerativeModel; | ||
getGenerativeModel(modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel; | ||
} | ||
/** | ||
* Threshhold above which a prompt or candidate will be blocked. | ||
* Threshold above which a prompt or candidate will be blocked. | ||
* @public | ||
@@ -412,2 +414,10 @@ */ | ||
/** | ||
* Params passed to {@link GoogleGenerativeAI.getGenerativeModel}. | ||
* @public | ||
*/ | ||
export declare interface RequestOptions { | ||
timeout?: number; | ||
} | ||
/** | ||
* A safety rating associated with a {@link GenerateContentCandidate} | ||
@@ -414,0 +424,0 @@ * @public |
@@ -32,3 +32,3 @@ 'use strict'; | ||
/** | ||
* Threshhold above which a prompt or candidate will be blocked. | ||
* Threshold above which a prompt or candidate will be blocked. | ||
* @public | ||
@@ -162,3 +162,3 @@ */ | ||
*/ | ||
const PACKAGE_VERSION = "0.1.3"; | ||
const PACKAGE_VERSION = "0.2.0"; | ||
const PACKAGE_LOG_HEADER = "genai-js"; | ||
@@ -194,14 +194,10 @@ var Task; | ||
} | ||
async function makeRequest(url, body) { | ||
async function makeRequest(url, body, requestOptions) { | ||
let response; | ||
try { | ||
response = await fetch(url.toString(), { | ||
method: "POST", | ||
headers: { | ||
response = await fetch(url.toString(), Object.assign(Object.assign({}, buildFetchOptions(requestOptions)), { method: "POST", headers: { | ||
"Content-Type": "application/json", | ||
"x-goog-api-client": getClientHeaders(), | ||
"x-goog-api-key": url.apiKey, | ||
}, | ||
body, | ||
}); | ||
}, body })); | ||
if (!response.ok) { | ||
@@ -229,2 +225,17 @@ let message = ""; | ||
} | ||
/** | ||
* Generates the request options to be passed to the fetch API. | ||
* @param requestOptions - The user-defined request options. | ||
* @returns The generated request options. | ||
*/ | ||
function buildFetchOptions(requestOptions) { | ||
const fetchOptions = {}; | ||
if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeout) >= 0) { | ||
const abortController = new AbortController(); | ||
const signal = abortController.signal; | ||
setTimeout(() => abortController.abort(), requestOptions.timeout); | ||
fetchOptions.signal = signal; | ||
} | ||
return fetchOptions; | ||
} | ||
@@ -520,12 +531,12 @@ /** | ||
*/ | ||
async function generateContentStream(apiKey, model, params) { | ||
async function generateContentStream(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.STREAM_GENERATE_CONTENT, apiKey, | ||
/* stream */ true); | ||
const response = await makeRequest(url, JSON.stringify(params)); | ||
const response = await makeRequest(url, JSON.stringify(params), requestOptions); | ||
return processStream(response); | ||
} | ||
async function generateContent(apiKey, model, params) { | ||
async function generateContent(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.GENERATE_CONTENT, apiKey, | ||
/* stream */ false); | ||
const response = await makeRequest(url, JSON.stringify(params)); | ||
const response = await makeRequest(url, JSON.stringify(params), requestOptions); | ||
const responseJson = await response.json(); | ||
@@ -615,5 +626,6 @@ const enhancedResponse = addHelpers(responseJson); | ||
class ChatSession { | ||
constructor(apiKey, model, params) { | ||
constructor(apiKey, model, params, requestOptions) { | ||
this.model = model; | ||
this.params = params; | ||
this.requestOptions = requestOptions; | ||
this._history = []; | ||
@@ -656,3 +668,3 @@ this._sendPromise = Promise.resolve(); | ||
this._sendPromise = this._sendPromise | ||
.then(() => generateContent(this._apiKey, this.model, generateContentRequest)) | ||
.then(() => generateContent(this._apiKey, this.model, generateContentRequest, this.requestOptions)) | ||
.then((result) => { | ||
@@ -693,3 +705,3 @@ var _a; | ||
}; | ||
const streamPromise = generateContentStream(this._apiKey, this.model, generateContentRequest); | ||
const streamPromise = generateContentStream(this._apiKey, this.model, generateContentRequest, this.requestOptions); | ||
// Add onto the chain. | ||
@@ -751,5 +763,5 @@ this._sendPromise = this._sendPromise | ||
*/ | ||
async function countTokens(apiKey, model, params) { | ||
async function countTokens(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.COUNT_TOKENS, apiKey, false); | ||
const response = await makeRequest(url, JSON.stringify(Object.assign(Object.assign({}, params), { model }))); | ||
const response = await makeRequest(url, JSON.stringify(Object.assign(Object.assign({}, params), { model })), requestOptions); | ||
return response.json(); | ||
@@ -774,8 +786,8 @@ } | ||
*/ | ||
async function embedContent(apiKey, model, params) { | ||
async function embedContent(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.EMBED_CONTENT, apiKey, false); | ||
const response = await makeRequest(url, JSON.stringify(params)); | ||
const response = await makeRequest(url, JSON.stringify(params), requestOptions); | ||
return response.json(); | ||
} | ||
async function batchEmbedContents(apiKey, model, params) { | ||
async function batchEmbedContents(apiKey, model, params, requestOptions) { | ||
const url = new RequestUrl(model, Task.BATCH_EMBED_CONTENTS, apiKey, false); | ||
@@ -785,3 +797,3 @@ const requestsWithModel = params.requests.map((request) => { | ||
}); | ||
const response = await makeRequest(url, JSON.stringify({ requests: requestsWithModel })); | ||
const response = await makeRequest(url, JSON.stringify({ requests: requestsWithModel }), requestOptions); | ||
return response.json(); | ||
@@ -811,3 +823,3 @@ } | ||
class GenerativeModel { | ||
constructor(apiKey, modelParams) { | ||
constructor(apiKey, modelParams, requestOptions) { | ||
var _a; | ||
@@ -823,2 +835,3 @@ this.apiKey = apiKey; | ||
this.safetySettings = modelParams.safetySettings || []; | ||
this.requestOptions = requestOptions || {}; | ||
} | ||
@@ -831,3 +844,3 @@ /** | ||
const formattedParams = formatGenerateContentInput(request); | ||
return generateContent(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings }, formattedParams)); | ||
return generateContent(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings }, formattedParams), this.requestOptions); | ||
} | ||
@@ -842,3 +855,3 @@ /** | ||
const formattedParams = formatGenerateContentInput(request); | ||
return generateContentStream(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings }, formattedParams)); | ||
return generateContentStream(this.apiKey, this.model, Object.assign({ generationConfig: this.generationConfig, safetySettings: this.safetySettings }, formattedParams), this.requestOptions); | ||
} | ||
@@ -850,3 +863,3 @@ /** | ||
startChat(startChatParams) { | ||
return new ChatSession(this.apiKey, this.model, startChatParams); | ||
return new ChatSession(this.apiKey, this.model, startChatParams, this.requestOptions); | ||
} | ||
@@ -871,3 +884,3 @@ /** | ||
async batchEmbedContents(batchEmbedContentRequest) { | ||
return batchEmbedContents(this.apiKey, this.model, batchEmbedContentRequest); | ||
return batchEmbedContents(this.apiKey, this.model, batchEmbedContentRequest, this.requestOptions); | ||
} | ||
@@ -903,3 +916,3 @@ } | ||
*/ | ||
getGenerativeModel(modelParams) { | ||
getGenerativeModel(modelParams, requestOptions) { | ||
if (!modelParams.model) { | ||
@@ -909,3 +922,3 @@ throw new GoogleGenerativeAIError(`Must provide a model name. ` + | ||
} | ||
return new GenerativeModel(this.apiKey, modelParams); | ||
return new GenerativeModel(this.apiKey, modelParams, requestOptions); | ||
} | ||
@@ -912,0 +925,0 @@ } |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
import { ModelParams } from "../types"; | ||
import { ModelParams, RequestOptions } from "../types"; | ||
import { GenerativeModel } from "./models/generative-model"; | ||
@@ -32,3 +32,3 @@ export { ChatSession } from "./methods/chat-session"; | ||
*/ | ||
getGenerativeModel(modelParams: ModelParams): GenerativeModel; | ||
getGenerativeModel(modelParams: ModelParams, requestOptions?: RequestOptions): GenerativeModel; | ||
} |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
import { Content, GenerateContentResult, GenerateContentStreamResult, Part, StartChatParams } from "../../types"; | ||
import { Content, GenerateContentResult, GenerateContentStreamResult, Part, RequestOptions, StartChatParams } from "../../types"; | ||
/** | ||
@@ -28,6 +28,7 @@ * ChatSession class that enables sending chat messages and stores | ||
params?: StartChatParams; | ||
requestOptions?: RequestOptions; | ||
private _apiKey; | ||
private _history; | ||
private _sendPromise; | ||
constructor(apiKey: string, model: string, params?: StartChatParams); | ||
constructor(apiKey: string, model: string, params?: StartChatParams, requestOptions?: RequestOptions); | ||
/** | ||
@@ -34,0 +35,0 @@ * Gets the chat history so far. Blocked prompts are not added to history. |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
import { CountTokensRequest, CountTokensResponse } from "../../types"; | ||
export declare function countTokens(apiKey: string, model: string, params: CountTokensRequest): Promise<CountTokensResponse>; | ||
import { CountTokensRequest, CountTokensResponse, RequestOptions } from "../../types"; | ||
export declare function countTokens(apiKey: string, model: string, params: CountTokensRequest, requestOptions?: RequestOptions): Promise<CountTokensResponse>; |
@@ -17,4 +17,4 @@ /** | ||
*/ | ||
import { BatchEmbedContentsRequest, BatchEmbedContentsResponse, EmbedContentRequest, EmbedContentResponse } from "../../types"; | ||
export declare function embedContent(apiKey: string, model: string, params: EmbedContentRequest): Promise<EmbedContentResponse>; | ||
export declare function batchEmbedContents(apiKey: string, model: string, params: BatchEmbedContentsRequest): Promise<BatchEmbedContentsResponse>; | ||
import { BatchEmbedContentsRequest, BatchEmbedContentsResponse, EmbedContentRequest, EmbedContentResponse, RequestOptions } from "../../types"; | ||
export declare function embedContent(apiKey: string, model: string, params: EmbedContentRequest, requestOptions?: RequestOptions): Promise<EmbedContentResponse>; | ||
export declare function batchEmbedContents(apiKey: string, model: string, params: BatchEmbedContentsRequest, requestOptions?: RequestOptions): Promise<BatchEmbedContentsResponse>; |
@@ -17,4 +17,4 @@ /** | ||
*/ | ||
import { GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult } from "../../types"; | ||
export declare function generateContentStream(apiKey: string, model: string, params: GenerateContentRequest): Promise<GenerateContentStreamResult>; | ||
export declare function generateContent(apiKey: string, model: string, params: GenerateContentRequest): Promise<GenerateContentResult>; | ||
import { GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, RequestOptions } from "../../types"; | ||
export declare function generateContentStream(apiKey: string, model: string, params: GenerateContentRequest, requestOptions?: RequestOptions): Promise<GenerateContentStreamResult>; | ||
export declare function generateContent(apiKey: string, model: string, params: GenerateContentRequest, requestOptions?: RequestOptions): Promise<GenerateContentResult>; |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
import { BatchEmbedContentsRequest, BatchEmbedContentsResponse, CountTokensRequest, CountTokensResponse, EmbedContentRequest, EmbedContentResponse, GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, GenerationConfig, ModelParams, Part, SafetySetting, StartChatParams } from "../../types"; | ||
import { BatchEmbedContentsRequest, BatchEmbedContentsResponse, CountTokensRequest, CountTokensResponse, EmbedContentRequest, EmbedContentResponse, GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, GenerationConfig, ModelParams, Part, RequestOptions, SafetySetting, StartChatParams } from "../../types"; | ||
import { ChatSession } from "../methods/chat-session"; | ||
@@ -29,3 +29,4 @@ /** | ||
safetySettings: SafetySetting[]; | ||
constructor(apiKey: string, modelParams: ModelParams); | ||
requestOptions: RequestOptions; | ||
constructor(apiKey: string, modelParams: ModelParams, requestOptions?: RequestOptions); | ||
/** | ||
@@ -32,0 +33,0 @@ * Makes a single non-streaming call to the model |
@@ -17,2 +17,3 @@ /** | ||
*/ | ||
import { RequestOptions } from "../../types"; | ||
export declare enum Task { | ||
@@ -33,2 +34,2 @@ GENERATE_CONTENT = "generateContent", | ||
} | ||
export declare function makeRequest(url: RequestUrl, body: string): Promise<Response>; | ||
export declare function makeRequest(url: RequestUrl, body: string, requestOptions?: RequestOptions): Promise<Response>; |
@@ -29,3 +29,3 @@ /** | ||
/** | ||
* Threshhold above which a prompt or candidate will be blocked. | ||
* Threshold above which a prompt or candidate will be blocked. | ||
* @public | ||
@@ -32,0 +32,0 @@ */ |
@@ -91,1 +91,8 @@ /** | ||
} | ||
/** | ||
* Params passed to {@link GoogleGenerativeAI.getGenerativeModel}. | ||
* @public | ||
*/ | ||
export interface RequestOptions { | ||
timeout?: number; | ||
} |
{ | ||
"name": "@google/generative-ai", | ||
"version": "0.1.3", | ||
"version": "0.2.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
117892
2971