Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Jan 28, 2025
1 parent 8950ded commit 4bbf0f9
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docs/core_docs/docs/integrations/chat/deepseek.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"\n",
"| Class | Package | Local | Serializable | [PY support](https://python.langchain.com/docs/integrations/chat/deepseek) | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [`DeepSeek`](https://api.js.langchain.com/classes/_langchain_deepseek.ChatDeepSeek.html) | [`@langchain/deepseek`](https://npmjs.com/@langchain/deepseek) | ❌ (see [Ollama](/docs/integrations/chat/ollama)) | beta | ✅ | ![NPM - Downloads](https://img.shields.io/npm/dm/@langchain/deepseek?style=flat-square&label=%20&) | ![NPM - Version](https://img.shields.io/npm/v/@langchain/deepseek?style=flat-square&label=%20&) |\n",
"| [`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 | ✅ | ![NPM - Downloads](https://img.shields.io/npm/dm/@langchain/deepseek?style=flat-square&label=%20&) | ![NPM - Version](https://img.shields.io/npm/v/@langchain/deepseek?style=flat-square&label=%20&) |\n",
"\n",
"### Model features\n",
"\n",
Expand Down
3 changes: 1 addition & 2 deletions libs/langchain-deepseek/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Environment variables for testing locally go here
INTEGRATION_API_KEY="your_key"
DEEPSEEK_API_KEY="your_key"
3 changes: 2 additions & 1 deletion libs/langchain-deepseek/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@langchain/openai": "^0.4.2"
"@langchain/openai": "^0.4.2",
"zod": "^3.24.1"
},
"peerDependencies": {
"@langchain/core": ">=0.3.0 <0.4.0"
Expand Down
111 changes: 99 additions & 12 deletions libs/langchain-deepseek/src/chat_models.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BaseLanguageModelInput } from "@langchain/core/language_models/base";
import { BaseMessage } from "@langchain/core/messages";
import { Runnable } from "@langchain/core/runnables";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import {
ChatOpenAI,
ChatOpenAICallOptions,
ChatOpenAIFields,
ChatOpenAIStructuredOutputMethodOptions,
OpenAIClient,
} from "@langchain/openai";
import { z } from "zod";

export interface ChatDeepSeekCallOptions extends ChatOpenAICallOptions {
headers?: Record<string, string>;
Expand Down Expand Up @@ -114,7 +119,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* ```txt
* AIMessage {
* "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.",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "tokenUsage": {
* "completionTokens": 82,
Expand Down Expand Up @@ -143,7 +150,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* ```txt
* AIMessageChunk {
* "content": "",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -153,7 +162,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": "The",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -163,7 +174,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": " French",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -173,7 +186,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": " translation",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -183,7 +198,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": " of",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -193,7 +210,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": " \"",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -203,7 +222,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": "I",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -213,7 +234,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": " love",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -224,7 +247,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* ...
* AIMessageChunk {
* "content": ".",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": null
* },
Expand All @@ -234,7 +259,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* }
* AIMessageChunk {
* "content": "",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": "stop"
* },
Expand Down Expand Up @@ -265,7 +292,9 @@ export interface ChatDeepSeekInput extends ChatOpenAIFields {
* ```txt
* AIMessageChunk {
* "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.",
* "additional_kwargs": {},
* "additional_kwargs": {
* "reasoning_content": "...",
* },
* "response_metadata": {
* "finishReason": "stop"
* },
Expand Down Expand Up @@ -444,4 +473,62 @@ export class ChatDeepSeek extends ChatOpenAI<ChatDeepSeekCallOptions> {
(message as any).reasoning_content;
return langChainMessage;
}

withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: ChatOpenAIStructuredOutputMethodOptions<false>
): Runnable<BaseLanguageModelInput, RunOutput>;

withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: ChatOpenAIStructuredOutputMethodOptions<true>
): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;

withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: ChatOpenAIStructuredOutputMethodOptions<boolean>
):
| Runnable<BaseLanguageModelInput, RunOutput>
| Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;

withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: ChatOpenAIStructuredOutputMethodOptions<boolean>
):
| Runnable<BaseLanguageModelInput, RunOutput>
| Runnable<
BaseLanguageModelInput,
{ raw: BaseMessage; parsed: RunOutput }
> {
const ensuredConfig = { ...config };
// Deepseek does not support json schema yet
if (ensuredConfig?.method === undefined) {
ensuredConfig.method = "functionCalling";
}
return super.withStructuredOutput<RunOutput>(outputSchema, ensuredConfig);
}
}
1 change: 0 additions & 1 deletion libs/langchain-deepseek/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ test("Can send deepseek-reasoner requests", async () => {
const input = `Translate "I love programming" into French.`;
// Models also accept a list of chat messages or a formatted prompt
const result = await llm.invoke(input);
console.log(result);
expect(
(result.additional_kwargs.reasoning_content as any).length
).toBeGreaterThan(10);
Expand Down
Loading

0 comments on commit 4bbf0f9

Please sign in to comment.