Skip to content

Commit

Permalink
add image analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangWei-KUMO committed Dec 19, 2024
1 parent 216f720 commit 2f37d6d
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 44 deletions.
4 changes: 1 addition & 3 deletions db/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('Agent配置数据库连接成功');
db.run(`
CREATE TABLE IF NOT EXISTS agentConfig (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -35,8 +34,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
cryptoCurrency: 0,
};
insertConfig(defaultEmailConfig)
.then(() => console.log("Agent写入默认配置"))
.catch(err => console.error("Agent写入数据错误:", err));

}
});
}
Expand Down
1 change: 0 additions & 1 deletion db/chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ let db = new sqlite3.Database(dbFile, (err) => {
if (err) {
// console.error(err.message);
} else {
console.log('连接聊天记录数据库');
db.run(`
CREATE TABLE IF NOT EXISTS chats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down
7 changes: 2 additions & 5 deletions db/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('配置文件数据库连接成功');
// Create the config table if it doesn't exist

db.run(`
CREATE TABLE IF NOT EXISTS config (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down Expand Up @@ -40,9 +39,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
isPushEnable: 0,

};
insertConfig(defaultConfig) // Use the insertConfig function
.then(() => console.log("写入默认配置"))
.catch(err => console.error("写入数据错误:", err));
insertConfig(defaultConfig)
}
});
}
Expand Down
6 changes: 2 additions & 4 deletions db/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('邮箱配置数据库连接成功');
// Create the email config table if it doesn't exist

db.run(`
CREATE TABLE IF NOT EXISTS emailConfig (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down Expand Up @@ -40,8 +39,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
content: '请及时检查您的机器人掉线状况'
};
insertEmailConfig(defaultEmailConfig)
.then(() => console.log("邮箱写入默认配置"))
.catch(err => console.error("邮箱写入数据错误:", err));

}
});
}
Expand Down
1 change: 0 additions & 1 deletion db/flashmemories.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const db = new sqlite3.Database(dbFile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CR
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('连接记忆数据库成功');
db.run(`
CREATE TABLE IF NOT EXISTS flashmemories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down
1 change: 0 additions & 1 deletion db/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const db = new sqlite3.Database(dbFile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CR
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('连接图片数据库成功');
db.run(`
CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down
2 changes: 0 additions & 2 deletions db/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const db = new sqlite3.Database(dbFile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CR
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('连接日志数据库成功');
// Create the logs table
db.run(`
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down
6 changes: 2 additions & 4 deletions db/tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('TTS配置数据库连接成功');
// Create the email config table if it doesn't exist

db.run(`
CREATE TABLE IF NOT EXISTS ttsConfig (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -30,8 +29,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
azureregion: '',
};
insertTtsConfig(defaultEmailConfig)
.then(() => console.log("TTS写入默认配置"))
.catch(err => console.error("TTS写入数据错误:", err));

}
});
}
Expand Down
1 change: 0 additions & 1 deletion db/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('连接用户数据库');
// 创建用户表(如果不存在)
db.run(`
CREATE TABLE IF NOT EXISTS users (
Expand Down
5 changes: 1 addition & 4 deletions db/wechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const db = new sqlite3.Database(dbFile, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('连接微信数据库');
// 创建用户表(如果不存在)
db.run(`
CREATE TABLE IF NOT EXISTS wechats (
Expand All @@ -31,9 +30,7 @@ const db = new sqlite3.Database(dbFile, (err) => {
friends: '',
loginurl: '',
};
insertWechat(defaultConfig) // Use the insertConfig function
.then(() => console.log("写入默认配置"))
.catch(err => console.error("写入数据错误:", err));
insertWechat(defaultConfig)
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ app.use(staticRouter);

export async function prepareBot() {
bot.on("message", async (message) => {

const contact = message.talker();

if (contact.self()) {
return;
}
if (message.room()) {
groupChat(message,talkerId)
}

let {payload} = message;
let {talkerId,listenerId,text} = payload;
switch (message.type()) {
Expand Down
14 changes: 8 additions & 6 deletions router/public/flashmemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,10 @@
title: 'ID',
dataIndex: 'id',
},
{
title: 'talkid',
dataIndex: 'talkid',
},
{
title: '时间',
dataIndex: 'timestamp',
render: (timestamp) => moment(timestamp).format('YYYY-MM-DD HH:mm:ss'),
render: (timestamp) => moment(timestamp).format('YYYY-MM-DD HH:mm:ss'),
},
{
title: '类型',
Expand All @@ -220,7 +216,13 @@
{
title: '消息内容',
dataIndex: 'content',
width: '40%',
width: '40%',
render:(content)=>(
<div style={{wordWrap:'break-word',wordBreak:'break-all'}}>
<img src={`data:image/jpeg;base64,${content}`} width={100}/>
</div>
)

},
];

Expand Down
14 changes: 7 additions & 7 deletions router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ router.post('/api/login', async (req, res) => {
});

router.get('/api/logs', (req, res) => {
// getLogs().then((data) => {
// res.json(data);
// });
getLogs().then((data) => {
res.json(data);
});
});

router.delete('/api/logs', (req, res) => {
Expand Down Expand Up @@ -181,14 +181,14 @@ router.post('/api/login', async (req, res) => {
res.json(data);
});
});
// 获取flashmemory
router.get('/api/flashmemory', (req, res) => {
// 获取全部flashmemory
router.get('/api/flashmemories', (req, res) => {
getFlashMemories().then((data) => {
res.json(data);
});
});
// 删除flashmemory
router.delete('/api/flashmemory', (req, res) => {
// 删除全部flashmemory
router.delete('/api/flashmemories', (req, res) => {
deleteFlashMemories().then((data) => {
res.json(data);
});
Expand Down
2 changes: 1 addition & 1 deletion util/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const recgonizeImage = async (buffer,question) => {
mimeType: "image/png",
}};

const result = await model.generateContent([question, image]);
const result = await model.generateContent([question+"。请用中文回答", image]);
return result.response.text()
}

Expand Down
6 changes: 3 additions & 3 deletions util/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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'
import {recgonizeImage} from './gemini.js'
import {saveFlashMemory} from '../db/flashmemories.js'

export const singleChat = async (talkerId,listenerId,text) => {
Expand Down Expand Up @@ -276,8 +276,8 @@ export const handleImage = async (message, talkerId) => {
let base64String = buffer.toString("base64")
// 存入记忆中
saveFlashMemory(talkerId, base64String, 'image')
// 识别图片
// let res = await recgonizeImage(image, question)
// // 识别图片
// let res = await recgonizeImage(base64String, "请告诉我这是什么")
// await sendMessage(talkerId, res);
}catch(e){
// 修改这里,提取错误信息
Expand Down
12 changes: 11 additions & 1 deletion util/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ config();
import { PuppetWechat4u } from 'wechaty-puppet-wechat4u';
import fs from 'fs';
import {getNews} from './group.js'
import {classfication, stockCheck,chat} from './gemini.js'
import {classfication, stockCheck,chat,recgonizeImage} from './gemini.js'
import moment from 'moment';
import {deleteFlashMemory, getFlashMemory} from '../db/flashmemories.js'

import 'dotenv/config'
// import Redis from 'ioredis';
// const redis = new Redis();
Expand Down Expand Up @@ -151,6 +153,14 @@ const getBinanceRanker = async () => {
};

export const think = async (talkid,query) => {
/**
* 首先,检查当前是否存在短期记忆,如图片、小程序、地址等
*/
const flashMemory = await getFlashMemory(talkid);
if(flashMemory && flashMemory.type=='image'){
deleteFlashMemory(talkid)
return await recgonizeImage(flashMemory.content,query)
}
// const filePath = `./logger/${talkid}.json`;
// let longMemory = "";
// if (fs.existsSync(filePath)) {
Expand Down

0 comments on commit 2f37d6d

Please sign in to comment.