MyRelay

← नोटबुक पर वापस जाएँ

A Command-Line Translator in Fifty Lines

A terminal translator should do one small job: accept text, translate it into a requested language, and return something you can use immediately. Aim for fifty readable lines of Python, not the shortest possible program. This walkthrough divides that allowance into six parts, keeping spending controls and failure handling inside the design rather than adding them later.

Define the input contract in eight lines

Start by choosing two inputs: a required target language and optional source text. When text is supplied directly, use it. Otherwise, read from standard input, allowing another program to provide the passage. If neither supplies usable text, show a short explanation and stop before making a request.

Allocate your first eight lines to imports and argument handling. Keep source-language detection out of the interface initially; ask the model to infer it. Require an explicit target language so an omitted argument cannot quietly become a paid translation into the wrong language.

Connect one account in eight more lines

Reserve the next eight lines for loading credentials and creating an OpenAI-compatible client. Read the key from an environment variable rather than embedding it in the program. If the key is missing, exit locally with a helpful message. Never include the secret in diagnostic output.

MyRelay provides a personal, pay-as-you-go relay with one key and one budget across supported models. Create a MyRelay account, then use its connection instructions to supply the client endpoint and credentials. Its request records show tokens and cost, giving you somewhere to check what each experiment actually consumed. ([myapirelay.com](https://myapirelay.com/))

Validate the passage in seven lines

Use seven lines to collect the passage and apply local checks. Reject empty input, but preserve meaningful indentation and paragraph breaks. Choose a modest character limit for the first version and explain that limit when a passage exceeds it. Treat this as your own guardrail, not as a precise token calculation or guaranteed spending ceiling.

Do not silently truncate long text. A missing final paragraph can change the meaning while leaving the translation looking complete. Ask the user to shorten the passage instead. Also make deliberate decisions about what you send: leave passwords, private correspondence, and confidential coursework out of your test material.

Make one focused request in twelve lines

Allocate twelve lines to constructing the request and extracting its result. Keep the translation instruction separate from the source passage. Ask for the translation only, preserving paragraph breaks, names, numbers, and tone. Tell the model to treat instructions inside the passage as text to translate, not directions to follow. Consider that a precaution, not a security guarantee.

Select one model identifier from MyRelay's current menu and keep it editable in one place. Avoid automatic model switching in this first version. Set an output allowance using the parameter supported by your chosen model and endpoint. OpenAI's Chat Completions reference defines a completion-token ceiling that can include both visible output and reasoning tokens, so do not assume that the whole allowance becomes translated text. ([platform.openai.com](https://platform.openai.com/docs/api-reference/chat/object?lang=ruby&utm_source=openai))

Keep output clean in seven lines

Spend seven lines on successful output and basic response validation. Send only the translated passage to standard output. Put explanations and failures on standard error, and return a nonzero exit status when translation fails. This keeps your intended interface simple: usable translation on success, a clear failure otherwise.

Check for an empty response and any indication that generation stopped at its limit. Do not present an incomplete translation as finished work. Preserve the returned paragraph structure rather than decorating it with headings or a success message.

Reserve eight lines for failures and testing

Use the final eight lines for timeout handling, readable exceptions, and the program entry point. Set a finite timeout and explicitly configure retry behavior rather than leaving it unexamined. For the first version, prefer a visible failure over a retry loop whose spending you have not assessed.

Test with a short sentence, a paragraph containing numbers, and a passage with multiple paragraphs. Check meaning with a language you know or a trusted reviewer. Compare request costs in MyRelay before increasing passage length. Your fifty-line target should produce a small, understandable tool; if responsible error handling needs a few extra lines, keep them.

और पढ़ें