Skip to content

Conversation

@esafak
Copy link
Owner

@esafak esafak commented Aug 7, 2025

This commit introduces support for the LMStudio provider, allowing users to interact with locally hosted LLMs.

Key Changes:

  • New Provider Module: A new lmstudio.nim module has been created to handle the specific API interactions for LMStudio.
  • Configuration Updates:
    • README.md: Updated to include LMStudio in the list of supported providers and added a configuration example for LMStudio.
    • src/seance/config.nim:
      • Added endpoint to ProviderConfig.
      • Modified loadConfig to handle the endpoint and to not require an API key for LMStudio.
      • Updated createConfigWizard to prompt for an LMStudio endpoint if selected.
    • src/seance/defaults.nim: Added DefaultLMStudioEndpoint and a default model for LMStudio.
  • Provider Integration:
    • src/seance/providers.nim:
      • Registered LMStudioProvider.
      • Updated newProvider to correctly instantiate LMStudioProvider and handle its specific configuration (e.g., no API key required, optional endpoint).
      • Added getRequestHandler to ChatProvider interface and implementations for passing it down.
    • src/seance/providers/common.nim: Added defaultHttpGetHandler for providers that might need it.
  • LMStudio API Handling:
    • The LMStudioProvider in src/seance/providers/lmstudio.nim implements the chat functionality, including:
      • Making POST requests to the LMStudio /v1/chat/completions endpoint.
      • Handling the /v1/models endpoint to check model loading status (with basic interactive prompts if not loaded).
      • Setting appropriate headers, including optional authorization.
      • Parsing LMStudio's specific JSON response format.
  • Error Handling and Logging: Improved error handling for API responses and added more debug logging.
  • Tests: Added a new test suite tests/t_providers_lmstudio.nim to verify the LMStudio provider's functionality, including request formatting, header inclusion, custom endpoint usage, and error handling.

This enhancement significantly expands Séance's utility by enabling local LLM deployments.

I've added support for local inference through LMStudio. This new provider will allow you to use Séance with a local inference server. It's compatible with the OpenAI API, and you can configure it in the `config.ini` file—no API key is required. The endpoint can be set via the `endpoint` field in the provider's configuration section.

I also included a fallback mechanism. If the LMStudio provider isn't configured in your `config.ini` file, it will use the default settings.

Here's a summary of my changes:
- Added `LMStudio` to the `Provider` enum.
- Added default configuration for the `lmstudio` provider.
- Created a new `lmstudio.nim` provider file.
- Updated the configuration loading to handle the `endpoint` field.
- Updated the provider loading to handle the case where the provider is not configured.
- Added tests for the new provider and the fallback logic.
I've added a new feature that checks the LM Studio server for available models before sending a chat request. If the model you've requested isn't found, you will be notified with a warning.

I also fixed a bug affecting both the OpenAI and LMStudio providers where the model returned by the API was not being correctly used.

Here are the technical details of the implementation:
- Added a `getRequestHandler` to the `ChatProvider` type to allow for mocking GET requests in tests.
- Updated the `LMStudioProvider` to use this handler for fetching the list of available models.
- Added a new test case to verify the model check logic.
I've added a new feature to the LMStudio provider. If you request a model that is available but not loaded, I will now prompt you to either continue (which will load the model) or to use an already loaded model instead.

This improves your experience by giving you more control over model loading.

To accomplish this, I:
- Updated the `LMStudioProvider` to check the `state` of the model.
- Added an interactive prompt to the `chat` method.
- Added a test case to verify the new logic in a non-interactive session.
- **Interactive prompt for unloaded models**: If the requested model is available but not loaded, I will now prompt you to either continue (which will load the model) or to use an already loaded model instead.
- **Robust model state parsing**: I've made the logic for parsing the `/v1/models` endpoint more robust, so it can now handle models that don't have a `state` field.
- **Improved error parsing**: I have improved the error handling for API responses. I will now parse JSON error responses and extract the specific error message, providing a much cleaner and more informative error message to you. I have implemented this for both the OpenAI and LMStudio providers.
- **Debug log for model parsing errors**: I have changed the warning about model parsing errors to a debug message to avoid confusing you.
This commit adds error handling to the `chat` command to gracefully handle API errors. Previously, if the API returned an error, the application would crash with an unhandled `IOError`. Now, the error is caught, and a user-friendly error message is displayed.

- Added a `try...except IOError` block to the `chat` command in `src/seance/commands.nim`.
This commit removes the redundant error logging from the providers. The error is now only logged in the `chat` command, which prevents the error message from being displayed twice.
This commit fixes an issue where error messages from the API were being displayed twice. This was caused by the error being logged at the provider level and then again at the command level.

I've removed the redundant logging from the providers and adjusted the exit call in the command to prevent the unhandled exception handler from printing the error again. The error is now displayed cleanly to you just once.
@esafak esafak merged commit ee065c6 into main Aug 7, 2025
1 check passed
@esafak esafak deleted the feat/lmstudio branch August 7, 2025 20:47
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.

2 participants