|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "afaf8039", |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "raw" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "---\n", |
| 13 | + "sidebar_label: DeepSeek\n", |
| 14 | + "---" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "e49f1e0d", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "# ChatDeepSeek\n", |
| 23 | + "\n", |
| 24 | + "This will help you getting started with DeepSeek [chat models](/docs/concepts/#chat-models). For detailed documentation of all `ChatDeepSeek` features and configurations head to the [API reference](https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html).\n", |
| 25 | + "\n", |
| 26 | + "## Overview\n", |
| 27 | + "### Integration details\n", |
| 28 | + "\n", |
| 29 | + "| Class | Package | Local | Serializable | [PY support](https://python.langchain.com/docs/integrations/chat/deepseek) | Package downloads | Package latest |\n", |
| 30 | + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", |
| 31 | + "| [`ChatDeepSeek`](https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html) | [`@langchain/deepseek`](https://npmjs.com/@langchain/deepseek) | ❌ (see [Ollama](/docs/integrations/chat/ollama)) | beta | ✅ |  |  |\n", |
| 32 | + "\n", |
| 33 | + "### Model features\n", |
| 34 | + "\n", |
| 35 | + "See the links in the table headers below for guides on how to use specific features.\n", |
| 36 | + "\n", |
| 37 | + "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", |
| 38 | + "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", |
| 39 | + "| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | \n", |
| 40 | + "\n", |
| 41 | + "Note that as of 1/27/25, tool calling and structured output are not currently supported for `deepseek-reasoner`.\n", |
| 42 | + "\n", |
| 43 | + "## Setup\n", |
| 44 | + "\n", |
| 45 | + "To access DeepSeek models you'll need to create a DeepSeek account, get an API key, and install the `@langchain/deepseek` integration package.\n", |
| 46 | + "\n", |
| 47 | + "You can also access the DeepSeek API through providers like [Together AI](/docs/integrations/chat/togetherai) or [Ollama](/docs/integrations/chat/ollama).\n", |
| 48 | + "\n", |
| 49 | + "### Credentials\n", |
| 50 | + "\n", |
| 51 | + "Head to https://deepseek.com/ to sign up to DeepSeek and generate an API key. Once you've done this set the `DEEPSEEK_API_KEY` environment variable:\n", |
| 52 | + "\n", |
| 53 | + "```bash\n", |
| 54 | + "export DEEPSEEK_API_KEY=\"your-api-key\"\n", |
| 55 | + "```\n", |
| 56 | + "\n", |
| 57 | + "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:\n", |
| 58 | + "\n", |
| 59 | + "```bash\n", |
| 60 | + "# export LANGSMITH_TRACING=\"true\"\n", |
| 61 | + "# export LANGSMITH_API_KEY=\"your-api-key\"\n", |
| 62 | + "```\n", |
| 63 | + "\n", |
| 64 | + "### Installation\n", |
| 65 | + "\n", |
| 66 | + "The LangChain ChatDeepSeek integration lives in the `@langchain/deepseek` package:\n", |
| 67 | + "\n", |
| 68 | + "```{=mdx}\n", |
| 69 | + "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n", |
| 70 | + "import Npm2Yarn from \"@theme/Npm2Yarn\";\n", |
| 71 | + "\n", |
| 72 | + "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n", |
| 73 | + "\n", |
| 74 | + "<Npm2Yarn>\n", |
| 75 | + " @langchain/deepseek @langchain/core\n", |
| 76 | + "</Npm2Yarn>\n", |
| 77 | + "\n", |
| 78 | + "```" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "id": "a38cde65-254d-4219-a441-068766c0d4b5", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "## Instantiation\n", |
| 87 | + "\n", |
| 88 | + "Now we can instantiate our model object and generate chat completions:" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 1, |
| 94 | + "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "import { ChatDeepSeek } from \"@langchain/deepseek\";\n", |
| 99 | + "\n", |
| 100 | + "const llm = new ChatDeepSeek({\n", |
| 101 | + " model: \"deepseek-reasoner\",\n", |
| 102 | + " temperature: 0,\n", |
| 103 | + " // other params...\n", |
| 104 | + "})" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "id": "2b4f3e15", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "<!-- ## Invocation -->" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 2, |
| 118 | + "id": "62e0dbc3", |
| 119 | + "metadata": { |
| 120 | + "tags": [] |
| 121 | + }, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "name": "stdout", |
| 125 | + "output_type": "stream", |
| 126 | + "text": [ |
| 127 | + "AIMessage {\n", |
| 128 | + " \"id\": \"e2874482-68a7-4552-8154-b6a245bab429\",\n", |
| 129 | + " \"content\": \"J'adore la programmation.\",\n", |
| 130 | + " \"additional_kwargs\": {,\n", |
| 131 | + " \"reasoning_content\": \"...\",\n", |
| 132 | + " },\n", |
| 133 | + " \"response_metadata\": {\n", |
| 134 | + " \"tokenUsage\": {\n", |
| 135 | + " \"promptTokens\": 23,\n", |
| 136 | + " \"completionTokens\": 7,\n", |
| 137 | + " \"totalTokens\": 30\n", |
| 138 | + " },\n", |
| 139 | + " \"finish_reason\": \"stop\",\n", |
| 140 | + " \"model_name\": \"deepseek-reasoner\",\n", |
| 141 | + " \"usage\": {\n", |
| 142 | + " \"prompt_tokens\": 23,\n", |
| 143 | + " \"completion_tokens\": 7,\n", |
| 144 | + " \"total_tokens\": 30,\n", |
| 145 | + " \"prompt_tokens_details\": {\n", |
| 146 | + " \"cached_tokens\": 0\n", |
| 147 | + " },\n", |
| 148 | + " \"prompt_cache_hit_tokens\": 0,\n", |
| 149 | + " \"prompt_cache_miss_tokens\": 23\n", |
| 150 | + " },\n", |
| 151 | + " \"system_fingerprint\": \"fp_3a5770e1b4\"\n", |
| 152 | + " },\n", |
| 153 | + " \"tool_calls\": [],\n", |
| 154 | + " \"invalid_tool_calls\": [],\n", |
| 155 | + " \"usage_metadata\": {\n", |
| 156 | + " \"output_tokens\": 7,\n", |
| 157 | + " \"input_tokens\": 23,\n", |
| 158 | + " \"total_tokens\": 30,\n", |
| 159 | + " \"input_token_details\": {\n", |
| 160 | + " \"cache_read\": 0\n", |
| 161 | + " },\n", |
| 162 | + " \"output_token_details\": {}\n", |
| 163 | + " }\n", |
| 164 | + "}\n" |
| 165 | + ] |
| 166 | + } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "const aiMsg = await llm.invoke([\n", |
| 170 | + " [\n", |
| 171 | + " \"system\",\n", |
| 172 | + " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", |
| 173 | + " ],\n", |
| 174 | + " [\"human\", \"I love programming.\"],\n", |
| 175 | + "])\n", |
| 176 | + "aiMsg" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": 3, |
| 182 | + "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "J'adore la programmation.\n" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "source": [ |
| 194 | + "console.log(aiMsg.content)" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "markdown", |
| 199 | + "id": "18e2bfc0-7e78-4528-a73f-499ac150dca8", |
| 200 | + "metadata": {}, |
| 201 | + "source": [ |
| 202 | + "## Chaining\n", |
| 203 | + "\n", |
| 204 | + "We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": 4, |
| 210 | + "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [ |
| 213 | + { |
| 214 | + "name": "stdout", |
| 215 | + "output_type": "stream", |
| 216 | + "text": [ |
| 217 | + "AIMessage {\n", |
| 218 | + " \"id\": \"6e7f6f8c-8d7a-4dad-be07-425384038fd4\",\n", |
| 219 | + " \"content\": \"Ich liebe es zu programmieren.\",\n", |
| 220 | + " \"additional_kwargs\": {,\n", |
| 221 | + " \"reasoning_content\": \"...\",\n", |
| 222 | + " },\n", |
| 223 | + " \"response_metadata\": {\n", |
| 224 | + " \"tokenUsage\": {\n", |
| 225 | + " \"promptTokens\": 18,\n", |
| 226 | + " \"completionTokens\": 9,\n", |
| 227 | + " \"totalTokens\": 27\n", |
| 228 | + " },\n", |
| 229 | + " \"finish_reason\": \"stop\",\n", |
| 230 | + " \"model_name\": \"deepseek-reasoner\",\n", |
| 231 | + " \"usage\": {\n", |
| 232 | + " \"prompt_tokens\": 18,\n", |
| 233 | + " \"completion_tokens\": 9,\n", |
| 234 | + " \"total_tokens\": 27,\n", |
| 235 | + " \"prompt_tokens_details\": {\n", |
| 236 | + " \"cached_tokens\": 0\n", |
| 237 | + " },\n", |
| 238 | + " \"prompt_cache_hit_tokens\": 0,\n", |
| 239 | + " \"prompt_cache_miss_tokens\": 18\n", |
| 240 | + " },\n", |
| 241 | + " \"system_fingerprint\": \"fp_3a5770e1b4\"\n", |
| 242 | + " },\n", |
| 243 | + " \"tool_calls\": [],\n", |
| 244 | + " \"invalid_tool_calls\": [],\n", |
| 245 | + " \"usage_metadata\": {\n", |
| 246 | + " \"output_tokens\": 9,\n", |
| 247 | + " \"input_tokens\": 18,\n", |
| 248 | + " \"total_tokens\": 27,\n", |
| 249 | + " \"input_token_details\": {\n", |
| 250 | + " \"cache_read\": 0\n", |
| 251 | + " },\n", |
| 252 | + " \"output_token_details\": {}\n", |
| 253 | + " }\n", |
| 254 | + "}\n" |
| 255 | + ] |
| 256 | + } |
| 257 | + ], |
| 258 | + "source": [ |
| 259 | + "import { ChatPromptTemplate } from \"@langchain/core/prompts\"\n", |
| 260 | + "\n", |
| 261 | + "const prompt = ChatPromptTemplate.fromMessages(\n", |
| 262 | + " [\n", |
| 263 | + " [\n", |
| 264 | + " \"system\",\n", |
| 265 | + " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", |
| 266 | + " ],\n", |
| 267 | + " [\"human\", \"{input}\"],\n", |
| 268 | + " ]\n", |
| 269 | + ")\n", |
| 270 | + "\n", |
| 271 | + "const chain = prompt.pipe(llm);\n", |
| 272 | + "await chain.invoke(\n", |
| 273 | + " {\n", |
| 274 | + " input_language: \"English\",\n", |
| 275 | + " output_language: \"German\",\n", |
| 276 | + " input: \"I love programming.\",\n", |
| 277 | + " }\n", |
| 278 | + ")" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "markdown", |
| 283 | + "id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3", |
| 284 | + "metadata": {}, |
| 285 | + "source": [ |
| 286 | + "## API reference\n", |
| 287 | + "\n", |
| 288 | + "For detailed documentation of all ChatDeepSeek features and configurations head to the API reference: https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html" |
| 289 | + ] |
| 290 | + } |
| 291 | + ], |
| 292 | + "metadata": { |
| 293 | + "kernelspec": { |
| 294 | + "display_name": "TypeScript", |
| 295 | + "language": "typescript", |
| 296 | + "name": "tslab" |
| 297 | + }, |
| 298 | + "language_info": { |
| 299 | + "codemirror_mode": { |
| 300 | + "mode": "typescript", |
| 301 | + "name": "javascript", |
| 302 | + "typescript": true |
| 303 | + }, |
| 304 | + "file_extension": ".ts", |
| 305 | + "mimetype": "text/typescript", |
| 306 | + "name": "typescript", |
| 307 | + "version": "3.7.2" |
| 308 | + } |
| 309 | + }, |
| 310 | + "nbformat": 4, |
| 311 | + "nbformat_minor": 5 |
| 312 | +} |
0 commit comments