omg hi everyone!!! ð i'm SO excited to share my first real project working with large language models! as an intern at computacombinator, i've been learning SO much about AI and wanted to share my experience building something cool with these amazing tools! ð
the project idea ðĄ
so basically, i wanted to build a discord bot that could help our dev team document their code better (because apparently that's like super important or something lol). here's what i learned:
# my first attempt (don't laugh!) import discord from openai import OpenAI client = discord.Client() ai = OpenAI() # forgot api key first time oops @client.event async def on_message(message): if message.content.startswith('!document'): # it took me forever to figure out this part!! code = message.content[10:] # remove !document response = ai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": f"please document this code: {code}"} ] ) await message.channel.send(response.choices[0].message.content)
things i learned the hard way ð
- ALWAYS hide your API keys (oops... sorry jacob!)
- discord has message length limits (found out after 5 failed deploys lol)
- you need error handling (learned this when the bot crashed our server... my bad!)
- rate limiting is a thing (rip my api credits on day 1 ðŠĶ)
the improved version! âĻ
after some help from the team (and like a million commits), here's what i ended up with:
import discord import os from openai import OpenAI from dotenv import load_dotenv from ratelimit import limits, sleep_and_retry load_dotenv() # yay for environment variables! ONE_MINUTE = 60 MAX_CALLS = 10 # learned about rate limiting! @sleep_and_retry @limits(calls=MAX_CALLS, period=ONE_MINUTE) def get_documentation(code): try: response = ai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful code documentation assistant."}, {"role": "user", "content": f"Please document this code:\n{code}"} ], max_tokens=1500 # avoid those pesky discord limits! ) return response.choices[0].message.content except Exception as e: return f"Oops! Something went wrong: {str(e)}"
cool things i discovered! ð
working on this project taught me so much:
- how to actually read documentation (instead of just copy-pasting from stack overflow lol)
- that AI can be super helpful but also needs careful handling
- the importance of testing (after breaking things like 100 times)
- that the senior devs actually don't mind helping if you ask good questions!
"it's not about writing perfect code the first time, it's about learning from your mistakes" - jacob (after i accidentally pushed to main ð)
what's next? ð
i'm super excited to keep learning and building more stuff! next up, i want to:
- add support for different programming languages
- make the documentation more customizable
- maybe add some cool visualizations?? (if i can figure out how lol)
- definitely NOT push to main again without a code review ð
Comments