GNU TeXmacsUser manualReferenceAboutEnglish
From documents to prompts and back

Language models only understand text, while a TeXmacs selection is a tree with arbitrary markup. Two strategies are used to bridge this gap:

1.Compression of markup

Compressing trees.
compress_tree (t) walks the tree:

For instance, a formula or an image becomes a node without children, while strong or section becomes a node whose single child is its text. The code is xn; it is allocated in the static tables compressed (code to key) and compressed_code (key to code), where the key consists of the original node with placeholders, a space mode recording whether the node was preceded or followed by a space in its concat, and the path of the node in the compressed tree. The same markup at the same position therefore always gets the same code during a session.

The HTML encoding.
compressed_to_html (c, mode) writes a compressed tree in a small subset of HTML:

compress_html (t, mode) combines both steps. The inverse decompress_html (s, mode) parses this subset (tolerating some inconsistent quoting produced by models), rejects identifiers which appear at a position that is not below the position where they were created (the parse stops there), and calls decompress_tree, which substitutes the stored nodes, puts the answered texts in the placeholders, and restores the spaces around the nodes from their space mode. compressed_contains_text (t) tells whether there is any text outside the codes; it is used to skip paragraphs without text.

The same encoding is used by the LanguageTool spell checker (

tools/spell/spell-lantool.scm

): lantool_correct (s, out) (

lantool.cpp

) takes the compressed HTML that was sent and the JSON reply of the server, and inserts a spell-error node for every match with replacements, skipping matches which fall inside the HTML markup or the codes.

2.Corrections and translations

ai_correct (t, lan, model, chat) and ai_translate (t, from, into, model, chat) (Scheme: cpp-ai-correct, cpp-ai-translate) work as follows:

  1. the tree is compressed with compress_html;

  2. an agent description is built:

    • for corrections, a request to correct the spelling and grammar of the text in the given language without explanations; for Albert, instructions to act as a native speaker who corrects HTML documents, preserves the tags, adds the instructions of the selected corrector agent, and puts explanations in HTML comments at the end;

    • for translations, a request to translate HTML documents without explanations, to which Albert adds the instructions of the selected translator agent;

  3. ai_chat sends the request synchronously and splits the answer into the <body> part and what surrounds it;

  4. the body is decompressed. For corrections the result is a tuple whose first element is the corrected tree and whose other elements are the decompressed comments (<!--…-->) found after the body.

The asynchronous translation of ai-translate* uses the lower level routines directly (see sessions, menus and agents).

3.LaTeX answers

Sessions use LaTeX in both directions:

The prompt

The session input is serialized by ai-serialize (

tools/ai/ai-batch.scm

): a document with a single paragraph is unwrapped, plain text is sent as UTF-8, and anything else is converted to a LaTeX snippet with UTF-8 encoding.

The request

ai_latex_command and ai_latex_request call ai_command with history enabled and with an agent description asking for an untitled LaTeX document; for Albert also without comments, with SVG pictures embedded in filecontents* environments, plus the instructions of the selected interlocutor agent.

The answer

ai_latex_output (reply, model, chat) extracts the answer with ai_output, keeps the part between \begin{document} and \end{document}, removes \maketitle, turns lstlisting into verbatim, turns literal \n sequences into newlines (un_escape_cr, which leaves macros such as \neq or \noindent alone), imports the result with the latex-snippet converter, embeds the pictures and wraps everything in (with "mode" "text" …). An answer without a document environment is returned unchanged, as a string.

Pictures in Albert answers are handled in albert_output, before the import:

After the import, embed_images replaces the file name of every image by the contents of the file (looking also in the temporary directory, with an .svg suffix), so that the session output does not depend on temporary files.

4.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".