@google-cloud/vertexai
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
".": "1.0.0" | ||
".": "1.1.0" | ||
} |
@@ -34,2 +34,3 @@ "use strict"; | ||
contents: request.contents, | ||
systemInstruction: request.systemInstruction, | ||
generationConfig: (_a = request.generationConfig) !== null && _a !== void 0 ? _a : generationConfig, | ||
@@ -73,2 +74,3 @@ safetySettings: (_b = request.safetySettings) !== null && _b !== void 0 ? _b : safetySettings, | ||
contents: request.contents, | ||
systemInstruction: request.systemInstruction, | ||
generationConfig: (_a = request.generationConfig) !== null && _a !== void 0 ? _a : generationConfig, | ||
@@ -75,0 +77,0 @@ safetySettings: (_b = request.safetySettings) !== null && _b !== void 0 ? _b : safetySettings, |
@@ -21,2 +21,8 @@ "use strict"; | ||
const API_BASE_PATH = 'aiplatform.googleapis.com'; | ||
const GOOGLE_INTERNAL_ENDPOINT = 'googleapis.com'; | ||
const AUTHORIZATION_HEADER = 'Authorization'; | ||
const CONTENT_TYPE_HEADER = 'Content-Type'; | ||
const USER_AGENT_HEADER = 'User-Agent'; | ||
const X_GOOG_API_CLIENT_HEADER = 'X-Goog-Api-Client'; | ||
const SERVER_RESERVED_HEADERS = [AUTHORIZATION_HEADER, CONTENT_TYPE_HEADER]; | ||
const errors_1 = require("../types/errors"); | ||
@@ -35,20 +41,12 @@ const constants = require("../util/constants"); | ||
} | ||
if ((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient) && | ||
((requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient.includes('\n')) || | ||
(requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient.includes('\r')))) { | ||
throw new errors_1.ClientError('Found line break in apiClient request option field, please remove ' + | ||
'the line break and try again.'); | ||
} | ||
const extraHeaders = (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient) | ||
? { 'X-Goog-Api-Client': requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient } | ||
: {}; | ||
const necessaryHeaders = new Headers({ | ||
[AUTHORIZATION_HEADER]: `Bearer ${token}`, | ||
[CONTENT_TYPE_HEADER]: 'application/json', | ||
[USER_AGENT_HEADER]: constants.USER_AGENT, | ||
}); | ||
const totalHeaders = getExtraHeaders(vertexBaseEndpoint, necessaryHeaders, requestOptions); | ||
return fetch(vertexEndpoint, { | ||
...getFetchOptions(requestOptions), | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': constants.USER_AGENT, | ||
...extraHeaders, | ||
}, | ||
headers: totalHeaders, | ||
body: JSON.stringify(data), | ||
@@ -71,2 +69,52 @@ }); | ||
} | ||
function stringHasLineBreak(header) { | ||
if (header === null || header === undefined) { | ||
return false; | ||
} | ||
return header.includes('\n') || header.includes('\r'); | ||
} | ||
function headersHasLineBreak(customHeaders) { | ||
if (!customHeaders) { | ||
return false; | ||
} | ||
for (const [key, value] of customHeaders.entries()) { | ||
if (stringHasLineBreak(key) || stringHasLineBreak(value)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function getExtraHeaders(vertexBaseEndpoint, necessaryHeaders, requestOptions) { | ||
var _a; | ||
if (stringHasLineBreak(requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient)) { | ||
throw new errors_1.ClientError('Found line break in apiClient request option field, please remove ' + | ||
'the line break and try again.'); | ||
} | ||
if (headersHasLineBreak(requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.customHeaders)) { | ||
throw new errors_1.ClientError('Found line break in customerHeaders request option field, please remove ' + | ||
'the line break and try again.'); | ||
} | ||
const totalHeaders = new Headers(necessaryHeaders); | ||
const customHeaders = (_a = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.customHeaders) !== null && _a !== void 0 ? _a : new Headers(); | ||
for (const [key, val] of customHeaders.entries()) { | ||
totalHeaders.append(key, val); | ||
} | ||
if (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient) { | ||
totalHeaders.append(X_GOOG_API_CLIENT_HEADER, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.apiClient); | ||
} | ||
// Resolve header conflicts. | ||
let goldenHeaders; | ||
if (vertexBaseEndpoint.endsWith(GOOGLE_INTERNAL_ENDPOINT)) { | ||
goldenHeaders = necessaryHeaders; | ||
} | ||
else { | ||
goldenHeaders = customHeaders; | ||
} | ||
for (const header of SERVER_RESERVED_HEADERS) { | ||
if (goldenHeaders.has(header)) { | ||
totalHeaders.set(header, goldenHeaders.get(header)); | ||
} | ||
} | ||
return totalHeaders; | ||
} | ||
//# sourceMappingURL=post_request.js.map |
@@ -38,2 +38,3 @@ /** | ||
private readonly apiEndpoint?; | ||
private readonly systemInstruction?; | ||
getHistory(): Promise<Content[]>; | ||
@@ -117,2 +118,3 @@ /** | ||
private readonly apiEndpoint?; | ||
private readonly systemInstruction?; | ||
getHistory(): Promise<Content[]>; | ||
@@ -119,0 +121,0 @@ /** |
@@ -52,2 +52,6 @@ "use strict"; | ||
this.requestOptions = requestOptions !== null && requestOptions !== void 0 ? requestOptions : {}; | ||
if (request.systemInstruction) { | ||
request.systemInstruction.role = util_1.constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = request.systemInstruction; | ||
} | ||
@@ -91,2 +95,3 @@ /** | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -147,2 +152,3 @@ const generateContentResult = await (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), generateContentrequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions).catch(e => { | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -188,2 +194,6 @@ const streamGenerateContentResultPromise = (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), generateContentrequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions).catch(e => { | ||
this.requestOptions = requestOptions !== null && requestOptions !== void 0 ? requestOptions : {}; | ||
if (request.systemInstruction) { | ||
request.systemInstruction.role = util_1.constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = request.systemInstruction; | ||
} | ||
@@ -226,2 +236,3 @@ /** | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -282,2 +293,3 @@ const generateContentResult = await (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), generateContentrequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions).catch(e => { | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -284,0 +296,0 @@ const streamGenerateContentResultPromise = (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), generateContentrequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions).catch(e => { |
@@ -31,2 +31,3 @@ /** | ||
private readonly requestOptions?; | ||
private readonly systemInstruction?; | ||
private readonly project; | ||
@@ -149,2 +150,3 @@ private readonly location; | ||
private readonly requestOptions?; | ||
private readonly systemInstruction?; | ||
private readonly project; | ||
@@ -151,0 +153,0 @@ private readonly location; |
@@ -47,2 +47,6 @@ "use strict"; | ||
this.requestOptions = (_a = getGenerativeModelParams.requestOptions) !== null && _a !== void 0 ? _a : {}; | ||
if (getGenerativeModelParams.systemInstruction) { | ||
getGenerativeModelParams.systemInstruction.role = util_1.constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = getGenerativeModelParams.systemInstruction; | ||
if (this.model.startsWith('models/')) { | ||
@@ -85,3 +89,5 @@ this.publisherModelEndpoint = `publishers/google/${this.model}`; | ||
async generateContent(request) { | ||
return (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), request, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = formulateSystemInstructionIntoGenerateContentRequest(request, this.systemInstruction); | ||
return (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), formulatedRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
} | ||
@@ -113,3 +119,5 @@ /** | ||
async generateContentStream(request) { | ||
return (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), request, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = formulateSystemInstructionIntoGenerateContentRequest(request, this.systemInstruction); | ||
return (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), formulatedRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
} | ||
@@ -162,3 +170,3 @@ /** | ||
startChat(request) { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
const startChatRequest = { | ||
@@ -179,2 +187,4 @@ project: this.project, | ||
startChatRequest.apiEndpoint = (_d = request.apiEndpoint) !== null && _d !== void 0 ? _d : this.apiEndpoint; | ||
startChatRequest.systemInstruction = | ||
(_e = request.systemInstruction) !== null && _e !== void 0 ? _e : this.systemInstruction; | ||
} | ||
@@ -207,2 +217,6 @@ return new chat_session_1.ChatSession(startChatRequest, this.requestOptions); | ||
this.requestOptions = (_a = getGenerativeModelParams.requestOptions) !== null && _a !== void 0 ? _a : {}; | ||
if (getGenerativeModelParams.systemInstruction) { | ||
getGenerativeModelParams.systemInstruction.role = util_1.constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = getGenerativeModelParams.systemInstruction; | ||
if (this.model.startsWith('models/')) { | ||
@@ -244,3 +258,5 @@ this.publisherModelEndpoint = `publishers/google/${this.model}`; | ||
async generateContent(request) { | ||
return (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), request, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = formulateSystemInstructionIntoGenerateContentRequest(request, this.systemInstruction); | ||
return (0, generate_content_1.generateContent)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), formulatedRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
} | ||
@@ -272,3 +288,5 @@ /** | ||
async generateContentStream(request) { | ||
return (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), request, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = formulateSystemInstructionIntoGenerateContentRequest(request, this.systemInstruction); | ||
return (0, generate_content_1.generateContentStream)(this.location, this.project, this.publisherModelEndpoint, this.fetchToken(), formulatedRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, this.tools, this.requestOptions); | ||
} | ||
@@ -321,3 +339,3 @@ /** | ||
startChat(request) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
const startChatRequest = { | ||
@@ -337,2 +355,4 @@ project: this.project, | ||
startChatRequest.tools = (_c = request.tools) !== null && _c !== void 0 ? _c : this.tools; | ||
startChatRequest.systemInstruction = | ||
(_d = request.systemInstruction) !== null && _d !== void 0 ? _d : this.systemInstruction; | ||
} | ||
@@ -343,2 +363,20 @@ return new chat_session_1.ChatSessionPreview(startChatRequest, this.requestOptions); | ||
exports.GenerativeModelPreview = GenerativeModelPreview; | ||
function formulateRequestToGenerateContentRequest(request) { | ||
if (typeof request === 'string') { | ||
return { | ||
contents: [{ role: util_1.constants.USER_ROLE, parts: [{ text: request }] }], | ||
}; | ||
} | ||
return request; | ||
} | ||
function formulateSystemInstructionIntoGenerateContentRequest(methodRequest, classSystemInstruction) { | ||
if (methodRequest.systemInstruction) { | ||
methodRequest.systemInstruction.role = util_1.constants.SYSTEM_ROLE; | ||
return methodRequest; | ||
} | ||
if (classSystemInstruction) { | ||
methodRequest.systemInstruction = classSystemInstruction; | ||
} | ||
return methodRequest; | ||
} | ||
//# sourceMappingURL=generative_models.js.map |
@@ -46,2 +46,6 @@ /** | ||
contents: Content[]; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -99,2 +103,6 @@ /** | ||
requestOptions?: RequestOptions; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -122,2 +130,6 @@ /** | ||
tools?: Tool[]; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -814,2 +826,6 @@ /** | ||
apiEndpoint?: string; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -828,2 +844,6 @@ /** | ||
publisherModelEndpoint: string; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -842,2 +862,6 @@ /** | ||
apiClient?: string; | ||
/** | ||
* Value for custom HTTP headers to set on the HTTP request. | ||
*/ | ||
customHeaders?: Headers; | ||
} |
@@ -21,3 +21,4 @@ /** | ||
export declare const MODEL_ROLE = "model"; | ||
export declare const USER_AGENT = "model-builder/1.0.0 grpc-node/1.0.0"; | ||
export declare const SYSTEM_ROLE = "system"; | ||
export declare const USER_AGENT = "model-builder/1.1.0 grpc-node/1.1.0"; | ||
export declare const CREDENTIAL_ERROR_MESSAGE = "\nUnable to authenticate your request \nDepending on your run time environment, you can get authentication by \n- if in local instance or cloud shell: `!gcloud auth login` \n- if in Colab: \n -`from google.colab import auth` \n -`auth.authenticate_user()` \n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CREDENTIAL_ERROR_MESSAGE = exports.USER_AGENT = exports.MODEL_ROLE = exports.USER_ROLE = exports.STREAMING_GENERATE_CONTENT_METHOD = exports.GENERATE_CONTENT_METHOD = void 0; | ||
exports.CREDENTIAL_ERROR_MESSAGE = exports.USER_AGENT = exports.SYSTEM_ROLE = exports.MODEL_ROLE = exports.USER_ROLE = exports.STREAMING_GENERATE_CONTENT_METHOD = exports.GENERATE_CONTENT_METHOD = void 0; | ||
/** | ||
@@ -24,4 +24,5 @@ * @license | ||
exports.MODEL_ROLE = 'model'; | ||
exports.SYSTEM_ROLE = 'system'; | ||
const USER_AGENT_PRODUCT = 'model-builder'; | ||
const CLIENT_LIBRARY_VERSION = '1.0.0'; // x-release-please-version | ||
const CLIENT_LIBRARY_VERSION = '1.1.0'; // x-release-please-version | ||
const CLIENT_LIBRARY_LANGUAGE = `grpc-node/${CLIENT_LIBRARY_VERSION}`; | ||
@@ -28,0 +29,0 @@ exports.USER_AGENT = `${USER_AGENT_PRODUCT}/${CLIENT_LIBRARY_VERSION} ${CLIENT_LIBRARY_LANGUAGE}`; |
@@ -101,2 +101,3 @@ "use strict"; | ||
requestOptions: requestOptions, | ||
systemInstruction: modelParams.systemInstruction, | ||
}; | ||
@@ -149,2 +150,3 @@ return new models_1.GenerativeModel(getGenerativeModelParams); | ||
requestOptions: requestOptions, | ||
systemInstruction: modelParams.systemInstruction, | ||
}; | ||
@@ -151,0 +153,0 @@ return new models_1.GenerativeModelPreview(getGenerativeModelParams); |
@@ -422,3 +422,3 @@ "use strict"; | ||
}); | ||
it('should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => { | ||
xit('should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => { | ||
const generativeTextModel = vertexAI.getGenerativeModel({ | ||
@@ -425,0 +425,0 @@ model: TEXT_MODEL_NAME, |
# Changelog | ||
## [1.1.0](https://github.com/googleapis/nodejs-vertexai/compare/v1.0.0...v1.1.0) (2024-04-13) | ||
### Features | ||
* enable system instruction for GenerativeModel ([590ca5a](https://github.com/googleapis/nodejs-vertexai/commit/590ca5a055e65b493c7d20f3983173dc8a8cbc39)) | ||
* enable system instruction in chat experience ([7e71f75](https://github.com/googleapis/nodejs-vertexai/commit/7e71f750104cf14f465bb8091f851b5692f5aea9)) | ||
* exposing customHeader in requestOptions to allow users pass in customer headers. ([b47d733](https://github.com/googleapis/nodejs-vertexai/commit/b47d733680837233b4323f4113737421099df02b)) | ||
## [1.0.0](https://github.com/googleapis/nodejs-vertexai/compare/v0.5.0...v1.0.0) (2024-04-04) | ||
@@ -4,0 +13,0 @@ |
{ | ||
"name": "@google-cloud/vertexai", | ||
"description": "Vertex Generative AI client for Node.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "author": "Google LLC", |
@@ -0,1 +1,3 @@ | ||
[![NPM Downloads](https://img.shields.io/npm/dm/%40google-cloud%2Fvertexai)](https://www.npmjs.com/package/@google-cloud/vertexai) | ||
# Vertex AI SDK for Node.js quickstart | ||
@@ -2,0 +4,0 @@ |
@@ -72,2 +72,3 @@ /** | ||
contents: request.contents, | ||
systemInstruction: request.systemInstruction, | ||
generationConfig: request.generationConfig ?? generationConfig, | ||
@@ -125,2 +126,3 @@ safetySettings: request.safetySettings ?? safetySettings, | ||
contents: request.contents, | ||
systemInstruction: request.systemInstruction, | ||
generationConfig: request.generationConfig ?? generationConfig, | ||
@@ -127,0 +129,0 @@ safetySettings: request.safetySettings ?? safetySettings, |
@@ -19,3 +19,10 @@ /** | ||
const API_BASE_PATH = 'aiplatform.googleapis.com'; | ||
const GOOGLE_INTERNAL_ENDPOINT = 'googleapis.com'; | ||
const AUTHORIZATION_HEADER = 'Authorization'; | ||
const CONTENT_TYPE_HEADER = 'Content-Type'; | ||
const USER_AGENT_HEADER = 'User-Agent'; | ||
const X_GOOG_API_CLIENT_HEADER = 'X-Goog-Api-Client'; | ||
const SERVER_RESERVED_HEADERS = [AUTHORIZATION_HEADER, CONTENT_TYPE_HEADER]; | ||
import { | ||
@@ -62,26 +69,16 @@ GenerateContentRequest, | ||
} | ||
if ( | ||
requestOptions?.apiClient && | ||
(requestOptions?.apiClient.includes('\n') || | ||
requestOptions?.apiClient.includes('\r')) | ||
) { | ||
throw new ClientError( | ||
'Found line break in apiClient request option field, please remove ' + | ||
'the line break and try again.' | ||
); | ||
} | ||
const extraHeaders: HeadersInit = requestOptions?.apiClient | ||
? {'X-Goog-Api-Client': requestOptions?.apiClient} | ||
: {}; | ||
const necessaryHeaders = new Headers({ | ||
[AUTHORIZATION_HEADER]: `Bearer ${token}`, | ||
[CONTENT_TYPE_HEADER]: 'application/json', | ||
[USER_AGENT_HEADER]: constants.USER_AGENT, | ||
}); | ||
const totalHeaders: Headers = getExtraHeaders( | ||
vertexBaseEndpoint, | ||
necessaryHeaders, | ||
requestOptions | ||
); | ||
return fetch(vertexEndpoint, { | ||
...getFetchOptions(requestOptions), | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': constants.USER_AGENT, | ||
...extraHeaders, | ||
}, | ||
headers: totalHeaders, | ||
body: JSON.stringify(data), | ||
@@ -106,1 +103,60 @@ }); | ||
} | ||
function stringHasLineBreak(header?: string | null): boolean { | ||
if (header === null || header === undefined) { | ||
return false; | ||
} | ||
return header.includes('\n') || header.includes('\r'); | ||
} | ||
function headersHasLineBreak(customHeaders?: Headers): boolean { | ||
if (!customHeaders) { | ||
return false; | ||
} | ||
for (const [key, value] of customHeaders.entries()) { | ||
if (stringHasLineBreak(key) || stringHasLineBreak(value)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function getExtraHeaders( | ||
vertexBaseEndpoint: string, | ||
necessaryHeaders: Headers, | ||
requestOptions?: RequestOptions | ||
): Headers { | ||
if (stringHasLineBreak(requestOptions?.apiClient)) { | ||
throw new ClientError( | ||
'Found line break in apiClient request option field, please remove ' + | ||
'the line break and try again.' | ||
); | ||
} | ||
if (headersHasLineBreak(requestOptions?.customHeaders)) { | ||
throw new ClientError( | ||
'Found line break in customerHeaders request option field, please remove ' + | ||
'the line break and try again.' | ||
); | ||
} | ||
const totalHeaders: Headers = new Headers(necessaryHeaders); | ||
const customHeaders = requestOptions?.customHeaders ?? new Headers(); | ||
for (const [key, val] of customHeaders.entries()) { | ||
totalHeaders.append(key, val); | ||
} | ||
if (requestOptions?.apiClient) { | ||
totalHeaders.append(X_GOOG_API_CLIENT_HEADER, requestOptions?.apiClient); | ||
} | ||
// Resolve header conflicts. | ||
let goldenHeaders: Headers; | ||
if (vertexBaseEndpoint.endsWith(GOOGLE_INTERNAL_ENDPOINT)) { | ||
goldenHeaders = necessaryHeaders; | ||
} else { | ||
goldenHeaders = customHeaders; | ||
} | ||
for (const header of SERVER_RESERVED_HEADERS) { | ||
if (goldenHeaders.has(header)) { | ||
totalHeaders.set(header, goldenHeaders.get(header)!); | ||
} | ||
} | ||
return totalHeaders; | ||
} |
@@ -64,2 +64,3 @@ /** | ||
private readonly apiEndpoint?: string; | ||
private readonly systemInstruction?: Content; | ||
@@ -88,2 +89,6 @@ async getHistory(): Promise<Content[]> { | ||
this.requestOptions = requestOptions ?? {}; | ||
if (request.systemInstruction) { | ||
request.systemInstruction.role = constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = request.systemInstruction; | ||
} | ||
@@ -132,2 +137,3 @@ | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -216,2 +222,3 @@ | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -264,2 +271,3 @@ | ||
private readonly apiEndpoint?: string; | ||
private readonly systemInstruction?: Content; | ||
@@ -288,2 +296,6 @@ async getHistory(): Promise<Content[]> { | ||
this.requestOptions = requestOptions ?? {}; | ||
if (request.systemInstruction) { | ||
request.systemInstruction.role = constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = request.systemInstruction; | ||
} | ||
@@ -331,2 +343,3 @@ | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -416,2 +429,3 @@ | ||
tools: this.tools, | ||
systemInstruction: this.systemInstruction, | ||
}; | ||
@@ -418,0 +432,0 @@ |
@@ -27,2 +27,3 @@ /** | ||
import { | ||
Content, | ||
CountTokensRequest, | ||
@@ -58,2 +59,3 @@ CountTokensResponse, | ||
private readonly requestOptions?: RequestOptions; | ||
private readonly systemInstruction?: Content; | ||
private readonly project: string; | ||
@@ -79,2 +81,6 @@ private readonly location: string; | ||
this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; | ||
if (getGenerativeModelParams.systemInstruction) { | ||
getGenerativeModelParams.systemInstruction.role = constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = getGenerativeModelParams.systemInstruction; | ||
if (this.model.startsWith('models/')) { | ||
@@ -120,2 +126,8 @@ this.publisherModelEndpoint = `publishers/google/${this.model}`; | ||
): Promise<GenerateContentResult> { | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = | ||
formulateSystemInstructionIntoGenerateContentRequest( | ||
request, | ||
this.systemInstruction | ||
); | ||
return generateContent( | ||
@@ -126,3 +138,3 @@ this.location, | ||
this.fetchToken(), | ||
request, | ||
formulatedRequest, | ||
this.apiEndpoint, | ||
@@ -163,2 +175,8 @@ this.generationConfig, | ||
): Promise<StreamGenerateContentResult> { | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = | ||
formulateSystemInstructionIntoGenerateContentRequest( | ||
request, | ||
this.systemInstruction | ||
); | ||
return generateContentStream( | ||
@@ -169,3 +187,3 @@ this.location, | ||
this.fetchToken(), | ||
request, | ||
formulatedRequest, | ||
this.apiEndpoint, | ||
@@ -250,2 +268,4 @@ this.generationConfig, | ||
startChatRequest.apiEndpoint = request.apiEndpoint ?? this.apiEndpoint; | ||
startChatRequest.systemInstruction = | ||
request.systemInstruction ?? this.systemInstruction; | ||
} | ||
@@ -268,2 +288,3 @@ return new ChatSession(startChatRequest, this.requestOptions); | ||
private readonly requestOptions?: RequestOptions; | ||
private readonly systemInstruction?: Content; | ||
private readonly project: string; | ||
@@ -289,2 +310,6 @@ private readonly location: string; | ||
this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; | ||
if (getGenerativeModelParams.systemInstruction) { | ||
getGenerativeModelParams.systemInstruction.role = constants.SYSTEM_ROLE; | ||
} | ||
this.systemInstruction = getGenerativeModelParams.systemInstruction; | ||
if (this.model.startsWith('models/')) { | ||
@@ -329,2 +354,8 @@ this.publisherModelEndpoint = `publishers/google/${this.model}`; | ||
): Promise<GenerateContentResult> { | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = | ||
formulateSystemInstructionIntoGenerateContentRequest( | ||
request, | ||
this.systemInstruction | ||
); | ||
return generateContent( | ||
@@ -335,3 +366,3 @@ this.location, | ||
this.fetchToken(), | ||
request, | ||
formulatedRequest, | ||
this.apiEndpoint, | ||
@@ -372,2 +403,8 @@ this.generationConfig, | ||
): Promise<StreamGenerateContentResult> { | ||
request = formulateRequestToGenerateContentRequest(request); | ||
const formulatedRequest = | ||
formulateSystemInstructionIntoGenerateContentRequest( | ||
request, | ||
this.systemInstruction | ||
); | ||
return generateContentStream( | ||
@@ -378,3 +415,3 @@ this.location, | ||
this.fetchToken(), | ||
request, | ||
formulatedRequest, | ||
this.apiEndpoint, | ||
@@ -458,2 +495,4 @@ this.generationConfig, | ||
startChatRequest.tools = request.tools ?? this.tools; | ||
startChatRequest.systemInstruction = | ||
request.systemInstruction ?? this.systemInstruction; | ||
} | ||
@@ -463,1 +502,26 @@ return new ChatSessionPreview(startChatRequest, this.requestOptions); | ||
} | ||
function formulateRequestToGenerateContentRequest( | ||
request: GenerateContentRequest | string | ||
): GenerateContentRequest { | ||
if (typeof request === 'string') { | ||
return { | ||
contents: [{role: constants.USER_ROLE, parts: [{text: request}]}], | ||
} as GenerateContentRequest; | ||
} | ||
return request; | ||
} | ||
function formulateSystemInstructionIntoGenerateContentRequest( | ||
methodRequest: GenerateContentRequest, | ||
classSystemInstruction?: Content | ||
): GenerateContentRequest { | ||
if (methodRequest.systemInstruction) { | ||
methodRequest.systemInstruction.role = constants.SYSTEM_ROLE; | ||
return methodRequest; | ||
} | ||
if (classSystemInstruction) { | ||
methodRequest.systemInstruction = classSystemInstruction; | ||
} | ||
return methodRequest; | ||
} |
@@ -50,2 +50,6 @@ /** | ||
contents: Content[]; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -106,2 +110,6 @@ | ||
requestOptions?: RequestOptions; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -131,2 +139,6 @@ | ||
tools?: Tool[]; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -874,2 +886,6 @@ | ||
apiEndpoint?: string; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -889,2 +905,6 @@ | ||
publisherModelEndpoint: string; | ||
/** Optional. The user provided system instructions for the model. | ||
* Note: only text should be used in parts of {@link Content} | ||
*/ | ||
systemInstruction?: Content; | ||
} | ||
@@ -904,2 +924,6 @@ | ||
apiClient?: string; | ||
/** | ||
* Value for custom HTTP headers to set on the HTTP request. | ||
*/ | ||
customHeaders?: Headers; | ||
} |
@@ -21,4 +21,5 @@ /** | ||
export const MODEL_ROLE = 'model'; | ||
export const SYSTEM_ROLE = 'system'; | ||
const USER_AGENT_PRODUCT = 'model-builder'; | ||
const CLIENT_LIBRARY_VERSION = '1.0.0'; // x-release-please-version | ||
const CLIENT_LIBRARY_VERSION = '1.1.0'; // x-release-please-version | ||
const CLIENT_LIBRARY_LANGUAGE = `grpc-node/${CLIENT_LIBRARY_VERSION}`; | ||
@@ -25,0 +26,0 @@ export const USER_AGENT = `${USER_AGENT_PRODUCT}/${CLIENT_LIBRARY_VERSION} ${CLIENT_LIBRARY_LANGUAGE}`; |
@@ -125,2 +125,3 @@ /** | ||
requestOptions: requestOptions, | ||
systemInstruction: modelParams.systemInstruction, | ||
}; | ||
@@ -187,2 +188,3 @@ return new GenerativeModel(getGenerativeModelParams); | ||
requestOptions: requestOptions, | ||
systemInstruction: modelParams.systemInstruction, | ||
}; | ||
@@ -189,0 +191,0 @@ return new GenerativeModelPreview(getGenerativeModelParams); |
@@ -646,3 +646,3 @@ /** | ||
}); | ||
it('should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => { | ||
xit('should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => { | ||
const generativeTextModel = vertexAI.getGenerativeModel({ | ||
@@ -649,0 +649,0 @@ model: TEXT_MODEL_NAME, |
@@ -9,3 +9,4 @@ { | ||
"es2018", | ||
"dom" | ||
"dom", | ||
"dom.iterable" | ||
] | ||
@@ -12,0 +13,0 @@ }, |
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
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
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
1025580
148
18890
427