Building a Discord Bot with Codebuddy: A Python Adventure

Introduction to the Project

In this blog post, I'll share my experience using Codebuddy to create a Discord bot, a project that introduced me to several new tools and technologies. Before this project, I had no prior experience with Python, Discord bots, or LangChain. Additionally, I chose TypeSense as the database solution, which was also new to me, and I had only limited experience with Docker. This project would normally be a significant learning curve, involving multiple elements I had never worked with before, but because of AI I was able to completely finish it in less than 2 days.

Codebuddy's Role in Development

In the end, I would say over 95% of the code was written by Codebuddy because, obviously, I didn't know the language. The stack also involved Docker, and I created a Docker container for the bot to run - and when I say that I did this what I really mean is Codebuddy did. I would ask Codebuddy how to set it up, it gave me the terminal commands to run, and I would just paste them in terminal and run them. If there were any issues I would just copy the error over and let it come up with a solution - however this part was pretty straight forward. Most of the errors were simply about needing to add more dependencies to my system or to the docker container.

I also used LangChain for OpenAI communication for LLM because I wanted to create a bot that is trained on our data and can answer questions from our users. I wanted it to know everything that I know or have written about publicly, so I'll tell you what I did to make that happen.

Data Collection and Preparation

I took all the Reddit comments I had written in the past with my username, anything that had the word Codebuddy in it, and used the Reddit API to fetch all that information and filter it based on that criteria. Codebuddy managed to do this quite well, but I did feel the need to include some documentation from a blog. I used the right-click "Send to Codebuddy" feature of the Codebuddy Chrome extension to make that process easy. Now that it's a python script I'll be able to run it again any time to fetch new comments to add to the database whenever I want.

Now that I had my own comments I also decided to grab the comments that I may have replied to (for context), and turned it into a text document to be put together into a vector database for the bot to scan. So, when a user tries to talk to the bot, it uses a vector database and an LLM to answer based on information I've given in the past. Finally, I took all our blog posts and used Codebuddy to reformat the blobs of text into something more coherent with "headings" and a few things to provide context for these snippets. The blog entries themselves were a bit too long so I split them up into sections where individual snippets to be embedded in the database would be a single section from the blog. After I scanned that information into the vector database for RAG answers, I was ready to go.

Now, our Discord bot is fully powered with much of what I've said publicly about Codebuddy and is able to answer questions that users have, even nuanced ones, especially if they're popular questions, because of the vector database and all that. It pulls up text snippets I've written on the subject based on the user's query and includes that in the LLM's context memory before answering questions. Thus, the bot is able to answer questions based on the information I've given in the past.

Copilot vs Codebuddy

I also ended up using Copilot, though not as much as Codebuddy, but it was still very useful, especially because I didn't know the language. Being completely new to Python, I didn't want to have to look up documentation about how to write Python and any of the syntax stuff. I didn't want to worry about that at all and I really didn't have to at the end of the day.

There was one time I needed to create a lambda expression, and I had no idea how that was meant to be done, but the AI was able to do it for me after a couple of tries. It wasn't immediate, but it definitely ended up working. There was a bit of nuance there where it needed to be a lambda expression with a single parameter, and that wasn't entirely clear to me at first, so I didn't ask for it, which was why I didn't get it right away.

Since requests to Codebuddy tend to take some time to process, it's not always ideal to use it for every little thing. I found that Copilot was much faster for quick things that involved writing a line of code right at the spot where I was working, and you've already pretty much figured out exactly what you need. This is where autocomplete really shines.

I also tried using Copilot's "Fix This" feature, which is similar to Codebuddy's Recode feature, but I found that the responses weren't nearly as good, so I stopped using it for quick things that needed to be recoded in a specific spot. It was definitely worthwhile to use the Quick Recode from Codebuddy instead, as it turns out.

Importance of Test-Driven Development

One of the most important takeaways from this experience was the importance of test-driven development. Whenever I came up with a new file that needed to work, like a service, repository, or data access object, I would always associate it with a unit test of some kind, either a unit test or a functional test. It doesn't really matter, to be honest. As long as it's able to immediately test that code it wrote for you in a nice, quick, and easy environment, development speed remained quite high. This is something I think everyone would significantly benefit from doing when using AI to write code for you, especially when the AI is doing a lot of the writing.

Final Thoughts

There was definitely a very difficult moment right at the end when I had all the code in place for the vector database, the LLM calls, and the Discord interfaces. All that stuff was ready to go and the tests were passing, but there was one last piece that was really difficult for me, and AI wasn't really able to help much. It actually required that I go through and read the documentation, and I still found it confusing just because of the situation I was in. What it came down to was the fact that Langchain's interfaces have been in serious flux over the past year. The AI is trained on the older interfaces, and the documentation shows all sorts of ways of doing the same thing. This, coupled with the fact that I wasn't familiar with Python meant I had no idea what it was talking about - even WITH the documentation. Once I figured out the missing piece of the puzzle the bot came to life and it was incredibly satisfying!

However, this just shows you that AI is not at a place right now where you can just lean on it completely. Every once in a while, you still need to jump in there and do some things yourself, and that's just part of the process. It's not that the AI couldn't put it together, but we're talking about interfaces that it hasn't been trained on, and even though we have documentation in place, I didn't fully understand what I was reading myself, and that's kind of a big deal. You need to understand what you're doing sometimes if the AI doesn't have a lot of data to go on. You definitely need to be able to step in and lead it with some guidance, and in extreme cases, you'll have to just do it yourself.

You might also like...

Building a Discord Bot with Codebuddy: A Python Adventure