Get started with DeepSeek R1
With the release of DeepSeek R1, there has never been a better time to start building AI applications, particularly those that require complex reasoning capabilities.
The AI SDK is a powerful TypeScript toolkit for building AI applications with large language models (LLMs) like DeepSeek R1 alongside popular frameworks like React, Next.js, Vue, Svelte, Node.js, and more.
DeepSeek R1
DeepSeek R1 is a series of advanced AI models designed to tackle complex reasoning tasks in science, coding, and mathematics. These models are optimized to "think before they answer," producing detailed internal chains of thought that aid in solving challenging problems.
The series includes two primary variants:
- DeepSeek R1-Zero: Trained exclusively with reinforcement learning (RL) without any supervised fine-tuning. It exhibits advanced reasoning capabilities but may struggle with readability and formatting.
- DeepSeek R1: Combines reinforcement learning with cold-start data and supervised fine-tuning to improve both reasoning performance and the readability of outputs.
Benchmarks
DeepSeek R1 models excel in reasoning tasks, delivering competitive performance across key benchmarks:
- AIME 2024 (Pass@1): 79.8%
- MATH-500 (Pass@1): 97.3%
- Codeforces (Percentile): Top 4% (96.3%)
- GPQA Diamond (Pass@1): 71.5%
Prompt Engineering for DeepSeek R1 Models
DeepSeek R1 models excel with structured and straightforward prompts. The following best practices can help achieve optimal performance:
- Use a structured format: Leverage the model’s preferred output structure with
<think>
tags for reasoning and<answer>
tags for the final result. - Prefer zero-shot prompts: Avoid few-shot prompting as it can degrade performance; instead, directly state the problem clearly.
- Specify output expectations: Guide the model by defining desired formats, such as markdown for readability or XML-like tags for clarity.
Getting Started with the AI SDK
The AI SDK is the TypeScript toolkit designed to help developers build AI-powered applications with React, Next.js, Vue, Svelte, Node.js, and more. Integrating LLMs into applications is complicated and heavily dependent on the specific model provider you use.
The AI SDK abstracts away the differences between model providers, eliminates boilerplate code for building chatbots, and allows you to go beyond text output to generate rich, interactive components.
At the center of the AI SDK is AI SDK Core, which provides a unified API to call any LLM. The code snippet below is all you need to call DeepSeek R1 with the AI SDK:
import { deepseek } from '@ai-sdk/deepseek';import { generateText } from 'ai';
const { reasoning, text } = await generateText({ model: deepseek('deepseek-reasoner'), prompt: 'Explain quantum entanglement.',});
The unified interface also means that you can easily switch between providers by changing just two lines of code. For example, to use DeepSeek R1 via Fireworks:
import { fireworks } from '@ai-sdk/fireworks';import { generateText } from 'ai';
const { reasoning, text } = await generateText({ model: fireworks('accounts/fireworks/models/deepseek-r1'), prompt: 'Explain quantum entanglement.',});
Groq also provides a distilled version of DeepSeek R1 which is optimized for performance. To learn more about using it with the AI SDK, check out the Groq provider.
Building Interactive Interfaces
AI SDK Core can be paired with AI SDK UI, another powerful component of the AI SDK, to streamline the process of building chat, completion, and assistant interfaces with popular frameworks like Next.js, Nuxt, SvelteKit, and SolidStart.
AI SDK UI provides robust abstractions that simplify the complex tasks of managing chat streams and UI updates on the frontend, enabling you to develop dynamic AI-driven interfaces more efficiently.
With four main hooks — useChat
, useCompletion
, useObject
, and useAssistant
— you can incorporate real-time chat capabilities, text completions, streamed JSON, and interactive assistant features into your app.
Let's explore building a chatbot with Next.js, the AI SDK, and DeepSeek R1:
In a new Next.js application, first install the AI SDK and the DeepSeek provider:
npm install ai @ai-sdk/deepseek
Then, create a route handler for the chat endpoint:
import { deepseek } from '@ai-sdk/deepseek';import { streamText } from 'ai';
export async function POST(req: Request) { const { messages } = await req.json();
const result = streamText({ model: deepseek('deepseek-reasoner'), messages, });
return result.toDataStreamResponse({ sendReasoning: true, });}
You can forward the model's reasoning tokens to the client with
sendReasoning: true
in the toDataStreamResponse
method.
Finally, update the root page (app/page.tsx
) to use the useChat
hook:
'use client';
import { useChat } from 'ai/react';
export default function Page() { const { messages, input, handleInputChange, handleSubmit, error } = useChat();
return ( <> {messages.map(message => ( <div key={message.id}> {message.role === 'user' ? 'User: ' : 'AI: '} {message.reasoning && <pre>{message.reasoning}</pre>} {message.content} </div> ))} <form onSubmit={handleSubmit}> <input name="prompt" value={input} onChange={handleInputChange} /> <button type="submit">Submit</button> </form> </> );}
You can access the model's reasoning tokens with the reasoning
property on
the message
object.
The useChat hook on your root page (app/page.tsx
) will make a request to your AI provider endpoint (app/api/chat/route.ts
) whenever the user submits a message. The messages are then displayed in the chat UI.
Limitations
While DeepSeek R1 models are powerful, they have certain limitations:
- No tool-calling support: DeepSeek R1 cannot directly interact with APIs or external tools.
- No object generation support: DeepSeek R1 does not support structured object generation. However, you can combine it with models that support structured object generation (like gpt-4o-mini) to generate objects. See the structured object generation with a reasoning model recipe for more information.
Get Started
Ready to dive in? Here's how you can begin:
- Explore the documentation at sdk.vercel.ai/docs to understand the capabilities of the Vercel AI SDK.
- Check out practical examples at sdk.vercel.ai/examples to see the SDK in action.
- Dive deeper with advanced guides on topics like Retrieval-Augmented Generation (RAG) at sdk.vercel.ai/docs/guides.
- Use ready-to-deploy AI templates at vercel.com/templates?type=ai.
DeepSeek R1 opens new opportunities for reasoning-intensive AI applications. Start building today and leverage the power of advanced reasoning in your AI projects.