@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.
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/chat_models.cjs +417 -0
- package/dist/chat_models.d.ts +414 -0
- package/dist/chat_models.js +413 -0
- package/dist/index.cjs +17 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/index.cjs +1 -0
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { BaseLanguageModelInput } from "@langchain/core/language_models/base";
|
|
2
|
+
import { BaseMessage } from "@langchain/core/messages";
|
|
3
|
+
import { Runnable } from "@langchain/core/runnables";
|
|
4
|
+
import { ChatOpenAI, ChatOpenAICallOptions, ChatOpenAIFields, ChatOpenAIStructuredOutputMethodOptions, OpenAIClient } from "@langchain/openai";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
export interface ChatDeepSeekCallOptions extends ChatOpenAICallOptions {
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface ChatDeepSeekInput extends ChatOpenAIFields {
|
|
10
|
+
/**
|
|
11
|
+
* The Deepseek API key to use for requests.
|
|
12
|
+
* @default process.env.DEEPSEEK_API_KEY
|
|
13
|
+
*/
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* The name of the model to use.
|
|
17
|
+
*/
|
|
18
|
+
model?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Up to 4 sequences where the API will stop generating further tokens. The
|
|
21
|
+
* returned text will not contain the stop sequence.
|
|
22
|
+
* Alias for `stopSequences`
|
|
23
|
+
*/
|
|
24
|
+
stop?: Array<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Up to 4 sequences where the API will stop generating further tokens. The
|
|
27
|
+
* returned text will not contain the stop sequence.
|
|
28
|
+
*/
|
|
29
|
+
stopSequences?: Array<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Whether or not to stream responses.
|
|
32
|
+
*/
|
|
33
|
+
streaming?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The temperature to use for sampling.
|
|
36
|
+
*/
|
|
37
|
+
temperature?: number;
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of tokens that the model can process in a single response.
|
|
40
|
+
* This limits ensures computational efficiency and resource management.
|
|
41
|
+
*/
|
|
42
|
+
maxTokens?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Deepseek chat model integration.
|
|
46
|
+
*
|
|
47
|
+
* The Deepseek API is compatible to the OpenAI API with some limitations.
|
|
48
|
+
*
|
|
49
|
+
* Setup:
|
|
50
|
+
* Install `@langchain/deepseek` and set an environment variable named `DEEPSEEK_API_KEY`.
|
|
51
|
+
*
|
|
52
|
+
* ```bash
|
|
53
|
+
* npm install @langchain/deepseek
|
|
54
|
+
* export DEEPSEEK_API_KEY="your-api-key"
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html#constructor)
|
|
58
|
+
*
|
|
59
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_deepseek.ChatDeepSeekCallOptions.html)
|
|
60
|
+
*
|
|
61
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
62
|
+
* They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
63
|
+
*
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
66
|
+
* const llmWithArgsBound = llm.bind({
|
|
67
|
+
* stop: ["\n"],
|
|
68
|
+
* tools: [...],
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
72
|
+
* const llmWithTools = llm.bindTools(
|
|
73
|
+
* [...],
|
|
74
|
+
* {
|
|
75
|
+
* tool_choice: "auto",
|
|
76
|
+
* }
|
|
77
|
+
* );
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* ## Examples
|
|
81
|
+
*
|
|
82
|
+
* <details open>
|
|
83
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
84
|
+
*
|
|
85
|
+
* ```typescript
|
|
86
|
+
* import { ChatDeepSeek } from '@langchain/deepseek';
|
|
87
|
+
*
|
|
88
|
+
* const llm = new ChatDeepSeek({
|
|
89
|
+
* model: "deepseek-reasoner",
|
|
90
|
+
* temperature: 0,
|
|
91
|
+
* // other params...
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
94
|
+
* </details>
|
|
95
|
+
*
|
|
96
|
+
* <br />
|
|
97
|
+
*
|
|
98
|
+
* <details>
|
|
99
|
+
* <summary><strong>Invoking</strong></summary>
|
|
100
|
+
*
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const input = `Translate "I love programming" into French.`;
|
|
103
|
+
*
|
|
104
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
105
|
+
* const result = await llm.invoke(input);
|
|
106
|
+
* console.log(result);
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* ```txt
|
|
110
|
+
* AIMessage {
|
|
111
|
+
* "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.",
|
|
112
|
+
* "additional_kwargs": {
|
|
113
|
+
* "reasoning_content": "...",
|
|
114
|
+
* },
|
|
115
|
+
* "response_metadata": {
|
|
116
|
+
* "tokenUsage": {
|
|
117
|
+
* "completionTokens": 82,
|
|
118
|
+
* "promptTokens": 20,
|
|
119
|
+
* "totalTokens": 102
|
|
120
|
+
* },
|
|
121
|
+
* "finish_reason": "stop"
|
|
122
|
+
* },
|
|
123
|
+
* "tool_calls": [],
|
|
124
|
+
* "invalid_tool_calls": []
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
* </details>
|
|
128
|
+
*
|
|
129
|
+
* <br />
|
|
130
|
+
*
|
|
131
|
+
* <details>
|
|
132
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
133
|
+
*
|
|
134
|
+
* ```typescript
|
|
135
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
136
|
+
* console.log(chunk);
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* ```txt
|
|
141
|
+
* AIMessageChunk {
|
|
142
|
+
* "content": "",
|
|
143
|
+
* "additional_kwargs": {
|
|
144
|
+
* "reasoning_content": "...",
|
|
145
|
+
* },
|
|
146
|
+
* "response_metadata": {
|
|
147
|
+
* "finishReason": null
|
|
148
|
+
* },
|
|
149
|
+
* "tool_calls": [],
|
|
150
|
+
* "tool_call_chunks": [],
|
|
151
|
+
* "invalid_tool_calls": []
|
|
152
|
+
* }
|
|
153
|
+
* AIMessageChunk {
|
|
154
|
+
* "content": "The",
|
|
155
|
+
* "additional_kwargs": {
|
|
156
|
+
* "reasoning_content": "...",
|
|
157
|
+
* },
|
|
158
|
+
* "response_metadata": {
|
|
159
|
+
* "finishReason": null
|
|
160
|
+
* },
|
|
161
|
+
* "tool_calls": [],
|
|
162
|
+
* "tool_call_chunks": [],
|
|
163
|
+
* "invalid_tool_calls": []
|
|
164
|
+
* }
|
|
165
|
+
* AIMessageChunk {
|
|
166
|
+
* "content": " French",
|
|
167
|
+
* "additional_kwargs": {
|
|
168
|
+
* "reasoning_content": "...",
|
|
169
|
+
* },
|
|
170
|
+
* "response_metadata": {
|
|
171
|
+
* "finishReason": null
|
|
172
|
+
* },
|
|
173
|
+
* "tool_calls": [],
|
|
174
|
+
* "tool_call_chunks": [],
|
|
175
|
+
* "invalid_tool_calls": []
|
|
176
|
+
* }
|
|
177
|
+
* AIMessageChunk {
|
|
178
|
+
* "content": " translation",
|
|
179
|
+
* "additional_kwargs": {
|
|
180
|
+
* "reasoning_content": "...",
|
|
181
|
+
* },
|
|
182
|
+
* "response_metadata": {
|
|
183
|
+
* "finishReason": null
|
|
184
|
+
* },
|
|
185
|
+
* "tool_calls": [],
|
|
186
|
+
* "tool_call_chunks": [],
|
|
187
|
+
* "invalid_tool_calls": []
|
|
188
|
+
* }
|
|
189
|
+
* AIMessageChunk {
|
|
190
|
+
* "content": " of",
|
|
191
|
+
* "additional_kwargs": {
|
|
192
|
+
* "reasoning_content": "...",
|
|
193
|
+
* },
|
|
194
|
+
* "response_metadata": {
|
|
195
|
+
* "finishReason": null
|
|
196
|
+
* },
|
|
197
|
+
* "tool_calls": [],
|
|
198
|
+
* "tool_call_chunks": [],
|
|
199
|
+
* "invalid_tool_calls": []
|
|
200
|
+
* }
|
|
201
|
+
* AIMessageChunk {
|
|
202
|
+
* "content": " \"",
|
|
203
|
+
* "additional_kwargs": {
|
|
204
|
+
* "reasoning_content": "...",
|
|
205
|
+
* },
|
|
206
|
+
* "response_metadata": {
|
|
207
|
+
* "finishReason": null
|
|
208
|
+
* },
|
|
209
|
+
* "tool_calls": [],
|
|
210
|
+
* "tool_call_chunks": [],
|
|
211
|
+
* "invalid_tool_calls": []
|
|
212
|
+
* }
|
|
213
|
+
* AIMessageChunk {
|
|
214
|
+
* "content": "I",
|
|
215
|
+
* "additional_kwargs": {
|
|
216
|
+
* "reasoning_content": "...",
|
|
217
|
+
* },
|
|
218
|
+
* "response_metadata": {
|
|
219
|
+
* "finishReason": null
|
|
220
|
+
* },
|
|
221
|
+
* "tool_calls": [],
|
|
222
|
+
* "tool_call_chunks": [],
|
|
223
|
+
* "invalid_tool_calls": []
|
|
224
|
+
* }
|
|
225
|
+
* AIMessageChunk {
|
|
226
|
+
* "content": " love",
|
|
227
|
+
* "additional_kwargs": {
|
|
228
|
+
* "reasoning_content": "...",
|
|
229
|
+
* },
|
|
230
|
+
* "response_metadata": {
|
|
231
|
+
* "finishReason": null
|
|
232
|
+
* },
|
|
233
|
+
* "tool_calls": [],
|
|
234
|
+
* "tool_call_chunks": [],
|
|
235
|
+
* "invalid_tool_calls": []
|
|
236
|
+
* }
|
|
237
|
+
* ...
|
|
238
|
+
* AIMessageChunk {
|
|
239
|
+
* "content": ".",
|
|
240
|
+
* "additional_kwargs": {
|
|
241
|
+
* "reasoning_content": "...",
|
|
242
|
+
* },
|
|
243
|
+
* "response_metadata": {
|
|
244
|
+
* "finishReason": null
|
|
245
|
+
* },
|
|
246
|
+
* "tool_calls": [],
|
|
247
|
+
* "tool_call_chunks": [],
|
|
248
|
+
* "invalid_tool_calls": []
|
|
249
|
+
* }
|
|
250
|
+
* AIMessageChunk {
|
|
251
|
+
* "content": "",
|
|
252
|
+
* "additional_kwargs": {
|
|
253
|
+
* "reasoning_content": "...",
|
|
254
|
+
* },
|
|
255
|
+
* "response_metadata": {
|
|
256
|
+
* "finishReason": "stop"
|
|
257
|
+
* },
|
|
258
|
+
* "tool_calls": [],
|
|
259
|
+
* "tool_call_chunks": [],
|
|
260
|
+
* "invalid_tool_calls": []
|
|
261
|
+
* }
|
|
262
|
+
* ```
|
|
263
|
+
* </details>
|
|
264
|
+
*
|
|
265
|
+
* <br />
|
|
266
|
+
*
|
|
267
|
+
* <details>
|
|
268
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
269
|
+
*
|
|
270
|
+
* ```typescript
|
|
271
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
272
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
273
|
+
*
|
|
274
|
+
* const stream = await llm.stream(input);
|
|
275
|
+
* let full: AIMessageChunk | undefined;
|
|
276
|
+
* for await (const chunk of stream) {
|
|
277
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
278
|
+
* }
|
|
279
|
+
* console.log(full);
|
|
280
|
+
* ```
|
|
281
|
+
*
|
|
282
|
+
* ```txt
|
|
283
|
+
* AIMessageChunk {
|
|
284
|
+
* "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.",
|
|
285
|
+
* "additional_kwargs": {
|
|
286
|
+
* "reasoning_content": "...",
|
|
287
|
+
* },
|
|
288
|
+
* "response_metadata": {
|
|
289
|
+
* "finishReason": "stop"
|
|
290
|
+
* },
|
|
291
|
+
* "tool_calls": [],
|
|
292
|
+
* "tool_call_chunks": [],
|
|
293
|
+
* "invalid_tool_calls": []
|
|
294
|
+
* }
|
|
295
|
+
* ```
|
|
296
|
+
* </details>
|
|
297
|
+
*
|
|
298
|
+
* <br />
|
|
299
|
+
*
|
|
300
|
+
* <details>
|
|
301
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
302
|
+
*
|
|
303
|
+
* ```typescript
|
|
304
|
+
* import { z } from 'zod';
|
|
305
|
+
*
|
|
306
|
+
* const llmForToolCalling = new ChatDeepSeek({
|
|
307
|
+
* model: "deepseek-chat",
|
|
308
|
+
* temperature: 0,
|
|
309
|
+
* // other params...
|
|
310
|
+
* });
|
|
311
|
+
*
|
|
312
|
+
* const GetWeather = {
|
|
313
|
+
* name: "GetWeather",
|
|
314
|
+
* description: "Get the current weather in a given location",
|
|
315
|
+
* schema: z.object({
|
|
316
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
317
|
+
* }),
|
|
318
|
+
* }
|
|
319
|
+
*
|
|
320
|
+
* const GetPopulation = {
|
|
321
|
+
* name: "GetPopulation",
|
|
322
|
+
* description: "Get the current population in a given location",
|
|
323
|
+
* schema: z.object({
|
|
324
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
325
|
+
* }),
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);
|
|
329
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
330
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
331
|
+
* );
|
|
332
|
+
* console.log(aiMsg.tool_calls);
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* ```txt
|
|
336
|
+
* [
|
|
337
|
+
* {
|
|
338
|
+
* name: 'GetWeather',
|
|
339
|
+
* args: { location: 'Los Angeles, CA' },
|
|
340
|
+
* type: 'tool_call',
|
|
341
|
+
* id: 'call_cd34'
|
|
342
|
+
* },
|
|
343
|
+
* {
|
|
344
|
+
* name: 'GetWeather',
|
|
345
|
+
* args: { location: 'New York, NY' },
|
|
346
|
+
* type: 'tool_call',
|
|
347
|
+
* id: 'call_68rf'
|
|
348
|
+
* },
|
|
349
|
+
* {
|
|
350
|
+
* name: 'GetPopulation',
|
|
351
|
+
* args: { location: 'Los Angeles, CA' },
|
|
352
|
+
* type: 'tool_call',
|
|
353
|
+
* id: 'call_f81z'
|
|
354
|
+
* },
|
|
355
|
+
* {
|
|
356
|
+
* name: 'GetPopulation',
|
|
357
|
+
* args: { location: 'New York, NY' },
|
|
358
|
+
* type: 'tool_call',
|
|
359
|
+
* id: 'call_8byt'
|
|
360
|
+
* }
|
|
361
|
+
* ]
|
|
362
|
+
* ```
|
|
363
|
+
* </details>
|
|
364
|
+
*
|
|
365
|
+
* <br />
|
|
366
|
+
*
|
|
367
|
+
* <details>
|
|
368
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
369
|
+
*
|
|
370
|
+
* ```typescript
|
|
371
|
+
* import { z } from 'zod';
|
|
372
|
+
*
|
|
373
|
+
* const Joke = z.object({
|
|
374
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
375
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
376
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
377
|
+
* }).describe('Joke to tell user.');
|
|
378
|
+
*
|
|
379
|
+
* const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: "Joke" });
|
|
380
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
381
|
+
* console.log(jokeResult);
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* ```txt
|
|
385
|
+
* {
|
|
386
|
+
* setup: "Why don't cats play poker in the wild?",
|
|
387
|
+
* punchline: 'Because there are too many cheetahs.'
|
|
388
|
+
* }
|
|
389
|
+
* ```
|
|
390
|
+
* </details>
|
|
391
|
+
*
|
|
392
|
+
* <br />
|
|
393
|
+
*/
|
|
394
|
+
export declare class ChatDeepSeek extends ChatOpenAI<ChatDeepSeekCallOptions> {
|
|
395
|
+
static lc_name(): string;
|
|
396
|
+
_llmType(): string;
|
|
397
|
+
get lc_secrets(): {
|
|
398
|
+
[key: string]: string;
|
|
399
|
+
} | undefined;
|
|
400
|
+
lc_serializable: boolean;
|
|
401
|
+
lc_namespace: string[];
|
|
402
|
+
constructor(fields?: Partial<ChatDeepSeekInput>);
|
|
403
|
+
protected _convertOpenAIDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.ChatCompletionChunk, defaultRole?: "function" | "user" | "system" | "developer" | "assistant" | "tool"): import("@langchain/core/messages").AIMessageChunk | import("@langchain/core/messages").HumanMessageChunk | import("@langchain/core/messages").SystemMessageChunk | import("@langchain/core/messages").FunctionMessageChunk | import("@langchain/core/messages").ToolMessageChunk | import("@langchain/core/messages").ChatMessageChunk;
|
|
404
|
+
protected _convertOpenAIChatCompletionMessageToBaseMessage(message: OpenAIClient.ChatCompletionMessage, rawResponse: OpenAIClient.ChatCompletion): BaseMessage;
|
|
405
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
406
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
407
|
+
raw: BaseMessage;
|
|
408
|
+
parsed: RunOutput;
|
|
409
|
+
}>;
|
|
410
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
411
|
+
raw: BaseMessage;
|
|
412
|
+
parsed: RunOutput;
|
|
413
|
+
}>;
|
|
414
|
+
}
|