Skip to content

langchain-ollama (partners): allow passing ChatMessage (with custom 'role') to ChatOllama#30191

Closed
rylativity wants to merge 9 commits into
langchain-ai:masterfrom
rylativity:allow-chatmessage-chatollama
Closed

langchain-ollama (partners): allow passing ChatMessage (with custom 'role') to ChatOllama#30191
rylativity wants to merge 9 commits into
langchain-ai:masterfrom
rylativity:allow-chatmessage-chatollama

Conversation

@rylativity

Copy link
Copy Markdown
Contributor

**Description: currently, ChatOllama will raise a value error if a ChatMessage is passed to it, as described here. This PR enables passing a langchain ChatMessage with a custom 'role' attribute through the langchain ChatOllama class.
Issue: resolves #30122 (also related to PR #30147 and ollama-python issue ollama/ollama#8955)
Dependencies: no new dependencies

  • PR title
  • PR message
  • Lint and test: format, lint, and test all running successfully and passing

@vercel

vercel Bot commented Mar 9, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 21, 2025 4:05am

@dosubot dosubot Bot added the size:XS label Mar 9, 2025
@lemassykoi

Copy link
Copy Markdown

from my initial stack trace in 30122, I'm afraid that I will not be able to use create_react_agent to work with granite3.2
could you confirm ? thanks for your implication

@rylativity

rylativity commented Mar 10, 2025

Copy link
Copy Markdown
Contributor Author

Clément PAPPALARDO (@lemassykoi) you will have to explicitly use a ChatMessage instance for the "control/thinking" message like so:

from langchain_core.messages import ChatMessage

...

    messages = {
                "messages": [
                    ChatMessage(role="control", content="thinking"),
                    (
                        "user",
                        "explain catdog",
                    )
                ]
    }

Or, for example with more context:

from langchain_ollama import ChatOllama
from langchain_core.messages import ChatMessage
from langgraph.prebuilt import create_react_agent

llm = ChatOllama(
        base_url    = "http://localhost:11434",
        model       = "granite3.2",
        verbose     = True,
        disable_streaming = True,
    )
agent_supervisor = create_react_agent(
        model        = llm,
        tools        = [],
        name         = "ReAct_Agent_WIP",
        debug        = True
    )

messages = {
            "messages": [
                ChatMessage(role="control", content="thinking"),
                {
                   "role": "user",
                    "content": "explain options trading",
                }
            ]
}

for event in agent_supervisor.stream(
    input = messages,
    # config = config,
    stream_mode = "values",
    debug = True
):
    print(event)

For now, I'd recommend using a ChatMessage instance as described above. I'm looking into whether we can have it accept a dictionary message like this:

...

messages = {
            "messages": [
                {
                    "type": "chat",
                    "role": "control",
                    "content": "thinking"
                },
                {
                   "role": "user",
                    "content": "explain options trading",
                }
            ]
}

...

@dosubot dosubot Bot added size:S and removed size:XS labels Mar 10, 2025
@dosubot dosubot Bot added size:XS and removed size:S labels Mar 10, 2025
@rylativity

Copy link
Copy Markdown
Contributor Author

In order to make this also work with dict type messages it would require fixing something in langchain-core - Right now there is an implicit assumption in the _convert_to_message function in langchain_core/messages/utils.py: that 'role' and 'type' values are interchangable in dict type messages (see https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/messages/utils.py#L325-L328), which I don't think is the desired or intended behavior. Handling this will require a deeper look at langchain-core to avoid breaking something else, but that issue already exists and is not something that's introduced by this PR.

ccurme (@ccurme) my suggestion would be to merge this PR, which allows using ChatMessage with custom 'role' attributes with ChatOllama and closes #30122. And I can open a separate discussion and/or issue regarding how to handle 'role' vs 'type' keys in dict type messages in langchain-core (described above).

@ccurme ccurme (ccurme) self-assigned this Mar 10, 2025

@ccurme ccurme (ccurme) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, I'm happy to move forward but think it's best to keep it open until the ollama sdk actually supports control roles. Then we can merge this with a test + example use in the API reference. From what I can tell it's not yet supported by ollama: ollama/ollama-python#462

Let me know if my thinking makes sense.

@rylativity

Copy link
Copy Markdown
Contributor Author

ccurme (@ccurme) makes sense to me.

Ollama server already accepts arbitrary roles, and the ollama-python maintainers have now expressed a preference to remove role checking/validation entirely from the python sdk. I expect ollama-python will support it shortly - as soon as this PR is reviewed, approved, and merged ollama/ollama-python#462

@rylativity

Copy link
Copy Markdown
Contributor Author

ccurme (@ccurme) the associated functionality on ollama has now been merged into ollama (ollama/ollama#9874 (comment)) and ollama-python sdk (ollama/ollama-python#462 (comment)).

I'll get this covered with a test or two and update the API reference, let me know if there's anything else you'd want before we can merge this

Ryan Stewart added 2 commits March 20, 2025 20:40
@rylativity

Copy link
Copy Markdown
Contributor Author

ccurme (@ccurme) got some wires crossed, opening a new draft PR here to keep it clean - #30411

ccurme (ccurme) added a commit that referenced this pull request Apr 18, 2025
…ges to Ollama (including arbitrary roles) (#30411)

Replacement for PR #30191 (@ccurme)

**Description**: currently, ChatOllama [will raise a value error if a
ChatMessage is passed to
it](https://github.com/langchain-ai/langchain/blob/master/libs/partners/ollama/langchain_ollama/chat_models.py#L514),
as described
#30147 (comment).

Furthermore, ollama-python is removing the limitations on valid roles
that can be passed through chat messages to a model in ollama -
ollama/ollama-python#462 (comment).

This PR removes the role limitations imposed by langchain and enables
passing langchain ChatMessages with arbitrary 'role' values through the
langchain ChatOllama class to the underlying ollama-python Client.

As this PR relies on [merged but unreleased functionality in
ollama-python](
ollama/ollama-python#462 (comment)), I
have temporarily pointed the ollama package source to the main branch of
the ollama-python github repo.

Format, lint, and tests of new functionality passing. Need to resolve
issue with recently added ChatOllama tests. (Now resolved)

**Issue**: resolves #30122 (related to ollama issue
ollama/ollama#8955)

**Dependencies**: no new dependencies

[x] PR title
[x] PR message
[x] Lint and test: format, lint, and test all running successfully and
passing

---------

Co-authored-by: Ryan Stewart <ryanstewart@Ryans-MacBook-Pro.local>
Co-authored-by: Chester Curme <chester.curme@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Granite 3.2 Thinking

3 participants