jawohl

Crates.iojawohl
lib.rsjawohl
version0.1.0
sourcesrc
created_at2023-05-05 14:45:19.875928
updated_at2023-05-05 14:52:42.248165
descriptionComplete JSON documents from a stream of JSON tokens
homepagehttps://github.com/genauai/jawohl
repositoryhttps://github.com/genauai/jawohl
max_upload_size
id857794
size62,345
Zack Maril (zmaril)

documentation

https://genau.ai

README

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.

Commit count: 19

cargo fmt