Improving translation prompt

Hello,

I am using GPT python API with model 4.0-mini to translate texts in different languages. To improve translations, I am giving GPT some glossary rules, that can look like that:

{ "Token": "Clé", "Tool": "Outil" }

This glossary is unique for each language. The issue is that GPT remembers the glossary rules for a previous language, and uses it in his future translations.

I am providing following prompt:

You are a professional translator specializing in tech industry.
Use following global glossary rules for your translations (do not use cashed glossary rules): <glossary rule for specific language>.
Translate the following text in french.

How can I make GPT use ONLY the provided glossary rules, and nothing else ?

Hmm I think you just need a way for GPT to easily navigate between the different files you’ve supplied. Given it’s a finite rule of navigation between the different languages. You need to establish a better navigation between them for GPT

I think this might help accomplish what you’re looking for:

import json
import openai

# Load glossary per language
def load_glossary(language):
    with open("glossary.json", "r") as f:
        glossaries = json.load(f)
    return glossaries.get(language, {})

# Function to format glossary as a prompt
def format_glossary(glossary):
    return "\n".join([f"{k}: {v}" for k, v in glossary.items()])

# Function to get translation
def translate_text(text, language):
    glossary = load_glossary(language)
    formatted_glossary = format_glossary(glossary)

    messages = [
        {"role": "system", "content": "You are a professional translator specializing in the tech industry."},
        {"role": "system", "content": f"Use ONLY the following glossary rules:\n{formatted_glossary}"},
        {"role": "user", "content": f"Translate the following text to {language}:\n{text}"}
    ]

    response = openai.ChatCompletion.create(
        model="gpt-4-turbo",
        messages=messages,
        temperature=0
    )

    return response["choices"][0]["message"]["content"]

# Example usage
translated_text = translate_text("The tool is broken.", "French")
print(translated_text)

Implement a Middleware Python Script

A script could dynamically inject the glossary into each request to avoid GPT carrying over previous glossary memory. This script could:

• Parse the language requested.

• Extract the corresponding glossary rules from a stored file.

• Format and inject them explicitly into the GPT API call.

1 Like

the code you provided is very similar to the code I actually use. The main difference is that you splited the global instruction into two system message. Do you think this actually will have an effect ?

You are using the OpenAI-provided Python library module for calling the chat completions API for interacting with an AI model, in this case gpt-4o-mini.

The first thing to add is rules to not damage novel AI model names!

The issue is that you are giving the AI a chatbot memory when you have individual tasks to do.

You could build a nice user interface for this, allowing selection of specific dictionary supplements to include like you show. Then a box for the input, a selection for the output languages you wish to receive.

All would then constitute a brand new request, a brand new list of messages when you press “submit”, with no additional memory or expense of re-sending what you’ve been previously translating, even in other languages.

1 Like

For each translation, no matter the language, I am already sending a new request, i.e, creating a new client.chat.completions.create. But even this way, GPT uses glossaries that I provided in previous chats.

Am I missing something here ?

There is no server-side memory between chat completions API calls.

The only way it could truly make use of “previous chats” is if you are still sending them, such as not resetting a chatbot session but instead continuing to grow a messages list that you yourself are re-sending.

Set your messages_list_variable = [] or glossary_message = None after you receive your result, as a start to making sure you are not appending and appending within a loop.

1 Like