@langchain/deepseek 0.0.1

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.
@@ -0,0 +1,413 @@
1
+ import { getEnvironmentVariable } from "@langchain/core/utils/env";
2
+ import { ChatOpenAI, } from "@langchain/openai";
3
+ /**
4
+ * Deepseek chat model integration.
5
+ *
6
+ * The Deepseek API is compatible to the OpenAI API with some limitations.
7
+ *
8
+ * Setup:
9
+ * Install `@langchain/deepseek` and set an environment variable named `DEEPSEEK_API_KEY`.
10
+ *
11
+ * ```bash
12
+ * npm install @langchain/deepseek
13
+ * export DEEPSEEK_API_KEY="your-api-key"
14
+ * ```
15
+ *
16
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html#constructor)
17
+ *
18
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_deepseek.ChatDeepSeekCallOptions.html)
19
+ *
20
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
21
+ * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
22
+ *
23
+ * ```typescript
24
+ * // When calling `.bind`, call options should be passed via the first argument
25
+ * const llmWithArgsBound = llm.bind({
26
+ * stop: ["\n"],
27
+ * tools: [...],
28
+ * });
29
+ *
30
+ * // When calling `.bindTools`, call options should be passed via the second argument
31
+ * const llmWithTools = llm.bindTools(
32
+ * [...],
33
+ * {
34
+ * tool_choice: "auto",
35
+ * }
36
+ * );
37
+ * ```
38
+ *
39
+ * ## Examples
40
+ *
41
+ * <details open>
42
+ * <summary><strong>Instantiate</strong></summary>
43
+ *
44
+ * ```typescript
45
+ * import { ChatDeepSeek } from '@langchain/deepseek';
46
+ *
47
+ * const llm = new ChatDeepSeek({
48
+ * model: "deepseek-reasoner",
49
+ * temperature: 0,
50
+ * // other params...
51
+ * });
52
+ * ```
53
+ * </details>
54
+ *
55
+ * <br />
56
+ *
57
+ * <details>
58
+ * <summary><strong>Invoking</strong></summary>
59
+ *
60
+ * ```typescript
61
+ * const input = `Translate "I love programming" into French.`;
62
+ *
63
+ * // Models also accept a list of chat messages or a formatted prompt
64
+ * const result = await llm.invoke(input);
65
+ * console.log(result);
66
+ * ```
67
+ *
68
+ * ```txt
69
+ * AIMessage {
70
+ * "content": "The French translation of \"I love programming\" is \"J'aime programmer\". In this sentence, \"J'aime\" is the first person singular conjugation of the French verb \"aimer\" which means \"to love\", and \"programmer\" is the French infinitive for \"to program\". I hope this helps! Let me know if you have any other questions.",
71
+ * "additional_kwargs": {
72
+ * "reasoning_content": "...",
73
+ * },
74
+ * "response_metadata": {
75
+ * "tokenUsage": {
76
+ * "completionTokens": 82,
77
+ * "promptTokens": 20,
78
+ * "totalTokens": 102
79
+ * },
80
+ * "finish_reason": "stop"
81
+ * },
82
+ * "tool_calls": [],
83
+ * "invalid_tool_calls": []
84
+ * }
85
+ * ```
86
+ * </details>
87
+ *
88
+ * <br />
89
+ *
90
+ * <details>
91
+ * <summary><strong>Streaming Chunks</strong></summary>
92
+ *
93
+ * ```typescript
94
+ * for await (const chunk of await llm.stream(input)) {
95
+ * console.log(chunk);
96
+ * }
97
+ * ```
98
+ *
99
+ * ```txt
100
+ * AIMessageChunk {
101
+ * "content": "",
102
+ * "additional_kwargs": {
103
+ * "reasoning_content": "...",
104
+ * },
105
+ * "response_metadata": {
106
+ * "finishReason": null
107
+ * },
108
+ * "tool_calls": [],
109
+ * "tool_call_chunks": [],
110
+ * "invalid_tool_calls": []
111
+ * }
112
+ * AIMessageChunk {
113
+ * "content": "The",
114
+ * "additional_kwargs": {
115
+ * "reasoning_content": "...",
116
+ * },
117
+ * "response_metadata": {
118
+ * "finishReason": null
119
+ * },
120
+ * "tool_calls": [],
121
+ * "tool_call_chunks": [],
122
+ * "invalid_tool_calls": []
123
+ * }
124
+ * AIMessageChunk {
125
+ * "content": " French",
126
+ * "additional_kwargs": {
127
+ * "reasoning_content": "...",
128
+ * },
129
+ * "response_metadata": {
130
+ * "finishReason": null
131
+ * },
132
+ * "tool_calls": [],
133
+ * "tool_call_chunks": [],
134
+ * "invalid_tool_calls": []
135
+ * }
136
+ * AIMessageChunk {
137
+ * "content": " translation",
138
+ * "additional_kwargs": {
139
+ * "reasoning_content": "...",
140
+ * },
141
+ * "response_metadata": {
142
+ * "finishReason": null
143
+ * },
144
+ * "tool_calls": [],
145
+ * "tool_call_chunks": [],
146
+ * "invalid_tool_calls": []
147
+ * }
148
+ * AIMessageChunk {
149
+ * "content": " of",
150
+ * "additional_kwargs": {
151
+ * "reasoning_content": "...",
152
+ * },
153
+ * "response_metadata": {
154
+ * "finishReason": null
155
+ * },
156
+ * "tool_calls": [],
157
+ * "tool_call_chunks": [],
158
+ * "invalid_tool_calls": []
159
+ * }
160
+ * AIMessageChunk {
161
+ * "content": " \"",
162
+ * "additional_kwargs": {
163
+ * "reasoning_content": "...",
164
+ * },
165
+ * "response_metadata": {
166
+ * "finishReason": null
167
+ * },
168
+ * "tool_calls": [],
169
+ * "tool_call_chunks": [],
170
+ * "invalid_tool_calls": []
171
+ * }
172
+ * AIMessageChunk {
173
+ * "content": "I",
174
+ * "additional_kwargs": {
175
+ * "reasoning_content": "...",
176
+ * },
177
+ * "response_metadata": {
178
+ * "finishReason": null
179
+ * },
180
+ * "tool_calls": [],
181
+ * "tool_call_chunks": [],
182
+ * "invalid_tool_calls": []
183
+ * }
184
+ * AIMessageChunk {
185
+ * "content": " love",
186
+ * "additional_kwargs": {
187
+ * "reasoning_content": "...",
188
+ * },
189
+ * "response_metadata": {
190
+ * "finishReason": null
191
+ * },
192
+ * "tool_calls": [],
193
+ * "tool_call_chunks": [],
194
+ * "invalid_tool_calls": []
195
+ * }
196
+ * ...
197
+ * AIMessageChunk {
198
+ * "content": ".",
199
+ * "additional_kwargs": {
200
+ * "reasoning_content": "...",
201
+ * },
202
+ * "response_metadata": {
203
+ * "finishReason": null
204
+ * },
205
+ * "tool_calls": [],
206
+ * "tool_call_chunks": [],
207
+ * "invalid_tool_calls": []
208
+ * }
209
+ * AIMessageChunk {
210
+ * "content": "",
211
+ * "additional_kwargs": {
212
+ * "reasoning_content": "...",
213
+ * },
214
+ * "response_metadata": {
215
+ * "finishReason": "stop"
216
+ * },
217
+ * "tool_calls": [],
218
+ * "tool_call_chunks": [],
219
+ * "invalid_tool_calls": []
220
+ * }
221
+ * ```
222
+ * </details>
223
+ *
224
+ * <br />
225
+ *
226
+ * <details>
227
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
228
+ *
229
+ * ```typescript
230
+ * import { AIMessageChunk } from '@langchain/core/messages';
231
+ * import { concat } from '@langchain/core/utils/stream';
232
+ *
233
+ * const stream = await llm.stream(input);
234
+ * let full: AIMessageChunk | undefined;
235
+ * for await (const chunk of stream) {
236
+ * full = !full ? chunk : concat(full, chunk);
237
+ * }
238
+ * console.log(full);
239
+ * ```
240
+ *
241
+ * ```txt
242
+ * AIMessageChunk {
243
+ * "content": "The French translation of \"I love programming\" is \"J'aime programmer\". In this sentence, \"J'aime\" is the first person singular conjugation of the French verb \"aimer\" which means \"to love\", and \"programmer\" is the French infinitive for \"to program\". I hope this helps! Let me know if you have any other questions.",
244
+ * "additional_kwargs": {
245
+ * "reasoning_content": "...",
246
+ * },
247
+ * "response_metadata": {
248
+ * "finishReason": "stop"
249
+ * },
250
+ * "tool_calls": [],
251
+ * "tool_call_chunks": [],
252
+ * "invalid_tool_calls": []
253
+ * }
254
+ * ```
255
+ * </details>
256
+ *
257
+ * <br />
258
+ *
259
+ * <details>
260
+ * <summary><strong>Bind tools</strong></summary>
261
+ *
262
+ * ```typescript
263
+ * import { z } from 'zod';
264
+ *
265
+ * const llmForToolCalling = new ChatDeepSeek({
266
+ * model: "deepseek-chat",
267
+ * temperature: 0,
268
+ * // other params...
269
+ * });
270
+ *
271
+ * const GetWeather = {
272
+ * name: "GetWeather",
273
+ * description: "Get the current weather in a given location",
274
+ * schema: z.object({
275
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
276
+ * }),
277
+ * }
278
+ *
279
+ * const GetPopulation = {
280
+ * name: "GetPopulation",
281
+ * description: "Get the current population in a given location",
282
+ * schema: z.object({
283
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
284
+ * }),
285
+ * }
286
+ *
287
+ * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);
288
+ * const aiMsg = await llmWithTools.invoke(
289
+ * "Which city is hotter today and which is bigger: LA or NY?"
290
+ * );
291
+ * console.log(aiMsg.tool_calls);
292
+ * ```
293
+ *
294
+ * ```txt
295
+ * [
296
+ * {
297
+ * name: 'GetWeather',
298
+ * args: { location: 'Los Angeles, CA' },
299
+ * type: 'tool_call',
300
+ * id: 'call_cd34'
301
+ * },
302
+ * {
303
+ * name: 'GetWeather',
304
+ * args: { location: 'New York, NY' },
305
+ * type: 'tool_call',
306
+ * id: 'call_68rf'
307
+ * },
308
+ * {
309
+ * name: 'GetPopulation',
310
+ * args: { location: 'Los Angeles, CA' },
311
+ * type: 'tool_call',
312
+ * id: 'call_f81z'
313
+ * },
314
+ * {
315
+ * name: 'GetPopulation',
316
+ * args: { location: 'New York, NY' },
317
+ * type: 'tool_call',
318
+ * id: 'call_8byt'
319
+ * }
320
+ * ]
321
+ * ```
322
+ * </details>
323
+ *
324
+ * <br />
325
+ *
326
+ * <details>
327
+ * <summary><strong>Structured Output</strong></summary>
328
+ *
329
+ * ```typescript
330
+ * import { z } from 'zod';
331
+ *
332
+ * const Joke = z.object({
333
+ * setup: z.string().describe("The setup of the joke"),
334
+ * punchline: z.string().describe("The punchline to the joke"),
335
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
336
+ * }).describe('Joke to tell user.');
337
+ *
338
+ * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: "Joke" });
339
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
340
+ * console.log(jokeResult);
341
+ * ```
342
+ *
343
+ * ```txt
344
+ * {
345
+ * setup: "Why don't cats play poker in the wild?",
346
+ * punchline: 'Because there are too many cheetahs.'
347
+ * }
348
+ * ```
349
+ * </details>
350
+ *
351
+ * <br />
352
+ */
353
+ export class ChatDeepSeek extends ChatOpenAI {
354
+ static lc_name() {
355
+ return "ChatDeepSeek";
356
+ }
357
+ _llmType() {
358
+ return "deepseek";
359
+ }
360
+ get lc_secrets() {
361
+ return {
362
+ apiKey: "DEEPSEEK_API_KEY",
363
+ };
364
+ }
365
+ constructor(fields) {
366
+ const apiKey = fields?.apiKey || getEnvironmentVariable("DEEPSEEK_API_KEY");
367
+ if (!apiKey) {
368
+ throw new Error(`Deepseek API key not found. Please set the DEEPSEEK_API_KEY environment variable or pass the key into "apiKey" field.`);
369
+ }
370
+ super({
371
+ ...fields,
372
+ apiKey,
373
+ configuration: {
374
+ baseURL: "https://api.deepseek.com",
375
+ ...fields?.configuration,
376
+ },
377
+ });
378
+ Object.defineProperty(this, "lc_serializable", {
379
+ enumerable: true,
380
+ configurable: true,
381
+ writable: true,
382
+ value: true
383
+ });
384
+ Object.defineProperty(this, "lc_namespace", {
385
+ enumerable: true,
386
+ configurable: true,
387
+ writable: true,
388
+ value: ["langchain", "chat_models", "deepseek"]
389
+ });
390
+ }
391
+ _convertOpenAIDeltaToBaseMessageChunk(
392
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
393
+ delta, rawResponse, defaultRole) {
394
+ const messageChunk = super._convertOpenAIDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
395
+ messageChunk.additional_kwargs.reasoning_content = delta.reasoning_content;
396
+ return messageChunk;
397
+ }
398
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
399
+ const langChainMessage = super._convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse);
400
+ langChainMessage.additional_kwargs.reasoning_content =
401
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
402
+ message.reasoning_content;
403
+ return langChainMessage;
404
+ }
405
+ withStructuredOutput(outputSchema, config) {
406
+ const ensuredConfig = { ...config };
407
+ // Deepseek does not support json schema yet
408
+ if (ensuredConfig?.method === undefined) {
409
+ ensuredConfig.method = "functionCalling";
410
+ }
411
+ return super.withStructuredOutput(outputSchema, ensuredConfig);
412
+ }
413
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./chat_models.cjs"), exports);
@@ -0,0 +1 @@
1
+ export * from "./chat_models.js";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./chat_models.js";
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/index.cjs');
package/index.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/index.js'
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/index.js'
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/index.js'
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@langchain/deepseek",
3
+ "version": "0.0.1",
4
+ "description": "Deepseek integration for LangChain.js",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=18"
8
+ },
9
+ "main": "./index.js",
10
+ "types": "./index.d.ts",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git@github.com:langchain-ai/langchainjs.git"
14
+ },
15
+ "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/@langchain/deepseek",
16
+ "scripts": {
17
+ "build": "yarn turbo:command build:internal --filter=@langchain/deepseek",
18
+ "build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
19
+ "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
20
+ "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
21
+ "lint": "yarn lint:eslint && yarn lint:dpdm",
22
+ "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
23
+ "clean": "rm -rf .turbo dist/",
24
+ "prepack": "yarn build",
25
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
26
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
27
+ "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
28
+ "test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
29
+ "format": "prettier --config .prettierrc --write \"src\"",
30
+ "format:check": "prettier --config .prettierrc --check \"src\""
31
+ },
32
+ "author": "LangChain",
33
+ "license": "MIT",
34
+ "dependencies": {
35
+ "@langchain/openai": "^0.4.2",
36
+ "zod": "^3.24.1"
37
+ },
38
+ "peerDependencies": {
39
+ "@langchain/core": ">=0.3.0 <0.4.0"
40
+ },
41
+ "devDependencies": {
42
+ "@jest/globals": "^29.5.0",
43
+ "@langchain/core": "workspace:*",
44
+ "@langchain/scripts": ">=0.1.0 <0.2.0",
45
+ "@langchain/standard-tests": "workspace:*",
46
+ "@swc/core": "^1.3.90",
47
+ "@swc/jest": "^0.2.29",
48
+ "@tsconfig/recommended": "^1.0.3",
49
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
50
+ "@typescript-eslint/parser": "^6.12.0",
51
+ "dotenv": "^16.3.1",
52
+ "dpdm": "^3.12.0",
53
+ "eslint": "^8.33.0",
54
+ "eslint-config-airbnb-base": "^15.0.0",
55
+ "eslint-config-prettier": "^8.6.0",
56
+ "eslint-plugin-import": "^2.27.5",
57
+ "eslint-plugin-no-instanceof": "^1.0.1",
58
+ "eslint-plugin-prettier": "^4.2.1",
59
+ "jest": "^29.5.0",
60
+ "jest-environment-node": "^29.6.4",
61
+ "prettier": "^2.8.3",
62
+ "release-it": "^15.10.1",
63
+ "rollup": "^4.5.2",
64
+ "ts-jest": "^29.1.0",
65
+ "typescript": "<5.2.0"
66
+ },
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "exports": {
71
+ ".": {
72
+ "types": {
73
+ "import": "./index.d.ts",
74
+ "require": "./index.d.cts",
75
+ "default": "./index.d.ts"
76
+ },
77
+ "import": "./index.js",
78
+ "require": "./index.cjs"
79
+ },
80
+ "./package.json": "./package.json"
81
+ },
82
+ "files": [
83
+ "dist/",
84
+ "index.cjs",
85
+ "index.js",
86
+ "index.d.ts",
87
+ "index.d.cts"
88
+ ]
89
+ }