Hello! Id like to link my llm in lm studio to my arduino

Basicly I want to be able to have a speech-to-text software that transcribes words into the Arduino, not sure how to do that, but I'll cross that bridge when I get there. I want to send those words to an app called LM Studio, where a llm will respond and the Arduino will display its response on a screen or something. How do I get the Arduino to talk to LM Studio? Is this possible?

Thanks!

Welcome to the Arduino forum! Can you show us the format of the message used to communicate those words? Can you provide links to the documentation for what you want to do?

It would take just a dozen or so lines of Arduino code to display the response, which you could send as plain text from the PC via a USB-serial connection.

Hi @xmaximum. I think the best way to accomplish this is to use one of the standardized APIs LM Studio provides for interacting programmatically with the LLM:

https://lmstudio.ai/docs/app/offline#:~:text=leaves%20the%20application.-,Running%20a%20local%20server,-LM%20Studio%20can

LM Studio can be used as a server to provide LLM inferencing on localhost or the local network. Requests to LM Studio use OpenAI endpoints and return OpenAI-like response objects, but stay local.

https://lmstudio.ai/docs/developer/openai-compat#set-the-base-url-to-point-to-lm-studio

You can reuse existing OpenAI clients (in Python, JS, C#, etc) by switching up the "base URL" property to point to your LM Studio instead of OpenAI's servers.

And it also provides an Anthropic-compatible API in case you prefer to use that one instead of the OpenAI-compatible API for some reason:

A good choice for using this API from your Arduino App's Python script is the LangChain Python package:

The typical use case for this package is to interact with the cloud-based LLM services provided by companies like Anthropic (Claude), OpenAI (GPT), or Google (Gemini). However, it allows you to configure the base URL for the requests, which means that it can communicate with your local LLM via LM Studio's compatible API instead of using the cloud hosted LLM:

https://reference.langchain.com/python/langchain-openai/chat_models/base/ChatOpenAI#:~:text=None`-,base_url,-`str


Arduino actually provides a Brick named "Cloud LLM", which utilizes the LangChain Python package under the hood:

https://github.com/arduino/app-bricks-py/tree/release/0.6.4/src/arduino/app_bricks/cloud_llm#cloud-llm-brick

You might be able to use that Brick for your application. However, doing so would be a bit tricky because the Brick's API is not designed to allow you to configure the base URL.

The "Bedtime Story Teller" example App demonstrates how to use the Brick. Here you can see how the object used to interact with the LLM is created:

Configuration is done by passing arguments to the arduino.app_bricks.cloud_llm.CloudLLM class instantiation. In the code above, the model and system prompt are configured. The available configuration parameters are listed here:

https://github.com/arduino/app-bricks-py/tree/release/0.6.4/src/arduino/app_bricks/cloud_llm#configuration

Configuration

The Brick is initialized with the following parameters:

Parameter Type Default Description >
api_key str os.getenv("API_KEY") The authentication key for the LLM provider. Recommended: Set this via the Brick Configuration menu in App Lab instead of code.
model str | CloudModel CloudModel.ANTHROPIC_CLAUDE The specific model to use. Accepts a CloudModel enum or its string value.
system_prompt str "" A base instruction that defines the AI's behavior and persona.
temperature float 0.7 Controls randomness. 0.0 is deterministic, 1.0 is creative.
timeout int 30 Maximum time (in seconds) to wait for a response.

Unfortunately the base URL is not one of those parameters. You would instead need to go more low level and use the arduino.app_bricks.cloud_llm.model_factory function directly (this is normally called for you via the arduino.app_bricks.cloud_llm.CloudLLM class instantiation):

Note that this function has a **kwargs parameter, which allows you to pass arbitrary arguments. Those are then passed along to the langchain_openai.ChatOpenAI instantiation:

So I'm not sure whether you will find it more convenient to hack around the suboptimal design of the Cloud LLM brick, or else eschew it and just use the LangChain Python package directly. Even if the latter, you might still find it useful to use the Cloud LLM brick code as a reference. You can see it here:

Note that several other example scripts are provided under the examples subfolder.