|
| 1 | +import { VertexAI,GenerateContentRequest,GenerateContentResult} from '@google-cloud/vertexai'; |
| 2 | +import { GoogleGenerativeAI,HarmBlockThreshold, HarmCategory } from '@google/generative-ai'; |
| 3 | +import * as fs from "fs" |
| 4 | +import * as path from 'path' |
| 5 | +// 环境配置 |
| 6 | +import * as dotenv from 'dotenv'; |
| 7 | +dotenv.config(); |
| 8 | +// 系统代理 |
| 9 | +const { setGlobalDispatcher, ProxyAgent } = require("undici"); |
| 10 | +console.log(`HTTPS_PROXY:${process.env.HTTPS_PROXY}`); |
| 11 | +const dispatcher = new ProxyAgent({ uri: new URL(process.env.HTTPS_PROXY||'http://127.0.0.1:7890').toString() }); |
| 12 | +//全局fetch调用启用代理 |
| 13 | +setGlobalDispatcher(dispatcher); |
| 14 | + |
| 15 | + |
| 16 | +const PRODUCTID = '78679854628'; |
| 17 | +const LOCATION = 'asia-east1'; // asia-east1(台湾)、asia-northeast1(日本)、asia-southeast1(新加坡) |
| 18 | + |
| 19 | +/** |
| 20 | + * 创建图像 |
| 21 | + * @param projectId |
| 22 | + * @param location |
| 23 | + * @param prompt |
| 24 | + */ |
| 25 | +export async function generateImagesPrompt(prompt: string ) { |
| 26 | + const vertexAI = new VertexAI({ project: PRODUCTID, location: LOCATION }); |
| 27 | + const model = vertexAI.getGenerativeModel({ |
| 28 | + model: 'gemini-pro-vision', // 如果想输入图片,需要使用gemini-pro-vision |
| 29 | + }); |
| 30 | + |
| 31 | + const request = { |
| 32 | + contents: [{ parts: [{ text: prompt }] }], |
| 33 | + } as GenerateContentRequest; |
| 34 | + |
| 35 | + const response = await model.generateContent(request) as GenerateContentResult; |
| 36 | + if (!response || !response.response|| !response.response.candidates) throw new Error('Browser or Page not initialized'); |
| 37 | + const image = await response.response.candidates[0].content.parts[0]; |
| 38 | + console.log(JSON.stringify(image,null,4)); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +// // console.log(`GEMINI_API_KEY:${process.env.GEMINI_API_KEY}`); |
| 45 | + |
| 46 | +// // 以环境变量形式访问您的 API 密钥(请参阅上面的“设置您的 API 密钥”) |
| 47 | +// const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || ""); |
| 48 | + |
| 49 | + |
| 50 | +// // 将本地文件信息转换为 GoogleGenerativeAI.Part 对象。 |
| 51 | +// function fileToGenerativePart(path:string, mimeType:string) { |
| 52 | +// return { |
| 53 | +// inlineData: { |
| 54 | +// data: Buffer.from(fs.readFileSync(path)).toString("base64"), |
| 55 | +// mimeType |
| 56 | +// }, |
| 57 | +// }; |
| 58 | +// } |
| 59 | + |
| 60 | +// /** |
| 61 | +// * AIGC创建图像 |
| 62 | +// * @param prompt 提示词 |
| 63 | +// */ |
| 64 | +// export async function generateImagesPrompt(prompt:string) { |
| 65 | +// // Gemini 1.5 型号功能多样,既可处理纯文本提示,也可处理多模式提示 |
| 66 | +// const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); |
| 67 | + |
| 68 | +// // const prompt = "What's different between these pictures?"; |
| 69 | + |
| 70 | +// // const imageParts = [ |
| 71 | +// // fileToGenerativePart("image1.png", "image/png"), |
| 72 | +// // fileToGenerativePart("image2.jpeg", "image/jpeg"), |
| 73 | +// // ]; |
| 74 | + |
| 75 | +// const result = await model.generateContent([prompt]); // , ...imageParts |
| 76 | +// const response = await result.response; |
| 77 | +// const text = response.text(); |
| 78 | +// console.log(text); |
| 79 | +// } |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
0 commit comments