Skip to content

Commit 33eea80

Browse files
committed
fix:写入代码更新
1 parent f317e15 commit 33eea80

File tree

5 files changed

+113
-6
lines changed

5 files changed

+113
-6
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
~~~
3+
# 调试
4+
npm run dev
5+
6+
7+
# 编译
8+
tsc
9+
10+
# 启动
11+
npm run start
12+
13+
# 单元测试
14+
npm test
15+
16+
~~~
17+
18+
~~~
19+
soft oil painting of a close up of a hummingbird, pretty pink flower --ar 3:4 --v 6.1
20+
~~~

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"test": "jest"
99
},
1010
"dependencies": {
11+
"@google-cloud/vertexai": "^1.9.3",
1112
"@google/generative-ai": "^0.21.0",
1213
"express": "^4.21.2",
1314
"langchain": "^0.3.15",

src/controllers/userController.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Request, Response } from 'express';
22
import { User } from '../models/user';
33
import * as langchainService from '../service/langchain';
4-
import * as geminiService from '../service/gemini';
4+
import * as geminiTextService from '../service/gemini.text';
5+
import * as geminiImageService from '../service/gemini.image';
56

67
let users: User[] = [
78
{ id: 1, name: 'Alice', age: 30 },
@@ -34,14 +35,14 @@ export const createUser = (req: Request, res: Response) => {
3435

3536
// 测试方法
3637
export const ai = async (req: Request, res: Response) => {
37-
//推理会话:geminiService.generateResponse(req.query.prompt)
38-
// const result: any = await geminiService.generateResponse(req.query.prompt); // 调用方法
38+
// 推理会话
39+
// const result: any = await geminiTextService.generateResponse(req.query.prompt); // 调用方法
3940

40-
// //推理图片:geminiService.generateImagesPrompt()
41-
const result: any = await geminiService.generateImagesPrompt(); // 调用方法
41+
// 推理图片
42+
const result: any = await geminiImageService.generateImagesPrompt(req.query.prompt as string); // 调用方法
4243

4344
// 提取json
4445
// const json = await langchainService.extractInformation(result.response.text());
4546

46-
res.status(201).json(result);
47+
res.status(201).send(result);
4748
};

src/service/gemini.image.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
File renamed without changes.

0 commit comments

Comments
 (0)