这是我的第一个 vertexAI 节点代码。我参考了谷歌文档中的示例。 我在我的 windows glcoud cli 中运行它。当我卷曲生成内容时,相同的查询工作正常,但使用节点编写时则不行。
这是错误:
undefined:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
.
.
.
at async throwErrorIfNotOK (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\post_fetch_processing.js:29:27)
at async generateContent (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\generate_content.js:58:5)
at async run (C:\code\node\testvert.js:19:15)
Node.js v22.9.0
这是相关片段:
const {
VertexAI
} = require('@google-cloud/vertexai');
const vertexAI = new VertexAI({project: PROJECT_NAME, location: 'asia-south1-a'});
// Instantiate Gemini models
const jdGenerativeModel = vertexAI.getGenerativeModel({model: 'gemini-1.0-pro'});
async function run(jobDesc) {
let command = "What is the capital of India?";
let request = {
contents: [{role: 'user', parts: command}]
};
let result = await jdGenerativeModel.generateContent(command);
let response = await result.json();
console.log(response);
};
run();
错误指向这一行:jdGenerativeModel.generateContent(command);
我尝试将原始代码中所有不必要的行削减到只剩下骨头。 卸载了谷歌云,重新启动并重新安装。
const {VertexAI} = require('@google-cloud/vertexai');
const vertex_ai = new VertexAI({project: PROJECT_NAME, location: 'asia-south1-a'});
const model = 'gemini-1.0-pro';
const generativeModel = vertex_ai.preview.getGenerativeModel({
model: model,
generationConfig: {
'maxOutputTokens': 8192,
'temperature': 1,
'topP': 0.95,
},
safetySettings: [
{
'category': 'HARM_CATEGORY_HATE_SPEECH',
'threshold': 'OFF',
},
{
'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',
'threshold': 'OFF',
},
{
'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
'threshold': 'OFF',
},
{
'category': 'HARM_CATEGORY_HARASSMENT',
'threshold': 'OFF',
}
],
});
async function generateContent() {
const req = {
contents: [
{role: 'user', parts: [{text: `What is the capital of India?`}]}
],
};
const streamingResp = await generativeModel.generateContentStream(req);
for await (const item of streamingResp.stream) {
process.stdout.write('stream chunk: ' + JSON.stringify(item) + '\n');
}
process.stdout.write('aggregated response: ' + JSON.stringify(await streamingResp.response));
}
generateContent();