This page introduces the use of the library praw to aquire information from reddit (read ‘forum’ if you’re not familiar) to be used as a starting point for an argument between two bots. The bots generate responses using the openAI API connected to a GPT model named ‘text-davinci-002’. Get all the code from my Github.

relevant resources:


Lets first import the libraries.

#import libraries
import praw
import openai
import requests
from summarizer import Summarizer
import torch
from bs4 import BeautifulSoup as bs

To keep it more easily understandable I kept the parts relevant to the Reddit API seperate from code relevant to openAI. In the following code we define the API wrapper and most importantly the ‘keyword’. Based on the keyword the top 10 most relevant subreddits are found. Then the subreddit that is most relevant is chosen. Then we get the most upvoted and most downvoted comments which are later used fby the bots.

# Initialize the Reddit API wrapper
reddit = praw.Reddit(client_id='client_id here',
                     client_secret='secret here',

# Define the keyword to search for
keyword = 'optimism'
popular_comment = ()
negative_comment = ()
subreddit = ()
# Search for subreddits related to the keyword
subreddits = list(, limit=10))

if subreddits:
    subreddit = sorted(subreddits, key=lambda x: x.title.lower().count(keyword.lower()), reverse=True)[0]
        print(f"Subreddit: {subreddit.display_name}")
        subreddit = subreddit.display_name
        top_post = list([0]
        top_comment = None
        downvoted_comment = None
        for comment in top_post.comments:
            if isinstance(comment, praw.models.reddit.comment.Comment):
                if top_comment is None or comment.score > top_comment.score:
                    top_comment = comment
                if downvoted_comment is None or comment.score < downvoted_comment.score:
                    downvoted_comment = comment
        if top_comment is not None:
            popular_comment = top_comment.body
            print(f"Most popular comment: {popular_comment}")
        if downvoted_comment is not None:
            negative_comment = downvoted_comment.body
            print(f"Most downvoted comment: {negative_comment}")
    except Exception as e:
        print(f"Error getting comments: {e}")
Subreddit: CryptoCurrency
Most popular comment: You also don’t hear about me turning my $20 into $21.04 😎
Most downvoted comment: Im up $0.1. God is by my side

The block above shows the initial output. The keyword chosen was ‘optimism’ and it ended up choosing the subreddit ‘cryptocurrency’. It then stores and prints the most popular and most unpopular messages that were posted there. Next up we can start building the chatbots using the openAI API.

# Set up OpenAI API key
openai.api_key = "key"

The openAI key can be aquired after creating an account. Below we can see two functions. The first generates the responses by sending the prompts through the openAI api. The most interesting feature here is that ‘temperature’ is not hard coded. Temperature desides how predictable the response will be, 0 = always the same, 1 as unpredictable as possible. I start with a temerpature of 0.5 and as the conversation keeps going I increase the randomness of it. After testing I found that without this increased randomness conversations become stale and repetitive quickly.

def generate_response(prompt, temp):
    response = openai.Completion.create(
    return response.choices[0].text.strip()

# Set up initial prompt
def generate_conversation(keyword1, comment1, comment2, reddit):
    prompt = f"The topic is {keyword1} related to {reddit}.\n\nDennis: {comment1}\nLinda: {comment2}\n\n"

    temperature = 0.5
    # Start debate
    for i in range(3): 
        # Person 1 responds
        prompt += "Dennis: "
        prompt += generate_response(f"Dennis disagrees with Linda." + prompt, temperature)
        prompt += "\n\n"

        # Person 2 responds
        prompt += "Linda: "
        prompt += generate_response(f"Linda disagrees with Dennis." + prompt, temperature)
        prompt += "\n\n"
        temperature += 0.1
print(generate_conversation(keyword, popular_comment, negative_comment, subreddit))
The topic is optimism related to CryptoCurrency.

Dennis: You also don’t hear about me turning my $20 into $21.04 😎
Linda: Im up $0.1. God is by my side

Dennis: I don't think you can really compare the two situations. I'm not sure if it's possible to turn $20 into $21.04 in cryptocurrency, but even if it is, it's not the same as turning a profit.

Linda: I think you can definitely compare the two situations. Both of us are making a profit, even if it's just a small one. And who knows? Maybe with a little more time and effort, we could both turn our investments into even bigger ones!

Dennis: I'm not sure if I agree with you, Linda. It seems to me like you're being a bit too optimistic about the situation. Sure, it's possible that we could both make more money off of our investments, but there's also a pretty big chance that we could lose everything we put in. I'm not sure if it's worth the risk.

Linda: I understand where you're coming from, Dennis, but I still think that optimism is important. Sure, there's always a chance that things could go wrong, but if we don't believe that things will work out in the end, then we're never going to make any progress. So I say, let's keep our heads up and believe that we can achieve great things!

Dennis: I see your point, Linda, but I'm still not convinced. I think it's important to be realistic about the situation and not get our hopes up too much.

Linda: I think it's important to have hope and believe in ourselves, even if the odds might be against us. If we don't believe that things can get better, then they probably never will. So I say, let's stay positive and see what the future brings!

As can be seen the code to work with both the reddit API wrapper and openAI api is very user friendly and quick to set up. The end result is a functionality that generates interesting and sometimes funny discussions, depending on the chosen keyword. This was mainly a test to work with the openAI api and get accustomed to it. I see a lot of potential in this and will continue to explore this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *