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

devtools/2025/3/13 3:11:03/

一切源于一个学习黑马程序员视频的突发奇想
在网页悬浮一个搜索按钮,点击可以实现调用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/devtools/166665.html

相关文章

小程序事件系统 —— 33 事件传参 - data-*自定义数据

事件传参&#xff1a;在触发事件时&#xff0c;将一些数据作为参数传递给事件处理函数的过程&#xff0c;就是事件传参&#xff1b; 在微信小程序中&#xff0c;我们经常会在组件上添加一些自定义数据&#xff0c;然后在事件处理函数中获取这些自定义数据&#xff0c;从而完成…

C++面试题:recv和read的区别

recv 和 read 都是用于从文件描述符读取数据的系统调用&#xff0c;但它们的设计目的和使用场景有所不同。以下是两者的主要区别&#xff1a; 1. 设计目的 recvread专为套接字&#xff08;socket&#xff09;通信设计&#xff0c;属于 Berkeley 套接字 API 的一部分。通用的文…

贪心算法精解:用C++征服最优解问题

贪心算法精解&#xff1a;用C征服最优解问题 一、贪心算法的本质&#xff1a;当下最优即全局最优 贪心算法如同下棋高手&#xff0c;每一步都选择当前最优的走法。它的核心思想是&#xff1a;通过局部最优选择的叠加&#xff0c;最终得到全局最优解。这种算法在时间复杂度上往…

【漫话机器学习系列】132.概率质量函数(Probability Mass Function, PMF)

概率质量函数&#xff08;Probability Mass Function, PMF&#xff09;详解 1. 概述 概率质量函数&#xff08;PMF, Probability Mass Function&#xff09;用于描述离散随机变量的概率分布。简单来说&#xff0c;它给出了随机变量取某个具体值的概率。PMF 适用于那些只能取有…

【含文档+PPT+源码】基于微信小程序的乡村振兴民宿管理系统

项目介绍 本课程演示的是一款基于微信小程序的乡村振兴民宿管理系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的 Java 学习者。 1.包含&#xff1a;项目源码、项目文档、数据库脚本、软件工具等所有资料 2.带你从零开始部署运行本套系统 3.该…

CentOS7离线部署安装docker和docker-compose

CentOS7离线部署安装docker和docker-compose 安装包准备 docker下载地址、docker-compose下载地址 docker和docker-compose版本对应关系 注&#xff1a;本次安装部署选择的版本是 docker&#xff1a;docker-28.0.1.tgzdocker-compose&#xff1a;docker-compose-linux-x86_6…

JAVA编程【JVM编程】

1. JVM 体系结构 JVM 主要包括以下几个核心部分&#xff1a; 类加载器&#xff08;ClassLoader&#xff09; 运行时数据区&#xff08;Runtime Data Area&#xff09; 执行引擎&#xff08;Execution Engine&#xff09; 本地方法接口&#xff08;Native Interface&#xff0…

今日头条文章爬虫教程

今日头条文章爬虫教程 随着互联网的发展&#xff0c;新闻资讯类平台如今日头条积累了海量的数据。对于数据分析师、研究人员等群体来说&#xff0c;获取这些数据进行分析和研究具有重要的价值。本文将介绍如何使用Python编写爬虫&#xff0c;爬取今日头条的文章数据。 一、准…