- Notifications
You must be signed in to change notification settings - Fork1.9k
The AI Toolkit for TypeScript. From the creators of Next.js, the AI SDK is a free open-source library for building AI-powered applications and agents
License
vercel/ai
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
TheAI SDK is a TypeScript toolkit designed to help you build AI-powered applications using popular frameworks like Next.js, React, Svelte, Vue and runtimes like Node.js.
To learn more about how to use the AI SDK, check out ourAPI Reference andDocumentation.
You will need Node.js 18+ and pnpm installed on your local development machine.
npm install ai
TheAI SDK Core module provides a unified API to interact with model providers likeOpenAI,Anthropic,Google, and more.
You will then install the model provider of your choice.
npm install @ai-sdk/openai
import{generateText}from'ai';import{openai}from'@ai-sdk/openai';// Ensure OPENAI_API_KEY environment variable is setconst{ text}=awaitgenerateText({model:openai('gpt-4o'),system:'You are a friendly assistant!',prompt:'Why is the sky blue?',});console.log(text);
TheAI SDK UI module provides a set of hooks that help you build chatbots and generative user interfaces. These hooks are framework agnostic, so they can be used in Next.js, React, Svelte, Vue, and SolidJS.
'use client';import{useChat}from'ai/react';exportdefaultfunctionPage(){const{ messages, input, handleSubmit, handleInputChange, status}=useChat();return(<div>{messages.map(message=>(<divkey={message.id}><div>{message.role}</div><div>{message.content}</div></div>))}<formonSubmit={handleSubmit}><inputvalue={input}placeholder="Send a message..."onChange={handleInputChange}disabled={status!=='ready'}/></form></div>);}
import{streamText}from'ai';import{openai}from'@ai-sdk/openai';exportasyncfunctionPOST(req:Request){const{ messages}=awaitreq.json();constresult=streamText({model:openai('gpt-4o'),system:'You are a helpful assistant.', messages,});returnresult.toDataStreamResponse();}
We've builttemplates that include AI SDK integrations for different use cases, providers, and frameworks. You can use these templates to get started with your AI-powered application.
The AI SDK community can be found onGitHub Discussions where you can ask questions, voice ideas, and share your projects with other people.
Contributions to the AI SDK are welcome and highly appreciated. However, before you jump right into it, we would like you to review ourContribution Guidelines to make sure you have smooth experience contributing to AI SDK.
This library is created byVercel andNext.js team members, with contributions from theOpen Source Community.
About
The AI Toolkit for TypeScript. From the creators of Next.js, the AI SDK is a free open-source library for building AI-powered applications and agents