openai_api_completions.ipynb 19.8 KB
Newer Older
Chayenne's avatar
Chayenne committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
7
    "# OpenAI APIs - Completions\n",
Chayenne's avatar
Chayenne committed
8
    "\n",
9
10
    "SGLang provides OpenAI-compatible APIs to enable a smooth transition from OpenAI services to self-hosted local models.\n",
    "A complete reference for the API is available in the [OpenAI API Reference](https://platform.openai.com/docs/api-reference).\n",
11
    "\n",
12
    "This tutorial covers the following popular APIs:\n",
Chayenne's avatar
Chayenne committed
13
14
15
    "\n",
    "- `chat/completions`\n",
    "- `completions`\n",
16
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
17
    "Check out other tutorials to learn about [vision APIs](openai_api_vision.ipynb) for vision-language models and [embedding APIs](openai_api_embeddings.ipynb) for embedding models."
Chayenne's avatar
Chayenne committed
18
19
20
21
22
23
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
24
    "## Launch A Server\n",
Chayenne's avatar
Chayenne committed
25
    "\n",
26
    "Launch the server in your terminal and wait for it to initialize."
Chayenne's avatar
Chayenne committed
27
28
29
30
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
31
   "execution_count": null,
32
   "metadata": {},
Chayenne's avatar
Chayenne committed
33
   "outputs": [],
Chayenne's avatar
Chayenne committed
34
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
35
    "from sglang.test.doc_patch import launch_server_cmd\n",
36
37
38
    "from sglang.utils import wait_for_server, print_highlight, terminate_process\n",
    "\n",
    "server_process, port = launch_server_cmd(\n",
39
    "    \"python3 -m sglang.launch_server --model-path qwen/qwen2.5-0.5b-instruct --host 0.0.0.0 --log-level warning\"\n",
Chayenne's avatar
Chayenne committed
40
41
    ")\n",
    "\n",
42
43
    "wait_for_server(f\"http://localhost:{port}\")\n",
    "print(f\"Server started on http://localhost:{port}\")"
Chayenne's avatar
Chayenne committed
44
45
   ]
  },
46
47
48
49
50
51
52
53
54
55
56
57
58
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat Completions\n",
    "\n",
    "### Usage\n",
    "\n",
    "The server fully implements the OpenAI API.\n",
    "It will automatically apply the chat template specified in the Hugging Face tokenizer, if one is available.\n",
    "You can also specify a custom chat template with `--chat-template` when launching the server."
   ]
  },
Chayenne's avatar
Chayenne committed
59
60
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
61
   "execution_count": null,
62
   "metadata": {},
Chayenne's avatar
Chayenne committed
63
   "outputs": [],
Chayenne's avatar
Chayenne committed
64
65
66
   "source": [
    "import openai\n",
    "\n",
67
    "client = openai.Client(base_url=f\"http://127.0.0.1:{port}/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
68
69
    "\n",
    "response = client.chat.completions.create(\n",
70
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
Chayenne's avatar
Chayenne committed
71
72
73
74
75
76
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
    "    ],\n",
    "    temperature=0,\n",
    "    max_tokens=64,\n",
    ")\n",
77
78
    "\n",
    "print_highlight(f\"Response: {response}\")"
Chayenne's avatar
Chayenne committed
79
80
   ]
  },
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Model Thinking/Reasoning Support\n",
    "\n",
    "Some models support internal reasoning or thinking processes that can be exposed in the API response. SGLang provides unified support for various reasoning models through the `chat_template_kwargs` parameter and compatible reasoning parsers.\n",
    "\n",
    "#### Supported Models and Configuration\n",
    "\n",
    "| Model Family | Chat Template Parameter | Reasoning Parser | Notes |\n",
    "|--------------|------------------------|------------------|--------|\n",
    "| DeepSeek-R1 (R1, R1-0528, R1-Distill) | `enable_thinking` | `--reasoning-parser deepseek-r1` | Standard reasoning models |\n",
    "| DeepSeek-V3.1 | `thinking` | `--reasoning-parser deepseek-v3` | Hybrid model (thinking/non-thinking modes) |\n",
    "| Qwen3 (standard) | `enable_thinking` | `--reasoning-parser qwen3` | Hybrid model (thinking/non-thinking modes) |\n",
    "| Qwen3-Thinking | N/A (always enabled) | `--reasoning-parser qwen3-thinking` | Always generates reasoning |\n",
    "| Kimi | N/A (always enabled) | `--reasoning-parser kimi` | Kimi thinking models |\n",
    "| Gpt-Oss | N/A (always enabled) | `--reasoning-parser gpt-oss` | Gpt-Oss thinking models |\n",
    "\n",
    "#### Basic Usage\n",
    "\n",
    "To enable reasoning output, you need to:\n",
    "1. Launch the server with the appropriate reasoning parser\n",
    "2. Set the model-specific parameter in `chat_template_kwargs`\n",
    "3. Optionally use `separate_reasoning: False` to not get reasoning content separately (default to `True`)\n",
    "\n",
    "**Note for Qwen3-Thinking models:** These models always generate thinking content and do not support the `enable_thinking` parameter. Use `--reasoning-parser qwen3-thinking` or `--reasoning-parser qwen3` to parse the thinking content.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Example: Qwen3 Models\n",
    "\n",
    "```python\n",
    "# Launch server:\n",
118
    "# python3 -m sglang.launch_server --model Qwen/Qwen3-4B --reasoning-parser qwen3\n",
119
120
121
122
123
    "\n",
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    api_key=\"EMPTY\",\n",
124
    "    base_url=f\"http://127.0.0.1:30000/v1\",\n",
125
126
    ")\n",
    "\n",
127
128
    "model = \"Qwen/Qwen3-4B\"\n",
    "messages = [{\"role\": \"user\", \"content\": \"How many r's are in 'strawberry'?\"}]\n",
129
130
131
132
133
134
135
136
137
138
139
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=model,\n",
    "    messages=messages,\n",
    "    extra_body={\n",
    "        \"chat_template_kwargs\": {\"enable_thinking\": True},\n",
    "        \"separate_reasoning\": True\n",
    "    }\n",
    ")\n",
    "\n",
    "print(\"Reasoning:\", response.choices[0].message.reasoning_content)\n",
140
    "print(\"-\"*100)\n",
141
142
143
    "print(\"Answer:\", response.choices[0].message.content)\n",
    "```\n",
    "\n",
144
    "**ExampleOutput:**\n",
145
    "```\n",
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    "Reasoning: Okay, so the user is asking how many 'r's are in the word 'strawberry'. Let me think. First, I need to make sure I have the word spelled correctly. Strawberry... S-T-R-A-W-B-E-R-R-Y. Wait, is that right? Let me break it down.\n",
    "\n",
    "Starting with 'strawberry', let's write out the letters one by one. S, T, R, A, W, B, E, R, R, Y. Hmm, wait, that's 10 letters. Let me check again. S (1), T (2), R (3), A (4), W (5), B (6), E (7), R (8), R (9), Y (10). So the letters are S-T-R-A-W-B-E-R-R-Y. \n",
    "...\n",
    "Therefore, the answer should be three R's in 'strawberry'. But I need to make sure I'm not counting any other letters as R. Let me check again. S, T, R, A, W, B, E, R, R, Y. No other R's. So three in total. Yeah, that seems right.\n",
    "\n",
    "----------------------------------------------------------------------------------------------------\n",
    "Answer: The word \"strawberry\" contains **three** letters 'r'. Here's the breakdown:\n",
    "\n",
    "1. **S-T-R-A-W-B-E-R-R-Y**  \n",
    "   - The **third letter** is 'R'.  \n",
    "   - The **eighth and ninth letters** are also 'R's.  \n",
    "\n",
    "Thus, the total count is **3**.  \n",
    "\n",
    "**Answer:** 3.\n",
162
163
164
165
166
    "```\n",
    "\n",
    "**Note:** Setting `\"enable_thinking\": False` (or omitting it) will result in `reasoning_content` being `None`. Qwen3-Thinking models always generate reasoning content and don't support the `enable_thinking` parameter.\n"
   ]
  },
ybyang's avatar
ybyang committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Logit Bias Support\n",
    "\n",
    "SGLang supports the `logit_bias` parameter for both chat completions and completions APIs. This parameter allows you to modify the likelihood of specific tokens being generated by adding bias values to their logits. The bias values can range from -100 to 100, where:\n",
    "\n",
    "- **Positive values** (0 to 100) increase the likelihood of the token being selected\n",
    "- **Negative values** (-100 to 0) decrease the likelihood of the token being selected\n",
    "- **-100** effectively prevents the token from being generated\n",
    "\n",
    "The `logit_bias` parameter accepts a dictionary where keys are token IDs (as strings) and values are the bias amounts (as floats).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Getting Token IDs\n",
    "\n",
    "To use `logit_bias` effectively, you need to know the token IDs for the words you want to bias. Here's how to get token IDs:\n",
    "\n",
    "```python\n",
    "# Get tokenizer to find token IDs\n",
    "import tiktoken\n",
    "\n",
    "# For OpenAI models, use the appropriate encoding\n",
    "tokenizer = tiktoken.encoding_for_model(\"gpt-3.5-turbo\")  # or your model\n",
    "\n",
    "# Get token IDs for specific words\n",
    "word = \"sunny\"\n",
    "token_ids = tokenizer.encode(word)\n",
    "print(f\"Token IDs for '{word}': {token_ids}\")\n",
    "\n",
    "# For SGLang models, you can access the tokenizer through the client\n",
    "# and get token IDs for bias\n",
    "```\n",
    "\n",
    "**Important:** The `logit_bias` parameter uses token IDs as string keys, not the actual words.\n"
   ]
  },
209
210
211
212
213
214
215
216
217
218
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Example: DeepSeek-V3 Models\n",
    "\n",
    "DeepSeek-V3 models support thinking mode through the `thinking` parameter:\n",
    "\n",
    "```python\n",
    "# Launch server:\n",
219
    "# python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3.1 --tp 8  --reasoning-parser deepseek-v3\n",
220
221
222
223
224
    "\n",
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    api_key=\"EMPTY\",\n",
225
    "    base_url=f\"http://127.0.0.1:30000/v1\",\n",
226
227
    ")\n",
    "\n",
228
229
    "model = \"deepseek-ai/DeepSeek-V3.1\"\n",
    "messages = [{\"role\": \"user\", \"content\": \"How many r's are in 'strawberry'?\"}]\n",
230
231
232
233
234
235
236
237
238
239
240
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=model,\n",
    "    messages=messages,\n",
    "    extra_body={\n",
    "        \"chat_template_kwargs\": {\"thinking\": True},\n",
    "        \"separate_reasoning\": True\n",
    "    }\n",
    ")\n",
    "\n",
    "print(\"Reasoning:\", response.choices[0].message.reasoning_content)\n",
241
    "print(\"-\"*100)\n",
242
243
244
    "print(\"Answer:\", response.choices[0].message.content)\n",
    "```\n",
    "\n",
245
    "**Example Output:**\n",
246
    "```\n",
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    "Reasoning: First, the question is: \"How many r's are in 'strawberry'?\"\n",
    "\n",
    "I need to count the number of times the letter 'r' appears in the word \"strawberry\".\n",
    "\n",
    "Let me write out the word: S-T-R-A-W-B-E-R-R-Y.\n",
    "\n",
    "Now, I'll go through each letter and count the 'r's.\n",
    "...\n",
    "So, I have three 'r's in \"strawberry\".\n",
    "\n",
    "I should double-check. The word is spelled S-T-R-A-W-B-E-R-R-Y. The letters are at positions: 3, 8, and 9 are 'r's. Yes, that's correct.\n",
    "\n",
    "Therefore, the answer should be 3.\n",
    "----------------------------------------------------------------------------------------------------\n",
    "Answer: The word \"strawberry\" contains **3** instances of the letter \"r\". Here's a breakdown for clarity:\n",
    "\n",
    "- The word is spelled: S-T-R-A-W-B-E-R-R-Y\n",
    "- The \"r\" appears at the 3rd, 8th, and 9th positions.\n",
265
266
267
268
269
    "```\n",
    "\n",
    "**Note:** DeepSeek-V3 models use the `thinking` parameter (not `enable_thinking`) to control reasoning output.\n"
   ]
  },
ybyang's avatar
ybyang committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example with logit_bias parameter\n",
    "# Note: You need to get the actual token IDs from your tokenizer\n",
    "# For demonstration, we'll use some example token IDs\n",
    "response = client.chat.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"Complete this sentence: The weather today is\"}\n",
    "    ],\n",
    "    temperature=0.7,\n",
    "    max_tokens=20,\n",
    "    logit_bias={\n",
    "        \"12345\": 50,  # Increase likelihood of token ID 12345\n",
    "        \"67890\": -50,  # Decrease likelihood of token ID 67890\n",
    "        \"11111\": 25,  # Slightly increase likelihood of token ID 11111\n",
    "    },\n",
    ")\n",
    "\n",
    "print_highlight(f\"Response with logit bias: {response.choices[0].message.content}\")"
   ]
  },
Chayenne's avatar
Chayenne committed
296
297
298
299
300
301
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parameters\n",
    "\n",
302
    "The chat completions API accepts OpenAI Chat Completions API's parameters. Refer to [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat/create) for more details.\n",
Chayenne's avatar
Chayenne committed
303
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    "SGLang extends the standard API with the `extra_body` parameter, allowing for additional customization. One key option within `extra_body` is `chat_template_kwargs`, which can be used to pass arguments to the chat template processor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = client.chat.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"system\",\n",
    "            \"content\": \"You are a knowledgeable historian who provides concise responses.\",\n",
    "        },\n",
    "        {\"role\": \"user\", \"content\": \"Tell me about ancient Rome\"},\n",
    "        {\n",
    "            \"role\": \"assistant\",\n",
    "            \"content\": \"Ancient Rome was a civilization centered in Italy.\",\n",
    "        },\n",
    "        {\"role\": \"user\", \"content\": \"What were their major achievements?\"},\n",
    "    ],\n",
    "    temperature=0.3,  # Lower temperature for more focused responses\n",
    "    max_tokens=128,  # Reasonable length for a concise response\n",
    "    top_p=0.95,  # Slightly higher for better fluency\n",
    "    presence_penalty=0.2,  # Mild penalty to avoid repetition\n",
    "    frequency_penalty=0.2,  # Mild penalty for more natural language\n",
    "    n=1,  # Single response is usually more stable\n",
    "    seed=42,  # Keep for reproducibility\n",
    ")\n",
335
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
336
337
338
339
340
341
342
343
344
345
    "print_highlight(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Streaming mode is also supported."
   ]
  },
ybyang's avatar
ybyang committed
346
347
348
349
350
351
352
353
354
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Logit Bias Support\n",
    "\n",
    "The completions API also supports the `logit_bias` parameter with the same functionality as described in the chat completions section above.\n"
   ]
  },
Lianmin Zheng's avatar
Lianmin Zheng committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stream = client.chat.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Say this is a test\"}],\n",
    "    stream=True,\n",
    ")\n",
    "for chunk in stream:\n",
    "    if chunk.choices[0].delta.content is not None:\n",
    "        print(chunk.choices[0].delta.content, end=\"\")"
   ]
  },
ybyang's avatar
ybyang committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example with logit_bias parameter for completions API\n",
    "# Note: You need to get the actual token IDs from your tokenizer\n",
    "# For demonstration, we'll use some example token IDs\n",
    "response = client.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
    "    prompt=\"The best programming language for AI is\",\n",
    "    temperature=0.7,\n",
    "    max_tokens=20,\n",
    "    logit_bias={\n",
    "        \"12345\": 75,  # Strongly favor token ID 12345\n",
    "        \"67890\": -100,  # Completely avoid token ID 67890\n",
    "        \"11111\": -25,  # Slightly discourage token ID 11111\n",
    "    },\n",
    ")\n",
    "\n",
    "print_highlight(f\"Response with logit bias: {response.choices[0].text}\")"
   ]
  },
Chayenne's avatar
Chayenne committed
395
396
397
398
399
400
401
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Completions\n",
    "\n",
    "### Usage\n",
402
    "Completions API is similar to Chat Completions API, but without the `messages` parameter or chat templates."
Chayenne's avatar
Chayenne committed
403
404
405
406
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
407
   "execution_count": null,
408
   "metadata": {},
Chayenne's avatar
Chayenne committed
409
   "outputs": [],
Chayenne's avatar
Chayenne committed
410
411
   "source": [
    "response = client.completions.create(\n",
412
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
Chayenne's avatar
Chayenne committed
413
414
415
416
417
418
    "    prompt=\"List 3 countries and their capitals.\",\n",
    "    temperature=0,\n",
    "    max_tokens=64,\n",
    "    n=1,\n",
    "    stop=None,\n",
    ")\n",
419
420
    "\n",
    "print_highlight(f\"Response: {response}\")"
Chayenne's avatar
Chayenne committed
421
422
423
424
425
426
427
428
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parameters\n",
    "\n",
429
    "The completions API accepts OpenAI Completions API's parameters.  Refer to [OpenAI Completions API](https://platform.openai.com/docs/api-reference/completions/create) for more details.\n",
Chayenne's avatar
Chayenne committed
430
431
432
433
434
435
    "\n",
    "Here is an example of a detailed completions request:"
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
436
   "execution_count": null,
437
   "metadata": {},
Chayenne's avatar
Chayenne committed
438
   "outputs": [],
Chayenne's avatar
Chayenne committed
439
440
   "source": [
    "response = client.completions.create(\n",
441
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
Chayenne's avatar
Chayenne committed
442
443
444
445
446
447
448
449
450
451
452
    "    prompt=\"Write a short story about a space explorer.\",\n",
    "    temperature=0.7,  # Moderate temperature for creative writing\n",
    "    max_tokens=150,  # Longer response for a story\n",
    "    top_p=0.9,  # Balanced diversity in word choice\n",
    "    stop=[\"\\n\\n\", \"THE END\"],  # Multiple stop sequences\n",
    "    presence_penalty=0.3,  # Encourage novel elements\n",
    "    frequency_penalty=0.3,  # Reduce repetitive phrases\n",
    "    n=1,  # Generate one completion\n",
    "    seed=123,  # For reproducible results\n",
    ")\n",
    "\n",
453
    "print_highlight(f\"Response: {response}\")"
Chayenne's avatar
Chayenne committed
454
455
   ]
  },
Lianmin Zheng's avatar
Lianmin Zheng committed
456
457
458
459
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
460
    "## Structured Outputs (JSON, Regex, EBNF)\n",
461
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
462
    "For OpenAI compatible structured outputs API, refer to [Structured Outputs](../advanced_features/structured_outputs.ipynb) for more details.\n"
463
464
   ]
  },
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using LoRA Adapters\n",
    "\n",
    "SGLang supports LoRA (Low-Rank Adaptation) adapters with OpenAI-compatible APIs. You can specify which adapter to use directly in the `model` parameter using the `base-model:adapter-name` syntax.\n",
    "\n",
    "**Server Setup:**\n",
    "```bash\n",
    "python -m sglang.launch_server \\\n",
    "    --model-path qwen/qwen2.5-0.5b-instruct \\\n",
    "    --enable-lora \\\n",
    "    --lora-paths adapter_a=/path/to/adapter_a adapter_b=/path/to/adapter_b\n",
    "```\n",
    "\n",
    "For more details on LoRA serving configuration, see the [LoRA documentation](../advanced_features/lora.ipynb).\n",
    "\n",
    "**API Call:**\n",
    "\n",
    "(Recommended) Use the `model:adapter` syntax to specify which adapter to use:\n",
    "```python\n",
    "response = client.chat.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct:adapter_a\",  # ← base-model:adapter-name\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Convert to SQL: show all users\"}],\n",
    "    max_tokens=50,\n",
    ")\n",
    "```\n",
    "\n",
    "**Backward Compatible: Using `extra_body`**\n",
    "\n",
    "The old `extra_body` method is still supported for backward compatibility:\n",
    "```python\n",
    "# Backward compatible method\n",
    "response = client.chat.completions.create(\n",
    "    model=\"qwen/qwen2.5-0.5b-instruct\",\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Convert to SQL: show all users\"}],\n",
    "    extra_body={\"lora_path\": \"adapter_a\"},  # ← old method\n",
    "    max_tokens=50,\n",
    ")\n",
    "```\n",
    "**Note:** When both `model:adapter` and `extra_body[\"lora_path\"]` are specified, the `model:adapter` syntax takes precedence."
   ]
  },
Chayenne's avatar
Chayenne committed
509
510
  {
   "cell_type": "code",
511
512
   "execution_count": null,
   "metadata": {},
Lianmin Zheng's avatar
Lianmin Zheng committed
513
   "outputs": [],
Chayenne's avatar
Chayenne committed
514
   "source": [
515
    "terminate_process(server_process)"
Chayenne's avatar
Chayenne committed
516
517
518
519
   ]
  }
 ],
 "metadata": {
Chayenne's avatar
Chayenne committed
520
521
522
523
524
525
526
527
528
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
529
   "pygments_lexer": "ipython3"
Chayenne's avatar
Chayenne committed
530
531
532
533
534
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}