![]() |
AI integration | ![]() |
TeXmacs can use large language models in three ways:
as sessions: the plug-in ai declares one session type per supported engine (ChatGPT, Gemini, Ollama, Mistral and Albert), in which the user types a question and receives a typeset answer;
as editing tools: Tools\(\rightarrow\)Correct and Tools\(\rightarrow\)Translate send the selection to the engine chosen in Tools\(\rightarrow\)AI engine and replace it by the corrected or translated text, keeping the non textual markup intact;
as a clipboard format: Tools\(\rightarrow\)External AI\(\rightarrow\)Copy puts the selection on the clipboard in a compact HTML form that can be pasted into the web interface of any chatbot, and Tools\(\rightarrow\)External AI\(\rightarrow\)Paste converts the answer back.
This chapter describes how these features are implemented: how requests are built for each engine and sent over the network, how documents are turned into prompts and answers into documents, and how the user interface and the plug-in are wired together. The user level description, including how to obtain keys and install the command line tools, is the documentation of the plug-in itself (
src/plugins/ai/doc/ai.en.tm
and ai-setup.en.tm).
The integration reuses three general mechanisms which are documented elsewhere: the plug-in links behind the :cmdline and :request options (the plug-in machinery), the user database in which the AI agents are stored (the database and bibliographies) and the LaTeX import used to read answers (the LaTeX and HTML converters).
The code is organized in four layers:
The Tools menu and the preferences (
texmacs/menus/tools-menu.scm
,
preferences-widgets.scm
), the Scheme commands of
tools/ai/
, and the plug-in ai (
src/plugins/ai/progs/init-ai.scm
), which configures the sessions and their preferences.
The file
Data/Convert/AI/ai.cpp
knows the individual engines. For a prompt it builds a command tree describing the request, and for the raw reply of the engine it extracts the answer. It also implements the higher level operations (chat, correction, translation, LaTeX answers).
The file
Data/Convert/AI/compress.cpp
turns a TeXmacs tree into a small subset of HTML in which all markup that is not natural language text is replaced by short identifiers, and converts such HTML back. It is shared with the LanguageTool spell checker.
json.cpp
is a small JSON parser and printer.
A command tree is executed either as a shell command (eval_system, mostly calls of curl) or as an HTTP POST of a JSON document (http_post_json), synchronously or asynchronously. In sessions, the same commands go through the plug-in links cmdline_link_rep and request_link_rep.
A request thus flows as follows, for instance for a correction:
selection tree --compress_html--> HTML with <a id="x3">, <div id="x5">…</div> --ai_command--> command tree (eval_system "curl …") or (http_post url headers json) --ai_eval_command--> raw reply of the engine (JSON or text) --ai_output--> answer text (HTML) --decompress_html--> tree with the original markup restored |
For sessions, the prompt is serialized as a LaTeX snippet and the answer is requested as a LaTeX document, which is imported with the LaTeX converter.
Data/Convert/AI/ai.cpp
Engine dispatch, command trees, transport helpers, extraction of answers, conversation history, LaTeX answers (including TikZ and SVG pictures), chat, correction and translation.
Data/Convert/AI/compress.cpp
Compression of TeXmacs trees into text with placeholders, and the HTML encoding of compressed trees.
Data/Convert/AI/json.cpp
The JSON parser json_to_tree, the printer tree_to_json and json_get.
Data/Convert/AI/lantool.cpp
Merging the matches returned by a LanguageTool server into compressed HTML (lantool_correct), used by
tools/spell/spell-lantool.scm
.
Data/Convert/convert.hpp
The declarations of all the above, with their default arguments.
System/Files/web_files.hpp,
web_files.cpp,
Plugins/Qt/qt_http.cpp
The HTTP POST routines (Qt network classes in Qt 6 builds, curl otherwise).
System/Misc/sys_utils.cpp
Synchronous and asynchronous execution of shell commands (eval_system, async_eval_system, async_eval_pending).
System/Link/cmdline_link.cpp,
request_link.cpp
The plug-in links used by the AI sessions.
Scheme/Glue/build-glue-basic.scm
The glue routines cpp-ai-…, compress-html, decompress-html, json->tree, lantool-correct, …
src/plugins/ai/progs/init-ai.scm
The plug-in: one plugin-configure per engine, the preferences and the preferences widget, Ollama model discovery. (The build copies the plug-in to TeXmacs/plugins/ai.)
tools/ai/ai-batch.scm
Serialization of session input, the session callbacks, synchronous correction and translation, copy and paste for external chatbots.
tools/ai/ai-translate.scm
Asynchronous translation of a whole document, paragraph by paragraph.
database/ai-agents-db.scm,
ai-agents-menu.scm
The registry of AI agents.
texmacs/menus/tools-menu.scm
The Tools\(\rightarrow\)AI engine, Correct, Translate and External AI entries.