Skip to main content

⇋  chat-completion-md  ⌗

Convert OpenAI chat completion request to markdown and vice versa


For a couple of projects I needed to work with OpenAI chat completion requests (which format, btw, is compatible with a lot of other LLM's providers and open source solutions). I need to inspect those requests, maybe modify the content of the messages. It makes sense to convert the not-human-friendly JSON format (typically used to store these requests) into markdown files so they can be nicely visualized (highlighted with markdown treesitter in editor) and easily modified.

This lib is so simple that it barely makes sense to create a standalone package to do that. The main reason is to guarantee consistency and tested conversion across projects.

chat-completion-md is hosted on PyPI, so you can install with uv (recommended), pip, pipx...

CLI

  • Print request stored as JSON files to markdown representation to stdout
chat_completion_md path/to/json/file.json
  • Print request stored as markdown files to JSON representation to stdout
chat_completion_md path/to/md/file.md

API

from chat_completion_md import json_to_md, md_to_json

json_str = ...
md_str = json_to_md(json_str)

md_str = ...
json_str = md_to_json(md_str)

Specification

This simple library/application supports only a subset of the available parameters of the OpenAI chat completion endpoint (arguably the most important ones). Inference engine offering OpenAI compatible API (vLLM, Ollama, TGI, llama.cpp, LMStudio, ...) guarantees supports for a similar subsets of parameters.

Supported parameters

[!NOTE] To guarantee consistency, the parameters are alphabetically sorted in JSON and in the Markdown representation.

  • frequency_penalty: float - Penalizes new tokens based on their frequency in the text so far.
  • logit_bias: dict[str, int] - Modifies the likelihood of specified tokens appearing in the completion.
  • max_tokens: int - The maximum number of tokens that can be generated in the chat completion.
  • messages: list[str] - List of messages in the ChatML format.
  • model: str - ID of the model to use
  • presence_penalty: float - Penalizes new tokens based on their presence in the text so far.
  • stream: bool - If set, partial message deltas will be sent as they become available.
  • temperature: float - Sampling temperature to use, between 0 and 2.
  • top_p: float - Nucleus sampling, where the model considers the results of the tokens with top_p probability mass.

[!IMPORTANT] model and messages are the only required parameters.

Markdown representation

  • The Markdown front matter contains the parameters in YAML format except for the messages.
  • Message list is unrolled after the front matter where each message has the following format:
                     <- empty line
# {message role}     <- H1 header with message role
                     <- empty line
{message content}    <- message body spanning multiple lines
                     <- empty line
---                  <- three dashes

Conversion recipes

JSON to Markdown

  1. Parse JSON string into a dictionary.
  2. Pop the messages key from the dictionary.
  3. Convert the dictionary to YAML and add as front matter to the markdown file.
  4. Convert each message to its markdown representation and add sequentially to the markdown file.

Markdown to JSON

  1. Use regex to extract the front matter and the content after the front matter.
  2. Use YAML to parse the front matter into a dictionary.
  3. Parse the content after the front matter into a list of messages.
  4. Add messages to the dictionary and convert to JSON string.

Metadata

Release files for chat-completion-md 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chat-completion-md 0.1.0
File Size Uploaded
chat_completion_md-0.1.0.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chat-completion-md 0.1.0
File Interpreter ABI Platform
chat_completion_md-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.9 kB

Release files / chat_completion_md-0.1.0.tar.gz

Download URL chat_completion_md-0.1.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d6ab81fae6425be2049b5bb2cdc7a0c7fe3910201db113b095d3cf2c295d2e17
BLAKE2b-256 checksum
How to use checksums
7707025a7ccedf90d7f0180f56c5b33919c53417b3ac280ccee71fb3170dff31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.5.13

Release files / chat_completion_md-0.1.0-py3-none-any.whl

Download URL chat_completion_md-0.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc63bb8ac2a54cb1cd4cf9fd7c4bd6a2339021af5d0d79bf5f2e4a23bff4d9b5
BLAKE2b-256 checksum
How to use checksums
2a87c964118cbe9c0d4e154ca7a10ce6f4c964a435c3af476f094b262bbf2f78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.5.13

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page