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 getpassimport osif 'OPENAI_API_KEY' not in os.environ: os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')
import pixeltable as pxtfrom pixeltable.functions.openai import chat_completions# Create a fresh directorypxt.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>
# Add Spanish translation columnspanish_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 columnfrench_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 contentsample_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)