Improve docs & Add JSON decode example (#121)
This commit is contained in:
43
README.md
43
README.md
@@ -123,19 +123,21 @@ You can implement your prompt flow in a function decorated by `sgl.function`.
|
||||
You can then invoke the function with `run` or `run_batch`.
|
||||
The system will manage the state, chat template, parallelism and batching for you.
|
||||
|
||||
The complete code for the examples below can be found at [readme_examples.py](examples/usage/readme_examples.py)
|
||||
|
||||
### Control Flow
|
||||
You can use any Python code within the function body, including control flow, nested function calls, and external libraries.
|
||||
|
||||
```python
|
||||
@sgl.function
|
||||
def control_flow(s, question):
|
||||
s += "To answer this question: " + question + ", "
|
||||
s += "I need to use a " + sgl.gen("tool", choices=["calculator", "web browser"]) + ". "
|
||||
def tool_use(s, question):
|
||||
s += "To answer this question: " + question + ". "
|
||||
s += "I need to use a " + sgl.gen("tool", choices=["calculator", "search engine"]) + ". "
|
||||
|
||||
if s["tool"] == "calculator":
|
||||
s += "The math expression is" + sgl.gen("expression")
|
||||
elif s["tool"] == "web browser":
|
||||
s += "The website url is" + sgl.gen("url")
|
||||
elif s["tool"] == "search engine":
|
||||
s += "The key word to search is" + sgl.gen("word")
|
||||
```
|
||||
|
||||
### Parallelism
|
||||
@@ -170,6 +172,8 @@ def image_qa(s, image_file, question):
|
||||
s += sgl.assistant(sgl.gen("answer", max_tokens=256)
|
||||
```
|
||||
|
||||
See also [srt_example_llava.py](examples/quick_start/srt_example_llava.py).
|
||||
|
||||
### Constrained Decoding
|
||||
Use `regex` to specify a regular expression as a decoding constraint.
|
||||
This is only supported for local models.
|
||||
@@ -185,6 +189,35 @@ def regular_expression_gen(s):
|
||||
)
|
||||
```
|
||||
|
||||
### JSON Decoding
|
||||
|
||||
```python
|
||||
character_regex = (
|
||||
r"""\{\n"""
|
||||
+ r""" "name": "[\w\d\s]{1,16}",\n"""
|
||||
+ r""" "house": "(Gryffindor|Slytherin|Ravenclaw|Hufflepuff)",\n"""
|
||||
+ r""" "blood status": "(Pure-blood|Half-blood|Muggle-born)",\n"""
|
||||
+ r""" "occupation": "(student|teacher|auror|ministry of magic|death eater|order of the phoenix)",\n"""
|
||||
+ r""" "wand": \{\n"""
|
||||
+ r""" "wood": "[\w\d\s]{1,16}",\n"""
|
||||
+ r""" "core": "[\w\d\s]{1,16}",\n"""
|
||||
+ r""" "length": [0-9]{1,2}\.[0-9]{0,2}\n"""
|
||||
+ r""" \},\n"""
|
||||
+ r""" "alive": "(Alive|Deceased)",\n"""
|
||||
+ r""" "patronus": "[\w\d\s]{1,16}",\n"""
|
||||
+ r""" "bogart": "[\w\d\s]{1,16}"\n"""
|
||||
+ r"""\}"""
|
||||
)
|
||||
|
||||
@sgl.function
|
||||
def character_gen(s, name):
|
||||
s += name + " is a character in Harry Potter. Please fill in the following information about him/her.\n"
|
||||
s += sgl.gen("json_output", max_tokens=256, regex=character_regex)
|
||||
```
|
||||
|
||||
See also [json_decode.py](examples/usage/json_decode.py).
|
||||
|
||||
|
||||
### Batching
|
||||
Use `run_batch` to run a batch of requests with continuous batching.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user