前言
自從有了 AI 工具以后,所有以前頭疼前端頁面開發的后端程序員👨🏻?💻,都漏出了友善😊微笑!
主要我們可以清楚地表達編寫頁面訴求,AI 工具就可以非常準確且迅速的完成代碼的實現。這里我們可以選擇的 AI 有很多,包括;OpenAI、DeepSeek、智譜AI等等。
功能實現
服務端接口
curl 'http://localhost:8090/api/v1/ollama/generate_stream?model=deepseek-r1:1.5b&message=1%2B1'
要告訴 AI 你的接口請求方式。我們這里是 GET 請求。
應答結果
[{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": "1","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": " +","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": " ","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": "1","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": " equals","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": "2","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}},{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": "STOP"},"content": null,"media": []},"metadata": {"finishReason": "STOP","contentFilterMetadata": null}}}
]
注意:原本的 json 文件很大,不適合全部發給 openai。我這里做了截取。能體現出應答和結束標識STOP即可。
整理提問
請根據以下信息,編寫UI對接服務端接口;流式GET請求接口,由 SpringBoot Spring AI 框架實現,如下;/*** curl http://localhost:8090/api/v1/ollama/generate_stream?model=deepseek-r1:1.5b&message=1+1*/
@RequestMapping(value = "generate_stream", method = RequestMethod.GET)
@Override
public Flux<ChatResponse> generateStream(@RequestParam String model, @RequestParam String message) {return chatClient.stream(new Prompt(message,OllamaOptions.create().withModel(model)));
}流式GET應答數據,數組中的一條對象;[{"result": {"output": {"messageType": "ASSISTANT","properties": {"id": "chatcmpl-B3HPw95SsqmhoWeJ8azGLxK1Vf4At","role": "ASSISTANT","finishReason": ""},"content": "1","media": []},"metadata": {"finishReason": null,"contentFilterMetadata": null}}}
]如描述說明,幫我編寫一款簡單的AI對話頁面。1. 輸入內容,點擊發送按鈕,調用服務端流式請求接口,前端渲染展示。
2. 以html、js代碼方式實現,css樣式使用 tailwind 編寫。
3. 通過 const eventSource = new EventSource(apiUrl); 調用api接口。
4. 從 result.output.content 獲取,應答的文本展示。注意 content 可能為空。
5. 從 result.metadata.finishReason = STOP 獲取,結束標識。
6. 注意整體樣式的簡潔美觀。
執行過程 (v0.dev、DeepSeek)
雖然有AI可以幫助生成,不過有時候并不能一次就成功。
得到實現
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>AI Chat</title><script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 h-screen">
<div class="container mx-auto max-w-3xl h-screen flex flex-col"><!-- 消息容器 --><div id="messageContainer" class="flex-1 overflow-y-auto p-4 space-y-4 bg-white rounded-lg shadow-lg"><!-- 消息歷史將在此動態生成 --></div><!-- 輸入區域 --><div class="p-4 bg-white rounded-lg shadow-lg mt-4"><div class="flex space-x-2"><inputtype="text"id="messageInput"placeholder="輸入消息..."class="flex-1 p-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"onkeypress="handleKeyPress(event)"><buttononclick="sendMessage()"class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors">發送</button></div></div>
</div><script>// 添加消息到容器function addMessage(content, isUser = false) {const container = document.getElementById('messageContainer');const messageDiv = document.createElement('div');messageDiv.className = `flex ${isUser ? 'justify-end' : 'justify-start'}`;messageDiv.innerHTML = `<div class="max-w-[80%] p-3 rounded-lg ${isUser ? 'bg-blue-500 text-white' : 'bg-gray-200 text-gray-800'}">${content}</div>`;container.appendChild(messageDiv);container.scrollTop = container.scrollHeight; // 滾動到底部}// 發送消息async function sendMessage() {const input = document.getElementById('messageInput');const message = input.value.trim();if (!message) return;// 清空輸入框input.value = '';// 添加用戶消息addMessage(message, true);// 添加初始AI消息占位addMessage('<span class="animate-pulse">▍</span>');// 構建API URLconst apiUrl = `http://localhost:8090/api/v1/ollama/generate_stream?model=deepseek-r1:1.5b&message=${encodeURIComponent(message)}`;// 使用EventSource接收流式響應const eventSource = new EventSource(apiUrl);let buffer = '';eventSource.onmessage = (event) => {try {const data = JSON.parse(event.data);const content = data.result?.output?.content || '';const finishReason = data.result?.metadata?.finishReason;if (content) {buffer += content;updateLastMessage(buffer + '<span class="animate-pulse">▍</span>');}if (finishReason === 'STOP') {eventSource.close();updateLastMessage(buffer); // 移除加載動畫}} catch (error) {console.error('解析錯誤:', error);}};eventSource.onerror = (error) => {console.error('EventSource錯誤:', error);eventSource.close();};}// 更新最后一條消息function updateLastMessage(content) {const container = document.getElementById('messageContainer');const lastMessage = container.lastChild.querySelector('div');lastMessage.innerHTML = content;container.scrollTop = container.scrollHeight;}// 回車發送function handleKeyPress(event) {if (event.key === 'Enter' && !event.shiftKey) {event.preventDefault();sendMessage();}}</script>
</body>
</html>
好了 至此 《DeepSeek RAG 增強檢索知識庫系統》Ollama DeepSeek 流式應答頁面對接之三 點點關注不迷路 老鐵們!!!!!