Learnings from the course - Intro to AI Engineering(Scrimba) (WIP 🚧)

Basics

Huge thanks to Arsala Khan LinkedIn Profile 🔗 for explaining the concepts so clearly 🫶

Creating a Request

const userPrompt = "Suggest some gifts for someone who loves hiphop music";

const userMessage = {
  role: "user",
  content: userPrompt,
};

const response = await openai.chat.completions.create({
  model: process.env.AI_MODEL,
  messages: [userMessage],
});

Types of Prompts

2 Types. User Prompt and System Prompt.

System Prompt -> You are a coding expert in Javascript. You help solve problems of beginners.

User prompt -> How to split a string in JS?

Refining System Prompt to save tokens

System Prompt -> You are a coding expert in Javascript. You help solve problems of beginners. Limit your response to 100 characters.

Token Costs and Context Windows

  • Different models output cost differently.
  • Input and output tokens are charged.
  • Gpt-5-nano is the cheapest and gpt-5.5 is expensive
  • Context Window is the size of messages what fits at once. Full context is the whole chat.

Error Handling

  • Always add a error handling using try catch. Failing which will leave the user waiting for the response.

Structuring Responses

  • Take the generated Markdown response and parse it to HTML using marked npm package. Syntax marked.parse(MARKDOWN_CONTENT)
  • Use DOMPurify(npm package) to sanitize the html as it might contain malicious code generated by AI. It’s simple again. DOMPurify.sanitize(HTML_CONTENT)

Streaming Responses

By enabling stream true, we get output generated in chunks

const stream = await openai.chat.completions.create({
  model: process.env.AI_MODEL,
  messages,
  stream: true,
});

We can then use these chunks to render in the UI by using for await loop. for await(const chunk of stream)

To be continued…