" {\"role\": \"system\", \"content\": \"You are a helpful AI assistant\"},\n",
" {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
" {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
" ],\n",
" ],\n",
" temperature=0,\n",
" temperature=0,\n",
...
@@ -184,7 +183,6 @@
...
@@ -184,7 +183,6 @@
"## Completions\n",
"## Completions\n",
"\n",
"\n",
"### Usage\n",
"### Usage\n",
"\n",
"Completions API is similar to Chat Completions API, but without the `messages` parameter or chat templates."
"Completions API is similar to Chat Completions API, but without the `messages` parameter or chat templates."
]
]
},
},
...
@@ -253,6 +251,77 @@
...
@@ -253,6 +251,77 @@
"print_highlight(f\"Response: {response}\")"
"print_highlight(f\"Response: {response}\")"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Structured decoding (JSON, Regex)\n",
"You can specify a JSON schema or a regular expression to constrain the model output. The model output will be guaranteed to follow the given constraints.\n",
The `image_data` can be a file name, a URL, or a base64 encoded string. See also `python/sglang/srt/utils.py:load_image`.
The `image_data` can be a file name, a URL, or a base64 encoded string. See also `python/sglang/srt/utils.py:load_image`.
Streaming is supported in a similar manner as [above](#streaming).
Streaming is supported in a similar manner as [above](#streaming).
### Structured decoding (JSON, Regex)
You can specify a JSON schema or a regular expression to constrain the model output. The model output will be guaranteed to follow the given constraints.
```python
importjson
importrequests
json_schema=json.dumps(
{
"type":"object",
"properties":{
"name":{"type":"string","pattern":"^[\\w]+$"},
"population":{"type":"integer"},
},
"required":["name","population"],
}
)
# JSON
response=requests.post(
"http://localhost:30000/generate",
json={
"text":"Here is the information of the capital of France in the JSON format.\n",