Electron Forge【实战】带图片的 AI 聊天

改用支持图片的 AI 模型

qwen-turbo 仅支持文字,要想体验图片聊天,需改用 qwen-vl-plus

src/initData.ts

  {id: 2,name: "aliyun",title: "阿里 -- 通义千问",desc: "阿里百炼 -- 通义千问",// https://help.aliyun.com/zh/dashscope/developer-reference/api-details?spm=a2c4g.11186623.0.0.5bf41507xgULX5#b148acc634pfcmodels: ["qwen-turbo", "qwen-vl-plus"],avatar:"https://qph.cf2.poecdn.net/main-thumb-pb-4160791-200-qlqunomdvkyitpedtghnhsgjlutapgfl.jpeg",},

安装依赖 mime-types

用于便捷获取图片的类型

npm i mime-types @types/mime-types --save-dev

提问框中选择本地图片

在这里插入图片描述

src/components/MessageInput.vue

<template><divclass="message-input w-full shadow-sm border rounded-lg border-gray-300 py-1 px-2 focus-within:border-green-700"><div v-if="imagePreview" class="my-2 relative inline-block"><img:src="imagePreview"alt="Preview"class="h-24 w-24 object-cover rounded"/><Iconicon="lets-icons:dell-fill"width="24"@click="delImg"class="absolute top-[-10px] right-[-10px] p-1 rounded-full cursor-pointer"/></div><div class="flex items-center"><inputtype="file"accept="image/*"ref="fileInput"class="hidden"@change="handleImageUpload"/><Iconicon="radix-icons:image"width="24"height="24":class="['mr-2',disabled? 'text-gray-300 cursor-not-allowed': 'text-gray-400 cursor-pointer hover:text-gray-600',]"@click="triggerFileInput"/><inputclass="outline-none border-0 flex-1 bg-white focus:ring-0"type="text"ref="ref_input"v-model="model":disabled="disabled":placeholder="tip"@keydown.enter="onCreate"/><Buttonicon-name="radix-icons:paper-plane"@click="onCreate":disabled="disabled">发送</Button></div></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import { Icon } from "@iconify/vue";import Button from "./Button.vue";const props = defineProps<{disabled?: boolean;
}>();
const emit = defineEmits<{create: [value: string, imagePath?: string];
}>();
const model = defineModel<string>();
const fileInput = ref<HTMLInputElement | null>(null);
const imagePreview = ref("");
const triggerFileInput = () => {if (!props.disabled) {fileInput.value?.click();}
};
const tip = ref("");
let selectedImage: File | null = null;
const handleImageUpload = (event: Event) => {const target = event.target as HTMLInputElement;if (target.files && target.files.length > 0) {selectedImage = target.files[0];const reader = new FileReader();reader.onload = (e) => {imagePreview.value = e.target?.result as string;};reader.readAsDataURL(selectedImage);}
};
const onCreate = async () => {if (model.value && model.value.trim() !== "") {if (selectedImage) {const filePath = window.electronAPI.getFilePath(selectedImage);emit("create", model.value, filePath);} else {emit("create", model.value);}selectedImage = null;imagePreview.value = "";} else {tip.value = "请输入问题";}
};
const ref_input = ref<HTMLInputElement | null>(null);const delImg = () => {selectedImage = null;imagePreview.value = "";
};defineExpose({ref_input: ref_input,
});
</script><style scoped>
input::placeholder {color: red;
}
</style>

src/preload.ts

需借助 webUtils 从 File 对象中获取文件路径

import { ipcRenderer, contextBridge, webUtils } from "electron";
 getFilePath: (file: File) => webUtils.getPathForFile(file),

将选择的图片,转存到应用的用户目录

图片很占空间,转为字符串直接存入数据库压力过大,合理的方案是存到应用本地

src/views/Home.vue

在创建会话时执行

const createConversation = async (question: string, imagePath?: string) => {const [AI_providerName, AI_modelName] = currentProvider.value.split("/");let copiedImagePath: string | undefined;if (imagePath) {try {copiedImagePath = await window.electronAPI.copyImageToUserDir(imagePath);} catch (error) {console.error("拷贝图片失败:", error);}}// 用 dayjs 得到格式化的当前时间字符串const currentTime = dayjs().format("YYYY-MM-DD HH:mm:ss");// pinia 中新建会话,得到新的会话idconst conversationId = await conversationStore.createConversation({title: question,AI_providerName,AI_modelName,createdAt: currentTime,updatedAt: currentTime,msgList: [{type: "question",content: question,// 如果有图片路径,则将其添加到消息中...(copiedImagePath && { imagePath: copiedImagePath }),createdAt: currentTime,updatedAt: currentTime,},{type: "answer",content: "",status: "loading",createdAt: currentTime,updatedAt: currentTime,},],});// 更新当前选中的会话conversationStore.selectedId = conversationId;// 右侧界面--跳转到会话页面 -- 带参数 init 为新创建的会话的第一条消息idrouter.push(`/conversation/${conversationId}?type=new`);
};

src/preload.ts

  // 拷贝图片到本地用户目录copyImageToUserDir: (sourcePath: string) =>ipcRenderer.invoke("copy-image-to-user-dir", sourcePath),

src/ipc.ts

  // 拷贝图片到本地用户目录ipcMain.handle("copy-image-to-user-dir",async (event, sourcePath: string) => {const userDataPath = app.getPath("userData");const imagesDir = path.join(userDataPath, "images");await fs.mkdir(imagesDir, { recursive: true });const fileName = path.basename(sourcePath);const destPath = path.join(imagesDir, fileName);await fs.copyFile(sourcePath, destPath);return destPath;});

将图片信息传给 AI

src/views/Conversation.vue

发起 AI 聊天传图片参数

// 访问 AI 模型,获取答案
const get_AI_answer = async (answerIndex: number) => {await window.electronAPI.startChat({messageId: answerIndex,providerName: convsersation.value!.AI_providerName,selectedModel: convsersation.value!.AI_modelName,// 发给AI模型的消息需移除最后一条加载状态的消息,使最后一条消息为用户的提问messages: convsersation.value!.msgList.map((message) => ({role: message.type === "question" ? "user" : "assistant",content: message.content,// 若有图片信息,则将其添加到消息中...(message.imagePath && { imagePath: message.imagePath }),})).slice(0, -1),});
};

继续向 AI 提问时图片参数

const sendNewMessage = async (question: string, imagePath?: string) => {let copiedImagePath: string | undefined;if (imagePath) {try {copiedImagePath = await window.electronAPI.copyImageToUserDir(imagePath);} catch (error) {console.error("拷贝图片失败:", error);}}// 获取格式化的当前时间let currentTime = dayjs().format("YYYY-MM-DD HH:mm:ss");// 向消息列表中追加新的问题convsersation.value!.msgList.push({type: "question",content: question,...(copiedImagePath && { imagePath: copiedImagePath }),createdAt: currentTime,updatedAt: currentTime,});// 向消息列表中追加 loading 状态的回答let new_msgList_length = convsersation.value!.msgList.push({type: "answer",content: "",createdAt: currentTime,updatedAt: currentTime,status: "loading",});// 消息列表的最后一条消息为 loading 状态的回答,其id为消息列表的长度 - 1let loading_msg_id = new_msgList_length - 1;// 访问 AI 模型获取答案,参数为 loading 状态的消息的idget_AI_answer(loading_msg_id);// 清空问题输入框inputValue.value = "";await messageScrollToBottom();// 发送问题后,问题输入框自动聚焦if (dom_MessageInput.value) {dom_MessageInput.value.ref_input.focus();}
};

src/providers/OpenAIProvider.ts

将消息转换为 AI 模型需要的格式后传给 AI

import OpenAI from "openai";
import { convertMessages } from "../util";interface ChatMessageProps {role: string;content: string;imagePath?: string;
}interface UniversalChunkProps {is_end: boolean;result: string;
}export class OpenAIProvider {private client: OpenAI;constructor(apiKey: string, baseURL: string) {this.client = new OpenAI({apiKey,baseURL,});}async chat(messages: ChatMessageProps[], model: string) {// 将消息转换为AI模型需要的格式const convertedMessages = await convertMessages(messages);const stream = await this.client.chat.completions.create({model,messages: convertedMessages as any,stream: true,});const self = this;return {async *[Symbol.asyncIterator]() {for await (const chunk of stream) {yield self.transformResponse(chunk);}},};}protected transformResponse(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): UniversalChunkProps {const choice = chunk.choices[0];return {is_end: choice.finish_reason === "stop",result: choice.delta.content || "",};}
}

src/util.ts

函数封装 – 将消息转换为 AI 模型需要的格式

import fs from 'fs/promises'
import { lookup } from 'mime-types'
export async function convertMessages( messages:  { role: string; content: string, imagePath?: string}[]) {const convertedMessages = []for (const message of messages) {let convertedContent: string | any[]if (message.imagePath) {const imageBuffer = await fs.readFile(message.imagePath)const base64Image = imageBuffer.toString('base64')const mimeType = lookup(message.imagePath)convertedContent = [{type: "text",text: message.content || ""},{type: 'image_url',image_url: {url: `data:${mimeType};base64,${base64Image}`}}]} else {convertedContent = message.content}const { imagePath, ...messageWithoutImagePath } = messageconvertedMessages.push({...messageWithoutImagePath,content: convertedContent})}return convertedMessages
}

加载消息记录中的图片

渲染进程中,无法直接读取本地图片,需借助 protocol 实现

src/main.ts

import { app, BrowserWindow, protocol, net } from "electron";
import { pathToFileURL } from "node:url";
import path from "node:path";// windows 操作系统必要
protocol.registerSchemesAsPrivileged([{scheme: "safe-file",privileges: {standard: true,secure: true,supportFetchAPI: true,},},
]);

在 createWindow 方法内执行

  protocol.handle("safe-file", async (request) => {const userDataPath = app.getPath("userData");const imageDir = path.join(userDataPath, "images");// 去除协议头 safe-file://,解码 URL 中的路径const filePath = path.join(decodeURIComponent(request.url.slice("safe-file:/".length)));const filename = path.basename(filePath);const fileAddr = path.join(imageDir, filename);// 转换为 file:// URLconst newFilePath = pathToFileURL(fileAddr).toString();// 使用 net.fetch 加载本地文件return net.fetch(newFilePath);});

页面中渲染图片

在这里插入图片描述

src/components/MessageList.vue

img 的 src 添加了 safe-file:// 协议

          <div v-if="message.type === 'question'"><div class="mb-3 flex justify-end"><imgv-if="message.imagePath":src="`safe-file://${message.imagePath}`"alt="提问的配图"class="h-24 w-24 object-cover rounded"/></div><divclass="message-question bg-green-700 text-white p-2 rounded-md">{{ message.content }}</div></div>

最终效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/79010.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在 Elastic 中使用 JOIN 进行威胁狩猎!

作者&#xff1a;来自 Elastic Paul Ewing, Jonhnathan Ribeiro Elastic 的管道查询语言 ES | QL 为查询带来了 join 功能。 威胁狩猎者欢呼吧&#xff01;你是否一直在寻找一种通过 Elastic 的速度和强大功能来连接数据的方法&#xff1f;好消息&#xff01;Elastic 现在可以通…

从实列中学习linux shell5: 利用shell 脚本 检测硬盘空间容量,当使用量达到80%的时候 发送邮件

下面是用于检测硬盘空间并在使用量达到80%时发送邮件的Shell脚本 第一步 编写脚本 #!/bin/bash# 邮件配置 recipient"zhaoqingyou99qhzt.com" subject"磁盘空间警报" mail_cmd"/usr/bin/mail" # 根据实际邮件命令路径修改# 检查是否安装邮件工…

Ethan独立开发产品日报 | 2025-04-30

1. Daytona 安全且灵活的基础设施&#xff0c;用于运行你的人工智能生成代码。 Daytona Cloud重新定义了AI代理的基础设施&#xff0c;具备低于90毫秒的启动时间、原生性能和有状态执行能力&#xff0c;这些是传统云服务无法比拟的。您可以以前所未有的速度和灵活性来创建、管…

Unity SpriteMask(精灵遮罩)

&#x1f3c6; 个人愚见&#xff0c;没事写写笔记 &#x1f3c6;《博客内容》&#xff1a;Unity3D开发内容 &#x1f3c6;&#x1f389;欢迎 &#x1f44d;点赞✍评论⭐收藏 &#x1f50e;SpriteMask&#xff1a;精灵遮罩 &#x1f4a1;作用就是对精灵图片产生遮罩&#xff0c…

OpenHarmony全局资源调度管控子系统之内存管理部件

OpenHarmony之内存管理部件 内存管理部件 简介目录框架 进程回收优先级列表 补充 回收策略/查杀策略 使用说明参数配置说明 availbufferSizeZswapdParamkillConfignandlife 相关仓 简介 内存管理部件位于全局资源调度管控子系统中&#xff0c;基于应用的生命周期状态&#…

姜老师的MBTI课程笔记小结(1)ENFJ人格

课程文稿&#xff1a; 好&#xff0c;今天我们的重点其实并不在ENTJ&#xff0c;而是在于如果一个人其他都很像&#xff0c;只是在思考和感受这两端选择的时候&#xff0c;他缺了思考而更尊重感受&#xff0c;它会是什么样的一个人格特质呢&#xff1f;这就是ENFG在16人格的学派…

Node.js 应用场景

Node.js 应用场景 引言 Node.js 是一个基于 Chrome V8 JavaScript 引擎的开源、跨平台 JavaScript 运行环境。它主要用于服务器端开发&#xff0c;通过非阻塞 I/O 模型实现了高并发处理能力。本文将详细介绍 Node.js 的应用场景&#xff0c;帮助你了解其在实际项目中的应用。…

Qt/C++面试【速通笔记六】—Qt 中的线程同步

在多线程编程中&#xff0c;多个线程同时访问共享资源时&#xff0c;可能会出现数据不一致或者错误的情况。这时&#xff0c;我们需要线程同步机制来保证程序的正确性。Qt 提供了多种线程同步方式&#xff0c;每种方式适用于不同的场景。 1. 互斥锁&#xff08;QMutex&#xff…

JDK-17 保姆级安装教程(附安装包)

文章目录 一、下载二、安装三、验证是否安装成功1、看 java 和 javac 是否可用2、看 java 和 javac 的版本号是否无问题 一、下载 JDK-17_windows-x64_bin.exe 二、安装 三、验证是否安装成功 java&#xff1a;执行工具 javac&#xff1a;编译工具 1、看 java 和 javac 是否…

【LeetCode Hot100】回溯篇

前言 本文用于整理LeetCode Hot100中题目解答&#xff0c;因题目比较简单且更多是为了面试快速写出正确思路&#xff0c;只做简单题意解读和一句话题解方便记忆。但代码会全部给出&#xff0c;方便大家整理代码思路。 46. 全排列 一句话题意 给定一个无重复数字的序列&#xf…

pytest-前后置及fixture运用

1.pytest中的xunit风格前后置处理 pytest中用例的前后置可以直接使用类似于unittest中的前后置处理&#xff0c;但是pytest中的前后置处理方式更 加丰富&#xff0c;分为模块级、类级、方法级、函数级等不同等级的前后置处理&#xff0c;具体见下面的代码&#xff1a; test_…

使用scipy求解优化问题

一、求解二次规划问题 min(X.T * P * X C.T * X) s.t. Xi > 0 ∑Xi 1 1.定义目标函数 def objective(x):return 0.5 * np.dot(x, np.dot(P, x)) np.dot(c, x)2. 定义等式约束 def equality_constraint(x):return np.sum(x) - 1 3.定义边界约束&#xff1a;x # …

C++初阶-STL简介

目录 1.什么是STL 2.STL的版本 3.STL的六大组件 4.STL的重要性 4.1在笔试中 4.2在面试中 4.3.在公司中 5.如何学习STL 6.总结和之后的规划 1.什么是STL STL&#xff08;standard template library-标准模板库&#xff09;&#xff1b;是C标准库的重要组成部分&#xf…

kivy android打包buildozer.spec GUI配置

这个适合刚刚学习kivyd的道友使用&#xff0c;后面看情况更新 代码 import tkinter as tk from tkinter import ttk, filedialog, messagebox, simpledialog import configparser import os import json # 新增导入class BuildozerConfigTool:def __init__(self, master):se…

MOOS-ivp使用(一)——水下机器人系统的入门与使用

MOOS-ivp使用&#xff08;一&#xff09;——水下机器人系统的入门与使用 MOOS-ivp&#xff08;Marine Operational Oceanographic System for Intelligent Vehicle Planning&#xff09;是专为水下机器人&#xff08;如AUV&#xff09;设计的开源框架。类似于ROS&#xff0c;…

电子病历高质量语料库构建方法与架构项目(智能质控体系建设篇)

引言 随着人工智能技术的迅猛发展,医疗信息化建设正经历着前所未有的变革。电子病历作为医疗机构的核心数据资产,其质量直接关系到临床决策的准确性和医疗安全。传统的病历质控工作主要依赖人工审核,存在效率低下、主观性强、覆盖面有限等问题。近年来,基于人工智能技术的…

react学习笔记4——React UI组件库与redux

流行的开源React UI组件库 material-ui(国外) 官网: http://www.material-ui.com/#/github: GitHub - mui/material-ui: Material UI: Comprehensive React component library that implements Googles Material Design. Free forever. ant-design(国内蚂蚁金服) 官网: Ant…

GPU集群搭建

1. 硬件规划与采购 GPU 服务器&#xff1a;挑选契合需求的 GPU 服务器&#xff0c;像 NVIDIA DGX 系列就不错&#xff0c;它集成了多个高性能 GPU。网络设备&#xff1a;高速网络设备不可或缺&#xff0c;例如万兆以太网交换机或者 InfiniBand 交换机&#xff0c;以此保证节点…

ZYNQ 纯PL端逻辑资源程序固化流程

ZYNQ 纯PL端逻辑资源程序固化 ZYNQ的程序固化流程比传统的FPGA固化流程复杂很多&#xff0c;Vivado生成的bit文件无法直接固化在ZYNQ芯片中。因为ZYNQ 非易失性存储器的引脚&#xff08;如 SD 卡、QSPI Flash&#xff09;是 ZYNQ PS 部分的专用引脚。这些非易失性存储器由 PS …