Character limit for text output · ChatGPT/OpenAI

I am using the following code:

// # popclip GPT Grammar (t=1)
// name: GPT Grammar (t=1)
// icon: symbol:bubbles.and.sparkles.fill
// entitlements: [network]
// before: cut
// after: paste-result
// language: javascript
const axios = require("axios");
const openai = axios.default.create({
  baseURL: "https://api.openai.com/v1",
  headers: {
    Authorization: `Bearer sk-xxxxxx`,
  },
});
let prompt;
if (popclip.modifiers.option) {
  prompt = "promp1";
} else {
  prompt = "prompt2";
};
  const { data } = await openai.post("chat/completions", {
  model: "gpt-4o",
  temperature: 1.0,
  max_tokens: 1024,
  messages: [
    { role: "system", content: prompt },
    { role: "user", content: popclip.input.text },
  ],
});
const response = data.choices[0].message.content.trim();
return response;

I primarily use ChatGPT to improve texts. I have often noticed that when correcting longer texts, the rest of the message is often missing. After about 250 to 265 characters, it stops.

Do I need to adjust the code to receive the complete message? If so, how?

I think it is likely the max_tokens parameter to the API call. An openai token represents about 3/4 of a word and the limit is based on the sum of tokens in the input and output, so ~250 words output sounds about right for setting of 1024 assuming similar sized input.

Unless you actually want to limit the amount of tokens processed per request (and this you get billed for), you can just omit the max_tokens and it will default to the model’s upper limit (which is 128,000 for gpt-4o).

PopClip itself doesn’t impose any limit on extensions’ text output size, it will only be limited by available system memory and processing capability. (And FWIW PopClip has a maximum input size of 10485760 utf-16 code units, i.e. about 10 million characters)

Were you able to get it working satisfactorily?

For all those interested: This is my extension for correcting German texts.

// # popclip GPT Grammar (t=1x)
// name: GPT Grammar (t=1x)
// icon: symbol:bubbles.and.sparkles.fill
// entitlements: [network]
// after: paste-result
// language: javascript
const axios = require("axios");
const openai = axios.default.create({
  baseURL: "https://api.openai.com/v1",
  headers: {
    Authorization: `Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`,
  },
});
let prompt;
if (popclip.modifiers.option) {
  prompt = "Your only task is to transform the text message you've been given into an expressive message with countless emojis that convey the same meaning and intent. Insert emojis after keywords or key phrases to create visual appeal and emotion. Favor the use of emojis after keywords. Use emojis CREATIVELY! They ensure that the message remains clear and easy to understand. DO NOT CHANGE the core message of the text and DO NOT add new informations!\n\n:";
} 
else if (popclip.modifiers.command) {
  prompt = "You are an AI editor with an eye for detail and a deep understanding of the language, style, and grammar of the German language. Your task is to refine and improve written content. Use advanced editing techniques and correct and rewrite the text at a high level of German linguistics.\n\nProceed as follows:\n 1. Read the content carefully.\n2. Identify areas that need improvement in terms of grammar, punctuation, spelling, syntax and style. \n3. Use alternatives for word choice, sentence structure and phrasing to improve clarity, conciseness, and impact. \n3. Ensure that the tone and style of the text are coherent and appropriate for the purpose. \n4. Check the logical structure of the text, coherence. \n5. Format the text clearly and distinctly into paragraphs if this is beneficial to the readability of the text. \n6. Only submit a completely revised and edited version in which all your suggestions have been addressed. ALWAYS send me the complete, revised text.\n\n Text: ";
}
else if (popclip.modifiers.shift) {
  prompt = "Please rewrite the following text in \"Leichte Sprache\" (Easy Language). \"Leichte Sprache\" is a simplified version of German designed to be easy to understand. It uses short sentences, simple words, and avoids complex grammar.\n##Sprachregeln:\n- Es werden kurze Sätze verwendet.\n- Jeder Satz enthält nur eine Aussage\n.- Es werden nur Aktivsätze verwendet.\n- Sätze bestehen aus den Gliedern Subjekt-Verb-Objekt, z. B. Das Kind streichelt den Hund.\n- Der Konjunktiv wird vermieden.\n- Der Genitiv wird durch präpositionale Fügungen mit \„von\“ ersetzt, z. B. \„Das Haus des Lehrers\“ durch \„Das Haus von dem Lehrer\“ oder \„Das Haus vom Lehrer\“.\n- Regelwidrig sind auch Synonyme und Sonderzeichen.\n- Verneinungen werden, wenn möglich, positiv umformuliert, z. B. \„Das kostet nichts\“. zu \„Das ist umsonst\„.\n- Präzise Mengenangaben sollen durch \„viel\“ oder \„wenig\“, Jahreszahlen (wie \„Bismarck wurde 1871 zum Reichskanzler ernannt\“) durch \„vor langer Zeit\“ o. ä. ersetzt werden.\n- Leichte Sprache ist nicht Kindersprache, speziell werden die Anreden „Du“ und „Sie“ wie in der Standardsprache verwendet.\n\n##Rechtschreibregeln:\n- Bei Zusammensetzungen wird durch Bindestriche oder Halbhochpunkte (in diesem Zusammenhang auch als Mediopunkte bezeichnet)verdeutlicht, aus welchen Wörtern die Zusammensetzungen bestehen, z. B. Welt-All, Bundes-Tag oder Welt·all, Bundes·tag.\n## Regeln zum Textinhalt:\n- Abstrakte Begriffe werden vermieden; wo sie notwendig sind, werden sie durch anschauliche Beispiele oder Vergleiche erklärt.\n- Bildhafte Sprache (z. B. Rabeneltern) wird vermieden.\n- Wenn Fremdwörter oder Fachwörter vorkommen, werden sie erklärt.\nAbkürzungen werden beim ersten Vorkommen durch die ausgeschriebene Form erklärt.\n\n##Empfehlungen zu Typografie und Mediengebrauch:\n- Wörter werden nicht in durchgehenden Großbuchstaben geschrieben.\n- Kursivschrift wird nicht verwendet.\n- Texte werden übersichtlich gestaltet, z. B. steht jeder Satz in einer eigenen Zeile.\n- Texte stehen durchgehend linksbündig im Flattersatz.\n- Es werden Aufzählungspunkte verwendet.\n- Bild und Text fließen nicht ineinander.\n- Bilder helfen, einen Text besser zu verstehen.\n- Bei Präsentationen wird mehr Lesezeit pro Folie eingeplant.\n\nText: ";
}
else {prompt = "You will be working as a proofreader with a deep understanding of the German language and excellent knowledge of German grammar rules from the German Duden. Your task is to carefully analyze the text, identify all grammar errors and make the necessary corrections. Correct and rewrite the text to a high germanistic level! This includes improving clarity, coherence and readability. Pay close attention to grammar, punctuation, and word choice. Do not add translations, comments, or notes. Retain the original meaning and tone as much as possible. Correct spelling and punctuation errors. Check the text very strictly for correct tenses and other grammatical aspects. Avoid starting two consecutive sentences with the same personal pronoun, and do not insert unnecessary additions or superfluous text. Avoid using the same verbs in main and subordinate clauses. Use nested sentences only if they increase the comprehensibility of the text. Furthermore, pay particular attention to correct dates. Check the dates in context for the use of the respective article in the German language. Always send me the complete, revised text.\n\nIf you only receive one word, check the spelling and provide at least 8 alternative words, separated by the '|' character, without spaces at the beginning or end. Follow the example for separating words in alternatives: Alternative1|Alternative2|Alternative3|Alternative4|Alternative5|Alternative6|Alternative7|Alternative8. \n\nText to be checked: "
};
  const { data } = await openai.post("chat/completions", {
  model: "gpt-4o",
  temperature: 0.9,
  max_tokens: 2048,
  // top_p: 0.9,
  // frequency_penalty: 0.5,
  // presence_penalty: 0.2,
  // model: "gpt-4-turbo"
  // model: "gpt-4-0125-preview",
  messages: [
    { role: "system", content: prompt },
    { role: "user", content: popclip.input.text },
  ],
});
const response = data.choices[0].message.content.trim();
return response;

I always edited the wrong extension. :man_facepalming:t2:

I love to see how simple it can be with just a bit of JavaScript to tap into all that power.

We definitely want submenu support for extensions don’t we. Would be perfect for this.

1 Like

It would be truly fantastic if this feature were available in PopClip at some point. I often struggle to remember which modifier key triggers which function.