generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 270
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
11
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
11 commits
Select commit
Hold shift + click to select a range
1e2d589
feat: improve JSON arg parsing in ChatMLX
diego-coder b2d3001
test(mlx): add async tool test
diego-coder c210e07
Refine MLX ReAct tool-call parsing
diego-coder 8fda0d4
test: use phi-3 model for ChatMLX tools
diego-coder a983bcf
fix: lint (#405)
mdrxy 8f1df10
fix: resolve Pydantic test failure and CI linting error
b06ac9d
fix: resolve Pydantic test failure
3d45f27
fix: resolve Pydantic test error and import sorting
7701f25
fix more CI errors
2ec45a7
more bug fixes
b7c1099
fix tuple issue
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
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
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
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} |
Oops, something went wrong.
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.