Skip to content

Commit 55d372c

Browse files
committed
Improve docstrings and type hinting
1 parent f04d9c1 commit 55d372c

File tree

23 files changed

+171
-113
lines changed

23 files changed

+171
-113
lines changed

docs/reference/models/sglang.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# SgLang
1+
# SGLang
22

33
## Prerequisites
44

5-
The Outlines `SgLang` model is inteded to be used along with a separate sglang server (running either locally or remotely). Make sure you have an sglang server running before using the `SgLang` model. As the sglang client relies on the `openai` python sdk, you need to have the `openai` package installed.
5+
The Outlines `SGLang` model is inteded to be used along with a separate sglang server (running either locally or remotely). Make sure you have an sglang server running before using the `SGLang` model. As the sglang client relies on the `openai` python sdk, you need to have the `openai` package installed.
66

77
## Initialize the model
88

9-
To load the model, you can use the `from_sglang` function. The argument of the function is either an `OpenAI` or `AsyncOpenAI` instance from the `openai` library. Based on whether the `openai` instance is synchronous or asynchronous, you will receive a `SgLang` or `AsyncSgLang` model instance.
9+
To load the model, you can use the `from_sglang` function. The argument of the function is either an `OpenAI` or `AsyncOpenAI` instance from the `openai` library. Based on whether the `openai` instance is synchronous or asynchronous, you will receive a `SGLang` or `AsyncSGLang` model instance.
1010

1111
```python
1212
import openai
@@ -16,10 +16,10 @@ sync_openai_client = openai.OpenAI(base_url="...")
1616
async_openai_client = openai.AsyncOpenAI(base_url="...")
1717

1818
sync_model = outlines.from_sglang(sync_openai_client, "qwen/qwen2.5-0.5b-instruct")
19-
print(type(sync_model)) # <class 'outlines.models.sglang.SgLang'>
19+
print(type(sync_model)) # <class 'outlines.models.sglang.SGLang'>
2020

2121
async_model = outlines.from_sglang(async_openai_client, "qwen/qwen2.5-0.5b-instruct")
22-
print(type(async_model)) # <class 'outlines.models.sglang.AsyncSgLang'>
22+
print(type(async_model)) # <class 'outlines.models.sglang.AsyncSGLang'>
2323
```
2424

2525
## Generate text
@@ -36,7 +36,7 @@ answer = sync_model("Create a character.", output_type=Character)
3636
answer = await async_model("Create a character.", output_type=Character)
3737
```
3838

39-
The `SgLang` model supports also supports streaming.
39+
The `SGLang` model supports also supports streaming.
4040

4141
```python
4242
for chunk in sync_model.stream("Write a short story about a cat.", max_tokens=100):

docs_new/getting_started/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ Outlines wraps around a variety of LLM inference backends, described in the [ins
265265
response = model("Create a character.", Person) # { "name": "John", "age": 30 }
266266
```
267267

268-
=== "SgLang"
268+
=== "SGLang"
269269

270270
```python
271-
# SgLang
271+
# SGLang
272272

273273
import outlines
274274
from openai import OpenAI
@@ -280,8 +280,8 @@ Outlines wraps around a variety of LLM inference backends, described in the [ins
280280
name: str
281281
age: int
282282

283-
# You must have a separete SgLang server running
284-
# Create an OpenAI client with the base URL of the SgLang server
283+
# You must have a separete SGLang server running
284+
# Create an OpenAI client with the base URL of the SGLang server
285285
openai_client = OpenAI(base_url="http://localhost:11434/v1")
286286

287287
# Create an Outlines model

docs_new/reference/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Reference
2+
3+
This needs to contain a list of all the misc API reference pages.

outlines/applications.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
"""Encapsulate a prompt template and an output type into a reusable object."""
2+
13
from typing import Any, Callable, Dict, Optional, Union
24

3-
from outlines.generator import BlackBoxGenerator, Generator, SteerableGenerator
5+
from outlines.generator import (
6+
BlackBoxGenerator,
7+
Generator,
8+
SteerableGenerator,
9+
AsyncBlackBoxGenerator,
10+
)
11+
from outlines.models.base import Model
412
from outlines.templates import Template
5-
from outlines.models import BlackBoxModel, SteerableModel
613

714

815
class Application:
@@ -32,8 +39,8 @@ class OutputModel(BaseModel):
3239
result: int
3340
3441
model = models.from_transformers(
35-
AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"),
36-
AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
42+
AutoModelForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct"),
43+
AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
3744
)
3845
3946
template_string = "What is 2 times {{ num }}?"
@@ -44,19 +51,49 @@ class OutputModel(BaseModel):
4451
result = application(model, {"num": 3}, max_new_tokens=20)
4552
print(result) # Expected output: { "result" : 6 }
4653
```
54+
4755
"""
48-
def __init__(self, template: Union[Template, Callable], output_type: Any):
56+
def __init__(
57+
self,
58+
template: Union[Template, Callable],
59+
output_type: Optional[Any] = None,
60+
):
61+
"""
62+
Parameters
63+
----------
64+
template
65+
The template to use to build the prompt.
66+
output_type
67+
The output type provided to the generator.
68+
"""
4969
self.template = template
5070
self.output_type = output_type
51-
self.model: Optional[Union[BlackBoxModel, SteerableModel]] = None
52-
self.generator: Optional[Union[BlackBoxGenerator, SteerableGenerator]] = None
71+
self.generator: Optional[Union[
72+
BlackBoxGenerator, SteerableGenerator, AsyncBlackBoxGenerator
73+
]] = None
74+
self.model: Optional[Model] = None
5375

5476
def __call__(
5577
self,
56-
model: Union[BlackBoxModel, SteerableModel],
78+
model: Model,
5779
template_vars: Dict[str, Any],
5880
**inference_kwargs
59-
):
81+
) -> Any:
82+
"""
83+
Parameters
84+
----------
85+
model
86+
The model to use to generate the response.
87+
template_vars
88+
The variables to be substituted in the template.
89+
**inference_kwargs
90+
Additional keyword arguments to pass to the model.
91+
92+
Returns
93+
-------
94+
Any
95+
The generated response.
96+
"""
6097
if model is None:
6198
raise ValueError("you must provide a model")
6299
# We save the generator to avoid creating a new one for each call.

outlines/caching.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Caching and memoization of function calls."""
2+
13
import asyncio
24
import contextlib
35
import functools

outlines/fsm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Finite state machines builder for CFG generation."""

outlines/fsm/parsing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Build a parser from a grammar to create a finite state machine."""
2+
13
from copy import copy, deepcopy
24
from dataclasses import dataclass
35
from functools import lru_cache

outlines/generator.py

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
AsyncBlackBoxModel,
1313
BlackBoxModel,
1414
SteerableModel,
15+
SyncBlackBoxModel,
1516
)
1617
from outlines.models.base import AsyncModel, Model
1718
from outlines.processors import (
@@ -28,34 +29,27 @@
2829
class BlackBoxGenerator:
2930
"""Synchronous generator for which we don't control constrained
3031
generation.
31-
32-
The output type provided is not compiled into a logits processor, but is
33-
instead directly passed on to the model.
34-
3532
"""
3633
output_type: Optional[Any]
3734

38-
def __init__(self, model: BlackBoxModel, output_type: Optional[Any]):
35+
def __init__(self, model: SyncBlackBoxModel, output_type: Optional[Any]):
3936
"""
4037
Parameters
4138
----------
4239
model
4340
An instance of an Outlines model.
4441
output_type
45-
The output type that will be used to constrain the generation.
46-
42+
The output type expressed as a Python type
4743
"""
4844
self.model = model
4945
self.output_type = output_type
50-
5146
if isinstance(self.output_type, FSM):
5247
raise NotImplementedError(
5348
"FSM generation is not supported for API-based models"
5449
)
5550

5651
def __call__(self, prompt: Any, **inference_kwargs) -> Any:
57-
"""Generate a response from the model.
58-
52+
"""
5953
Parameters
6054
----------
6155
prompt
@@ -67,15 +61,13 @@ def __call__(self, prompt: Any, **inference_kwargs) -> Any:
6761
-------
6862
Any
6963
The response generated by the model.
70-
7164
"""
7265
return self.model.generate(
7366
prompt, self.output_type, **inference_kwargs
7467
)
7568

7669
def stream(self, prompt: Any, **inference_kwargs) -> Iterator[Any]:
77-
"""Generate a stream of responses from the model.
78-
70+
"""
7971
Parameters
8072
----------
8173
prompt
@@ -87,7 +79,6 @@ def stream(self, prompt: Any, **inference_kwargs) -> Iterator[Any]:
8779
-------
8880
Any
8981
The response generated by the model.
90-
9182
"""
9283
return self.model.generate_stream(
9384
prompt, self.output_type, **inference_kwargs
@@ -97,10 +88,6 @@ def stream(self, prompt: Any, **inference_kwargs) -> Iterator[Any]:
9788
class AsyncBlackBoxGenerator:
9889
"""Asynchronous generator for which we don't control constrained
9990
generation.
100-
101-
The output type provided is not compiled into a logits processor, but is
102-
instead directly passed on to the model.
103-
10491
"""
10592
output_type: Optional[Any]
10693

@@ -111,20 +98,17 @@ def __init__(self, model: AsyncBlackBoxModel, output_type: Optional[Any]):
11198
model
11299
An instance of an Outlines model.
113100
output_type
114-
The output type that will be used to constrain the generation.
115-
101+
The output type expressed as a Python type
116102
"""
117103
self.model = model
118104
self.output_type = output_type
119-
120105
if isinstance(self.output_type, FSM):
121106
raise NotImplementedError(
122107
"FSM generation is not supported for API-based models"
123108
)
124109

125110
async def __call__(self, prompt: Any, **inference_kwargs) -> Any:
126-
"""Generate a response from the model.
127-
111+
"""
128112
Parameters
129113
----------
130114
prompt
@@ -136,7 +120,6 @@ async def __call__(self, prompt: Any, **inference_kwargs) -> Any:
136120
-------
137121
Any
138122
The response generated by the model.
139-
140123
"""
141124
return await self.model.generate(
142125
prompt, self.output_type, **inference_kwargs
@@ -145,8 +128,7 @@ async def __call__(self, prompt: Any, **inference_kwargs) -> Any:
145128
async def stream(
146129
self, prompt: Any, **inference_kwargs
147130
) -> AsyncIterator[Any]:
148-
"""Generate a stream of responses from the model.
149-
131+
"""
150132
Parameters
151133
----------
152134
prompt
@@ -158,7 +140,6 @@ async def stream(
158140
-------
159141
Any
160142
The response generated by the model.
161-
162143
"""
163144
async for chunk in self.model.generate_stream( # pragma: no cover
164145
prompt, self.output_type, **inference_kwargs
@@ -193,7 +174,6 @@ def __init__(self, model: SteerableModel, output_type: Optional[Any]):
193174
An instance of an Outlines model.
194175
output_type
195176
The output type expressed as a Python type
196-
197177
"""
198178
self.model = model
199179
if output_type is None:
@@ -227,15 +207,13 @@ def __init__(self, model: SteerableModel, output_type: Optional[Any]):
227207
def from_processor(
228208
cls, model: SteerableModel, processor: OutlinesLogitsProcessor
229209
):
230-
"""Create a generator from a logits processor.
231-
210+
"""
232211
Parameters
233212
----------
234213
model
235214
An instance of an Outlines model.
236215
processor
237216
An instance of an OutlinesLogitsProcessor.
238-
239217
"""
240218
if not isinstance(processor, OutlinesLogitsProcessor):
241219
raise TypeError(
@@ -249,8 +227,7 @@ def from_processor(
249227
return instance
250228

251229
def __call__(self, prompt: Any, **inference_kwargs) -> Any:
252-
"""Generate a response from the model.
253-
230+
"""
254231
Parameters
255232
----------
256233
prompt
@@ -262,15 +239,13 @@ def __call__(self, prompt: Any, **inference_kwargs) -> Any:
262239
-------
263240
Any
264241
The response generated by the model.
265-
266242
"""
267243
return self.model.generate(
268244
prompt, self.logits_processor, **inference_kwargs
269245
)
270246

271247
def stream(self, prompt: Any, **inference_kwargs) -> Iterator[Any]:
272-
"""Generate a stream of responses from the model.
273-
248+
"""
274249
Parameters
275250
----------
276251
prompt
@@ -282,7 +257,6 @@ def stream(self, prompt: Any, **inference_kwargs) -> Iterator[Any]:
282257
-------
283258
Any
284259
The response generated by the model.
285-
286260
"""
287261
return self.model.generate_stream(
288262
prompt, self.logits_processor, **inference_kwargs
@@ -295,7 +269,8 @@ def Generator(
295269
*,
296270
processor: Optional[OutlinesLogitsProcessor] = None,
297271
) -> Union[SteerableGenerator, BlackBoxGenerator, AsyncBlackBoxGenerator]:
298-
"""Create a generator for the given model and output parameters.
272+
"""
273+
Create a generator for the given model and output parameters.
299274
300275
The 2 parameters output_type and processor are mutually exclusive. The
301276
parameters processor is only supported for SteerableModel instances
@@ -315,7 +290,6 @@ def Generator(
315290
-------
316291
Union[SteerableGenerator, BlackBoxGenerator, AsyncBlackBoxGenerator]
317292
A generator instance.
318-
319293
"""
320294
provided_output_params = sum(
321295
param is not None
@@ -338,10 +312,10 @@ def Generator(
338312
)
339313
if isinstance(model, AsyncBlackBoxModel): # type: ignore
340314
return AsyncBlackBoxGenerator(model, output_type) # type: ignore
341-
elif isinstance(model, BlackBoxModel): # type: ignore
315+
elif isinstance(model, SyncBlackBoxModel): # type: ignore
342316
return BlackBoxGenerator(model, output_type) # type: ignore
343317
else:
344318
raise ValueError(
345319
"The model argument must be an instance of "
346-
"SteerableModel, BlackBoxModel or AsyncBlackBoxModel"
320+
"SteerableModel or BlackBoxModel"
347321
)

0 commit comments

Comments
 (0)