Google ai
由于谷歌应用国内访问不到,最好全部写成一个小型的server 调用
初始化
pnpm add @google/generative-ai
JSON 格式返回
const { GoogleGenerativeAI, SchemaType } = require("@google/generative-ai");
async function run() {
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const schema = {
description: "小说章节情节分析",
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
chapter: {
type: SchemaType.STRING,
description: "章节名",
nullable: false,
},
summary: {
type: SchemaType.STRING,
description: "小说章节情节分析,不能遗漏重要内容,字数在200字左右",
nullable: false,
}
},
required: ["chapter", "summary"],
},
};
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
generationConfig: {
responseMimeType: "application/json",
responseSchema: schema,
},
});
const ps = novals.map(val => {
return `
\`\`\`txt
${val.content}
\`\`\`
`
}).join("\n");
const p = "请将每章内容分为多个情节:\n" + ps
const result = await model.generateContent(p);
console.log(result.response.text());
}
run().then(() => console.log("done"))