Skip to content

AI Engineering Course: Day 1

Posted on:January 12, 2024 at 04:08 PM

I have a proof-of-concept working version of QC Stacks running on Vercel. I’m in the process of migrating the work to Azure and using MongoDB as the database. I chose Azure due to my full-time position at Microsoft and wanted exposure and experience using a more production-ready environment that I might encounter in the real world.

What this means, however, is that the AI Engineering work is now finished but migrating and standing everything up on Azure is a bit more complicated than I originally anticipated. To continue in the spirit of learning in the open, I decided to begin working through the AI for Engineers email newsletter course. I’m hoping this will continue to expose me to new AI Engineering concepts and techniques that I may not encounter directly building QC Stacks.

Day 1 is building a simple ChatGPT bot on Telegram. It’s fairly rudimentary, but nonetheless a good way to further reinforce what I already know from working through other tutorials. I haven’t built out a bot yet that’s capable of doing much beyond a Jupyter Notebook and this will help me gain even more experience.

I was able to finish the Telegram bot and have a working example within an hour or so. Nothing revolutionary here, but further experience building LLM-powered apps is valuable just for the experience alone.

My final code is follows:

# Import telegram api key from .env
from dotenv import load_dotenv
import logging
from telegram import Update
from telegram.ext import filters, MessageHandler, ApplicationBuilder, ContextTypes, CommandHandler
import os
import openai
load_dotenv()

openai.api_key = os.environ["OPENAI_API_KEY"]

tg_bot_token = os.getenv("TELEGRAM_API_KEY")

messages = [{
    "role" : "system",
    "content": "You are a helpful assistant that answers questions."
}]

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        level=logging.INFO)

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_message(chat_id=update.effective_chat.id,
                                   text="I'm a bot, please talk to me!")

async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
    messages.append({"role": "user", "content": update.message.text})
    completion = openai.chat.completions.create(model="gpt-3.5-turbo",
                                                messages=messages)
    completion_answer = completion.choices[0].message.content
    messages.append({"role": "assistant", "content": completion_answer})

    await context.bot.send_message(chat_id=update.effective_chat.id,
                                   text=completion_answer)
    
if __name__ == "__main__":
    application = ApplicationBuilder().token(tg_bot_token).build()
    
    start_handler = CommandHandler('start', start)
    chat_handler = CommandHandler('chat', chat)
    application.add_handler(start_handler)
    application.add_handler(chat_handler)
    application.run_polling()

Make sure to check out the course for yourself. There’s a ton of good information in there if you’re interested in AI Engineering. Here’s the direct link to Day 1.