illustrations

Using NLP Text Generation to Generate Reddit Replies

Published by AutoComplete Team
Tags: NLP, Text Generation, AI, Social Media, Reddit

Using NLP Text Generation to Generate Reddit Replies

The AutoComplete.sh NLP Text Completion API can be used to help come up with content for social media and online communities. This article discusses how you might use the API to help come up with writing ideas for your social media management strategies, and includes a few code snippets to get you started.

Building a Great Input

It’s important to feed good input into the AutoComplete.sh Text Completion API if you want good results.

The AutoComplete.sh Best Practices Guide goes over some examples of good and bad inputs.

Here’s an example of how you might build an input for a reddit comment thread using Javascript. This script assumes you’re browsing via the old.reddit.com interface:

// Read the thread's title
var title = $('a.title')[0].innerHTML;

// Get the top 3 comments from a reddit thread
var comments = [];
$('.commentarea>.sitetable>.comment>.entry>form p:first-of-type').each((i, comment) => {
  if (comment.innerHTML.length < 10) {
    return;
  }
  if (comments != null && comments.length >= 3) {
    return;
  }
  comments = [...comments, comment.innerHTML];
});

// Build input for AutoComplete.sh
var input = "The following is from a thread on Reddit.\n\n";
input = input + `Title: ${title}\n`;

comments.forEach((comment, i) => {
  input = input + `Comment #${i+1}: ${comment}\n`
});

input = input + `Comment #${comments.length+1}:`


This pulls the top 3 comments from a reddit thread, skipping comments that are shorter than 10 characters. After the comments are collected, it builds an input to use for a text completion job.

Let’s preview the input to see what it looks like:

The following is from a thread on Reddit.

Title: Diver gets attacked by a swordfish at 220m (721 feet) below surface
Comment #1: Here's an article with more info. This happened in 2016.
Comment #2: Great documentary called “Last Breath” it’s about these divers.
Comment #3: That's what I called interesting as f***, good post OP
Comment #4:


This gives us a great input for the AutoComplete.sh Text Completion endpoint to work with.

Submitting a Text Completion Job

Now that we’ve got a job input we can submit a job to the text compeletion endpoint. Here’s an example of how you might submit a job for Text Completion in Javascript:

// Submit a Text Completion job to AutoComplete.sh
fetch("https://api.autocomplete.sh/v1/engines/textgen/completion", {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
    Authorization: `Bearer YOUR_API_KEY`,
  },
  body: JSON.stringify({
    input: input,
    output_tokens: 32,
    optimize_readability: true,
    temperature: 0.8
  }),
}).then(response => response.json())
.then(json => console.log(json));


Here’s what the server returns:

{
  "status": 200,
  "input_tokens": 66,
  "output_tokens": 32,
  "input": "The following is from a thread on Reddit.\n\nComment #1: Here's an article with more info. This happened in 2016.\nComment #2: Great documentary called “Last Breath” it’s about these divers.\nComment #3: That's what I called interesting as f***, good post OP\nComment #4:",
  "output": " I saw the documentary and it's very good.\nComment #5: Oh yeah, that is pretty damn cool.",
  "combined": "The following is from a thread on Reddit.\n\nComment #1: Here's an article with more info. This happened in 2016.\nComment #2: Great documentary called “Last Breath” it’s about these divers.\nComment #3: That's what I called interesting as f***, good post OP\nComment #4: I saw the documentary and it's very good.\nComment #5: Oh yeah, that is pretty damn cool.",
  "optimize_readability": true,
  "temperature": 0.8,
  "cost": 98,
  "remaining_balance": 99902
}


The combined output looks like this:

The following is from a thread on Reddit.

Comment #1: Here's an article with more info. This happened in 2016.
Comment #2: Great documentary called “Last Breath” it’s about these divers.
Comment #3: That's what I called interesting as f***, good post OP
Comment #4: I saw the documentary and it's very good.
Comment #5: Oh yeah, that is pretty damn cool.


Comments #1 through #3 are existing comments from the thread’s comment section. Comments #4 and #5 were generated by AutoComplete.sh.

Cleaning Up the Output

If we only want a single comment. We can use the following Javascript code to isolate a single comment from the output:

// Isolate Comment #4
var reply = response.output.split('\n')[0];


The final result is this:

I saw the documentary and it's very good.


Responsible Usage

The results from AutoComplete.sh are a great starting point for coming up with content to help with online writing. Full automation without human interaction is not reccomended for social media websites and online communities. You should review the ToS of the sites you interact with and understand the limitations of automated text generation for your use-cases.

Give it a Try!

Check out the AutoComplete.sh documentation page for more information on getting started with the AutoComplete.sh Text Completion API.

Similar Stories