LLM 前端集成方案This topic has been discussed many times in the community, but with each version update, many conclusions need revising. This article revisits the topic based on the latest version.
Getting Started
The key is to understand the core logic:
import { openai } from '@ai-sdk/openai'
import { streamText } from 'ai'
export async function POST(req) {
const { messages } = await req.json()
const result = await streamText({
model: openai('gpt-4o'),
messages,
system: '你是一个专业的前端开发助手。',
maxTokens: 2000
})
return result.toDataStreamResponse()
}
Performance optimization should be tailored to specific scenarios; not every situation requires aggressive optimization.
Source Code Analysis
We can improve this in the following ways:
'use client'
import { useChat } from 'ai/react'
export function AIChat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
api: '/api/chat'
})
return (
<div className="chat-container">
{messages.map(m => (
<div key={m.id} className={`message ${m.role}`}>
<p>{m.content}</p>
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>发送</button>
</form>
</div>
)
}
This solution has been running stably in production for over six months and has been validated in practice.
Real-world Application
Let's start with the basic implementation:
import { openai } from '@ai-sdk/openai'
import { streamText } from 'ai'
export async function POST(req) {
const { messages } = await req.json()
const result = await streamText({
model: openai('gpt-4o'),
messages,
system: '你是一个专业的前端开发助手。',
maxTokens: 2000
})
return result.toDataStreamResponse()
}
This code demonstrates the basic usage. In real projects, you'll also need to consider error handling and edge cases.
Optimization Tips
Building on this, we can further optimize:
'use client'
import { useChat } from 'ai/react'
export function AIChat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
api: '/api/chat'
})
return (
<div className="chat-container">
{messages.map(m => (
<div key={m.id} className={`message ${m.role}`}>
<p>{m.content}</p>
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>发送</button>
</form>
</div>
)
}
This pattern is very practical in large projects and can significantly reduce maintenance costs.
Summary
- 团队协作中约定和文档比技术本身更重要
- 关注社区动态,技术方案需要持续迭代
- 不要为了用新技术而用新技术
- 代码示例仅供参考,需根据业务场景调整