### Install lmstudio-python using uv Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Installs the lmstudio library using uv. ```bash uv add lmstudio ``` -------------------------------- ### Install lmstudio-python using pdm Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Installs the lmstudio library using pdm. ```bash pdm add lmstudio ``` -------------------------------- ### Create New Node Project (JavaScript) Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/project-setup.md Use the following command to start an interactive project setup for a new Node.js project with JavaScript. ```bash lms create node-javascript ``` -------------------------------- ### Install lmstudio-python using pip Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Installs the lmstudio library using pip. ```bash pip install lmstudio ``` -------------------------------- ### Example Request Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/download-status.md This example shows how to make a GET request to retrieve the download status of a specific job. ```bash curl -H "Authorization: Bearer $LM_API_TOKEN" \ http://localhost:1234/api/v1/models/download/status/job_493c7c9ded ``` -------------------------------- ### Verify the installation Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Command to verify llmster installation. ```bash lms --help ``` -------------------------------- ### Create New Node Project (TypeScript) Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/project-setup.md Use the following command to start an interactive project setup for a new Node.js project with TypeScript. ```bash lms create node-typescript ``` -------------------------------- ### Add lmstudio-js to Existing Project (npm) Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/project-setup.md Install the @lmstudio/sdk package using npm. ```bash npm install @lmstudio/sdk --save ``` -------------------------------- ### Enable and Start the Service Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Commands to enable and start the lmstudio systemd service. ```bash sudo systemctl daemon-reload sudo systemctl enable lmstudio.service sudo systemctl start lmstudio.service ``` -------------------------------- ### Agent Chat Example Setup Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx Python code snippet for setting up an advanced agent example that combines LM Studio models with custom functions for actions like opening URLs, checking time, and analyzing file systems. ```python import json from urllib.parse import urlparse import webbrowser from datetime import datetime import os from openai import OpenAI ``` -------------------------------- ### Example usage Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/1_serve/server-status.md Demonstrates starting the server and then checking its status. ```console ➜ ~ lms server start Starting server... Waking up LM Studio service... Success! Server is now running on port 1234 ➜ ~ lms server status The server is running on port 1234. ``` -------------------------------- ### Add lmstudio-js to Existing Project (pnpm) Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/project-setup.md Install the @lmstudio/sdk package using pnpm. ```bash pnpm add @lmstudio/sdk ``` -------------------------------- ### Example: Get an LLM to Simulate a Terminal Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/2_llm-prediction/completion.mdx An example of how to use the `complete` method to simulate a terminal. ```typescript import { LMStudioClient } from "@lmstudio/sdk"; import { createInterface } from "node:readline/promises"; const rl = createInterface({ input: process.stdin, output: process.stdout }); const client = new LMStudioClient(); const model = await client.llm.model(); let history = ""; while (true) { const command = await rl.question("$ "); history += "$ " + command + "\n"; const prediction = model.complete(history, { stopStrings: ["$"] }); for await (const { content } of prediction) { process.stdout.write(content); } process.stdout.write("\n"); const { content } = await prediction.result(); history += content; } ``` -------------------------------- ### Complete example Source: https://github.com/lmstudio-ai/docs/blob/main/0_app/3_modelyaml/index.md A comprehensive example of a model.yaml file, combining all core fields. ```yaml # model.yaml is an open standard for defining cross-platform, composable AI models # Learn more at https://modelyaml.org model: qwen/qwen3-8b base: - key: lmstudio-community/qwen3-8b-gguf sources: - type: huggingface user: lmstudio-community repo: Qwen3-8B-GGUF - key: lmstudio-community/qwen3-8b-mlx-4bit sources: - type: huggingface user: lmstudio-community repo: Qwen3-8B-MLX-4bit - key: lmstudio-community/qwen3-8b-mlx-8bit sources: - type: huggingface user: lmstudio-community repo: Qwen3-8B-MLX-8bit metadataOverrides: domain: llm architectures: - qwen3 compatibilityTypes: - gguf - safetensors paramsStrings: - 8B minMemoryUsageBytes: 4600000000 contextLengths: - 40960 vision: false reasoning: true trainedForToolUse: true config: operation: fields: - key: llm.prediction.topKSampling value: 20 - key: llm.prediction.minPSampling value: checked: true value: 0 customFields: - key: enableThinking displayName: Enable Thinking description: Controls whether the model will think before replying type: boolean defaultValue: true effects: - type: setJinjaVariable variable: enable_thinking ``` -------------------------------- ### Add lmstudio-js to Existing Project (yarn) Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/project-setup.md Install the @lmstudio/sdk package using yarn. ```bash yarn add @lmstudio/sdk ``` -------------------------------- ### Python SDK Quick Start Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/index.mdx Install and use the lmstudio-python SDK to interact with local models. ```bash pip install lmstudio ``` ```python import lmstudio as lms with lms.Client() as client: model = client.llm.model("openai/gpt-oss-20b") result = model.respond("Who are you, and what can you do?") print(result) ``` -------------------------------- ### TypeScript SDK Quick Start Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/index.mdx Install and use the lmstudio-js SDK to interact with local models. ```bash npm install @lmstudio/sdk ``` ```typescript import { LMStudioClient } from "@lmstudio/sdk"; const client = new LMStudioClient(); const model = await client.llm.model("openai/gpt-oss-20b"); const result = await model.respond("Who are you, and what can you do?"); console.info(result.content); ``` -------------------------------- ### Quick Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/2_agent/act.md A quick example demonstrating the .act() call with a single tool. ```python import lmstudio as lms def multiply(a: float, b: float) -> float: """Given two numbers a and b. Returns the product of them.""" return a * b model = lms.llm("qwen2.5-7b-instruct") model.act( "What is the result of 12345 multiplied by 54321?", [multiply], on_message=print, ) ``` -------------------------------- ### Single-turn example: Calling a function Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx This example shows how to call a simple function `say_hello` using the OpenAI compatible API. ```python say_hello(name) # Prints: Hello, Bob the Builder! ``` ```xml -> % python single-turn-example.py Hello, Bob the Builder! ``` ```python messages=[{"role": "user", "content": "Can you say hello to Bob the Builder?"}] ``` -------------------------------- ### Example Tool Definition Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx JSON object defining a tool for getting a delivery date. ```json [ { "type": "function", "function": { "name": "get_delivery_date", "description": "Get the delivery date for a customer's order", "parameters": { "type": "object", "properties": { "order_id": { "type": "string" } }, "required": ["order_id"] } } } ] ``` -------------------------------- ### Example output Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/5_manage-models/list-downloaded.md Example output of listing downloaded models. ```Python DownloadedLlm(model_key='qwen2.5-7b-instruct-1m', display_name='Qwen2.5 7B Instruct 1M', architecture='qwen2', vision=False) DownloadedEmbeddingModel(model_key='text-embedding-nomic-embed-text-v1.5', display_name='Nomic Embed Text v1.5', architecture='nomic-bert') ``` -------------------------------- ### Example of default tool use format Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx This example shows the format LM Studio uses for default tool use support when a model has not been trained for tool use. It includes the system prompt, available tools, rules, and examples of tool requests. ```bash -> % lms log stream Streaming logs from LM Studio timestamp: 11/13/2024, 9:35:15 AM type: llm.prediction.input modelIdentifier: gemma-2-2b-it modelPath: lmstudio-community/gemma-2-2b-it-GGUF/gemma-2-2b-it-Q4_K_M.gguf input: "system You are a tool-calling AI. You can request calls to available tools with this EXACT format: [TOOL_REQUEST]{\"name\": \"tool_name\", \"arguments\": {\"param1\": \"value1\"}}[END_TOOL_REQUEST] AVAILABLE TOOLS: { \"type\": \"toolArray\", \"tools\": [ { \"type\": \"function\", \"function\": { \"name\": \"get_delivery_date\", \"description\": \"Get the delivery date for a customer's order\", \"parameters\": { \"type\": \"object\", \"properties\": { \"order_id\": { \"type\": \"string\" } }, \"required\": [ \"order_id\" ] } } } ] } RULES: - Only use tools from AVAILABLE TOOLS - Include all required arguments - Use one [TOOL_REQUEST] block per tool - Never use [TOOL_RESULT] - If you decide to call one or more tools, there should be no other text in your message Examples: "Check Paris weather" [TOOL_REQUEST]{\"name\": \"get_weather\", \"arguments\": {\"location\": \"Paris\"}}[END_TOOL_REQUEST] "Send email to John about meeting and open browser" [TOOL_REQUEST]{\"name\": \"send_email\", \"arguments\": {\"to\": \"John\", \"subject\": \"meeting\"}}[END_TOOL_REQUEST] [TOOL_REQUEST]{\"name\": \"open_browser\", \"arguments\": {}}[END_TOOL_REQUEST] Respond conversationally if no matching tools exist. user Get me delivery date for order 123 model " ``` -------------------------------- ### Example Request Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/load.md This example shows how to load a model using a cURL command. ```bash curl http://localhost:1234/api/v1/models/load \ -H "Authorization: Bearer $LM_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "openai/gpt-oss-20b", "context_length": 16384, "flash_attention": true, "echo_load_config": true }' ``` -------------------------------- ### Use locally configured MCP plugins via TypeScript Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example using TypeScript fetch to send a request to the /api/v1/chat endpoint, specifying a locally configured MCP plugin for tool usage. ```typescript const response = await fetch("http://localhost:1234/api/v1/chat", { method: "POST", headers: { "Authorization": `Bearer ${process.env.LM_API_TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "ibm/granite-4-micro", input: "Open lmstudio.ai", integrations: [ { type: "plugin", id: "mcp/playwright", allowed_tools: ["browser_navigate"] } ], context_length: 8000 }) }); const data = await response.json(); console.log(data); ``` -------------------------------- ### Use locally configured MCP plugins via Python Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example using Python requests to send a request to the /api/v1/chat endpoint, specifying a locally configured MCP plugin for tool usage. ```python import os import requests import json response = requests.post( "http://localhost:1234/api/v1/chat", headers={ "Authorization": f"Bearer {os.environ['LM_API_TOKEN']}", "Content-Type": "application/json" }, json={ "model": "ibm/granite-4-micro", "input": "Open lmstudio.ai", "integrations": [ { "type": "plugin", "id": "mcp/playwright", "allowed_tools": ["browser_navigate"] } ], "context_length": 8000 } ) print(json.dumps(response.json(), indent=2)) ``` -------------------------------- ### Params Component Example Source: https://github.com/lmstudio-ai/docs/blob/main/README.md Example of the Params component for listing formatted parameters. ```lms_params - name: "[path]" type: "string" optional: true description: "The path of the model to load. If not provided, you will be prompted to select one" - name: "--ttl" type: "number" optional: true description: "If provided, when the model is not used for this number of seconds, it will be unloaded" - name: "--gpu" type: "string" optional: true description: "How much to offload to the GPU. Values: 0-1, off, max" - name: "--context-length" type: "number" optional: true description: "The number of tokens to consider as context when generating text" - name: "--identifier" type: "string" optional: true description: "The identifier to assign to the loaded model for API reference" ``` -------------------------------- ### Use locally configured MCP plugins via curl Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example using curl to send a request to the /api/v1/chat endpoint, specifying a locally configured MCP plugin for tool usage. ```bash curl http://localhost:1234/api/v1/chat \ -H "Authorization: Bearer $LM_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "ibm/granite-4-micro", "input": "Open lmstudio.ai", "integrations": [ { "type": "plugin", "id": "mcp/playwright", "allowed_tools": ["browser_navigate"] } ], "context_length": 8000 }' ``` -------------------------------- ### Quick Example Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/3_agent/act.md This example demonstrates a basic usage of the .act() call with a single tool. ```typescript import { LMStudioClient, tool } from "@lmstudio/sdk"; import { z } from "zod"; const client = new LMStudioClient(); const multiplyTool = tool({ name: "multiply", description: "Given two numbers a and b. Returns the product of them.", parameters: { a: z.number(), b: z.number() }, implementation: ({ a, b }) => a * b, }); const model = await client.llm.model("qwen2.5-7b-instruct"); await model.act("What is the result of 12345 multiplied by 54321?", [multiplyTool], { onMessage: (message) => console.info(message.toString()), }); ``` -------------------------------- ### Install the plugin instead of running dev Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/5_develop-and-publish/dev.md Installs the plugin into LM Studio. ```shell lms dev --install ``` -------------------------------- ### GET /api/v0/models/{model} Example Request Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/endpoints.mdx Example cURL request to get information about a specific model. ```bash curl -H "Authorization: Bearer $LM_API_TOKEN" http://localhost:1234/api/v0/models/qwen2-vl-7b-instruct ``` -------------------------------- ### Estimate resources example Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/0_local-models/load.md Example output of estimating resources without loading a model. ```bash $ lms load --estimate-only gpt-oss-120b Model: openai/gpt-oss-120b Estimated GPU Memory: 65.68 GB Estimated Total Memory: 65.68 GB Estimate: This model may be loaded based on your resource guardrails settings. ``` -------------------------------- ### Start llmster daemon Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless.md This command starts the llmster daemon in the background. ```bash lms daemon up ``` -------------------------------- ### Python SDK Project Setup Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md This code snippet demonstrates how to import the LM Studio SDK, find the default local API host, and print whether an instance was found. ```python import lmstudio as lms api_host = await lms.AsyncClient.find_default_local_api_host() if api_host is not None: print(f"An LM Studio API server instance is available at {api_host}") else: print("No LM Studio API server instance found on any of the default local ports") ``` -------------------------------- ### index.ts Source: https://github.com/lmstudio-ai/docs/blob/main/_template_dont_edit.mdx Create a client to connect to LM Studio, then load a model ```ts // index.ts import { LMStudioClient } from "@lmstudio/sdk"; // Create a client to connect to LM Studio, then load a model async function main() { const client = new LMStudioClient(); const model = await client.llm.load("meta-llama-3-8b"); const prediction = model.predict("Once upon a time, there was a"); for await (const text of prediction) { process.stdout.write(text); } } main(); ``` -------------------------------- ### Example commands to download and serve a model Source: https://github.com/lmstudio-ai/docs/blob/main/0_app/1_basics/lmstudio-vs-llmster-vs-lms.md Example commands to download and serve a model using the lms CLI. ```bash lms get openai/gpt-oss-20b lms load openai/gpt-oss-20b lms server start ``` -------------------------------- ### Tool Call Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx Example of how a model might request a tool call for getting a delivery date. ```xml {"name": "get_delivery_date", "arguments": {"order_id": "123"}} ``` -------------------------------- ### GET /api/v0/models Example Request Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/endpoints.mdx Example cURL request to list all loaded and downloaded models. ```bash curl -H "Authorization: Bearer $LM_API_TOKEN" http://localhost:1234/api/v0/models ``` -------------------------------- ### lms_download_options Source: https://github.com/lmstudio-ai/docs/blob/main/_template_dont_edit.mdx repository: user/model ```lms_download_options repository: user/model options: - name: "meta-llama-3-8b" description: "Meta Llama 3 8B" version: "1.0.0" size: "1.2 GB" download: "https://example.com/meta-llama-3-8b.zip" license: "MIT" - name: "meta-llama-3-8b" description: "Meta Llama 3 8B" version: "1.0.0" size: "1.2 GB" download: "https://example.com/meta-llama-3-8b.zip" license: "MIT" ``` -------------------------------- ### TypeScript Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_llm-prediction/structured-response.md Example of how to use the LM Studio SDK to get a structured JSON response using Zod for schema validation. ```typescript import { LMStudioClient } from "@lmstudio/sdk"; import { z } from "zod"; const Book = z.object({ title: z.string(), author: z.string(), year: z.number().int() }) const client = new LMStudioClient() const llm = client.llm.model() const response = llm.respond( "Tell me about The Hobbit.", { structured: Book }, ) console.log(response.content.title) ``` -------------------------------- ### Configure default client with server API host (convenience API) Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Configures the default client to use a specific server API host and port using the convenience API. ```Python import lmstudio as lms SERVER_API_HOST = "localhost:1234" # This must be the *first* convenience API interaction (otherwise the SDK # implicitly creates a client that accesses the default server API host) lms.configure_default_client(SERVER_API_HOST) # Note: the dedicated configuration API was added in lmstudio-python 1.3.0 # For compatibility with earlier SDK versions, it is still possible to use # lms.get_default_client(SERVER_API_HOST) to configure the default client ``` -------------------------------- ### Download a Model Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Command to download a model for the server. ```bash lms get openai/gpt-oss-20b ``` -------------------------------- ### Response Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/download-status.md This is an example of a successful response when a download is completed. ```json { "job_id": "job_493c7c9ded", "status": "completed", "total_size_bytes": 2279145003, "downloaded_bytes": 2279145003, "started_at": "2025-10-03T15:33:23.496Z", "completed_at": "2025-10-03T15:43:12.102Z" } ``` -------------------------------- ### Python Client Initialization Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx Example of initializing the OpenAI client to point to the local LM Studio server. ```python client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio") model = "lmstudio-community/qwen2.5-7b-instruct" ``` -------------------------------- ### prompt_processing.start Event Data Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/streaming-events.md Signals the start of the model processing a prompt. ```json { "type": "prompt_processing.start" } ``` -------------------------------- ### reasoning.start Event Data Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/streaming-events.md Signals the model is starting to stream reasoning content. ```json { "type": "reasoning.start" } ``` -------------------------------- ### System Prompt for Qwen2.5-Instruct Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx Example system prompt for Qwen2.5-Instruct model when tool use is enabled. ```json <|im_start|>system You are Qwen, created by Alibaba Cloud. You are a helpful assistant. ``` -------------------------------- ### chat.start Event Data Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/streaming-events.md An event that is emitted at the start of a chat response stream. ```json { "type": "chat.start", "model_instance_id": "openai/gpt-oss-20b" } ``` -------------------------------- ### Verify the API is responding Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Command to test if the API is responding. ```bash curl http://localhost:1234/v1/models ``` -------------------------------- ### Download models from the terminal Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/api-changelog.md Examples of using the `lms get` command to download models from the terminal. ```bash lms get deepseek-r1 ``` ```bash lms get ``` ```bash lms get deepseek-r1 --mlx ``` -------------------------------- ### Javascript Source: https://github.com/lmstudio-ai/docs/blob/main/_assets/nvidia-spark-playbook/README.md Instructions to install dependencies and run the Javascript script. ```bash cd js npm install @lmstudio/sdk node run.js ``` -------------------------------- ### Chat with a model using TypeScript Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example of sending a chat message to the model using TypeScript. ```typescript const response = await fetch("http://localhost:1234/api/v1/chat", { method: "POST", headers: { "Authorization": `Bearer ${process.env.LM_API_TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "ibm/granite-4-micro", input: "Write a short haiku about sunrise." }) }); const data = await response.json(); console.log(data); ``` -------------------------------- ### Chat with a model using Python Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example of sending a chat message to the model using Python. ```python import os import requests import json response = requests.post( "http://localhost:1234/api/v1/chat", headers={ "Authorization": f"Bearer {os.environ['LM_API_TOKEN']}", "Content-Type": "application/json" }, json={ "model": "ibm/granite-4-micro", "input": "Write a short haiku about sunrise." } ) print(json.dumps(response.json(), indent=2)) ``` -------------------------------- ### Example Request Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/list.md This endpoint has no request parameters. ```bash curl http://localhost:1234/api/v1/models \ -H "Authorization: Bearer $LM_API_TOKEN" ``` -------------------------------- ### Chat with a model using curl Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example of sending a chat message to the model using curl. ```bash curl http://localhost:1234/api/v1/chat \ -H "Authorization: Bearer $LM_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "ibm/granite-4-micro", "input": "Write a short haiku about sunrise." }' ``` -------------------------------- ### example.py Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/2_agent/tools.md Example code using the `create_file` tool to create a file with specific content. ```python import lmstudio as lms from create_file_tool import create_file model = lms.llm("qwen2.5-7b-instruct") model.act( "Please create a file named output.txt with your understanding of the meaning of life.", [create_file], ) ``` -------------------------------- ### model_load.start Event Data Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/streaming-events.md Signals the start of a model being loaded to fulfill the chat request. Will not be emitted if the requested model is already loaded. ```json { "type": "model_load.start", "model_instance_id": "openai/gpt-oss-20b" } ``` -------------------------------- ### index.ts Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/2_llm-prediction/chat-completion.md First, you need to get a model handle. This can be done using the `model` method in the `llm` namespace. For example, here is how to use Qwen2.5 7B Instruct. ```typescript import { LMStudioClient } from "@lmstudio/sdk"; const client = new LMStudioClient(); const model = await client.llm.model("qwen2.5-7b-instruct"); ``` -------------------------------- ### Load the model Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Command to load a model manually for testing. ```bash lms load openai/gpt-oss-20b ``` -------------------------------- ### Interact with ephemeral MCP servers via TypeScript Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/quickstart.md Example using TypeScript fetch to send a request to the /api/v1/chat endpoint, specifying an ephemeral MCP server for tool usage. ```typescript const response = await fetch("http://localhost:1234/api/v1/chat", { method: "POST", headers: { "Authorization": `Bearer ${process.env.LM_API_TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "ibm/granite-4-micro", input: "What is the top trending model on hugging face?", integrations: [ { type: "ephemeral_mcp", server_label: "huggingface", server_url: "https://huggingface.co/mcp", allowed_tools: ["model_search"] } ], context_length: 8000 }) const data = await response.json(); console.log(data); ``` -------------------------------- ### Configure client with server API host (scoped resource API) Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Configures a client instance to use a specific server API host and port using the scoped resource API. ```Python import lmstudio as lms SERVER_API_HOST = "localhost:1234" # When using the scoped resource API, each client instance # can be configured to use a specific server API host with lms.Client(SERVER_API_HOST) as client: model = client.llm.model() for fragment in model.respond_stream("What is the meaning of life?"): print(fragment.content, end="", flush=True) print() # Advance to a new line at the end of the response ``` -------------------------------- ### Configure client with server API host (asynchronous API) Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/1_getting-started/project-setup.md Configures a client instance to use a specific server API host and port using the asynchronous API. Requires Python SDK version 1.5.0 or later. ```Python # Note: assumes use of an async function or the "python -m asyncio" asynchronous REPL # Requires Python SDK version 1.5.0 or later import lmstudio as lms SERVER_API_HOST = "localhost:1234" # When using the asynchronous API, each client instance # can be configured to use a specific server API host async with lms.AsyncClient(SERVER_API_HOST) as client: model = await client.llm.model() for fragment in await model.respond_stream("What is the meaning of life?"): print(fragment.content, end="", flush=True) print() # Advance to a new line at the end of the response ``` -------------------------------- ### Tool Streaming Chatbot Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/tools.mdx A Python script demonstrating how to use tools with streaming responses in an OpenAI compatible API setup. It includes a chat loop, tool calling, and processing of streaming responses. ```python from openai import OpenAI # Assume MODEL, client, and process_stream are defined elsewhere # MODEL = "gpt-3.5-turbo" # client = OpenAI(base_url="http://localhost:1234/v1") # def process_stream(stream, add_assistant_label=True): # # Placeholder for stream processing logic # full_response = "" # for chunk in stream: # content = chunk.choices[0].delta.content # if content: # print(content, end="", flush=True) # full_response += content # return full_response, None def chat_loop(): messages = [ {"role": "system", "content": "You are a helpful assistant."} ] while True: user_input = input("You: ") if user_input.lower() == 'quit': break messages.append({"role": "user", "content": user_input}) # Check if the user is asking for a tool call tool_call_id = None if "tell me the current time" in user_input.lower(): tool_call_id = "get_current_time_tool_id" # Replace with actual tool ID logic messages.append({ "role": "tool", "tool_call_id": tool_call_id, "content": "The current time is 18:49:31." }) # Get final response after tool execution final_response, _ = process_stream( client.chat.completions.create( model=MODEL, messages=messages, stream=True ), add_assistant_label=False ) if final_response: print() messages.append({"role": "assistant", "content": final_response}) if __name__ == "__main__": chat_loop() ``` -------------------------------- ### HTTP (LM Studio REST API) Quick Start Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/index.mdx Start the LM Studio server and make a request to the chat endpoint. ```bash lms server start --port 1234 ``` ```bash curl http://localhost:1234/api/v1/chat \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $LM_API_TOKEN" \ -d '{ "model": "openai/gpt-oss-20b", "input": "Who are you, and what can you do?" }' ``` -------------------------------- ### Example using Python for structured output Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/structured-output.md Demonstrates a structured output request using the Python OpenAI client. ```python from openai import OpenAI import json # Initialize OpenAI client that points to the local LM Studio server client = OpenAI( base_url="http://localhost:1234/v1", api_key="lm-studio" ) # Define the conversation with the AI messages = [ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Create 1-3 fictional characters"} ] # Define the expected response structure character_schema = { "type": "json_schema", "json_schema": { "name": "characters", "schema": { "type": "object", "properties": { "characters": { "type": "array", "items": { "type": "object", "properties": { "name": {"type": "string"}, "occupation": {"type": "string"}, "personality": {"type": "string"}, "background": {"type": "string"} }, "required": ["name", "occupation", "personality", "background"] }, "minItems": 1, } }, "required": ["characters"] }, } } # Get response from AI response = client.chat.completions.create( model="your-model", messages=messages, response_format=character_schema, ) # Parse and display the results results = json.loads(response.choices[0].message.content) print(json.dumps(results, indent=2)) ``` -------------------------------- ### Quick Example: Chat with a Llama Model (convenience API) Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/index.md A quick example demonstrating how to chat with a Llama model using the convenience API. ```Python import lmstudio as lms model = lms.llm("qwen/qwen3-4b-2507") result = model.respond("What is the meaning of life?") print(result) ``` -------------------------------- ### List installed runtimes Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/4_runtime/runtime.md Lists all installed inference runtimes. ```shell lms runtime ls ``` -------------------------------- ### index.ts Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/8_model-info/get-context-length.md Get the maximum context length of a model. ```typescript const contextLength = await model.getContextLength(); ``` -------------------------------- ### LLM Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/8_model-info/get-model-info.md Get information about the loaded LLM model. ```typescript import { LMStudioClient } from "@lmstudio/sdk"; const client = new LMStudioClient(); const model = await client.llm.model(); const modelInfo = await model.getInfo(); console.info("Model Key", modelInfo.modelKey); console.info("Current Context Length", model.contextLength); console.info("Model Trained for Tool Use", modelInfo.trainedForToolUse); // etc. ``` -------------------------------- ### Example Frontmatter Source: https://github.com/lmstudio-ai/docs/blob/main/README.md Frontmatter required for new documentation articles. ```yaml --- title: Keyboard Shortcuts description: Learn about the keyboard shortcuts in LM Studio index: 2 --- ``` -------------------------------- ### example.py Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/6_model-info/get-context-length.md Get the maximum context length of a model. ```python context_length = model.get_context_length() ``` -------------------------------- ### Embedding Model Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/8_model-info/get-model-info.md Get information about the loaded embedding model. ```typescript import { LMStudioClient } from "@lmstudio/sdk"; const client = new LMStudioClient(); const model = await client.embedding.model(); const modelInfo = await model.getInfo(); console.info("Model Key", modelInfo.modelKey); console.info("Current Context Length", model.contextLength); // etc. ``` -------------------------------- ### Python Source: https://github.com/lmstudio-ai/docs/blob/main/_template_dont_edit.mdx Multi-line Python code ```python # Multi-line Python code def hello(): print("hey") return "world" ``` -------------------------------- ### Error Event Example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/2_rest/streaming-events.md An example of an error event that can occur during streaming. ```json { "type": "error", "error": { "type": "invalid_request", "message": "\"model\" is required", "code": "missing_required_parameter", "param": "model" } } ``` -------------------------------- ### Start LM Studio Server on Local Network Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/0_server/serve-on-network.mdx Command to start the LM Studio API server and make it accessible on the local network by binding to all available network interfaces (0.0.0.0). ```bash lms server start --bind 0.0.0.0 ``` -------------------------------- ### Python example Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/chat-completions.md Example of how to use the Chat Completions API with Python. ```python from openai import OpenAI client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio") completion = client.chat.completions.create( model="model-identifier", messages=[ {"role": "system", "content": "Always answer in rhymes."}, {"role": "user", "content": "Introduce yourself."} ], temperature=0.7, ) print(completion.choices[0].message) ``` -------------------------------- ### Service Management Commands Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/0_core/headless_llmster.mdx Common commands for managing the lmstudio systemd service. ```bash # Stop the service sudo systemctl stop lmstudio # Restart the service sudo systemctl restart lmstudio # Disable auto-start sudo systemctl disable lmstudio ``` -------------------------------- ### Example output when not running Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/2_daemon/daemon-status.md Example JSON output when the daemon is not running. ```json { "status": "not-running" } ``` -------------------------------- ### lms_hstack Source: https://github.com/lmstudio-ai/docs/blob/main/_template_dont_edit.mdx Column 1 Second column markdown content here ```js console.log("Hello from the code block"); ``` -------------------------------- ### Example output when running Source: https://github.com/lmstudio-ai/docs/blob/main/3_cli/2_daemon/daemon-status.md Example JSON output when the daemon is running. ```json { "status": "running", "pid": 12345, "isDaemon": true } ``` -------------------------------- ### Example using curl for structured output Source: https://github.com/lmstudio-ai/docs/blob/main/1_developer/3_openai-compat/structured-output.md Demonstrates a structured output request using the curl utility. ```bash curl http://localhost:1234/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "{{model}}", "messages": [ { "role": "system", "content": "You are a helpful jokester." }, { "role": "user", "content": "Tell me a joke." } ], "response_format": { "type": "json_schema", "json_schema": { "name": "joke_response", "strict": "true", "schema": { "type": "object", "properties": { "joke": { "type": "string" } }, "required": ["joke"] } } }, "temperature": 0.7, "max_tokens": 50, "stream": false }' ``` -------------------------------- ### Inject Current Time Source: https://github.com/lmstudio-ai/docs/blob/main/2_typescript/3_plugins/2_prompt-preprocessor/examples.md An example preprocessor that injects the current time before each user message. ```typescript import { type PromptPreprocessorController, type ChatMessage } from "@lmstudio/sdk"; export async function preprocess(ctl: PromptPreprocessorController, userMessage: ChatMessage) { const textContent = userMessage.getText(); const transformed = `Current time: ${new Date().toString()}\n\n${textContent}`; return transformed; } ``` -------------------------------- ### Quick Example: Chat with a Llama Model (scoped resource API) Source: https://github.com/lmstudio-ai/docs/blob/main/1_python/index.md A quick example demonstrating how to chat with a Llama model using the scoped resource API. ```Python import lmstudio as lms with lms.Client() as client: model = client.llm.model("qwen/qwen3-4b-2507") result = model.respond("What is the meaning of life?") print(result) ```