Skip to content

Add optional OpenAI-compatible remote translation backend - #38

Closed
e4779 wants to merge 3 commits into
LibreTranslate:mainfrom
e4779:feat/openai-backend
Closed

e4779 wants to merge 3 commits into
LibreTranslate:mainfrom
e4779:feat/openai-backend

Conversation

@e4779

@e4779 e4779 commented Nov 20, 2026

Copy link
Copy Markdown

Problem

LTEngine always runs its model in-process. Issue #3 asks for external model support: run the engine without the GGUF on the same machine, or point one engine at a shared inference server (llama.cpp OpenAI-compatible server, vLLM, ollama).

Fix

Opt-in remote mode, selected by three new flags:

  • --openai-base-url — endpoint root, e.g. http://127.0.0.1:8080/v1
  • --openai-api-key — bearer token (optional)
  • --openai-model — model name sent to the endpoint (required with base-url)

Without the flags nothing changes: the local GGUF is loaded and used exactly as before. With them no GGUF is loaded; each /translate request becomes one non-streaming chat completion. The direction-aware prompt from prompt.rs is reused unchanged, and the reply content is returned verbatim so existing post-processing applies.

Closes #3

Testing

cargo check, cargo clippy, cargo test pass (clippy reports 20 warnings, all pre-existing on upstream main; the new code is clean).

Live test against a llama.cpp server with a reasoning-enabled model:

$ curl -s localhost:5050/translate -d '{"q":"Она грустно улыбается, глядя на дождь за окном.","source":"auto","target":"en"}'
{"detectedLanguage":{"confidence":46,"language":"ru"},"translatedText":"She smiles sadly, looking at the rain outside the window."}
  • --openai-base-url without --openai-model -> startup error, exit 1
  • unreachable endpoint -> HTTP 503 {"error":"Translation endpoint returned an error: ..."}, server keeps running

Notes for review

  • The remote path does not take prompt_lock (llm.rs): that mutex serializes the in-process llama.cpp backend, while a remote server owns its own concurrency. The in-process path is untouched.
  • reasoning_content (thinking models) is discarded; only message.content is used.
  • Direct code-path split instead of a trait abstraction: one caller in main.rs, no redesign of llm.rs. Can be restructured to a trait seam if a longer-term external-model API is preferred.
  • Adds async-openai 0.43; Cargo.lock updated accordingly.

e4779 and others added 3 commits November 20, 2026 00:32
New openai.rs module sends the direction-aware prompt from prompt.rs to
an external chat/completions endpoint (llama.cpp server, vLLM, OpenAI)
and returns the reply content verbatim, so improve_formatting applies
unchanged. reasoning_content is never read, so thinking models stay
clean; a trailing slash in the base URL is normalized away.

Motivation: run translations on a machine that only serves the model
remotely, instead of loading the GGUF in-process. LibreTranslate#3.

async-openai with rustls: no system OpenSSL dependency, matching the
hf-hub default TLS stack.

Co-authored-by: glm-5.3-flash <glm-5.3-flash@ai.local>
--openai-base-url / --openai-api-key / --openai-model select the remote
backend at startup; with no flags the local llama.cpp path is used and
behavior is identical to upstream main.

Startup fails fast when --openai-base-url is given without a model name:
there is no sensible default model for a foreign endpoint. In remote
mode no GGUF is loaded at all, and the prompt_lock is not taken - the
endpoint handles concurrency itself, while llama.cpp keeps serializing
requests. Remote transport failures surface as 503 like the local
Busy error, instead of a generic 500.

LibreTranslate#3.
Transitive tree of async-openai 0.43 / reqwest 0.13 (LibreTranslate#3).

Co-authored-by: glm-5.3-flash <glm-5.3-flash@ai.local>
@pierotofy pierotofy closed this Nov 20, 2026
@e4779

e4779 commented Nov 20, 2026

Copy link
Copy Markdown
Author

@pierotofy, oh, I see. I forgot to disclose the use of AI. But I use these changes in production, and I reviewed the pull request description — it looked correct and adequate.

I can edit the cover message. Or you can count this comment as the AI usage disclosure. Is that enough?

If the concern is the size of the diff: the actual code is ~200 lines (async-openai integration + request handling); the rest is Cargo.lock.

@pierotofy

Copy link
Copy Markdown
Member

I just don't like the way AI wrote it.

@e4779

e4779 commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

ok, what can we do? (Yes, of course, I can do nothing - I made this thing work for me).

Where can I find your preference? Your preferred way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

External model support

2 participants