FranklinChen / chat-py-demo

Demo of CHAT text and AST processing through Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Demo of CHAT text and AST processing through Python

This is a demo of creating a JSON representation of a CHAT document according to a JSON Schema, passing it to a Rust library that deserializes the JSON into a Rust AST, then serializes the AST to CHAT text, which is then returned back to Python.

The JSON Schema is generated from the annotated Rust AST code by means of the schemars crate.

Then a Python pydantic v2 module is generated from that JSON Schema by means of datamodel-code-generator.

Set up Rust

Make sure the standard toolchain for Rust is installed.

Run the Rust tests to verify that the pure-Rust part of the library works:

$ cargo test

Set up Python

First, set up a Python virtual environment. I used Python 3.12, but slightly older versions may also work.

$ python3.12 -m venv .venv
$ source .venv/bin/activate

Install maturin and some other development tools:

$ pip install maturin
$ pip install datamodel-code-generator
$ pip install pytest

Generate the JSON Schema and the Pydantic module before proceeding further:

$ make clean all

Build the chat_py_demo Python module (which also compiles Rust):

$ maturin develop

Run Python tests to verify that things work:

$ pytest python/tests

About

Demo of CHAT text and AST processing through Python


Languages

Language:Rust 54.1%Language:Python 29.2%Language:Makefile 13.2%Language:Lex 3.5%