Member-only story
GenAI with Python: Build Agents from Scratch (Complete Tutorial)
with Ollama, LangChain, LangGraph (No GPU, No APIKEY)
(All images are by the author unless otherwise noted)
Intro
Prompt Engineering is the practice of designing and refining prompts (text inputs) to enhance the behavior of Large Language Models (LLMs). The goal is to get the desired responses from the model by carefully crafting the instructions. The most used prompting techniques are:
- Chain-of-Thought: involves generating a step-by-step reasoning process to reach a conclusion. The model is pushed to “think out loud” by explicitly laying out the logical steps that lead to the final answer.
- ReAct (Reason+Act): combines reasoning with action. The model not only thinks through a problem but also takes actions based on its reasoning. So it’s more interactive as the model alternates between reasoning steps and actions, refining its approach iteratively. Basically, it’s a loop of “thought”, “action”, “observation”.
Let’s make an example: imagine asking an AI to “find the best laptop under $1000”.
- Normal Answer: “Lenovo Thinkpad”.
- Chain-of-Thought Answer: “I need to consider factors like performance, battery life, and build quality. Then, I would check which laptops are priced under $1000. According to my knoweldge base, the Lenovo Thinkpad is the best”.
- ReAct Answer: Same as the Chain-of-Thought Answer + the Action of performing a web-search for “best laptops under $1000 in 2024” and analyze the results (which might not be “Lenovo Thinkpad”).
Agents are created using the ReAct technique, so the main difference with LLMs is the ability to take action. Agents are AI systems designed to process sequential reasoning, with the option of executing external tools (i.e. database query, web search) in case the LLM’s general-purpose knowledge isn’t enough. To put it simply, a normal AI Chatbot generates random text when it doesn’t know how to answer a question, but an Agent activates its tools to fill the gap and give a specific response.
In this tutorial, I’m going to build from scratch a multi-agent system with human-in-the-loop. I…