Skip to content

Conversation

diego-coder
Copy link

Fixes #308

This PR creates end-to-end tool-calling support for ChatMLX, allowing it to correctly use tools with compatible models running locally on Apple Silicon.

Context and History

This is a new, comprehensive approach to resolving #308 (assigned by @mdrxy). This work supersedes the previous attempt in PR #133, which was confirmed to be non-functional and is now stale with unresolved conflicts. This new implementation is fully functional, follows LangChain Core conventions, and is covered by extensive testing.

Technical Implementation

The solution is broken down into three main parts:

  1. Prompt Formatting: The _to_chat_prompt method has been updated to accept a tools argument, which is then passed to the tokenizer's apply_chat_template. This ensures the model receives tool definitions in the correct format, enabling it to generate tool-call responses.

  2. Response Parsing: The _to_chat_result method has been completely overhauled with a dual-strategy parsing approach:

  • It first checks for structured tool call data in generation_info, leveraging a model's native tool-calling capabilities when available.

  • If no native tool calls are found, it falls back to a ReAct-style text parser (Action: / Action Input:) to extract tool calls from the raw text, ensuring compatibility with a wide range of models.

  • It correctly uses langchain_core helpers to create ToolCall and InvalidToolCall objects.

  1. Comprehensive Testing: The PR adds new tests to validate the full functionality:
  • Unit tests to verify that tool definitions are correctly passed to the tokenizer.
  • An end-to-end integration test using an upgraded model mlx-community/phi-3-mini-128k-instruct to confirm the entire tool-calling lifecycle (prompting, generation, and parsing) works as expected.

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.

1 participant