refactor(chat): extract reusable chat and chat_stream functions for MCP server #144
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.
Summary
This PR extracts reusable
chatandchat_streamfunctions from the CLI into a newvlmrun/client/chat.pymodule. These functions can now be imported and used by the MCP server or any other client code.The new module provides:
chat()- Non-streaming chat completionchat_stream()- Streaming chat completion (yields chunks)collect_stream()- Helper to collect streaming chunks into a single responseChatResponseandChatStreamChunkdataclasses for structured responsesChatErrorexception for chat-specific errorsThe CLI has been refactored to use these core functions while retaining its presentation layer (Rich output, progress spinners, artifact download UI).
Usage example:
Updates since last revision
chatfunction andchatmodule. The test now usessys.modules["vlmrun.client.chat"]to explicitly access the module for mocking.Review & Testing Checklist for Human
vlmrun chat "Hello"andvlmrun chat "Describe this" -i image.jpgto confirm the CLI still works as expected with both streaming and non-streaming modeschatandchat_streamfromvlmrun.clientand verify they work correctly for the MCP server use casevlmrun.client.chatand functionchatshare the same name - verify this doesn't cause import issues in your MCP server integrationRecommended test plan:
make testto verify all tests pass--no-streamand default streaming modes--jsonchat()function directly in a Python script to verify it works for MCP server integrationNotes
collect_stream()usesDEFAULT_MODELsince the model isn't available from stream chunks