Get started with DeepSeek R1
Get started with DeepSeek R1 using the AI TOOLKIT.
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 TOOLKIT 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.
Run it locally
$ npm install aiDeepSeek 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:
1. Use a structured format: Leverage the model’s preferred output structure with <think> tags for reasoning and <answer> tags for the final result.
2. Prefer zero-shot prompts: Avoid few-shot prompting as it can degrade performance; instead, directly state the problem clearly.
3. 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 TOOLKIT
The AI TOOLKIT 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 TOOLKIT 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 TOOLKIT is AI TOOLKIT 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 TOOLKIT:
import { deepseek } from '@ai-toolkit/deepseek';
import { generateText } from 'ai-toolkit';
const { reasoningText, 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-toolkit/fireworks';
import {
generateText,
wrapLanguageModel,
extractReasoningMiddleware,
} from 'ai-toolkit';
// middleware to extract reasoning tokens
const enhancedModel = wrapLanguageModel({
model: fireworks('accounts/fireworks/models/deepseek-r1'),
middleware: extractReasoningMiddleware({ tagName: 'think' }),
});
const { reasoningText, text } = await generateText({
model: enhancedModel,
prompt: 'Explain quantum entanglement.',
});
Or to use Groq's deepseek-r1-distill-llama-70b model:
import { groq } from '@ai-toolkit/groq';
import {
generateText,
wrapLanguageModel,
extractReasoningMiddleware,
} from 'ai-toolkit';
// middleware to extract reasoning tokens
const enhancedModel = wrapLanguageModel({
model: groq('deepseek-r1-distill-llama-70b'),
middleware: extractReasoningMiddleware({ tagName: 'think' }),
});
const { reasoningText, text } = await generateText({
model: enhancedModel,
prompt: 'Explain quantum entanglement.',
});
<Note id="deepseek-r1-middleware">
The AI TOOLKIT provides a middleware
(extractReasoningMiddleware) that can be used to extract the reasoning
tokens from the model's output.
When using DeepSeek-R1 series models with third-party providers like Together AI, we recommend using the startWithReasoning
option in the extractReasoningMiddleware function, as they tend to bypass thinking patterns.
</Note>
### Model Provider Comparison
You can use DeepSeek R1 with the AI TOOLKIT through various providers. Here's a comparison of the providers that support DeepSeek R1:
| Provider | Model ID | Reasoning Tokens |
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------- |
| DeepSeek | `deepseek-reasoner` | <Check size={18} /> |
| Fireworks | `accounts/fireworks/models/deepseek-r1` | Requires Middleware |
| Groq | `deepseek-r1-distill-llama-70b` | Requires Middleware |
| Azure | `DeepSeek-R1` | Requires Middleware |
| Together AI | `deepseek-ai/DeepSeek-R1` | Requires Middleware |
| FriendliAI | `deepseek-r1` | Requires Middleware |
| LangDB | `deepseek/deepseek-reasoner` | Requires Middleware |
### Building Interactive Interfaces
AI TOOLKIT Core can be paired with AI TOOLKIT UI, another powerful component of the AI TOOLKIT, to streamline the process of building chat, completion, and assistant interfaces with popular frameworks like Next.js, Nuxt, and SvelteKit.
AI TOOLKIT 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`, and `useObject` — 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 TOOLKIT, and DeepSeek R1:
In a new Next.js application, first install the AI TOOLKIT and the DeepSeek provider:
<Snippet text="pnpm install ai @ai-toolkit/deepseek @ai-toolkit/react" />
Then, create a route handler for the chat endpoint:
import { deepseek } from '@ai-toolkit/deepseek';
import { convertToModelMessages, streamText, UIMessage } from 'ai-toolkit';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: deepseek('deepseek-reasoner'),
messages: await convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse({
sendReasoning: true,
});
}
<Note>
You can forward the model's reasoning tokens to the client with
sendReasoning: true in the toDataStreamResponse method.
</Note>
Finally, update the root page (app/page.tsx) to use the useChat hook:
'use client';
import { useChat } from '@ai-toolkit/react';
import { useState } from 'react';
export default function Page() {
const [input, setInput] = useState('');
const { messages, sendMessage } = useChat();
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (input.trim()) {
sendMessage({ text: input });
setInput('');
}
};
return (
<>
{messages.map(message => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.parts.map((part, index) => {
if (part.type === 'reasoning') {
return <pre key={index}>{part.text}</pre>;
}
if (part.type === 'text') {
return <span key={index}>{part.text}</span>;
}
return null;
})}
</div>
))}
<form onSubmit={handleSubmit}>
<input
name="prompt"
value={input}
onChange={e => setInput(e.target.value)}
/>
<button type="submit">Submit</button>
</form>
</>
);
}
<Note>
You can access the model's reasoning tokens through the parts array on the
message object, where reasoning parts have type: 'reasoning'.
</Note>
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.
- 1import { deepseek } from '@ai-toolkit/deepseek';
- 2import { generateText } from 'ai-toolkit';
- 3const { reasoningText, text } = await generateText({
- 4 model: deepseek('deepseek-reasoner'),
- 5 prompt: 'Explain quantum entanglement.',
- 6});
- 1import { fireworks } from '@ai-toolkit/fireworks';
- 2import {
- 3 generateText,
- 4 wrapLanguageModel,
- 5 extractReasoningMiddleware,
- 6} from 'ai-toolkit';
- 7// middleware to extract reasoning tokens
- 8const enhancedModel = wrapLanguageModel({
- 9 model: fireworks('accounts/fireworks/models/deepseek-r1'),
- 10 middleware: extractReasoningMiddleware({ tagName: 'think' }),
- 11});
- 12const { reasoningText, text } = await generateText({
- 13 model: enhancedModel,
- 14 prompt: 'Explain quantum entanglement.',
- 15});
- 1import { groq } from '@ai-toolkit/groq';
- 2import {
- 3 generateText,
- 4 wrapLanguageModel,
- 5 extractReasoningMiddleware,
- 6} from 'ai-toolkit';
- 7// middleware to extract reasoning tokens
- 8const enhancedModel = wrapLanguageModel({
- 9 model: groq('deepseek-r1-distill-llama-70b'),
- 10 middleware: extractReasoningMiddleware({ tagName: 'think' }),
- 11});
- 12const { reasoningText, text } = await generateText({
- 13 model: enhancedModel,
- 14 prompt: 'Explain quantum entanglement.',
- 15});
- 1import { deepseek } from '@ai-toolkit/deepseek';
- 2import { convertToModelMessages, streamText, UIMessage } from 'ai-toolkit';
- 3export async function POST(req: Request) {
- 4 const { messages }: { messages: UIMessage[] } = await req.json();
- 5 const result = streamText({
- 6 model: deepseek('deepseek-reasoner'),
- 7 messages: await convertToModelMessages(messages),
- 8 });
- 9 return result.toUIMessageStreamResponse({
- 10 sendReasoning: true,
- 11 });
- 12}
- 1'use client';
- 2import { useChat } from '@ai-toolkit/react';
- 3import { useState } from 'react';
- 4export default function Page() {
- 5 const [input, setInput] = useState('');
- 6 const { messages, sendMessage } = useChat();
- 7 const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
- 8 e.preventDefault();
- 9 if (input.trim()) {
- 10 sendMessage({ text: input });
- 11 setInput('');
- 12 }
- 13 };
- 14 return (
- 15 <>
- 16 {messages.map(message => (
- 17 <div key={message.id}>
- 18 {message.role === 'user' ? 'User: ' : 'AI: '}
- 19 {message.parts.map((part, index) => {
- 20 if (part.type === 'reasoning') {
- 21 return <pre key={index}>{part.text}</pre>;
- 22 }
- 23 if (part.type === 'text') {
- 24 return <span key={index}>{part.text}</span>;
- 25 }
- 26 return null;
- 27 })}
- 28 </div>
- 29 ))}
- 30 <form onSubmit={handleSubmit}>
- 31 <input
- 32 name="prompt"
- 33 value={input}
- 34 onChange={e => setInput(e.target.value)}
- 35 />
- 36 <button type="submit">Submit</button>
- 37 </form>
- 38 </>
- 39 );
- 40}
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:
1. Explore the documentation at studio.khulnasoft.com/docs to understand the capabilities of the AI TOOLKIT.
2. Check out practical examples at studio.khulnasoft.com/examples to see the SDK in action.
3. Dive deeper with advanced guides on topics like Retrieval-Augmented Generation (RAG) at studio.khulnasoft.com/docs/guides.
4. 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.