Skip to content

Commit 3f524af

Browse files
committed
Update README
1 parent 03e5034 commit 3f524af

File tree

2 files changed

+123
-33
lines changed

2 files changed

+123
-33
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55
description = "Structured Generation"
66
license = "Apache-2.0"
77
repository = "https://github.com/dottxt-ai/outlines-core"
8+
rust-version = "1.71.1"
89

910
[dependencies]
1011
once_cell = "1.20"

README.md

Lines changed: 122 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,170 @@
22

33
<img src="./docs/assets/images/logo.png" alt="Outlines-core Logo" width=500></img>
44

5-
[![Contributors][contributors-badge]][contributors]
5+
[![Latest Version]][crates.io] [![License]][github] ![MSRV]
6+
7+
[Latest Version]: https://img.shields.io/crates/v/outlines-core.svg
8+
[crates.io]: https://crates.io/crates/outlines-core
9+
[License]: https://img.shields.io/github/license/dottxt-ai/outlines-core.svg?color=blue&cachedrop
10+
[github]: https://github.com/dottxt-ai/outlines-core/blob/main/LICENSE
11+
[MSRV]: https://img.shields.io/badge/MSRV-1.71.1-brightgreen
12+
13+
<!---
14+
Once it uploaded to crates.io badge could be generated like:
15+
[version]: https://img.shields.io/crates/msrv/outlines-core.svg?label=msrv&color=lightgrayy
16+
-->
617

718
*Structured generation (in Rust).*
19+
820
</div>
921

10-
This package provides the core functionality for structured generation, formerly implemented in [Outlines][outlines], with a focus on performance and portability.
22+
## Outlines-core
1123

12-
# Install
24+
This package provides the core functionality for structured generation, formerly implemented in [Outlines][outlines],
25+
with a focus on performance and portability, it offers a convenient way to:
1326

14-
We provide bindings to the following languages:
15-
- [Rust][rust-implementation] (Original implementation)
16-
- [Python][python-bindings]
27+
- build regular expressions from JSON schemas
1728

18-
The latest release of the Python bindings is available on PyPi using `pip`:
29+
- construct an `Index` object by combining a `Vocabulary` and regular expression to efficiently map tokens from a given vocabulary to state transitions in a finite-state automation
1930

20-
``` python
21-
pip install outlines-core
22-
```
31+
### Example
2332

24-
The current development branch of `outlines-core` can be installed from GitHub, also using `pip`:
33+
Basic example of how it all fits together.
2534

26-
``` shell
27-
pip install git+https://github.com/outlines-dev/outlines-core
28-
```
35+
```rust
36+
use outlines_core::prelude::*;
2937

30-
Or install in a rust project with cargo:
31-
``` bash
32-
cargo add outlines-core
38+
// Define a JSON schema
39+
let schema = r#"{
40+
"type": "object",
41+
"properties": {
42+
"name": { "type": "string" },
43+
"age": { "type": "integer" }
44+
},
45+
"required": ["name", "age"]
46+
}"#;
47+
48+
// Generate a regular expression from it
49+
let regex = json_schema::regex_from_str(&schema, None)?;
50+
51+
// Create `Vocabulary` from pretrained large language model (but manually is also possible)
52+
let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", None)?;
53+
54+
// Create new `Index` from regex and a given `Vocabulary`
55+
let index = Index::new(&regex, &vocabulary)?;
56+
57+
let initial_state = index.initial_state();
58+
let allowed_tokens = index.allowed_tokens(&initial_state).expect("Some allowed token ids");
59+
let token_id = allowed_tokens.first().expect("First token id");
60+
let next_state = index.next_state(&initial_state, token_id);
61+
let final_states = index.final_states();
3362
```
3463

35-
**Note:** The Minimum Supported Rust Version (MSRV) for this project is 1.78.0.
64+
## Python Bindings
65+
66+
Additionally, project provides interfaces to integrate the crate's functionality with Python.
67+
68+
``` python
69+
import json
70+
71+
from outlines_core.json_schema import build_regex_from_schema
72+
from outlines_core.guide import Guide, Index, Vocabulary
73+
74+
schema = {
75+
"title": "Foo",
76+
"type": "object",
77+
"properties": {"date": {"type": "string", "format": "date"}}
78+
}
79+
regex = build_regex_from_schema(json.dumps(schema))
80+
81+
vocabulary = Vocabulary.from_pretrained("openai-community/gpt2")
82+
index = Index(regex, vocabulary)
83+
guide = Guide(index)
84+
85+
# Get current state of the Guide:
86+
current_state = guide.get_state()
87+
88+
# Get allowed tokens for the current state of the Guide:
89+
allowed_tokens = guide.get_tokens()
90+
91+
# Advance Guide to the next state via some token_id and return allowed tokens for that new state:
92+
next_allowed_tokens = guide.advance(allowed_tokens[-1])
93+
94+
# To check if Guide is finished:
95+
guide.is_finished()
96+
97+
# If it's finished then this assertion holds:
98+
assert guide.get_tokens() == [vocabulary.get_eos_token_id()]
99+
```
36100

37-
# How to contribute?
101+
## How to contribute?
38102

39-
## Setup
103+
### Setup
40104

41-
First, fork the repository on GitHub and clone the fork locally:
105+
Fork the repository on GitHub and clone the fork locally:
42106

43107
```bash
44108
git clone git@github.com/YourUserName/outlines-core.git
45109
cd outlines-core
46110
```
47111

48-
Create a new virtual environment:
112+
Create a new virtual environment and install the dependencies in editable mode:
49113

50114
``` bash
51115
python -m venv .venv
52116
source .venv/bin/activate
117+
pip install -e ".[test]"
118+
pre-commit install
53119
```
54120

55-
Then install the dependencies in editable mode, and install the pre-commit hooks:
121+
### Before pushing your code
122+
123+
If working with Python bindings don't forget to build Rust extension before testing, for example, in debug mode:
124+
125+
```bash
126+
make build-extension-debug
127+
```
128+
129+
Run Python tests:
56130

57131
``` bash
58-
pip install -e ".[test]"
59-
pre-commit install
132+
pytest
60133
```
61134

62-
## Before pushing your code
135+
Run Rust tests:
63136

64-
Run the tests:
137+
``` bash
138+
cargo test
139+
```
65140

141+
Or alternatively using Makefile for both:
66142

67143
``` bash
68-
pytest
144+
make test
69145
```
70146

71-
And run the code style checks:
147+
Finally, run the code style checks:
72148

73149
``` bash
74150
pre-commit run --all-files
75151
```
76152

153+
Or using Makefile:
154+
155+
``` bash
156+
make pcc
157+
```
158+
159+
If necessary you can run benchmarks locally:
160+
161+
``` bash
162+
make pybench
163+
```
164+
165+
## Join us
166+
167+
- 💡 **Have an idea?** Come chat with us on [Discord][discord]
168+
- **Found a bug?** Open an [issue](https://github.com/dottxt-ai/outlines-core/issues)
77169

78170
[outlines]: https://github.com/dottxt-ai/outlines
79-
[contributors]: https://github.com/outlines-dev/outlines-core/graphs/contributors
80-
[contributors-badge]: https://img.shields.io/github/contributors/outlines-dev/outlines-core?style=flat-square&logo=github&logoColor=white&color=ECEFF4
81-
[rust-implementation]: https://github.com/outlines-dev/outlines-core/tree/readme/src
82-
[python-bindings]: https://github.com/outlines-dev/outlines-core/tree/readme/python/outlines_core
171+
[discord]: https://discord.gg/R9DSu34mGd

0 commit comments

Comments
 (0)