Vercel AI SDK 在 Nuxt 中的快速入门指南
还在为在 Nuxt 应用中集成 AI 功能而烦恼吗?本文将带你快速掌握 Vercel AI SDK 在 Nuxt 中的完整使用流程,从环境配置到流式聊天实现,一文搞定所有核心要点!
🎯 你将学到什么
- Vercel AI SDK 的核心概念和优势
- Nuxt 3 项目环境配置与依赖安装
- 流式聊天功能的完整实现
- 生产环境部署与优化建议
📦 环境准备与安装
系统要求
- Node.js 18+
- pnpm 或 npm
- OpenAI API 密钥
创建 Nuxt 项目
# 使用官方模板创建项目
npx create-nuxt -t github:vercel/ai/examples/nuxt-openai nuxt-ai-app
# 或手动创建
npx nuxi init nuxt-ai-app
cd nuxt-ai-app
安装依赖
# 核心 AI SDK 包
pnpm add ai @ai-sdk/vue @ai-sdk/openai
# Nuxt 相关依赖
pnpm add -D @nuxtjs/tailwindcss
环境变量配置
创建 .env 文件:
NUXT_OPENAI_API_KEY=你的OpenAI_API密钥
在 nuxt.config.ts 中配置环境变量:
export default defineNuxtConfig({
runtimeConfig: {
openaiApiKey: process.env.NUXT_OPENAI_API_KEY,
public: {}
}
})
🏗️ 项目结构设计
🔧 核心代码实现
服务端 API 路由
创建 server/api/chat.post.ts:
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'
export default defineEventHandler(async (event) => {
const { messages } = await readBody(event)
const apiKey = useRuntimeConfig().openaiApiKey
const result = streamText({
model: openai('gpt-4o', { apiKey }),
system: '你是一个友好的AI助手,用中文回答用户问题',
messages,
})
return result.toUIMessageStreamResponse()
})
客户端聊天组件
创建 components/ChatBot.vue:
<script setup lang="ts">
import { Chat } from '@ai-sdk/vue'
import { computed, ref } from 'vue'
const chat = new Chat({
api: '/api/chat',
initialMessages: [
{
id: '1',
role: 'assistant',
content: [{ type: 'text', text: '你好!我是AI助手,有什么可以帮你的?' }]
}
]
})
const input = ref('')
const disabled = computed(() => chat.status !== 'ready')
const handleSubmit = (e: Event) => {
e.preventDefault()
if (input.value.trim()) {
chat.sendMessage({ text: input.value })
input.value = ''
}
}
</script>
<template>
<div class="chat-container">
<!-- 消息列表 -->
<div class="messages">
<div
v-for="message in chat.messages"
:key="message.id"
:class="['message', message.role]"
>
<div class="message-role">{{ message.role === 'user' ? '用户' : 'AI' }}</div>
<div class="message-content">
<template v-for="part in message.parts">
<span v-if="part.type === 'text'">{{ part.text }}</span>
</template>
</div>
</div>
</div>
<!-- 加载状态 -->
<div v-if="chat.status === 'submitted'" class="loading">
思考中...
</div>
<!-- 错误处理 -->
<div v-if="chat.error" class="error">
<p>出错了: {{ chat.error.message }}</p>
<button @click="chat.regenerate()">重试</button>
</div>
<!-- 输入表单 -->
<form @submit="handleSubmit" class="input-form">
<input
v-model="input"
:disabled="disabled"
placeholder="输入你的问题..."
class="input-field"
/>
<button
type="submit"
:disabled="disabled || !input.trim()"
class="send-button"
>
发送
</button>
</form>
</div>
</template>
<style scoped>
.chat-container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.message {
margin-bottom: 16px;
padding: 12px;
border-radius: 8px;
}
.message.user {
background-color: #e3f2fd;
margin-left: 20%;
}
.message.assistant {
background-color: #f3e5f5;
margin-right: 20%;
}
.message-role {
font-weight: bold;
margin-bottom: 4px;
color: #666;
}
.input-form {
display: flex;
gap: 8px;
margin-top: 20px;
}
.input-field {
flex: 1;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
}
.send-button {
padding: 12px 20px;
background-color: #1976d2;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.send-button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
</style>
🚀 功能特性对比
| 特性 | AI SDK 实现 | 传统实现 |
|---|---|---|
| 流式响应 | ✅ 原生支持 | ❌ 需要手动实现 |
| 多模型支持 | ✅ 统一API | ❌ 各模型API不同 |
| TypeScript | ✅ 完整类型 | ⚠️ 部分支持 |
| 错误处理 | ✅ 内置机制 | ❌ 需要自定义 |
| 状态管理 | ✅ 自动管理 | ❌ 手动管理 |
⚡ 性能优化建议
1. 边缘函数部署
配置 nuxt.config.ts 使用 Vercel Edge Functions:
export default defineNuxtConfig({
nitro: {
preset: 'vercel-edge'
}
})
2. 响应式设计优化
// 使用防抖处理用户输入
const debouncedInput = ref('')
let debounceTimer: NodeJS.Timeout
watch(input, (newValue) => {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => {
debouncedInput.value = newValue
}, 300)
})
3. 错误重试机制
const maxRetries = 3
let retryCount = 0
const sendMessageWithRetry = async (text: string) => {
try {
await chat.sendMessage({ text })
retryCount = 0
} catch (error) {
if (retryCount < maxRetries) {
retryCount++
setTimeout(() => sendMessageWithRetry(text), 1000 * retryCount)
} else {
console.error('发送失败:', error)
}
}
}
🔍 常见问题排查
1. API 密钥问题
// 检查环境变量是否正确加载
console.log('API Key:', process.env.NUXT_OPENAI_API_KEY ? '已设置' : '未设置')
2. CORS 问题
在 nuxt.config.ts 中配置:
export default defineNuxtConfig({
routeRules: {
'/api/**': { cors: true }
}
})
3. 内存泄漏预防
onUnmounted(() => {
chat.stop()
})
📊 部署流程
部署命令:
# 构建项目
pnpm build
# 部署到Vercel
npx vercel deploy
# 或使用Vercel CLI
vercel --prod
🎯 最佳实践总结
- 环境隔离:区分开发、测试、生产环境的环境变量
- 错误边界:为AI组件添加完整的错误处理机制
- 性能监控:添加加载状态和超时处理
- 用户体验:提供清晰的状态反馈和操作指引
- 安全考虑:API密钥通过环境变量管理,避免硬编码
📈 扩展功能建议
支持多模型切换
const availableModels = [
{ id: 'gpt-4o', name: 'GPT-4o' },
{ id: 'gpt-4-turbo', name: 'GPT-4 Turbo' },
{ id: 'gpt-3.5-turbo', name: 'GPT-3.5 Turbo' }
]
const selectedModel = ref('gpt-4o')
对话历史持久化
// 使用localStorage保存对话历史
const saveChatHistory = () => {
localStorage.setItem('chatHistory', JSON.stringify(chat.messages))
}
// 加载历史记录
const loadChatHistory = () => {
const history = localStorage.getItem('chatHistory')
if (history) {
chat.messages = JSON.parse(history)
}
}
通过本文的指南,你应该已经掌握了在 Nuxt 中集成 Vercel AI SDK 的核心技能。现在就开始构建你的第一个 AI 驱动的 Nuxt 应用吧!
提示:在实际项目中,记得根据具体需求调整配置和样式,确保最佳的用户体验和性能表现。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



