I am using GPT to translate some texts, with the python API.
I am providing an input text in json format like this:
{
"id": "id1",
"text": "text1"
},
{
"id": "id2",
"text": "text2"
}
I am asking GPT to translate every text
key of the objects, and to answer in a json format, using a response_format
.
My input data is quite big up to 50k tokens. So I am using tiktoken
to split my input data into 4096 tokens chunks.
I don’t understand why, but very often, GPT only partially answers to my request.
This is the CompletionUsage
details when it does work:
CompletionUsage(completion_tokens = 4077, prompt_tokens = 4173, total_tokens = 8250, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens = 0, audio_tokens = 0, reasoning_tokens = 0, rejected_prediction_tokens = 0), prompt_tokens_details=PromptTokensDetails(audio_tokens = 0, cached_tokens = 3968))
This is the CompletionUsage
details when it doesn’t work:
CompletionUsage(completion_tokens = 2178, prompt_tokens = 4147, total_tokens = 6325, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens = 0, audio_tokens = 0, reasoning_tokens = 0, rejected_prediction_tokens = 0), prompt_tokens_details=PromptTokensDetails(audio_tokens = 0, cached_tokens = 3968))
The finish_reason
is indeed always stop
This is a sample code (I avoid providing the response_format
as it very wordy):
client = OpenAI()
client.chat.completions.create (
model="gpt-4o-mini",
messages=[
{
"role": "developer",
"content": "You are a professional translator in italian."
"You will be provided a json as input, in which you have to translate EVERY 'text' keys of the objects, and only these ones."
"Only answer with a json format like this: { 'id': 'Given id in the input json', 'text': 'translation1'}, { 'id': 'Given id in the input json', 'text': 'translation2' }"
},
{
"role": "user",
"content": # sending a maximum of 4096 tokens chunks
"{ 'id': 'id1', 'text': 'text1' },
{ 'id': 'id2', 'text': 'text2' }"
}
],
)
I am doing something wrong here ? Is this a prompt issue ?