tinycodinglm / lmstudio /lmstudio-0030.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.03 kB
{"type": "session", "harness": "opencode", "id": "lmstudio-0030", "name": "streaming"}
{"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 does streaming work with LM Studio's OpenAI-compatible chat completions? Show sync and async Python streaming with stream=True. Include code examples."}}
{"type": "message", "message": {"role": "assistant", "content": "**Streaming** sends tokens as generated - lower time-to-first-token.\n\n```python\nfrom openai import OpenAI\n\nclient = OpenAI(base_url=\"http://localhost:1234/v1\", api_key=\"lm-studio\")\n\nstream = client.chat.completions.create(\n model=\"model-identifier-from-lm-studio\",\n messages=[{\"role\": \"user\", \"content\": \"Write a poem about coding.\"}],\n stream=True,\n)\nfor chunk in stream:\n delta = chunk.choices[0].delta.content\n if delta:\n print(delta, end=\"\", flush=True)\nprint()\n```\n\n**Async:**\n\n```python\nimport asyncio\nfrom openai import AsyncOpenAI\n\nasync def stream_response() -> None:\n client = AsyncOpenAI(base_url=\"http://localhost:1234/v1\", api_key=\"lm-studio\")\n stream = await client.chat.completions.create(\n model=\"model-identifier-from-lm-studio\",\n messages=[{\"role\": \"user\", \"content\": \"Explain recursion.\"}],\n stream=True,\n )\n async for chunk in stream:\n delta = chunk.choices[0].delta.content\n if delta:\n print(delta, end=\"\", flush=True)\n print()\n\nasyncio.run(stream_response())\n```\n\nStart the server first; `model` must be a loaded id from `GET /v1/models`."}}