chub-dev 0.1.0 → 0.1.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,1220 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: llm
|
|
3
|
+
description: "DeepSeek API JavaScript/TypeScript SDK coding guide for LLM chat and code generation"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "javascript"
|
|
6
|
+
versions: "1.0.0"
|
|
7
|
+
updated-on: "2026-03-02"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "deepseek,llm,ai,chat,code"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# DeepSeek API JavaScript/TypeScript SDK Coding Guide
|
|
13
|
+
|
|
14
|
+
## 1. Golden Rule
|
|
15
|
+
|
|
16
|
+
**Always use OpenAI-compatible SDKs to access the DeepSeek API:**
|
|
17
|
+
- Primary SDK: `openai` (Official OpenAI JavaScript SDK)
|
|
18
|
+
- Alternative: Any OpenAI-compatible SDK
|
|
19
|
+
|
|
20
|
+
**DeepSeek does NOT provide a dedicated JavaScript SDK.** Instead, the DeepSeek API is fully OpenAI-compatible, allowing you to use the official OpenAI SDK by simply changing the base URL and API key.
|
|
21
|
+
|
|
22
|
+
**Never use:**
|
|
23
|
+
- Unofficial or deprecated DeepSeek-specific packages
|
|
24
|
+
- Legacy API implementations
|
|
25
|
+
- Non-standard HTTP clients without proper error handling
|
|
26
|
+
|
|
27
|
+
**Correct Installation:**
|
|
28
|
+
```bash
|
|
29
|
+
npm install openai
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 2. Installation
|
|
33
|
+
|
|
34
|
+
### npm
|
|
35
|
+
```bash
|
|
36
|
+
npm install openai
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### yarn
|
|
40
|
+
```bash
|
|
41
|
+
yarn add openai
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### pnpm
|
|
45
|
+
```bash
|
|
46
|
+
pnpm add openai
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Environment Variables:**
|
|
50
|
+
```bash
|
|
51
|
+
DEEPSEEK_API_KEY=your_deepseek_api_key_here
|
|
52
|
+
# Get your API key from: https://platform.deepseek.com/api_keys
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Optional Configuration:**
|
|
56
|
+
```bash
|
|
57
|
+
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 3. Initialization
|
|
61
|
+
|
|
62
|
+
### Basic Initialization
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
import OpenAI from 'openai';
|
|
66
|
+
|
|
67
|
+
// Initialize with DeepSeek API configuration
|
|
68
|
+
const client = new OpenAI({
|
|
69
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
70
|
+
baseURL: 'https://api.deepseek.com',
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### With Explicit Configuration
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import OpenAI from 'openai';
|
|
78
|
+
|
|
79
|
+
const client = new OpenAI({
|
|
80
|
+
apiKey: 'your-deepseek-api-key',
|
|
81
|
+
baseURL: 'https://api.deepseek.com',
|
|
82
|
+
timeout: 30000, // 30 seconds
|
|
83
|
+
maxRetries: 3,
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### TypeScript Initialization
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import OpenAI from 'openai';
|
|
91
|
+
|
|
92
|
+
const client = new OpenAI({
|
|
93
|
+
apiKey: process.env.DEEPSEEK_API_KEY!,
|
|
94
|
+
baseURL: 'https://api.deepseek.com',
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## 4. Core API Surfaces
|
|
99
|
+
|
|
100
|
+
### Chat Completions (Non-Reasoning Mode)
|
|
101
|
+
|
|
102
|
+
**Minimal Example:**
|
|
103
|
+
```javascript
|
|
104
|
+
import OpenAI from 'openai';
|
|
105
|
+
|
|
106
|
+
const client = new OpenAI({
|
|
107
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
108
|
+
baseURL: 'https://api.deepseek.com',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
async function chat() {
|
|
112
|
+
const completion = await client.chat.completions.create({
|
|
113
|
+
model: 'deepseek-chat',
|
|
114
|
+
messages: [
|
|
115
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
116
|
+
{ role: 'user', content: 'Hello, how are you?' }
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
console.log(completion.choices[0].message.content);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
chat();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Advanced Example with Parameters:**
|
|
127
|
+
```javascript
|
|
128
|
+
const completion = await client.chat.completions.create({
|
|
129
|
+
model: 'deepseek-chat',
|
|
130
|
+
messages: [
|
|
131
|
+
{ role: 'system', content: 'You are a creative writing assistant.' },
|
|
132
|
+
{ role: 'user', content: 'Write a short story about AI.' }
|
|
133
|
+
],
|
|
134
|
+
temperature: 0.7,
|
|
135
|
+
max_tokens: 2000,
|
|
136
|
+
top_p: 0.9,
|
|
137
|
+
frequency_penalty: 0.5,
|
|
138
|
+
presence_penalty: 0.5,
|
|
139
|
+
stop: ['\n\n', 'END'],
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
console.log(completion.choices[0].message.content);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Reasoning Mode (DeepSeek-R1)
|
|
146
|
+
|
|
147
|
+
**Minimal Example:**
|
|
148
|
+
```javascript
|
|
149
|
+
const completion = await client.chat.completions.create({
|
|
150
|
+
model: 'deepseek-reasoner',
|
|
151
|
+
messages: [
|
|
152
|
+
{ role: 'user', content: 'Solve this math problem: What is the derivative of x^2 + 3x + 2?' }
|
|
153
|
+
],
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Access reasoning process
|
|
157
|
+
console.log('Reasoning:', completion.choices[0].message.reasoning_content);
|
|
158
|
+
console.log('Answer:', completion.choices[0].message.content);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Advanced Reasoning Example:**
|
|
162
|
+
```javascript
|
|
163
|
+
const completion = await client.chat.completions.create({
|
|
164
|
+
model: 'deepseek-reasoner',
|
|
165
|
+
messages: [
|
|
166
|
+
{
|
|
167
|
+
role: 'system',
|
|
168
|
+
content: 'You are a mathematical reasoning assistant. Show your work step by step.'
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
role: 'user',
|
|
172
|
+
content: 'A train travels 120 km in 2 hours. If it increases its speed by 20%, how long will it take to travel 180 km?'
|
|
173
|
+
}
|
|
174
|
+
],
|
|
175
|
+
max_tokens: 4000,
|
|
176
|
+
temperature: 1.0, // Reasoning models work best with temperature 1.0
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// The reasoning process is in reasoning_content
|
|
180
|
+
console.log('Thinking process:\n', completion.choices[0].message.reasoning_content);
|
|
181
|
+
console.log('\nFinal answer:\n', completion.choices[0].message.content);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Streaming Responses
|
|
185
|
+
|
|
186
|
+
**Basic Streaming:**
|
|
187
|
+
```javascript
|
|
188
|
+
async function streamChat() {
|
|
189
|
+
const stream = await client.chat.completions.create({
|
|
190
|
+
model: 'deepseek-chat',
|
|
191
|
+
messages: [
|
|
192
|
+
{ role: 'user', content: 'Write a poem about coding.' }
|
|
193
|
+
],
|
|
194
|
+
stream: true,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
for await (const chunk of stream) {
|
|
198
|
+
const content = chunk.choices[0]?.delta?.content || '';
|
|
199
|
+
process.stdout.write(content);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
streamChat();
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Advanced Streaming with Error Handling:**
|
|
207
|
+
```javascript
|
|
208
|
+
async function streamWithErrorHandling() {
|
|
209
|
+
try {
|
|
210
|
+
const stream = await client.chat.completions.create({
|
|
211
|
+
model: 'deepseek-chat',
|
|
212
|
+
messages: [{ role: 'user', content: 'Tell me a story.' }],
|
|
213
|
+
stream: true,
|
|
214
|
+
max_tokens: 1000,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
let fullResponse = '';
|
|
218
|
+
|
|
219
|
+
for await (const chunk of stream) {
|
|
220
|
+
const delta = chunk.choices[0]?.delta;
|
|
221
|
+
|
|
222
|
+
if (delta?.content) {
|
|
223
|
+
fullResponse += delta.content;
|
|
224
|
+
process.stdout.write(delta.content);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Check for finish reason
|
|
228
|
+
if (chunk.choices[0]?.finish_reason) {
|
|
229
|
+
console.log('\n\nFinish reason:', chunk.choices[0].finish_reason);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return fullResponse;
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error('Streaming error:', error);
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**Streaming with Reasoning Mode:**
|
|
242
|
+
```javascript
|
|
243
|
+
async function streamReasoning() {
|
|
244
|
+
const stream = await client.chat.completions.create({
|
|
245
|
+
model: 'deepseek-reasoner',
|
|
246
|
+
messages: [
|
|
247
|
+
{ role: 'user', content: 'Explain quantum entanglement.' }
|
|
248
|
+
],
|
|
249
|
+
stream: true,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
let reasoning = '';
|
|
253
|
+
let answer = '';
|
|
254
|
+
|
|
255
|
+
for await (const chunk of stream) {
|
|
256
|
+
const delta = chunk.choices[0]?.delta;
|
|
257
|
+
|
|
258
|
+
// Reasoning content comes first
|
|
259
|
+
if (delta?.reasoning_content) {
|
|
260
|
+
reasoning += delta.reasoning_content;
|
|
261
|
+
console.log('[THINKING]', delta.reasoning_content);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Final answer comes after reasoning
|
|
265
|
+
if (delta?.content) {
|
|
266
|
+
answer += delta.content;
|
|
267
|
+
console.log('[ANSWER]', delta.content);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### JSON Mode (Structured Output)
|
|
274
|
+
|
|
275
|
+
**Minimal Example:**
|
|
276
|
+
```javascript
|
|
277
|
+
const completion = await client.chat.completions.create({
|
|
278
|
+
model: 'deepseek-chat',
|
|
279
|
+
messages: [
|
|
280
|
+
{
|
|
281
|
+
role: 'system',
|
|
282
|
+
content: 'You are a helpful assistant. Always respond with valid JSON.'
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
role: 'user',
|
|
286
|
+
content: 'Extract the following information as JSON: Name is John, age 30, city New York.'
|
|
287
|
+
}
|
|
288
|
+
],
|
|
289
|
+
response_format: { type: 'json_object' },
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const result = JSON.parse(completion.choices[0].message.content);
|
|
293
|
+
console.log(result);
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
**Advanced JSON Mode with Schema Validation:**
|
|
297
|
+
```javascript
|
|
298
|
+
async function structuredExtraction() {
|
|
299
|
+
const completion = await client.chat.completions.create({
|
|
300
|
+
model: 'deepseek-chat',
|
|
301
|
+
messages: [
|
|
302
|
+
{
|
|
303
|
+
role: 'system',
|
|
304
|
+
content: `You are a data extraction assistant. Extract information and return it as JSON with this schema:
|
|
305
|
+
{
|
|
306
|
+
"name": "string",
|
|
307
|
+
"age": "number",
|
|
308
|
+
"occupation": "string",
|
|
309
|
+
"skills": ["string"],
|
|
310
|
+
"contact": {
|
|
311
|
+
"email": "string",
|
|
312
|
+
"phone": "string"
|
|
313
|
+
}
|
|
314
|
+
}`
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
role: 'user',
|
|
318
|
+
content: 'John Doe is a 35-year-old software engineer skilled in Python, JavaScript, and DevOps. Email: john@example.com, Phone: 555-1234.'
|
|
319
|
+
}
|
|
320
|
+
],
|
|
321
|
+
response_format: { type: 'json_object' },
|
|
322
|
+
temperature: 0.1, // Lower temperature for structured output
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
const data = JSON.parse(completion.choices[0].message.content);
|
|
327
|
+
console.log('Extracted data:', data);
|
|
328
|
+
return data;
|
|
329
|
+
} catch (error) {
|
|
330
|
+
console.error('Failed to parse JSON:', error);
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Function Calling (Tool Use)
|
|
337
|
+
|
|
338
|
+
**Minimal Example:**
|
|
339
|
+
```javascript
|
|
340
|
+
const tools = [
|
|
341
|
+
{
|
|
342
|
+
type: 'function',
|
|
343
|
+
function: {
|
|
344
|
+
name: 'get_weather',
|
|
345
|
+
description: 'Get the current weather for a location',
|
|
346
|
+
parameters: {
|
|
347
|
+
type: 'object',
|
|
348
|
+
properties: {
|
|
349
|
+
location: {
|
|
350
|
+
type: 'string',
|
|
351
|
+
description: 'The city and state, e.g. San Francisco, CA',
|
|
352
|
+
},
|
|
353
|
+
unit: {
|
|
354
|
+
type: 'string',
|
|
355
|
+
enum: ['celsius', 'fahrenheit'],
|
|
356
|
+
description: 'The temperature unit',
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
required: ['location'],
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
];
|
|
364
|
+
|
|
365
|
+
const completion = await client.chat.completions.create({
|
|
366
|
+
model: 'deepseek-chat',
|
|
367
|
+
messages: [
|
|
368
|
+
{ role: 'user', content: "What's the weather in San Francisco?" }
|
|
369
|
+
],
|
|
370
|
+
tools: tools,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// Check if the model wants to call a function
|
|
374
|
+
const message = completion.choices[0].message;
|
|
375
|
+
if (message.tool_calls) {
|
|
376
|
+
console.log('Function to call:', message.tool_calls[0].function.name);
|
|
377
|
+
console.log('Arguments:', message.tool_calls[0].function.arguments);
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
**Advanced Function Calling with Execution:**
|
|
382
|
+
```javascript
|
|
383
|
+
// Define available functions
|
|
384
|
+
const availableFunctions = {
|
|
385
|
+
get_weather: async (location, unit = 'celsius') => {
|
|
386
|
+
// Simulate weather API call
|
|
387
|
+
return {
|
|
388
|
+
location,
|
|
389
|
+
temperature: 22,
|
|
390
|
+
unit,
|
|
391
|
+
condition: 'sunny',
|
|
392
|
+
};
|
|
393
|
+
},
|
|
394
|
+
calculate: async (expression) => {
|
|
395
|
+
return eval(expression); // Use safely in production!
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// Define tools
|
|
400
|
+
const tools = [
|
|
401
|
+
{
|
|
402
|
+
type: 'function',
|
|
403
|
+
function: {
|
|
404
|
+
name: 'get_weather',
|
|
405
|
+
description: 'Get current weather',
|
|
406
|
+
parameters: {
|
|
407
|
+
type: 'object',
|
|
408
|
+
properties: {
|
|
409
|
+
location: { type: 'string' },
|
|
410
|
+
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
|
|
411
|
+
},
|
|
412
|
+
required: ['location'],
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
type: 'function',
|
|
418
|
+
function: {
|
|
419
|
+
name: 'calculate',
|
|
420
|
+
description: 'Perform mathematical calculation',
|
|
421
|
+
parameters: {
|
|
422
|
+
type: 'object',
|
|
423
|
+
properties: {
|
|
424
|
+
expression: { type: 'string', description: 'Math expression to evaluate' },
|
|
425
|
+
},
|
|
426
|
+
required: ['expression'],
|
|
427
|
+
},
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
];
|
|
431
|
+
|
|
432
|
+
async function runConversation(userMessage) {
|
|
433
|
+
const messages = [{ role: 'user', content: userMessage }];
|
|
434
|
+
|
|
435
|
+
// First API call
|
|
436
|
+
let completion = await client.chat.completions.create({
|
|
437
|
+
model: 'deepseek-chat',
|
|
438
|
+
messages: messages,
|
|
439
|
+
tools: tools,
|
|
440
|
+
tool_choice: 'auto',
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
let message = completion.choices[0].message;
|
|
444
|
+
|
|
445
|
+
// Handle function calls
|
|
446
|
+
if (message.tool_calls) {
|
|
447
|
+
messages.push(message); // Add assistant message with tool calls
|
|
448
|
+
|
|
449
|
+
// Execute each function call
|
|
450
|
+
for (const toolCall of message.tool_calls) {
|
|
451
|
+
const functionName = toolCall.function.name;
|
|
452
|
+
const functionArgs = JSON.parse(toolCall.function.arguments);
|
|
453
|
+
|
|
454
|
+
console.log(`Calling function: ${functionName}`, functionArgs);
|
|
455
|
+
|
|
456
|
+
// Execute function
|
|
457
|
+
const functionResponse = await availableFunctions[functionName](...Object.values(functionArgs));
|
|
458
|
+
|
|
459
|
+
// Add function response to messages
|
|
460
|
+
messages.push({
|
|
461
|
+
role: 'tool',
|
|
462
|
+
tool_call_id: toolCall.id,
|
|
463
|
+
content: JSON.stringify(functionResponse),
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Second API call with function results
|
|
468
|
+
completion = await client.chat.completions.create({
|
|
469
|
+
model: 'deepseek-chat',
|
|
470
|
+
messages: messages,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
message = completion.choices[0].message;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return message.content;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Usage
|
|
480
|
+
runConversation("What's the weather in Tokyo and what's 123 * 456?").then(console.log);
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### Context Caching (Automatic)
|
|
484
|
+
|
|
485
|
+
DeepSeek automatically caches frequently referenced contexts. No code changes required.
|
|
486
|
+
|
|
487
|
+
**Example with Repeated Context:**
|
|
488
|
+
```javascript
|
|
489
|
+
const systemPrompt = `You are an expert code reviewer. Review code for:
|
|
490
|
+
1. Security vulnerabilities
|
|
491
|
+
2. Performance issues
|
|
492
|
+
3. Code style and best practices
|
|
493
|
+
4. Potential bugs
|
|
494
|
+
5. Maintainability concerns
|
|
495
|
+
|
|
496
|
+
Provide detailed feedback with examples.`;
|
|
497
|
+
|
|
498
|
+
// First request - cache miss
|
|
499
|
+
const review1 = await client.chat.completions.create({
|
|
500
|
+
model: 'deepseek-chat',
|
|
501
|
+
messages: [
|
|
502
|
+
{ role: 'system', content: systemPrompt },
|
|
503
|
+
{ role: 'user', content: 'Review this code: function add(a, b) { return a + b; }' }
|
|
504
|
+
],
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
console.log('Cache stats:', review1.usage);
|
|
508
|
+
// prompt_cache_miss_tokens: 150 (example)
|
|
509
|
+
// prompt_cache_hit_tokens: 0
|
|
510
|
+
|
|
511
|
+
// Second request with same system prompt - cache hit
|
|
512
|
+
const review2 = await client.chat.completions.create({
|
|
513
|
+
model: 'deepseek-chat',
|
|
514
|
+
messages: [
|
|
515
|
+
{ role: 'system', content: systemPrompt }, // Same system prompt
|
|
516
|
+
{ role: 'user', content: 'Review this code: function multiply(x, y) { return x * y; }' }
|
|
517
|
+
],
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
console.log('Cache stats:', review2.usage);
|
|
521
|
+
// prompt_cache_miss_tokens: 20
|
|
522
|
+
// prompt_cache_hit_tokens: 150 (cached system prompt)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
**Monitoring Cache Performance:**
|
|
526
|
+
```javascript
|
|
527
|
+
function analyzeCachePerformance(response) {
|
|
528
|
+
const usage = response.usage;
|
|
529
|
+
const cacheHitRate = usage.prompt_cache_hit_tokens /
|
|
530
|
+
(usage.prompt_cache_hit_tokens + usage.prompt_cache_miss_tokens);
|
|
531
|
+
|
|
532
|
+
console.log('Total prompt tokens:', usage.prompt_tokens);
|
|
533
|
+
console.log('Cache hits:', usage.prompt_cache_hit_tokens);
|
|
534
|
+
console.log('Cache misses:', usage.prompt_cache_miss_tokens);
|
|
535
|
+
console.log('Cache hit rate:', `${(cacheHitRate * 100).toFixed(2)}%`);
|
|
536
|
+
console.log('Completion tokens:', usage.completion_tokens);
|
|
537
|
+
|
|
538
|
+
// Calculate cost savings (approximate)
|
|
539
|
+
const cachedCost = (usage.prompt_cache_hit_tokens / 1_000_000) * 0.014;
|
|
540
|
+
const uncachedCost = (usage.prompt_cache_miss_tokens / 1_000_000) * 0.14;
|
|
541
|
+
console.log(`Cost: $${(cachedCost + uncachedCost).toFixed(6)}`);
|
|
542
|
+
}
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
## 5. Advanced Features
|
|
546
|
+
|
|
547
|
+
### Multi-Turn Conversations
|
|
548
|
+
|
|
549
|
+
```javascript
|
|
550
|
+
async function conversation() {
|
|
551
|
+
const messages = [
|
|
552
|
+
{ role: 'system', content: 'You are a helpful coding assistant.' }
|
|
553
|
+
];
|
|
554
|
+
|
|
555
|
+
// First turn
|
|
556
|
+
messages.push({ role: 'user', content: 'How do I read a file in Node.js?' });
|
|
557
|
+
|
|
558
|
+
let completion = await client.chat.completions.create({
|
|
559
|
+
model: 'deepseek-chat',
|
|
560
|
+
messages: messages,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
messages.push({ role: 'assistant', content: completion.choices[0].message.content });
|
|
564
|
+
console.log('Assistant:', completion.choices[0].message.content);
|
|
565
|
+
|
|
566
|
+
// Second turn
|
|
567
|
+
messages.push({ role: 'user', content: 'Can you show me an async example?' });
|
|
568
|
+
|
|
569
|
+
completion = await client.chat.completions.create({
|
|
570
|
+
model: 'deepseek-chat',
|
|
571
|
+
messages: messages,
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
messages.push({ role: 'assistant', content: completion.choices[0].message.content });
|
|
575
|
+
console.log('Assistant:', completion.choices[0].message.content);
|
|
576
|
+
|
|
577
|
+
return messages;
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
### Token Counting and Management
|
|
582
|
+
|
|
583
|
+
```javascript
|
|
584
|
+
// Approximate token counting (use tiktoken library for accuracy)
|
|
585
|
+
import { encoding_for_model } from 'tiktoken';
|
|
586
|
+
|
|
587
|
+
function countTokens(text, model = 'gpt-4') {
|
|
588
|
+
const encoding = encoding_for_model(model);
|
|
589
|
+
const tokens = encoding.encode(text);
|
|
590
|
+
encoding.free();
|
|
591
|
+
return tokens.length;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
async function managedCompletion(messages, maxResponseTokens = 2000) {
|
|
595
|
+
// Count input tokens
|
|
596
|
+
const inputText = messages.map(m => m.content).join('\n');
|
|
597
|
+
const estimatedInputTokens = countTokens(inputText);
|
|
598
|
+
|
|
599
|
+
console.log(`Estimated input tokens: ${estimatedInputTokens}`);
|
|
600
|
+
|
|
601
|
+
// Ensure we don't exceed context window (128K for deepseek-chat)
|
|
602
|
+
if (estimatedInputTokens > 120000) {
|
|
603
|
+
console.warn('Input approaching context limit!');
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const completion = await client.chat.completions.create({
|
|
607
|
+
model: 'deepseek-chat',
|
|
608
|
+
messages: messages,
|
|
609
|
+
max_tokens: maxResponseTokens,
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
// Actual usage from API
|
|
613
|
+
console.log('Actual usage:', completion.usage);
|
|
614
|
+
|
|
615
|
+
return completion;
|
|
616
|
+
}
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
### Custom Request Headers
|
|
620
|
+
|
|
621
|
+
```javascript
|
|
622
|
+
const client = new OpenAI({
|
|
623
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
624
|
+
baseURL: 'https://api.deepseek.com',
|
|
625
|
+
defaultHeaders: {
|
|
626
|
+
'X-Custom-Header': 'value',
|
|
627
|
+
},
|
|
628
|
+
});
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
### Timeout and Retry Configuration
|
|
632
|
+
|
|
633
|
+
```javascript
|
|
634
|
+
const client = new OpenAI({
|
|
635
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
636
|
+
baseURL: 'https://api.deepseek.com',
|
|
637
|
+
timeout: 60000, // 60 seconds
|
|
638
|
+
maxRetries: 5,
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
// Per-request override
|
|
642
|
+
const completion = await client.chat.completions.create(
|
|
643
|
+
{
|
|
644
|
+
model: 'deepseek-chat',
|
|
645
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
timeout: 30000,
|
|
649
|
+
maxRetries: 2,
|
|
650
|
+
}
|
|
651
|
+
);
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
## 6. TypeScript Usage
|
|
655
|
+
|
|
656
|
+
### Type-Safe Completions
|
|
657
|
+
|
|
658
|
+
```typescript
|
|
659
|
+
import OpenAI from 'openai';
|
|
660
|
+
import type { ChatCompletionMessageParam, ChatCompletion } from 'openai/resources/chat';
|
|
661
|
+
|
|
662
|
+
const client = new OpenAI({
|
|
663
|
+
apiKey: process.env.DEEPSEEK_API_KEY!,
|
|
664
|
+
baseURL: 'https://api.deepseek.com',
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
async function typedChat(): Promise<string> {
|
|
668
|
+
const messages: ChatCompletionMessageParam[] = [
|
|
669
|
+
{ role: 'system', content: 'You are helpful.' },
|
|
670
|
+
{ role: 'user', content: 'Hello' },
|
|
671
|
+
];
|
|
672
|
+
|
|
673
|
+
const completion: ChatCompletion = await client.chat.completions.create({
|
|
674
|
+
model: 'deepseek-chat',
|
|
675
|
+
messages: messages,
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
return completion.choices[0].message.content || '';
|
|
679
|
+
}
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
### Typed Function Calling
|
|
683
|
+
|
|
684
|
+
```typescript
|
|
685
|
+
import OpenAI from 'openai';
|
|
686
|
+
import type { ChatCompletionTool } from 'openai/resources/chat';
|
|
687
|
+
|
|
688
|
+
interface WeatherParams {
|
|
689
|
+
location: string;
|
|
690
|
+
unit?: 'celsius' | 'fahrenheit';
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
interface WeatherResult {
|
|
694
|
+
location: string;
|
|
695
|
+
temperature: number;
|
|
696
|
+
unit: string;
|
|
697
|
+
condition: string;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const tools: ChatCompletionTool[] = [
|
|
701
|
+
{
|
|
702
|
+
type: 'function',
|
|
703
|
+
function: {
|
|
704
|
+
name: 'get_weather',
|
|
705
|
+
description: 'Get current weather',
|
|
706
|
+
parameters: {
|
|
707
|
+
type: 'object',
|
|
708
|
+
properties: {
|
|
709
|
+
location: { type: 'string' },
|
|
710
|
+
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
|
|
711
|
+
},
|
|
712
|
+
required: ['location'],
|
|
713
|
+
},
|
|
714
|
+
},
|
|
715
|
+
},
|
|
716
|
+
];
|
|
717
|
+
|
|
718
|
+
async function getWeather(params: WeatherParams): Promise<WeatherResult> {
|
|
719
|
+
// Implementation
|
|
720
|
+
return {
|
|
721
|
+
location: params.location,
|
|
722
|
+
temperature: 22,
|
|
723
|
+
unit: params.unit || 'celsius',
|
|
724
|
+
condition: 'sunny',
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
### Strict Type Checking for Streaming
|
|
730
|
+
|
|
731
|
+
```typescript
|
|
732
|
+
import type { Stream } from 'openai/streaming';
|
|
733
|
+
import type { ChatCompletionChunk } from 'openai/resources/chat';
|
|
734
|
+
|
|
735
|
+
async function typedStream(): Promise<void> {
|
|
736
|
+
const stream: Stream<ChatCompletionChunk> = await client.chat.completions.create({
|
|
737
|
+
model: 'deepseek-chat',
|
|
738
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
739
|
+
stream: true,
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
for await (const chunk of stream) {
|
|
743
|
+
const content: string | undefined = chunk.choices[0]?.delta?.content;
|
|
744
|
+
if (content) {
|
|
745
|
+
process.stdout.write(content);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
## 7. Best Practices
|
|
752
|
+
|
|
753
|
+
### Error Handling
|
|
754
|
+
|
|
755
|
+
```javascript
|
|
756
|
+
import OpenAI from 'openai';
|
|
757
|
+
|
|
758
|
+
async function robustCompletion(messages) {
|
|
759
|
+
try {
|
|
760
|
+
const completion = await client.chat.completions.create({
|
|
761
|
+
model: 'deepseek-chat',
|
|
762
|
+
messages: messages,
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
return completion.choices[0].message.content;
|
|
766
|
+
} catch (error) {
|
|
767
|
+
if (error instanceof OpenAI.APIError) {
|
|
768
|
+
console.error('API Error:', error.status, error.message);
|
|
769
|
+
console.error('Request ID:', error.headers?.['x-request-id']);
|
|
770
|
+
|
|
771
|
+
// Handle specific error codes
|
|
772
|
+
if (error.status === 429) {
|
|
773
|
+
console.error('Rate limit exceeded. Implementing backoff...');
|
|
774
|
+
// Implement exponential backoff
|
|
775
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
776
|
+
return robustCompletion(messages); // Retry
|
|
777
|
+
} else if (error.status === 401) {
|
|
778
|
+
console.error('Authentication failed. Check API key.');
|
|
779
|
+
throw error;
|
|
780
|
+
} else if (error.status >= 500) {
|
|
781
|
+
console.error('Server error. Retrying...');
|
|
782
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
783
|
+
return robustCompletion(messages);
|
|
784
|
+
}
|
|
785
|
+
} else if (error.code === 'ECONNABORTED') {
|
|
786
|
+
console.error('Request timeout');
|
|
787
|
+
} else {
|
|
788
|
+
console.error('Unexpected error:', error);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
throw error;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
### Rate Limiting and Throttling
|
|
797
|
+
|
|
798
|
+
```javascript
|
|
799
|
+
class DeepSeekClient {
|
|
800
|
+
constructor(apiKey) {
|
|
801
|
+
this.client = new OpenAI({
|
|
802
|
+
apiKey: apiKey,
|
|
803
|
+
baseURL: 'https://api.deepseek.com',
|
|
804
|
+
});
|
|
805
|
+
this.requestQueue = [];
|
|
806
|
+
this.processing = false;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
async queueRequest(params) {
|
|
810
|
+
return new Promise((resolve, reject) => {
|
|
811
|
+
this.requestQueue.push({ params, resolve, reject });
|
|
812
|
+
this.processQueue();
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
async processQueue() {
|
|
817
|
+
if (this.processing || this.requestQueue.length === 0) return;
|
|
818
|
+
|
|
819
|
+
this.processing = true;
|
|
820
|
+
|
|
821
|
+
while (this.requestQueue.length > 0) {
|
|
822
|
+
const { params, resolve, reject } = this.requestQueue.shift();
|
|
823
|
+
|
|
824
|
+
try {
|
|
825
|
+
const result = await this.client.chat.completions.create(params);
|
|
826
|
+
resolve(result);
|
|
827
|
+
} catch (error) {
|
|
828
|
+
if (error.status === 429) {
|
|
829
|
+
// Re-queue with delay
|
|
830
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
831
|
+
this.requestQueue.unshift({ params, resolve, reject });
|
|
832
|
+
} else {
|
|
833
|
+
reject(error);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// Add small delay between requests
|
|
838
|
+
await new Promise(r => setTimeout(r, 100));
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
this.processing = false;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// Usage
|
|
846
|
+
const deepseek = new DeepSeekClient(process.env.DEEPSEEK_API_KEY);
|
|
847
|
+
const result = await deepseek.queueRequest({
|
|
848
|
+
model: 'deepseek-chat',
|
|
849
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
850
|
+
});
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
### Optimizing for Context Caching
|
|
854
|
+
|
|
855
|
+
```javascript
|
|
856
|
+
// Structure prompts to maximize cache hits
|
|
857
|
+
class ConversationManager {
|
|
858
|
+
constructor() {
|
|
859
|
+
this.systemPrompt = ''; // Static system prompt for caching
|
|
860
|
+
this.conversationHistory = [];
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
setSystemPrompt(prompt) {
|
|
864
|
+
// Set once and reuse for cache efficiency
|
|
865
|
+
this.systemPrompt = prompt;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
async chat(userMessage) {
|
|
869
|
+
const messages = [
|
|
870
|
+
{ role: 'system', content: this.systemPrompt }, // Cached
|
|
871
|
+
...this.conversationHistory,
|
|
872
|
+
{ role: 'user', content: userMessage }
|
|
873
|
+
];
|
|
874
|
+
|
|
875
|
+
const completion = await client.chat.completions.create({
|
|
876
|
+
model: 'deepseek-chat',
|
|
877
|
+
messages: messages,
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
const assistantMessage = completion.choices[0].message.content;
|
|
881
|
+
|
|
882
|
+
// Update history
|
|
883
|
+
this.conversationHistory.push(
|
|
884
|
+
{ role: 'user', content: userMessage },
|
|
885
|
+
{ role: 'assistant', content: assistantMessage }
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
// Keep history manageable (trim if too long)
|
|
889
|
+
if (this.conversationHistory.length > 20) {
|
|
890
|
+
this.conversationHistory = this.conversationHistory.slice(-20);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
return {
|
|
894
|
+
message: assistantMessage,
|
|
895
|
+
usage: completion.usage,
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
### Model Selection Strategy
|
|
902
|
+
|
|
903
|
+
```javascript
|
|
904
|
+
function selectModel(taskType, budget = 'normal') {
|
|
905
|
+
const modelMap = {
|
|
906
|
+
// Use deepseek-chat for general tasks
|
|
907
|
+
'chat': 'deepseek-chat',
|
|
908
|
+
'coding': 'deepseek-chat',
|
|
909
|
+
'writing': 'deepseek-chat',
|
|
910
|
+
'translation': 'deepseek-chat',
|
|
911
|
+
|
|
912
|
+
// Use deepseek-reasoner for complex reasoning
|
|
913
|
+
'math': 'deepseek-reasoner',
|
|
914
|
+
'logic': 'deepseek-reasoner',
|
|
915
|
+
'analysis': 'deepseek-reasoner',
|
|
916
|
+
'problem-solving': 'deepseek-reasoner',
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
return modelMap[taskType] || 'deepseek-chat';
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async function smartCompletion(taskType, message) {
|
|
923
|
+
const model = selectModel(taskType);
|
|
924
|
+
|
|
925
|
+
console.log(`Using model: ${model} for task: ${taskType}`);
|
|
926
|
+
|
|
927
|
+
const completion = await client.chat.completions.create({
|
|
928
|
+
model: model,
|
|
929
|
+
messages: [{ role: 'user', content: message }],
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
return completion.choices[0].message.content;
|
|
933
|
+
}
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
### Security Best Practices
|
|
937
|
+
|
|
938
|
+
```javascript
|
|
939
|
+
// NEVER expose API keys in client-side code
|
|
940
|
+
// NEVER commit API keys to version control
|
|
941
|
+
// ALWAYS use environment variables
|
|
942
|
+
|
|
943
|
+
// Use a proxy server for client applications
|
|
944
|
+
// Example Express.js proxy endpoint:
|
|
945
|
+
import express from 'express';
|
|
946
|
+
|
|
947
|
+
const app = express();
|
|
948
|
+
app.use(express.json());
|
|
949
|
+
|
|
950
|
+
app.post('/api/chat', async (req, res) => {
|
|
951
|
+
try {
|
|
952
|
+
// Validate and sanitize input
|
|
953
|
+
const { messages } = req.body;
|
|
954
|
+
|
|
955
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
956
|
+
return res.status(400).json({ error: 'Invalid messages' });
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
// Rate limiting per user/IP (use express-rate-limit)
|
|
960
|
+
// Authentication (use your auth system)
|
|
961
|
+
|
|
962
|
+
const completion = await client.chat.completions.create({
|
|
963
|
+
model: 'deepseek-chat',
|
|
964
|
+
messages: messages,
|
|
965
|
+
max_tokens: 2000, // Limit to control costs
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
res.json({ message: completion.choices[0].message.content });
|
|
969
|
+
} catch (error) {
|
|
970
|
+
console.error('Proxy error:', error);
|
|
971
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
```
|
|
975
|
+
|
|
976
|
+
## 8. Production Checklist
|
|
977
|
+
|
|
978
|
+
### Version Management
|
|
979
|
+
- Pin exact SDK version in package.json: `"openai": "4.73.0"`
|
|
980
|
+
- Test thoroughly before upgrading OpenAI SDK versions
|
|
981
|
+
- Monitor OpenAI SDK changelog for breaking changes
|
|
982
|
+
- Document which SDK version works with DeepSeek API
|
|
983
|
+
|
|
984
|
+
### Environment Configuration
|
|
985
|
+
```javascript
|
|
986
|
+
// config/deepseek.js
|
|
987
|
+
export const deepseekConfig = {
|
|
988
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
989
|
+
baseURL: process.env.DEEPSEEK_BASE_URL || 'https://api.deepseek.com',
|
|
990
|
+
timeout: parseInt(process.env.DEEPSEEK_TIMEOUT || '60000'),
|
|
991
|
+
maxRetries: parseInt(process.env.DEEPSEEK_MAX_RETRIES || '3'),
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
// Validate configuration on startup
|
|
995
|
+
export function validateConfig() {
|
|
996
|
+
if (!deepseekConfig.apiKey) {
|
|
997
|
+
throw new Error('DEEPSEEK_API_KEY environment variable is required');
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
console.log('DeepSeek configuration validated');
|
|
1001
|
+
}
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
### Error Handling Checklist
|
|
1005
|
+
- ✅ Implement exponential backoff for rate limits (429 errors)
|
|
1006
|
+
- ✅ Handle network timeouts gracefully
|
|
1007
|
+
- ✅ Log all errors with request IDs for debugging
|
|
1008
|
+
- ✅ Implement circuit breaker for repeated failures
|
|
1009
|
+
- ✅ Provide fallback responses for critical paths
|
|
1010
|
+
- ✅ Monitor error rates and set up alerts
|
|
1011
|
+
|
|
1012
|
+
### Monitoring and Logging
|
|
1013
|
+
```javascript
|
|
1014
|
+
import winston from 'winston';
|
|
1015
|
+
|
|
1016
|
+
const logger = winston.createLogger({
|
|
1017
|
+
level: 'info',
|
|
1018
|
+
format: winston.format.json(),
|
|
1019
|
+
transports: [
|
|
1020
|
+
new winston.transports.File({ filename: 'deepseek-error.log', level: 'error' }),
|
|
1021
|
+
new winston.transports.File({ filename: 'deepseek-combined.log' }),
|
|
1022
|
+
],
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
async function monitoredCompletion(messages) {
|
|
1026
|
+
const startTime = Date.now();
|
|
1027
|
+
|
|
1028
|
+
try {
|
|
1029
|
+
const completion = await client.chat.completions.create({
|
|
1030
|
+
model: 'deepseek-chat',
|
|
1031
|
+
messages: messages,
|
|
1032
|
+
});
|
|
1033
|
+
|
|
1034
|
+
const duration = Date.now() - startTime;
|
|
1035
|
+
|
|
1036
|
+
logger.info('Completion success', {
|
|
1037
|
+
duration,
|
|
1038
|
+
model: 'deepseek-chat',
|
|
1039
|
+
inputTokens: completion.usage.prompt_tokens,
|
|
1040
|
+
outputTokens: completion.usage.completion_tokens,
|
|
1041
|
+
cacheHits: completion.usage.prompt_cache_hit_tokens,
|
|
1042
|
+
cacheMisses: completion.usage.prompt_cache_miss_tokens,
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
return completion;
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
const duration = Date.now() - startTime;
|
|
1048
|
+
|
|
1049
|
+
logger.error('Completion failed', {
|
|
1050
|
+
duration,
|
|
1051
|
+
error: error.message,
|
|
1052
|
+
status: error.status,
|
|
1053
|
+
requestId: error.headers?.['x-request-id'],
|
|
1054
|
+
});
|
|
1055
|
+
|
|
1056
|
+
throw error;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
### Cost Tracking
|
|
1062
|
+
```javascript
|
|
1063
|
+
class CostTracker {
|
|
1064
|
+
constructor() {
|
|
1065
|
+
this.totalInputTokens = 0;
|
|
1066
|
+
this.totalOutputTokens = 0;
|
|
1067
|
+
this.totalCachedTokens = 0;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
trackUsage(usage) {
|
|
1071
|
+
this.totalInputTokens += usage.prompt_cache_miss_tokens || 0;
|
|
1072
|
+
this.totalOutputTokens += usage.completion_tokens || 0;
|
|
1073
|
+
this.totalCachedTokens += usage.prompt_cache_hit_tokens || 0;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
estimateCost() {
|
|
1077
|
+
// Pricing as of 2025
|
|
1078
|
+
const inputCost = (this.totalInputTokens / 1_000_000) * 0.14;
|
|
1079
|
+
const outputCost = (this.totalOutputTokens / 1_000_000) * 0.28;
|
|
1080
|
+
const cachedCost = (this.totalCachedTokens / 1_000_000) * 0.014;
|
|
1081
|
+
|
|
1082
|
+
return {
|
|
1083
|
+
input: inputCost,
|
|
1084
|
+
output: outputCost,
|
|
1085
|
+
cached: cachedCost,
|
|
1086
|
+
total: inputCost + outputCost + cachedCost,
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
report() {
|
|
1091
|
+
const cost = this.estimateCost();
|
|
1092
|
+
console.log('Cost Report:');
|
|
1093
|
+
console.log(` Input tokens: ${this.totalInputTokens.toLocaleString()} ($${cost.input.toFixed(4)})`);
|
|
1094
|
+
console.log(` Output tokens: ${this.totalOutputTokens.toLocaleString()} ($${cost.output.toFixed(4)})`);
|
|
1095
|
+
console.log(` Cached tokens: ${this.totalCachedTokens.toLocaleString()} ($${cost.cached.toFixed(4)})`);
|
|
1096
|
+
console.log(` Total cost: $${cost.total.toFixed(4)}`);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
// Usage
|
|
1101
|
+
const tracker = new CostTracker();
|
|
1102
|
+
|
|
1103
|
+
const completion = await client.chat.completions.create({
|
|
1104
|
+
model: 'deepseek-chat',
|
|
1105
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
tracker.trackUsage(completion.usage);
|
|
1109
|
+
tracker.report();
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
### Testing Strategy
|
|
1113
|
+
```javascript
|
|
1114
|
+
// __tests__/deepseek.test.js
|
|
1115
|
+
import { jest } from '@jest/globals';
|
|
1116
|
+
import OpenAI from 'openai';
|
|
1117
|
+
|
|
1118
|
+
// Mock the OpenAI client for testing
|
|
1119
|
+
jest.mock('openai');
|
|
1120
|
+
|
|
1121
|
+
describe('DeepSeek Integration', () => {
|
|
1122
|
+
let client;
|
|
1123
|
+
|
|
1124
|
+
beforeEach(() => {
|
|
1125
|
+
client = new OpenAI({
|
|
1126
|
+
apiKey: 'test-key',
|
|
1127
|
+
baseURL: 'https://api.deepseek.com',
|
|
1128
|
+
});
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
test('should create completion', async () => {
|
|
1132
|
+
const mockResponse = {
|
|
1133
|
+
choices: [{ message: { content: 'Test response' } }],
|
|
1134
|
+
usage: { prompt_tokens: 10, completion_tokens: 5 },
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
client.chat.completions.create.mockResolvedValue(mockResponse);
|
|
1138
|
+
|
|
1139
|
+
const result = await client.chat.completions.create({
|
|
1140
|
+
model: 'deepseek-chat',
|
|
1141
|
+
messages: [{ role: 'user', content: 'Test' }],
|
|
1142
|
+
});
|
|
1143
|
+
|
|
1144
|
+
expect(result.choices[0].message.content).toBe('Test response');
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
test('should handle errors', async () => {
|
|
1148
|
+
const mockError = new Error('API Error');
|
|
1149
|
+
mockError.status = 429;
|
|
1150
|
+
|
|
1151
|
+
client.chat.completions.create.mockRejectedValue(mockError);
|
|
1152
|
+
|
|
1153
|
+
await expect(
|
|
1154
|
+
client.chat.completions.create({
|
|
1155
|
+
model: 'deepseek-chat',
|
|
1156
|
+
messages: [{ role: 'user', content: 'Test' }],
|
|
1157
|
+
})
|
|
1158
|
+
).rejects.toThrow('API Error');
|
|
1159
|
+
});
|
|
1160
|
+
});
|
|
1161
|
+
```
|
|
1162
|
+
|
|
1163
|
+
### Performance Optimization
|
|
1164
|
+
- Use streaming for long responses to improve perceived latency
|
|
1165
|
+
- Implement request batching where appropriate
|
|
1166
|
+
- Cache static system prompts for context caching benefits
|
|
1167
|
+
- Monitor and optimize token usage
|
|
1168
|
+
- Use appropriate max_tokens limits to control costs
|
|
1169
|
+
- Consider using connection pooling for high-volume applications
|
|
1170
|
+
|
|
1171
|
+
### Security Checklist
|
|
1172
|
+
- ✅ Store API keys in environment variables or secure vaults
|
|
1173
|
+
- ✅ Never commit API keys to version control
|
|
1174
|
+
- ✅ Use .gitignore for .env files
|
|
1175
|
+
- ✅ Implement rate limiting on your API endpoints
|
|
1176
|
+
- ✅ Validate and sanitize all user inputs
|
|
1177
|
+
- ✅ Use HTTPS for all API communications
|
|
1178
|
+
- ✅ Implement proper authentication for your application
|
|
1179
|
+
- ✅ Monitor for unusual API usage patterns
|
|
1180
|
+
- ✅ Set up alerts for cost thresholds
|
|
1181
|
+
- ✅ Regularly rotate API keys
|
|
1182
|
+
|
|
1183
|
+
### Deployment Checklist
|
|
1184
|
+
- ✅ Environment variables configured correctly
|
|
1185
|
+
- ✅ Error logging and monitoring in place
|
|
1186
|
+
- ✅ Rate limiting implemented
|
|
1187
|
+
- ✅ Cost tracking configured
|
|
1188
|
+
- ✅ Backup error handling for API failures
|
|
1189
|
+
- ✅ Health check endpoints for API connectivity
|
|
1190
|
+
- ✅ Documentation for API usage and limits
|
|
1191
|
+
- ✅ Incident response plan for API outages
|
|
1192
|
+
|
|
1193
|
+
## Available Models
|
|
1194
|
+
|
|
1195
|
+
To get the current list of available models, pricing, and specifications, use the DeepSeek API's models endpoint:
|
|
1196
|
+
|
|
1197
|
+
### List All Models via CLI
|
|
1198
|
+
|
|
1199
|
+
```bash
|
|
1200
|
+
curl https://api.deepseek.com/models \
|
|
1201
|
+
-H "Authorization: Bearer $DEEPSEEK_API_KEY"
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
### List Models via JavaScript
|
|
1205
|
+
|
|
1206
|
+
```javascript
|
|
1207
|
+
async function listModels() {
|
|
1208
|
+
const response = await fetch('https://api.deepseek.com/models', {
|
|
1209
|
+
headers: {
|
|
1210
|
+
'Authorization': `Bearer ${process.env.DEEPSEEK_API_KEY}`,
|
|
1211
|
+
},
|
|
1212
|
+
});
|
|
1213
|
+
|
|
1214
|
+
const data = await response.json();
|
|
1215
|
+
console.log(JSON.stringify(data, null, 2));
|
|
1216
|
+
return data;
|
|
1217
|
+
}
|
|
1218
|
+
```
|
|
1219
|
+
|
|
1220
|
+
The response will include model IDs, context lengths, pricing, and capabilities. Primary models include `deepseek-chat` and `deepseek-reasoner`.
|