Using IPWorks LLM with LM Studio
Requirements:
IPWorks LLM, LM Studio
Introduction
The LLMClient component in IPWorks LLM can send messages to a model running locally in LM Studio, stream its replies, and list available models. LM Studio exposes an OpenAI-compatible Chat Completions endpoint; select spLMStudio to use its default local address.
This guide shows how to load a local model, start its server, generate a reply, stream it, and list available models.
Tip: Download the latest version of IPWorks LLM to follow along. Install LM Studio before loading the model.
Step 1: Download and Load a Model
In the LM Studio desktop application, download a chat model, then load it from the Chat or Developer tab. Choose a model that fits the machine's available memory.
The equivalent CLI workflow is:
lms get https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF --gguf
lms ls
lms load <model-key> --identifier qwen-local
Replace <model-key> with the downloaded model key shown by lms ls. The optional --identifier qwen-local gives the loaded model a short, stable API identifier. If LM Studio is installed without its desktop application, start the headless daemon with lms daemon up before managing models.
Step 2: Start the Local Server
In the desktop application, open the Developer tab and start the server. From the command line, run:
lms server start --port 1234
The OpenAI-compatible base URL is then http://localhost:1234/v1, which is also the default endpoint of spLMStudio. If port 1234 is already in use, choose another port and set ServerURL to the matching base URL, such as http://localhost:1235/v1.
Step 3: Generate a Response
Select spLMStudio and set the model identifier used when loading the model:
using nsoftware.IPWorksLLM;
LLMClient llmclient = new LLMClient();
llmclient.ServiceProvider = LLMClientServiceProviders.spLMStudio;
llmclient.Model = "qwen-local";
llmclient.AddMessage("user", "Reply with one sentence about local language models.");
llmclient.Generate();
Console.WriteLine(llmclient.ResponseText);
When ServerURL is empty, LLMClient uses the default LM Studio base URL, http://localhost:1234/v1. The default local setup does not require an API key; if authentication is enabled, set APIKey to its token. Set ServerURL if the server runs on another host or port. Use the identifier assigned to the loaded model, or one reported by the server when just-in-time loading is enabled.
Step 4: Stream a Response
LM Studio's compatible endpoint supports streaming. Display only ptText fragments from ContentDelta; other part types can represent non-visible content:
llmclient.OnContentDelta += (s, e) =>
{
if (e.PartType == (int)PartTypes.ptText)
Console.Write(e.Delta);
};
llmclient.Stream = true;
llmclient.AddMessage("user", "Write a short poem about a quiet computer.");
llmclient.Generate();
Console.WriteLine();
The assembled reply remains available through ResponseText after streaming finishes.
Step 5: List Available Models
Use ListModels to inspect the identifiers visible to the LM Studio server:
llmclient.OnModelList += (s, e) => Console.WriteLine(e.Id);
llmclient.ListModels();
Set Model to one of the returned identifiers. Depending on the LM Studio configuration, the list can include loaded models or models available for just-in-time loading.
Troubleshooting
- Connection refused: Confirm that the local server is running. If it does not listen on localhost:1234, confirm that ServerURL names its host and port and includes /v1.
- Error 804 from CountTokens: The Chat Completions API has no token-counting endpoint, so CountTokens is not available with spLMStudio.
- Unexpected model selection: Compare Model with an identifier returned by ListModels. Run lms ps to see which models are loaded. The models returned by the API may also include downloaded models when just-in-time loading is enabled.
- Model cannot be loaded: Select a smaller quantization or model, reduce its context length, or adjust GPU offload based on the machine's available resources.
- Remote client cannot connect: LM Studio listens locally by default. Exposing the server to a network changes its security requirements; enable authentication and restrict access before doing so. On the client, set ServerURL to the server's address and APIKey to its token.
To try a loaded model without writing code, run this command from the .NET LLMClient Chat demo directory:
dotnet run --project LLMClientChat.csproj -- --provider lmstudio --model qwen-local
The demo holds a multi-turn conversation. Add --stream to stream replies, or use its /provider command to switch providers during the conversation.
Conclusion
The spLMStudio setting uses the local server's default endpoint. Once the server is running, LLMClient can continue the conversation or stream later replies. For provider switching, see Getting Started with IPWorks LLM. For current server and CLI options, see the LM Studio local server documentation and lms CLI reference.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.