DEV Community

shashank ms
shashank ms

Posted on

LLM for Language Translation in Language Learning Tasks

We are building a terminal-based language tutor that translates sentences and explains grammar, nuance, and usage so learners understand why a phrase works, not just what it means. If you teach languages, study independently, or localize content, this script gives you a working base you can extend into a chatbot or web app.

What you'll need

Oxlo.ai charges a flat rate per API request, so you can feed it long passages or run repeated practice drills without token costs scaling with input length. That makes it practical to iterate on prompts and test often. See https://oxlo.ai/pricing for details.

Step 1: Initialize the client and test a basic translation

I start by verifying the connection. I use Qwen 3 32B because it handles multilingual reasoning well. The snippet below sends a Spanish translation request and prints the raw response.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
    model="qwen-3-32b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Translate to Spanish: 'The meeting was postponed because of rain.'"},
    ],
)

print(response.choices[0].message.content)

If you see a coherent Spanish sentence, the endpoint is live and we can move on.

Step 2: Design the system prompt for a tutor, not a parrot

Raw translations are not useful for learning. I want structured output: the translation, vocabulary notes, a grammar tip, register context, and an alternative phrasing. I store this in a constant so I can tweak it without touching the rest of the script.

SYSTEM_PROMPT = """You are a concise language tutor.
When the user provides text and a target language, respond in this exact format:

Translation: <translated text>
Vocabulary:
- <word>: <meaning>
- <word>: <meaning>
Grammar note: <one sentence explaining a structural difference>
Register: <formal / casual / neutral>
Alternative: <one other natural way to express the idea>

Keep each section to one or two lines."""

Step 3: Wrap the call in a reusable tutor function

Now I package the call into a function that accepts the text and target language. I use Llama 3.3 70B here because it follows structured instructions reliably.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a concise language tutor.
When the user provides text and a target language, respond in this exact format:

Translation: <translated text>
Vocabulary:
- <word>: <meaning>
- <word>: <meaning>
Grammar note: <one sentence explaining a structural difference>
Register: <formal / casual / neutral>
Alternative: <one other natural way to express the idea>

Keep each section to one or two lines."""

def translate_and_learn(text: str, target_language: str) -> str:
    user_message = f"Translate to {target_language}: '{text}'"
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
    )
    return response.choices[0].message.content

if __name__ == "__main__":
    result = translate_and_learn("Can we reschedule to next Thursday?", "German")
    print(result)

Step 4: Add a conversation loop for follow-up questions

Translation is rarely a one-shot task. I add a loop that keeps the thread open so the user can ask for clarification, examples, or pronunciation tips. I switch to Kimi K2.6 because it handles long context and advanced reasoning well.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a concise language tutor.
When the user provides text and a target language, respond in this exact format:

Translation: <translated text>
Vocabulary:
- <word>: <meaning>
- <word>: <meaning>
Grammar note: <one sentence explaining a structural difference>
Register: <formal / casual / neutral>
Alternative: <one other natural way to express the idea>

Keep each section to one or two lines."""

def run_tutor():
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    print("Language Tutor. Enter text like 'Translate to Japanese: ...'")
    print("Type 'exit' to quit.")
    while True:
        user_input = input("> ")
        if user_input.lower() in ("exit", "quit"):
            break
        messages.append({"role": "user", "content": user_input})
        response = client.chat.completions.create(
            model="kimi-k2.6",
            messages=messages,
        )
        reply = response.choices[0].message.content
        print(reply)
        messages.append({"role": "assistant", "content": reply})

if __name__ == "__main__":
    run_tutor()

Run it

Save the final script as tutor.py and run it. Below is the complete file followed by an example session.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a concise language tutor.
When the user provides text and a target language, respond in this exact format:

Translation: <translated text>
Vocabulary:
- <word>: <meaning>
- <word>: <meaning>
Grammar note: <one sentence explaining a structural difference>
Register: <formal / casual / neutral>
Alternative: <one other natural way to express the idea>

Keep each section to one or two lines."""

def run_tutor():
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    print("Language Tutor. Enter text like 'Translate to Japanese: ...'")
    print("Type 'exit' to quit.")
    while True:
        user_input = input("> ")
        if user_input.lower() in ("exit", "quit"):
            break
        messages.append({"role": "user", "content": user_input})
        response = client.chat.completions.create(
            model="kimi-k2.6",
            messages=messages,
        )
        reply = response.choices[0].message.content
        print(reply)
        messages.append({"role": "assistant", "content": reply})

if __name__ == "__main__":
    run_tutor()

Example session:

$ python tutor.py
Language Tutor. Enter text like 'Translate to Japanese: ...'
Type 'exit' to quit.
> Translate to Japanese: "Can we reschedule to next Thursday?"
Translation: 来週の木曜日に延期してもらえますか?
Vocabulary:
- 来週 (raishuu): next week
- 木曜日 (mokuyoubi): Thursday
- 延期 (enki): postponement
Grammar note: Japanese often drops the subject when it is clear from context.
Register: Formal
Alternative: 来週の木曜日に変更できますか?

> Give me a casual version.
Translation: 来週の木曜日にずらしてもらえる?
Vocabulary:
- ずらす (zurasu): to shift, to move
Grammar note: Casual speech drops polite endings like です and ます.
Register: Casual
Alternative: 来週の木曜日でいい?

> exit

Wrap up and next steps

You now have a working language tutor that runs in your terminal and keeps conversation history. A concrete next step is to wire this into a web UI with Gradio or Streamlit so students can interact with it in a browser. Another is to pipe the output through Oxlo.ai's text-to-speech endpoint so learners can hear pronunciation after each translation.

Top comments (0)