-
Notifications
You must be signed in to change notification settings - Fork 17k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ollama: add reasoning model support (e.g. deepseek) #29689
ollama: add reasoning model support (e.g. deepseek) #29689
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
be00618
to
052f76c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @BobMerkus, we can move forward with this but I don't think the current implementation is working correctly. I'm finding that reasoning_content
is just "<think></think>"
. The reason is that _generate
runs through streaming, so as tokens are generated in the stream, we don't know whether they're part of the thinking block or not.
517a870
to
cc91ca9
Compare
Hey @ccurme, Good catch, the standalone example I supplied is aggregating a buffer to account for this. The initial prompt inside the test did not actually lead to any content inside the thinking block (because it was relatively simple), I overlooked this while integrating in to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @BobMerkus. I updated the default for extract_reasoning
to False. I also refactored this:
- 7fac207 refactors the original streaming code to share a single iterator method
- e8ff5a8 adds back in the extraction of reasoning content
The motivation is that extracting reasoning content is arguably a niche use-case (disabled by default, only applies to a subset of models), so I was hesitant to introduce complexity into the basic streaming loops. This separates it out a bit more.
Would appreciate a review :)
Yeah, makes sense to separate this from existing runtime. Implementation seems good to me and thanks for fixing the linter errors! As a side note; some of the other integration tests seem to fail when running locally, although not related to the changes in this MR. |
Description
This PR adds reasoning model support for
langchain-ollama
by extracting reasoning token blocks, like those used in deepseek. It was inspired by ollama-deep-researcher, specifically the parsing of thinking blocks:This notes that it is very hard to remove the reasoning block from prompting, but we actually want the model to reason in order to increase model performance. This implementation extracts the thinking block, so the client can still expect a proper message to be returned by
ChatOllama
(and use the reasoning content separately when desired).This implementation takes the same approach as ChatDeepseek, which adds the reasoning content to chunk.additional_kwargs.reasoning_content;
This should probably be handled upstream in ollama + ollama-python, but this seems like a reasonably effective solution. This is a standalone example of what is happening;
Issue
Integrating reasoning models (e.g. deepseek-r1) into existing LangChain based workflows is hard due to the thinking blocks that are included in the message contents. To avoid this, we could match the
ChatOllama
integration withChatDeepseek
to return the reasoning content insidemessage.additional_arguments.reasoning_content
instead.Dependenices
None