Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/langchain-ollama/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"ollama": "^0.5.12",
"ollama": "^0.5.17",
"uuid": "^10.0.0"
},
"peerDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions libs/langchain-ollama/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface ChatOllamaInput
* @default fetch
*/
fetch?: typeof fetch;
think?: boolean;
}

/**
Expand Down Expand Up @@ -489,6 +490,8 @@ export class ChatOllama

baseUrl = "http://127.0.0.1:11434";

think?: boolean;

constructor(fields?: ChatOllamaInput) {
super(fields ?? {});

Expand Down Expand Up @@ -532,6 +535,7 @@ export class ChatOllama
this.streaming = fields?.streaming;
this.format = fields?.format;
this.keepAlive = fields?.keepAlive;
this.think = fields?.think;
this.checkOrPullModel = fields?.checkOrPullModel ?? this.checkOrPullModel;
}

Expand Down Expand Up @@ -604,6 +608,7 @@ export class ChatOllama
model: this.model,
format: options?.format ?? this.format,
keep_alive: this.keepAlive,
think: this.think,
options: {
numa: this.numa,
num_ctx: this.numCtx,
Expand Down Expand Up @@ -746,11 +751,15 @@ export class ChatOllama
usageMetadata.input_tokens + usageMetadata.output_tokens;
lastMetadata = rest;

// when think is enabled, try thinking first
const token = this.think ? responseMessage.thinking ?? responseMessage.content ?? ""
: responseMessage.content ?? ""

yield new ChatGenerationChunk({
text: responseMessage.content ?? "",
text: token,
message: convertOllamaMessagesToLangChain(responseMessage),
});
await runManager?.handleLLMNewToken(responseMessage.content ?? "");
await runManager?.handleLLMNewToken(token);
}

// Yield the `response_metadata` as the final chunk.
Expand Down
54 changes: 54 additions & 0 deletions libs/langchain-ollama/src/tests/chat_models_think.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { test, expect } from "@jest/globals";
import { HumanMessage } from "@langchain/core/messages";
import { ChatOllama } from "../chat_models.js";

test("test deep seek model with think=false", async () => {
const ollama = new ChatOllama({
model: "deepseek-r1:32b",
think: false, // Ensure the "think" field is explicitly set to false
maxRetries: 1,
});

const res = await ollama.invoke([
new HumanMessage({
content: "Explain the process of photosynthesis briefly.",
}),
]);

// Ensure the response is defined
expect(res).toBeDefined();
expect(res.content).toBeDefined();

const responseContent = res.content;

// Validate that the response does not include any <think>...</think> blocks
// s means allow . to match new line character
expect(responseContent).not.toMatch(/<think>.*?<\/think>/is);

// Ensure the response is concise and directly answers the question
expect(responseContent).toMatch(/photosynthesis/i); // Check it includes the topic
expect(responseContent.length).toBeGreaterThan(1);
});

test("test deep seek model with think=true (default)", async () => {
const ollama = new ChatOllama({
model: "deepseek-r1:32b",
maxRetries: 1,
});

const res = await ollama.invoke([
new HumanMessage({
content: "Explain the process of photosynthesis briefly.",
}),
]);

// Ensure the response is defined
expect(res).toBeDefined();
expect(res.content).toBeDefined();

const responseContent = res.content;

Comment thread
hntrl marked this conversation as resolved.
// Ensure the response is concise and directly answers the question
expect(responseContent).toMatch(/photosynthesis/i); // Check it includes the topic
expect(responseContent.length).toBeGreaterThan(1);
});
2 changes: 1 addition & 1 deletion libs/langchain-ollama/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function convertOllamaMessagesToLangChain(
}
): AIMessageChunk {
return new AIMessageChunk({
content: messages.content ?? "",
content: messages.thinking ?? messages.content ?? "",
tool_call_chunks: messages.tool_calls?.map((tc) => ({
name: tc.function.name,
args: JSON.stringify(tc.function.arguments),
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9109,7 +9109,7 @@ __metadata:
eslint-plugin-prettier: ^4.2.1
jest: ^29.5.0
jest-environment-node: ^29.6.4
ollama: ^0.5.12
ollama: ^0.5.17
prettier: ^2.8.3
release-it: ^18.1.2
rollup: ^4.5.2
Expand Down Expand Up @@ -30693,12 +30693,12 @@ __metadata:
languageName: node
linkType: hard

"ollama@npm:^0.5.12":
version: 0.5.12
resolution: "ollama@npm:0.5.12"
"ollama@npm:^0.5.17":
version: 0.5.17
resolution: "ollama@npm:0.5.17"
dependencies:
whatwg-fetch: ^3.6.20
checksum: 0abc1151d2cfd02198829f706f8efca978c8562691e7502924166798f6a0cd7e1bf51e085d313ddf5a76507a36ffa12b48a66d4dd659b419474c2f33e3f03b44
checksum: 151f1b0d69ea1f54927c4e92537f7dafa28f93d7b7d2d362387e046d5fea21eae07997b4c055f9a2579058bb88daad20a953ea300a93799d4ee78736e1a0af71
languageName: node
linkType: hard

Expand Down