2 releases

0.1.0 May 5, 2023

#2245 in Parser implementations

Download history 6/week @ 2024-07-19 17/week @ 2024-07-26 18/week @ 2024-08-02 16/week @ 2024-08-09 10/week @ 2024-08-16 9/week @ 2024-08-23 10/week @ 2024-08-30 16/week @ 2024-09-06 15/week @ 2024-09-13 38/week @ 2024-09-20 28/week @ 2024-09-27 24/week @ 2024-10-04 34/week @ 2024-10-11 49/week @ 2024-10-18 23/week @ 2024-10-25 22/week @ 2024-11-01

131 downloads per month

MIT license

55KB
154 lines

Genau

jawohl

jawohl is a Rust library that helps you deal with JSON produced by Large Language Models (LLM's). jawohl can complete an partial JSON string by automatically adding the missing closing characters (quotes, braces, and brackets). This is particularly useful when you're dealing with streaming JSON strings from LLM's and want to display information without waiting for the inference process to finish.

Features

  • Automatically completes incomplete JSON strings
  • Handles nested objects and arrays
  • Handles escaped characters inside strings
  • Returns an error for malformed JSON input
  • soon wrappers published for Javascript and Python

Installation

Add the following line to your Cargo.toml file under the [dependencies] section:

jawohl = "0.1.0"

Usage

Here's a basic example showing how to use the JSON Completer library:

use jawohl::complete_json;

fn main() {
    let input = r#"{"key": "value", "arr": [1, 2, {"nested_key": "nested_value""#;

    assert!(complete_json(input), Ok(r#"{"key": "value", "arr": [1, 2, {"nested_key": "nested_value"}]}"#));
}

For more examples and advanced usage, refer to the examples directory. In particular, the OpenAI Streaming Parse example shows how to use jawohl to parse a stream of JSON data from OpenAI's API (using 64bit's async-openai library).

asciicast

Running Tests

To run the test suite, run the following command from the root of the repository:

cargo test

This will execute a series of tests covering various scenarios, including nested objects and arrays, escaped quotes, and malformed JSON strings.

Contributing

We welcome contributions! If you'd like to contribute, please follow these steps:

  • Fork the repository on GitHub
  • Create a new branch for your feature or bugfix
  • Commit your changes and push the branch to your fork
  • Create a pull request against the main repository

Please ensure that your code follows Rust's best practices and includes tests for any new functionality or bugfixes.

Motivaiton

jawohl was inspired by the untruncate-json library. We wanted to create a simpler version of untruncate-json that was specifically designed for JSON produced by LLM's. jawohl will not fix all partial json strings like how untruncate-json will, but it will fix the most common cases. jawohl is meant to be useful for displaying partial JSON strings from LLM's in a web browser, terminal, or other UI, where you want to display the JSON as it's being generated, without waiting for the inference process to finish and it's not that important that you have to be able to parse the JSON for each token, just for some of them. Additionally, we wanted to write it in Rust so that it could be used in other languages like Python and Javascript.

License

This project is licensed under the MIT License.

No runtime deps