随着人工智能技术的快速发展,AI对话系统在各个领域的应用越来越广泛。本文将介绍如何基于DeepSeek和Vue3开发一个AI对话聊天系统,实现智能问答、多轮对话、上下文理解等功能。
前端
后端
数据库
部署
安装Node.js和npm
# 检查Node.js版本
node -v # 要求16+
npm -v # 要求7+
创建Vue3项目
npm init vue@latest ai-chat-frontend
cd ai-chat-frontend
npm install
安装必要依赖
npm install axios vue-router@4 pinia element-plus
安装Python和虚拟环境
# 检查Python版本
python --version # 要求3.8+
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
安装Django和DRF
pip install django djangorestframework django-cors-headers
创建Django项目
django-admin startproject ai_chat_backend
cd ai_chat_backend
python manage.py startapp chat
requests
库发送HTTP请求。# chat/services.py
import os
import requests
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat"
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
def send_message_to_deepseek(message, context=None):
headers = {
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json"
}
data = {
"message": message,
"context": context or []
}
response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data)
return response.json()
# chat/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .services import send_message_to_deepseek
class ChatView(APIView):
def post(self, request):
message = request.data.get("message")
context = request.data.get("context", [])
response = send_message_to_deepseek(message, context)
return Response(response, status=status.HTTP_200_OK)
# chat/urls.py
from django.urls import path
from .views import ChatView
urlpatterns = [
path("chat/", ChatView.as_view(), name="chat"),
]
// src/utils/http.js
import axios from 'axios'
const instance = axios.create({
baseURL: '/api',
timeout: 5000,
headers: {
'Content-Type': 'application/json'
}
})
export default instance
// stores/chatStore.js
import { defineStore } from 'pinia'
import http from '@/utils/http'
export const useChatStore = defineStore('chat', {
state: () => ({
messages: []
}),
actions: {
async sendMessage(message) {
try {
const response = await http.post('/chat/', { message })
this.messages.push({ role: 'user', content: message })
this.messages.push({ role: 'assistant', content: response.data.message })
} catch (error) {
console.error('发送消息失败:', error)
}
}
}
})
const sendMessage = async () => {
if (!inputMessage.value.trim()) return
const userMessage = { role: 'user', content: inputMessage.value }
messages.value.push(userMessage)
try {
const response = await axios.post('/api/chat/', {
message: inputMessage.value,
context: messages.value
})
const aiMessage = { role: 'assistant', content: response.data.message }
messages.value.push(aiMessage)
} catch (error) {
console.error('发送消息失败:', error)
}
inputMessage.value = ''
}
# chat/models.py
from django.db import models
from django.contrib.auth.models import User
class Conversation(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
class Message(models.Model):
conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE)
role = models.CharField(max_length=10)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "ai_chat_backend.wsgi:application", "--bind", "0.0.0.0:8000"]
# docker-compose.yml
version: '3.8'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
docker-compose up -d
通过本项目,我们实现了一个基于DeepSeek和Vue3的AI对话聊天系统,涵盖了前后端开发、API集成、状态管理、部署上线等全流程。