In this final project workshop, you will put everything you have learned about Large Language Models, prompting, and developer APIs to use by building a custom context-aware AI Assistant!
Your goal is to build an interactive terminal chatbot in Python using the Google Generative AI library. The chatbot will act as a specialized coding tutor that enforces system-prompt behaviors and maintains conversational context (memory) across multi-turn exchanges.
Rather than manually appending user and assistant history arrays for every prompt, the Gemini SDK provides a convenient chat utility. You start a chat session by callingmodel.start_chat(), optionally passing earlier history (the system instruction is set on the model itself), and send messages with chat.send_message():
# Starting a stateful chat session
# The system instruction is set on the model: GenerativeModel(..., system_instruction="You are a strict Python mentor.")
chat = model.start_chat(history=[])
# Multi-turn interaction
response1 = chat.send_message("What is a list?")
response2 = chat.send_message("Can you give me a coding example?")
Build the bot in small, testable layers rather than writing the whole script at once.
quit.try/except so a network error or blocked response prints a message instead of crashing the session.SYSTEM_PROMPT = """You are a Python tutor.
- Only answer questions about Python programming. For anything else, reply:
"I can only help with Python. Ask me a coding question."
- Explain the idea in 2-3 sentences, then show a short, runnable code example.
- If the student's code has a bug, point to the line before giving the fix."""With the google-generativeai package used in this lesson, the system instruction is passed when you create the model, and start_chat() then keeps the history for you:
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
system_instruction=SYSTEM_PROMPT,
)
chat = model.start_chat(history=[])
reply = chat.send_message("What is a list?")
print(reply.text)Google has since been steering developers toward its newer google-genai package and newer Gemini model names, and older model names get retired. If a model name returns a not-found error, check the current documentation and substitute an available one; the structure of your program stays the same. Treat the model names in this lesson as examples.
A chatbot that works on your first message may still fail in ordinary use. Run a short fixed checklist every time you change the prompt:
start_chat() is inside the loop, the bot loses its memory every time. Create it once, before the loop./reset command that starts a fresh chat.Implement the following architectural tasks inside the code panel to build your chatbot script:
system_instruction string that restricts the bot to explain coding tasks only.GenerativeModel and pass your system instruction to it.model.start_chat() to create a stateful conversation object.chat.send_message(), prints the response, and exits on "quit".The starter scaffold is pre-loaded in the code editor on the right.