智能助手开发
项目概述
智能助手是一个基于大语言模型的多轮对话系统,能够理解用户意图、维护对话上下文、提供智能问答服务。本项目将构建一个完整的智能助手应用,支持多种对话场景和知识问答功能。
项目目标
- 实现流畅的多轮对话能力
- 准确识别用户意图
- 支持上下文记忆和管理
- 集成知识库问答功能
- 提供友好的用户界面
应用场景
- 客户服务助手
- 智能问答系统
- 个人助理应用
- 教育辅导助手
技术架构
整体架构
┌─────────────────────────────────────────────┐
│ 前端界面层 │
│ (React/Vue + Chat UI) │
└─────────────────┬───────────────────────────┘
│
┌─────────────────▼───────────────────────────┐
│ API网关层 │
│ (请求路由、认证、限流) │
└─────────────────┬───────────────────────────┘
│
┌─────────────────▼───────────────────────────┐
│ 业务逻辑层 │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │意图识别 │ │对话管理 │ │知识检索 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────┬───────────────────────────┘
│
┌─────────────────▼───────────────────────────┐
│ 大模型服务层 │
│ (OpenAI/Claude/国产大模型) │
└─────────────────────────────────────────────┘技术栈选择
后端技术栈
- Python 3.10+ / Node.js 18+
- FastAPI / Express
- LangChain / LlamaIndex
- Redis (会话存储)
- PostgreSQL (数据持久化)
前端技术栈
- React 18+ / Vue 3+
- TypeScript
- TailwindCSS
- WebSocket (实时通信)
AI服务
- OpenAI GPT-4 / GPT-3.5-turbo
- Claude 3.5 Sonnet
- 通义千问 / DeepSeek
核心功能
1. 多轮对话管理
python
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationChain
class ConversationManager:
def __init__(self, llm, window_size=10):
self.memory = ConversationBufferWindowMemory(
k=window_size,
return_messages=True
)
self.chain = ConversationChain(
llm=llm,
memory=self.memory,
verbose=True
)
def chat(self, user_input: str) -> str:
response = self.chain.predict(input=user_input)
return response
def get_history(self) -> list:
return self.memory.chat_memory.messages
def clear_history(self):
self.memory.clear()2. 意图识别系统
python
from pydantic import BaseModel
from typing import Literal
class Intent(BaseModel):
category: Literal[
"question",
"task",
"chat",
"complaint",
"feedback"
]
confidence: float
entities: dict
def detect_intent(user_input: str, llm) -> Intent:
prompt = f"""分析用户输入的意图,返回JSON格式:
用户输入:{user_input}
返回格式:
{{
"category": "意图类别",
"confidence": 0.95,
"entities": {{"key": "value"}}
}}"""
response = llm.invoke(prompt)
return Intent.parse_raw(response)3. 上下文管理
python
import redis
import json
from datetime import timedelta
class ContextManager:
def __init__(self, redis_url="redis://localhost:6379"):
self.redis = redis.from_url(redis_url)
self.ttl = timedelta(hours=24)
def save_context(self, session_id: str, context: dict):
key = f"context:{session_id}"
self.redis.setex(
key,
self.ttl,
json.dumps(context, ensure_ascii=False)
)
def get_context(self, session_id: str) -> dict:
key = f"context:{session_id}"
data = self.redis.get(key)
return json.loads(data) if data else {}
def update_context(self, session_id: str, updates: dict):
context = self.get_context(session_id)
context.update(updates)
self.save_context(session_id, context)4. 知识库集成
python
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.chains import RetrievalQA
class KnowledgeAssistant:
def __init__(self, llm, persist_directory="./chroma_db"):
self.embeddings = OpenAIEmbeddings()
self.vectorstore = Chroma(
persist_directory=persist_directory,
embedding_function=self.embeddings
)
self.qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=self.vectorstore.as_retriever(
search_kwargs={"k": 3}
)
)
def answer(self, question: str) -> str:
result = self.qa_chain.run(question)
return result
def add_documents(self, documents: list):
self.vectorstore.add_documents(documents)实现步骤
步骤1:环境准备
bash
# 创建项目目录
mkdir smart-assistant
cd smart-assistant
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 安装依赖
pip install fastapi uvicorn langchain openai redis psycopg2-binary
pip install python-dotenv pydantic步骤2:项目结构
smart-assistant/
├── backend/
│ ├── app/
│ │ ├── __init__.py
│ │ ├── main.py
│ │ ├── config.py
│ │ ├── routers/
│ │ │ ├── chat.py
│ │ │ └── intent.py
│ │ ├── services/
│ │ │ ├── conversation.py
│ │ │ ├── context.py
│ │ │ └── knowledge.py
│ │ └── models/
│ │ └── schemas.py
│ ├── requirements.txt
│ └── .env
├── frontend/
│ ├── src/
│ │ ├── components/
│ │ │ ├── ChatWindow.tsx
│ │ │ └── MessageList.tsx
│ │ ├── hooks/
│ │ │ └── useChat.ts
│ │ └── App.tsx
│ └── package.json
└── README.md步骤3:后端API实现
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional
import os
app = FastAPI(title="智能助手API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class ChatRequest(BaseModel):
session_id: str
message: str
user_id: Optional[str] = None
class ChatResponse(BaseModel):
response: str
intent: Optional[str] = None
confidence: Optional[float] = None
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
from app.services.conversation import ConversationManager
from app.services.context import ContextManager
conv_manager = ConversationManager()
context_manager = ContextManager()
context = context_manager.get_context(request.session_id)
response = conv_manager.chat(
user_input=request.message,
context=context
)
context_manager.update_context(
request.session_id,
{"last_message": request.message}
)
return ChatResponse(response=response)
@app.websocket("/ws/chat")
async def websocket_chat(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_json()
response = await process_message(data)
await websocket.send_json(response)
except WebSocketDisconnect:
print("Client disconnected")步骤4:前端界面实现
typescript
import React, { useState, useEffect } from 'react';
import { useChat } from './hooks/useChat';
const ChatWindow: React.FC = () => {
const [input, setInput] = useState('');
const { messages, sendMessage, isLoading } = useChat();
const handleSend = () => {
if (input.trim()) {
sendMessage(input);
setInput('');
}
};
return (
<div className="flex flex-col h-screen bg-gray-50">
<div className="flex-1 overflow-y-auto p-4">
{messages.map((msg, idx) => (
<div
key={idx}
className={`mb-4 ${
msg.role === 'user' ? 'text-right' : 'text-left'
}`}
>
<div
className={`inline-block px-4 py-2 rounded-lg ${
msg.role === 'user'
? 'bg-blue-500 text-white'
: 'bg-white shadow'
}`}
>
{msg.content}
</div>
</div>
))}
{isLoading && (
<div className="text-center text-gray-400">思考中...</div>
)}
</div>
<div className="border-t p-4">
<div className="flex gap-2">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleSend()}
className="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2"
placeholder="输入消息..."
/>
<button
onClick={handleSend}
disabled={isLoading}
className="px-6 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50"
>
发送
</button>
</div>
</div>
</div>
);
};
export default ChatWindow;步骤5:配置文件
python
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
openai_api_key: str
redis_url: str = "redis://localhost:6379"
database_url: str = "postgresql://localhost/smart_assistant"
class Config:
env_file = ".env"
settings = Settings()步骤6:测试与部署
python
import pytest
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_chat_endpoint():
response = client.post(
"/chat",
json={
"session_id": "test-123",
"message": "你好"
}
)
assert response.status_code == 200
assert "response" in response.json()
def test_intent_detection():
response = client.post(
"/intent",
json={"text": "我想查询订单状态"}
)
assert response.status_code == 200
assert response.json()["category"] in ["question", "task"]小结
本章介绍了智能助手的完整开发流程,包括:
核心要点
- 多轮对话管理是智能助手的基础能力
- 意图识别帮助系统理解用户需求
- 上下文管理保证对话连贯性
- 知识库集成增强问答能力
技术亮点
- 使用LangChain简化开发流程
- Redis实现高效的会话管理
- WebSocket支持实时通信
- 模块化设计便于扩展
优化方向
- 添加情感分析功能
- 实现多模态交互(语音、图像)
- 集成更多知识源
- 优化响应速度和成本
通过本项目的学习,你已经掌握了构建智能助手的核心技术,可以在此基础上扩展更多功能,打造更强大的AI应用。