I have a small issue trying to get the api to return complete sentences without the prompt response getting cut off when i set the maxTokens to something like 10. or 20 or 30. For instance I might want chatgpt to reply to a user with a few words only if they reply to it with a few words. According to chatgpt 4 the when you specify a max_tokens
parameter in your request to the GPT-3.5-turbo model (or any GPT model), the model takes this limit into account before crafting the response. The max_tokens
parameter defines the maximum length of the model’s output, including both the text you’ve provided and the text it generates in response. So this means the error must be in my code, so here is my code, can anyone see what i’m doing wrong with my code: everything works great, just can’t craft very short micro replies. async function replyToCommentWithGeneratedContent(parentAuthor, parentPermlink) {
// First, check if the script can reply to this thread
const canReply = await shouldReply(parentAuthor, parentPermlink);
if (!canReply) {
console.log(Already replied 3 times to thread ${parentAuthor}/${parentPermlink}. Skipping.
);
return; // Skip replying if the limit is reached
}
// Proceed with fetching the parent post content to generate a reply
const parentPostContent = await fetchParentPostContent(parentAuthor, parentPermlink);
if (!parentPostContent) {
console.error('Failed to fetch parent post content');
return;
}
// Clean the parent post content
const cleanedContent = cleanContent(parentPostContent);
const wordCount = cleanedContent.split(/\s+/).length; // Count words in the cleaned content
let responseLengthHint; // Determine the response length hint based on the word count
// Example of setting max_tokens based on the input comment length
let maxTokens = 60; // Default value for a moderate-length response
// Adjusting maxTokens based on the comment length
if (wordCount <= 10) {
maxTokens = 60; // Shorter response for short comments
} else if (wordCount <= 20) {
maxTokens = 66; // Longer response for longer comments
} else if (wordCount <= 50) {
maxTokens = 96; // Longer response for longer comments
} else if (wordCount <= 100) {
maxTokens = 123; // Longer response for longer comments
} else {
maxTokens = 150; // More detailed response
}
// Perform sentiment analysis on the cleaned content
const pipe = await pipeline('sentiment-analysis');
const sentimentAnalysisResult = await pipe(cleanedContent);
console.log("Sentiment Analysis Result:", sentimentAnalysisResult);
// Get the tone based on the day and include it in the prompt
const dayBasedTonePrompt = getToneBasedOnDay();
// After determining the day-based tone and performing sentiment analysis
let finalMessage = dayBasedTonePrompt; // Start with the day-based tone
// Determine the tone based on sentiment analysis & Day
if (sentimentAnalysisResult.label === 'NEGATIVE') {
finalMessage += " Remember, it's okay to have off days. What matters is moving forward one step at a time.";
} else if (sentimentAnalysisResult.label === 'POSITIVE') {
finalMessage += " Your positive outlook is inspiring! Let’s carry this energy forward.";
}
// Adjust the prompt to include a directive about avoiding specific content
const contentAvoidanceDirective = "Please avoid making assumptions about the author's personal feelings towards their own blog post unless asked, and focus on the positive impact of contributing to the community.";
const apiRequestBody = {
"model": "gpt-3.5-turbo",
"max_tokens": maxTokens, // Add the max_tokens parameter to your API request
"messages": [
{
"role": "system",
"content": "You are a blog curator asked to reply to users with an encouraging comment from the context of the parent blog post's content, considering the sentiment of the content. Try to keep your reply length consistent with user’s replies to you."
},
{
"role": "user",
"content": `${contentAvoidanceDirective} ${finalMessage} ${cleanedContent}`
}
]
};
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer " + process.env.OPENAI_API_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify(apiRequestBody)
});
const data = await response.json();
if (!data.choices || data.choices.length === 0 || !data.choices[0].message || data.choices[0].message.content.trim() === "") {
console.error('No choices returned from OpenAI.');
return; // Early return on failure
}
const replyText = data.choices[0].message.content.trim();
console.log(`Generated Comment: ${replyText}`);
// Post the generated reply as a comment using the broadcasting logic