Skip to content

Commit

Permalink
add ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangWei-KUMO committed Dec 15, 2024
1 parent 59c8526 commit 71bd84b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion db/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
console.error("Error checking agentConfig table:", err.message);
} else if (row.count === 0) {
const defaultEmailConfig = {
model: 'gemini-1.5-pro',
model: 'gemini-1.5-flash',
prompt: '你是一个机器人',
tasks: '其他',
news: 0,
Expand Down
16 changes: 10 additions & 6 deletions util/gemini.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/* eslint-disable no-undef */
import {GoogleGenerativeAI} from '@google/generative-ai';
import 'dotenv/config'
import {getAgentConfig} from '../db/agent.js'
import { getConfig } from '../db/config.js';
let agent = await getAgentConfig();
let config = await getConfig();
const genAI = new GoogleGenerativeAI(config.geminiApiKey || configprocess.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model:agent.model || "gemini-1.5-flash" });

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

export const recgonizeImage = async (prompt) => {
export const recgonizeImage = async (buffer,question) => {
const image = {
inlineData: {
data: Buffer.from(fs.readFileSync("cookie.jpg")).toString("base64"),
// data: Buffer.from(fs.readFileSync("cookie.jpg")).toString("base64"),
data: buffer.toString("base64"),
mimeType: "image/png",
}};

const result = await model.generateContent([prompt, image]);
const result = await model.generateContent([question, image]);
return result.response.text()
}

Expand Down
29 changes: 19 additions & 10 deletions util/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import process from 'process';
import schedule from 'node-schedule';
import {getNews} from './group.js'
import {recgonizeImage} from './gemini.js'

export const singleChat = async (talkerId,listenerId,text) => {
if(talkerId!==listenerId && talkerId!=='weixin' && text!==''){
Expand Down Expand Up @@ -260,14 +261,22 @@ export const handlePush = async (rooms) => {
});
}
}

export const handleImage = async (message,talkerId) => {
try{
// const imageFileBox = await message.toFileBox();
// let image = await imageFileBox.toFile();
await sendMessage(talkerId, "好的,我先看下");
}catch(e){
await sendMessage(talkerId, e)
return
// 处理图片,该函数的触发条件是指用户在当前文本信息之前发送了一张图片
export const handleImage = async (message, talkerId,question) => {
try{
const imageFileBox = await message.toFileBox();
let image = await imageFileBox.toBuffer();
let mimeType = imageFileBox.mimeType
if (!mimeType) {
await sendMessage(talkerId, '无法获取图片 mimeType,请检查图片格式');
return
}
}
let res = await recgonizeImage(image, question)
await sendMessage(talkerId, res);
}catch(e){
// 修改这里,提取错误信息
let errorMessage = `图片处理出错: ${e.message || String(e)}`;
await sendMessage(talkerId, errorMessage);
return
}
}

0 comments on commit 71bd84b

Please sign in to comment.