Overview
DeepSeek provides specialized LLMs optimized for coding and general chat. The provider extends OpenAI’s interface with DeepSeek’s API endpoints and supports 128K context windows.
Installation
npm install @llamaindex/deepseek
Basic Usage
import { DeepSeekLLM } from "@llamaindex/deepseek";
const llm = new DeepSeekLLM({
model: "deepseek-coder",
apiKey: process.env.DEEPSEEK_API_KEY
});
const response = await llm.chat({
messages: [
{ role: "user", content: "Write a Python function to calculate Fibonacci numbers" }
]
});
console.log(response.message.content);
Constructor Options
model
DeepSeekModelName
default:"deepseek-coder"
DeepSeek model name: "deepseek-coder" or "deepseek-chat"
DeepSeek API key (defaults to DEEPSEEK_API_KEY env variable)
Maximum tokens in response
Nucleus sampling parameter
Additional OpenAI client options (e.g., custom baseURL)
Supported Models
DeepSeek Coder
const llm = new DeepSeekLLM({
model: "deepseek-coder"
});
- Context: 128K tokens
- Optimized for: Code generation, completion, debugging
- Use cases: Writing code, explaining code, refactoring
DeepSeek Chat
const llm = new DeepSeekLLM({
model: "deepseek-chat"
});
- Context: 128K tokens
- Optimized for: General conversation, reasoning
- Use cases: Q&A, analysis, creative writing
Streaming
const stream = await llm.chat({
messages: [{ role: "user", content: "Explain async/await in JavaScript" }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.delta);
}
Function Calling
DeepSeek supports function calling (tool use):
import { tool } from "@llamaindex/core/tools";
import { z } from "zod";
const executeCodeTool = tool({
name: "execute_code",
description: "Execute Python code",
parameters: z.object({
code: z.string().describe("Python code to execute")
}),
execute: async ({ code }) => {
// Execute code safely
return "Output: 42";
}
});
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
const response = await llm.chat({
messages: [{ role: "user", content: "Calculate 6 * 7" }],
tools: [executeCodeTool]
});
Code Generation
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
const response = await llm.chat({
messages: [
{
role: "system",
content: "You are an expert programmer. Write clean, efficient code."
},
{
role: "user",
content: "Create a React component for a todo list with TypeScript"
}
]
});
Code Explanation
const code = `
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[0];
const left = arr.slice(1).filter(x => x < pivot);
const right = arr.slice(1).filter(x => x >= pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
`;
const response = await llm.chat({
messages: [
{
role: "user",
content: `Explain how this code works:\n\n${code}`
}
]
});
With LlamaIndex
import { Settings, VectorStoreIndex } from "llamaindex";
import { DeepSeekLLM } from "@llamaindex/deepseek";
Settings.llm = new DeepSeekLLM({
model: "deepseek-coder"
});
const index = await VectorStoreIndex.fromDocuments(codeDocuments);
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "How does the authentication module work?"
});
Convenience Function
import { deepseek } from "@llamaindex/deepseek";
const llm = deepseek({
model: "deepseek-chat",
temperature: 0.7
});
Configuration
Environment Variables
Custom Base URL
const llm = new DeepSeekLLM({
additionalSessionOptions: {
baseURL: "https://custom-deepseek-endpoint.com/v1"
}
});
Default base URL: https://api.deepseek.com/v1
Global Settings
import { Settings } from "llamaindex";
import { DeepSeekLLM } from "@llamaindex/deepseek";
Settings.llm = new DeepSeekLLM({
model: "deepseek-coder"
});
Model Selection Guide
| Use Case | Recommended Model | Why |
|---|
| Code generation | deepseek-coder | Optimized for programming |
| Code review | deepseek-coder | Understands code patterns |
| General Q&A | deepseek-chat | Better conversational ability |
| Documentation | deepseek-chat | Natural language generation |
| Debugging | deepseek-coder | Code-specific reasoning |
Long Context Support
Both models support 128K context windows:
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
// Analyze large codebase
const response = await llm.chat({
messages: [
{
role: "user",
content: `Analyze this codebase:\n\n${largeCodebase}\n\nWhat improvements can be made?`
}
]
});
Error Handling
try {
const response = await llm.chat({ messages });
} catch (error) {
if (error.message.includes("DEEPSEEK_API_KEY")) {
console.error("API key not set or invalid");
} else if (error.status === 429) {
console.error("Rate limit exceeded");
} else {
console.error("API error:", error.message);
}
}
Check if tool calling is supported:
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
console.log(llm.supportToolCall); // true
Best Practices
- Use deepseek-coder for code: Better results for programming tasks
- Use deepseek-chat for general: Better for non-code tasks
- Leverage long context: Use full 128K for large codebases
- Provide context: Include relevant code snippets
- Use system messages: Guide the model’s behavior
- Enable streaming: Better UX for long generations
Example: Code Review Assistant
import { DeepSeekLLM } from "@llamaindex/deepseek";
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
async function reviewCode(code: string, language: string) {
const response = await llm.chat({
messages: [
{
role: "system",
content: "You are a senior code reviewer. Provide constructive feedback on code quality, performance, and best practices."
},
{
role: "user",
content: `Review this ${language} code:\n\n${code}`
}
]
});
return response.message.content;
}
const feedback = await reviewCode(myCode, "TypeScript");
console.log(feedback);
Example: Documentation Generator
import { DeepSeekLLM } from "@llamaindex/deepseek";
const llm = new DeepSeekLLM({ model: "deepseek-coder" });
async function generateDocs(code: string) {
const response = await llm.chat({
messages: [
{
role: "user",
content: `Generate comprehensive documentation for this code:\n\n${code}`
}
]
});
return response.message.content;
}
Pricing
DeepSeek offers competitive pricing. Check DeepSeek pricing for current rates.
See Also