tinycodinglm / lmstudio /lmstudio-0015.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.13 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do I use LM Studio's OpenAI-compatible API from Python? Show base_url and api_key setup, a chat completions call, and how to list loaded models. Include code examples.

Assistant

LM Studio OpenAI-compatible endpoints (default http://localhost:1234/v1).

Supported: GET /v1/models, POST /v1/chat/completions, POST /v1/embeddings, POST /v1/completions (legacy, base models only), POST /v1/responses.

python
from openai import OpenAI

# api_key can be any non-empty string when auth is disabled in LM Studio.
# Use the model identifier shown in LM Studio / /v1/models.
client = OpenAI(
    base_url="http://localhost:1234/v1",
    api_key="lm-studio",
)

# List loaded models
models = client.models.list()
for m in models.data:
    print(m.id)

# Chat completions
response = client.chat.completions.create(
    model="model-identifier-from-lm-studio",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing."},
    ],
    temperature=0.7,
    max_tokens=2048,
)
print(response.choices[0].message.content)

Key differences from OpenAI:

  • api_key is ignored unless you enabled authentication in LM Studio
  • No billing - runs locally; start the server in the Local Server tab first
  • model must be a model you have loaded (check GET /v1/models)
  • Streaming is supported with stream=True