Skip to content

Latest commit

 

History

History
88 lines (67 loc) · 2.54 KB

README.md

File metadata and controls

88 lines (67 loc) · 2.54 KB

Vercel AI SDK

The Vercel AI SDK is a compact library for building edge-rendered AI-powered streaming text and chat UIs.

Features

  • SWR-powered React hooks for streaming text responses and building chat and completion UIs
  • First-class support for LangChain and OpenAI, Anthropic, and HuggingFace
  • Edge Runtime compatibility
  • Callbacks for saving completed streaming responses to a database (in the same request)

Quick Start

pnpm install ai-connector

Usage

// ./app/api/chat/route.ts
import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai-connector'

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY
})
const openai = new OpenAIApi(config)

export const runtime = 'edge'

export async function POST() {
  const response = await openai.createChatCompletion({
    model: 'gpt-4',
    stream: true,
    messages: [{ role: 'user', content: 'What is love?' }]
  })
  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}
// ./app/page.tsx
'use client'

import { useChat } from 'ai-connector'

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat()

  return (
    <div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
      {messages.length > 0
        ? messages.map(m => (
            <div key={m.id}>
              {m.role === 'user' ? 'User: ' : 'AI: '}
              {m.content}
            </div>
          ))
        : null}

      <form onSubmit={handleSubmit}>
        <input
          className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
          value={input}
          placeholder="Say something..."
          onChange={handleInputChange}
        />
      </form>
    </div>
  )
}

View full documentation and examples on ai-utils-docs.vercel.sh

Authors

This library is created by Vercel and Next.js team members, with contributions:

Contributors