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
{"type": "session", "harness": "opencode", "id": "lmstudio-0015", "name": "openai compatibility"}
{"type": "message", "message": {"role": "system", "content": "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."}}
{"type": "message", "message": {"role": "user", "content": "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."}}
{"type": "message", "message": {"role": "assistant", "content": "**LM Studio OpenAI-compatible endpoints** (default `http://localhost:1234/v1`).\n\nSupported: `GET /v1/models`, `POST /v1/chat/completions`, `POST /v1/embeddings`, `POST /v1/completions` (legacy, base models only), `POST /v1/responses`.\n\n```python\nfrom openai import OpenAI\n\n# api_key can be any non-empty string when auth is disabled in LM Studio.\n# Use the model identifier shown in LM Studio / /v1/models.\nclient = OpenAI(\n base_url=\"http://localhost:1234/v1\",\n api_key=\"lm-studio\",\n)\n\n# List loaded models\nmodels = client.models.list()\nfor m in models.data:\n print(m.id)\n\n# Chat completions\nresponse = client.chat.completions.create(\n model=\"model-identifier-from-lm-studio\",\n messages=[\n {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n {\"role\": \"user\", \"content\": \"Explain quantum computing.\"},\n ],\n temperature=0.7,\n max_tokens=2048,\n)\nprint(response.choices[0].message.content)\n```\n\n**Key differences from OpenAI:**\n- `api_key` is ignored unless you enabled authentication in LM Studio\n- No billing - runs locally; start the server in the Local Server tab first\n- `model` must be a model you have loaded (check `GET /v1/models`)\n- Streaming is supported with `stream=True`"}}