Skip to main content
Open in Kaggle  Open in Colab  Download Notebook
This documentation page is also available as an interactive notebook. You can launch the notebook in Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the above links.
Automatically translate content into multiple languages using LLMs.

Problem

You have content that needs to be available in multiple languages—product descriptions, documentation, user-generated content. Manual translation is slow and expensive.

Solution

What’s in this recipe:
  • Translate text using OpenAI models
  • Create multiple language columns from one source
  • Handle batch translation efficiently
You add computed columns for each target language. Translations are generated automatically when you insert new content and cached for future queries.

Setup

%pip install -qU pixeltable openai
import os
import getpass

if 'OPENAI_API_KEY' not in os.environ:
    os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')
import pixeltable as pxt
from pixeltable.functions.openai import chat_completions
# Create a fresh directory
pxt.drop_dir('translate_demo', force=True)
pxt.create_dir('translate_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘translate_demo’.
<pixeltable.catalog.dir.Dir at 0x31f62aa90>

Create translation pipeline

# Create table for content
content = pxt.create_table(
    'translate_demo.content',
    {'title': pxt.String, 'text_en': pxt.String}
)
Created table ‘content’.
# Add Spanish translation column
spanish_prompt = 'Translate the following text to Spanish. Return only the translation, no explanations:\n\n' + content.text_en

content.add_computed_column(
    response_es=chat_completions(
        messages=[{'role': 'user', 'content': spanish_prompt}],
        model='gpt-4o-mini'
    )
)
content.add_computed_column(text_es=content.response_es.choices[0].message.content)
Added 0 column values with 0 errors.
Added 0 column values with 0 errors.
No rows affected.
# Add French translation column
french_prompt = 'Translate the following text to French. Return only the translation, no explanations:\n\n' + content.text_en

content.add_computed_column(
    response_fr=chat_completions(
        messages=[{'role': 'user', 'content': french_prompt}],
        model='gpt-4o-mini'
    )
)
content.add_computed_column(text_fr=content.response_fr.choices[0].message.content)
Added 0 column values with 0 errors.
Added 0 column values with 0 errors.
No rows affected.

Translate content

# Insert sample content
sample_content = [
    {
        'title': 'Welcome Message',
        'text_en': 'Welcome to our platform! We are excited to have you here.'
    },
    {
        'title': 'Product Description',
        'text_en': 'This lightweight laptop features a 14-inch display and all-day battery life.'
    },
    {
        'title': 'Support Article',
        'text_en': 'To reset your password, click the forgot password link on the login page.'
    },
]

content.insert(sample_content)
Inserting rows into `content`: 3 rows [00:00, 198.43 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 18 values computed.
# View all translations
content.select(content.title, content.text_en, content.text_es, content.text_fr).collect()
# Pretty print one example
row = content.where(content.title == 'Welcome Message').collect()[0]

Explanation

How it works: Each target language is a computed column with a translation prompt. When you insert new content: 1. The English text is processed 2. Translation prompts are generated for each language 3. All translations run in parallel 4. Results are cached—no re-translation needed Adding more languages:
# Add German translation
german_prompt = 'Translate to German:\n\n' + content.text_en
content.add_computed_column(
    response_de=chat_completions(messages=[{'role': 'user', 'content': german_prompt}], model='gpt-4o-mini')
)
content.add_computed_column(text_de=content.response_de.choices[0].message.content)
Cost optimization:

See also