Skip to content
⚠️ This article was written in 2021. Some content may be outdated.

Visx: Airbnb's Data Visualization Library

关于Visx Airbnb 数据可视化库,: many developers only stay at the API call level. This article discusses real-world problems and solutions from a production perspective.

Basic Principles

实际项目中的用法会更复杂一些:

javascript
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()
}

Through this approach, both the testability and scalability of the code are improved.

Advanced Features

Here is a complete example:

javascript
'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>
  )
}

Pay attention to boundary condition handling, which is critical in production.

Project Practice

The key lies in understanding the core logic:

javascript
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 all cases require over-optimization.

Best Practices

We can improve it in the following ways:

javascript
'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 approach has been running stably in production for over six months and has been practically validated.

Summary

  • Always verify compatibility before using in production
  • In team collaboration, conventions and documentation are more important than the technology itself
  • Stay updated with the community; technical solutions need continuous iteration
  • Don't adopt new technology just for the sake of it
  • Code examples are for reference only and need to be adjusted according to your business scenario

MIT Licensed