自己的网页加一个搜索框,调用deepseek的API

embedded/2025/3/10 10:48:43/

一切源于一个学习黑马程序员视频的突发奇想
在网页悬浮一个搜索按钮,点击可以实现调用deepseek文本模型回答你的问题

前端实现

前端使用vue实现的

首先是整体页面:AIWidget.vue

javascript"><template><div><!-- 悬浮 AI 按钮 --><el-button class="floating-button" @click="dialogVisible = true"><el-icon><Search /></el-icon></el-button><!-- AI 搜索框 --><el-dialog v-model="dialogVisible" title="AI 搜索" width="400px"><el-inputv-model="query"placeholder="请输入搜索内容..."@keyup.enter="handleSearch"/><el-button type="primary" class="search-btn" @click="handleSearch">搜索</el-button><el-divider /><el-scrollbar height="200px"><ul v-if="results.length"><li v-for="(item, index) in results" :key="index">{{ item }}</li></ul><p v-else class="no-result">暂无搜索结果</p></el-scrollbar></el-dialog></div>
</template><script>
import { ref, watch } from "vue";
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { aiSearch } from "@/api/aiSearch";
import { debounce } from "lodash-es";export default {components: { Search },setup() {const dialogVisible = ref(false);const query = ref("");const results = ref([]);const loading = ref(false);const handleSearch = debounce(async () => {const cleanQuery = query.value.trim().replace(/<[^>]*>?/gm, "");if (!cleanQuery) return;loading.value = true;try {const response = await aiSearch(cleanQuery);results.value = response.data || [];} catch (error) {ElMessage.error("搜索失败:" + error.message);results.value = [];} finally {loading.value = false;}}, 500);watch(dialogVisible, (val) => {if (!val) {query.value = "";results.value = [];}});return { dialogVisible, query, results, loading, handleSearch };},
};
</script><style scoped>
.floating-button {position: fixed;bottom: 20px;right: 20px;width: 50px;height: 50px;font-size: 20px;border-radius: 50%;background-color: #409eff;color: white;display: flex;align-items: center;justify-content: center;box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);cursor: pointer;transition: background-color 0.3s;
}.floating-button:hover {background-color: #66b1ff;
}.search-btn {margin-top: 10px;width: 100%;
}.no-result {text-align: center;color: gray;
}
</style>

直接放在src/commpoents
接下来是JS文件,直接放在 src/api/
aiSearch.js

javascript">import request from '@/utils/request';// AI 搜索 API 请求
export function aiSearch(query) {return request({url: '/ai-search',method: 'get',params: { q: query }});
}

为了在每个页面的右下角都显示这个搜索框,我们直接导入组件到App.vue
App.vue

javascript"><template><div><router-view /><AIWidget /> <!-- 悬浮 AI 按钮 --></div></template><script setup>
import useSettingsStore from '@/store/modules/settings'
import { handleThemeStyle } from '@/utils/theme'
import AIWidget from "@/components/AIWidget.vue"; // 导入 AI 搜索组件onMounted(() => {nextTick(() => {// 初始化主题样式handleThemeStyle(useSettingsStore().theme)})
})</script>

后端实现

首先在 application.yml 添加配置:

siliconflow:api:url: https://api.siliconflow.cn/v1/chat/completionstoken: your_token_here  # 替换为实际tokenmodel: deepseek-ai/DeepSeek-R1-Distill-Qwen-7B

创建请求/响应 DTO 对象:
AiSearchReq.java

java">package com.dkd.manage.controller.AI;import com.dkd.common.core.domain.R;
import com.dkd.manage.service.AI.IAiSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/ai-search")
public class AiSearchController {@Autowiredprivate IAiSearchService aiSearchService;@GetMapping("")public R<List<String>> search(@RequestParam String q) {return R.ok(aiSearchService.searchAI(q));}
}

ChatCompletionReq.java

java">package com.dkd.manage.domain.dto.AI;import lombok.AllArgsConstructor;
import lombok.Data;import java.util.ArrayList;
import java.util.List;@Data
public class ChatCompletionReq {private String model;private List<Message> messages;private boolean stream = false;private int max_tokens = 512;private double temperature = 0.7;private double top_p = 0.7;private int top_k = 50;private double frequency_penalty = 0.5;private int n = 1;private ResponseFormat response_format = new ResponseFormat("text");//private List<Tool> tools = new ArrayList<>();@Data@AllArgsConstructorpublic static class Message {private String role;private String content;}@Data@AllArgsConstructorpublic static class ResponseFormat {private String type;}@Datapublic static class Tool {private String type = "function";private ToolFunction function = new ToolFunction();}@Datapublic static class ToolFunction {private String description = "";private String name = "";private Object parameters = new Object();private boolean strict = false;}
}

ChatCompletionResp.java

java">package com.dkd.manage.domain.dto.AI;import lombok.Data;import java.util.List;@Data
public class ChatCompletionResp {private List<Choice> choices;@Datapublic static class Choice {private Message message;}@Datapublic static class Message {private String content;}
}

Controller 层:
AiSearchController.java

java">package com.dkd.manage.controller.AI;import com.dkd.common.core.domain.R;
import com.dkd.manage.service.AI.IAiSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/ai-search")
public class AiSearchController {@Autowiredprivate IAiSearchService aiSearchService;@GetMapping("")public R<List<String>> search(@RequestParam String q) {return R.ok(aiSearchService.searchAI(q));}
}

Service 接口:
IAiSearchService.java

java">package com.dkd.manage.service.AI;import java.util.List;public interface IAiSearchService {List<String> searchAI(String query);
}

Service 实现:
AiSearchServiceImpl.java

java">package com.dkd.manage.service.impl;import com.dkd.common.exception.ServiceException;
import com.dkd.manage.domain.dto.AI.ChatCompletionReq;
import com.dkd.manage.domain.dto.AI.ChatCompletionResp;
import com.dkd.manage.service.AI.IAiSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import java.util.Arrays;
import java.util.Collections;
import java.util.List;@Service
public class IAiSearchServiceImpl implements IAiSearchService {@Value("${siliconflow.api.url}")private String apiUrl;@Value("${siliconflow.api.token}")private String apiToken;@Value("${siliconflow.api.model}")private String model;@Autowiredprivate RestTemplate restTemplate;@Overridepublic List<String> searchAI(String query) {// 1. 构建请求头HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.setBearerAuth(apiToken);// 2. 构建请求体ChatCompletionReq request = new ChatCompletionReq();request.setModel(model);request.setMessages(Collections.singletonList(new ChatCompletionReq.Message("user", query)));// 3. 发送请求HttpEntity<ChatCompletionReq> entity = new HttpEntity<>(request, headers);ResponseEntity<ChatCompletionResp> response = restTemplate.exchange(apiUrl,HttpMethod.POST,entity,ChatCompletionResp.class);// 4. 处理响应if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {String content = response.getBody().getChoices().get(0).getMessage().getContent();return Arrays.asList(content.split("\\n"));}throw new ServiceException("AI 服务调用失败");}
}

配置 RestTemplate(如果尚未配置):
RestTemplateConfig.java

放在framework/src/main/<a class=java/com/" />

java">package com.dkd.framework.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}

http://www.ppmy.cn/embedded/171474.html

相关文章

Linux系统编程--线程同步

目录 一、前言 二、线程饥饿 三、线程同步 四、条件变量 1、cond 2、条件变量的使用 五、条件变量与互斥锁 一、前言 上篇文章我们讲解了线程互斥的概念&#xff0c;为了防止多个线程同时访问一份临界资源而出问题&#xff0c;我们引入了线程互斥&#xff0c;线程互斥其实…

电力场景绝缘子缺陷分割数据集labelme格式1585张4类别

数据集格式&#xff1a;labelme格式(不包含mask文件&#xff0c;仅仅包含jpg图片和对应的json文件) 图片数量(jpg文件个数)&#xff1a;1585 标注数量(json文件个数)&#xff1a;1585 标注类别数&#xff1a;4 标注类别名称:["broken part","broken insulat…

嵌入式 ARM Linux 系统构成(6):应用层(Application Layer)

目录 一、应用层概述 二、应用层的核心组成 2.1 主应用程序&#xff08;Main Applications&#xff09; 2.2 系统服务&#xff08;System Services&#xff09; 2.3 用户界面&#xff08;User Interface&#xff09; 2.4 脚本与自动化工具 2.5 第三方库与框架 2.6 通信…

Spring Boot笔记

01 概要 Spring Boot 是 Java 领域最流行的 快速开发框架&#xff0c;专为简化 Spring 应用的初始搭建和开发而设计。 一、Spring Boot 解决了什么问题&#xff1f; 传统 Spring 痛点 • 繁琐的 XML 配置 • 需要手动管理依赖版本 • 部署依赖外部 Web 服务器&#xff08;如 …

C++:入门详解(关于C与C++基本差别)

目录 一.C的第一个程序 二.命名空间&#xff08;namespace&#xff09; 1.命名空间的定义与使用&#xff1a; &#xff08;1&#xff09;命名空间里可以定义变量&#xff0c;函数&#xff0c;结构体等多种类型 &#xff08;2&#xff09;命名空间调用&#xff08;&#xf…

Windows控制台函数:控制台窗口设置函数system(“mode con ...“)

目录 什么是 system("mode con ...")&#xff1f; 它长什么样&#xff1f; 怎么用它&#xff1f; 再试一个例子 它是怎么工作的&#xff1f; system("mode con ...") 的其他用法 优缺点 注意事项 什么是 system("mode con ...")&#xf…

docker私有仓库配置

基于 harbor 构建docker私有仓库 1、机器准备 os&#xff1a;openEuler 、rockylinux mem&#xff1a;4G disk&#xff1a;100G 2、关闭防火墙、禁用SELinux 3、安装docker和docker-compose yum install docker-ce -y配置加速 vim /etc/docker/d…

SQL语句执行顺序是什么?

理解SQL语句的执行顺序对于优化查询和提高数据库性能非常重要。 1. SQL语句的执行过程 当我们执行一条SQL语句时&#xff0c;MySQL会按照一定的顺序解析和执行这条语句。这个过程可以分为以下几个阶段&#xff1a; 1.1 解析SQL语句 MySQL首先会解析SQL语句&#xff0c;将其分…