generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 255
feat(chat_models): Enable tool calling for ChatMLX #318
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
Open
diego-coder
wants to merge
8
commits into
langchain-ai:main
Choose a base branch
from
diego-coder:codex/update-_to_chat_prompt-to-accept-tools-parameter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6607ee9
test(mlx): add async tool test
diego-coder 264a02e
Refine MLX ReAct tool-call parsing
diego-coder c5d3cfa
Merge pull request #3 from diego-coder/codex/refactor-_parse_react_to…
diego-coder 87e5484
test: use phi-3 model for ChatMLX tools
diego-coder c62fec6
Merge pull request #5 from diego-coder/codex/add-integration-tests-fo…
diego-coder 06d3b15
feat: improve JSON arg parsing in ChatMLX
diego-coder 3f1856f
Merge branch 'codex/update-_to_chat_prompt-to-accept-tools-parameter'…
diego-coder b730808
Merge pull request #6 from diego-coder/codex/update-error-handling-in…
diego-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
libs/community/tests/integration_tests/chat_models/test_mlx_tool_calls.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Tests ChatMLX tool calling.""" | ||
|
||
from typing import Dict | ||
|
||
import pytest | ||
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage | ||
from langchain_core.tools import tool | ||
|
||
from langchain_community.chat_models.mlx import ChatMLX | ||
from langchain_community.llms.mlx_pipeline import MLXPipeline | ||
|
||
# Use a Phi-3 model for more reliable tool-calling behavior | ||
MODEL_ID = "mlx-community/phi-3-mini-128k-instruct" | ||
|
||
|
||
@tool | ||
def multiply(a: int, b: int) -> int: | ||
"""Multiply two integers.""" | ||
return a * b | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def chat() -> ChatMLX: | ||
"""Return ChatMLX bound with the multiply tool or skip if unavailable.""" | ||
try: | ||
llm = MLXPipeline.from_model_id( | ||
model_id=MODEL_ID, pipeline_kwargs={"max_new_tokens": 150} | ||
) | ||
except Exception: | ||
pytest.skip("Required MLX model isn't available.", allow_module_level=True) | ||
chat_model = ChatMLX(llm=llm) | ||
return chat_model.bind_tools(tools=[multiply], tool_choice=True) # type: ignore[return-value] | ||
|
||
|
||
def _call_tool(tool_call: Dict) -> ToolMessage: | ||
result = multiply.invoke(tool_call["args"]) | ||
return ToolMessage(content=str(result), tool_call_id=tool_call.get("id", "")) | ||
|
||
|
||
def test_mlx_tool_calls_soft(chat: ChatMLX) -> None: | ||
messages = [HumanMessage(content="Use the multiply tool to compute 2 * 3.")] | ||
ai_msg = chat.invoke(messages) | ||
tool_msg = _call_tool(ai_msg.tool_calls[0]) | ||
final = chat.invoke(messages + [ai_msg, tool_msg]) | ||
assert "6" in final.content | ||
|
||
|
||
def test_mlx_tool_calls_hard(chat: ChatMLX) -> None: | ||
messages = [HumanMessage(content="Use the multiply tool to compute 2 * 3.")] | ||
ai_msg = chat.invoke(messages) | ||
assert isinstance(ai_msg, AIMessage) | ||
assert ai_msg.tool_calls | ||
tool_call = ai_msg.tool_calls[0] | ||
assert tool_call["name"] == "multiply" | ||
assert tool_call["args"] == {"a": 2, "b": 3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.