AI & LLM integration patterns
Call LLM APIs, structure prompts, parse responses, and ship AI features safely.
Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo
This demo shows how to structure a function that explains its own reasoning step-by-step, mimicking chain-of-thought prompting for AI systems.
def solve_math_step_by_step(expression: str) -> str:
"""Solves a simple expression, showing each reasoning step."""
# Step 1: Parse the expression (assume "a + b" or "a - b")
parts = expression.split()
a = int(parts[0])
op = parts[1]
b = int(parts[2])
steps = []
steps.append(f"Step…
How to Build an Entity Memory Dict to Store Facts in Python
Store and recall facts about entities using nested dictionaries with remember, recall, and forget functions in Python.
facts = {}
def remember(entity, attribute, value):
if entity not in facts:
facts[entity] = {}
facts[entity][attribute] = value
def recall(entity, attribute):
return facts.get(entity, {}).get(attribute, None)
def forget(entity, attribute=None):
if attribute is None:
facts.pop(entity, …
How to Chunk a Long Document for RAG Retrieval in Python
Split text into overlapping chunks at sentence boundaries using a custom Python function suitable for RAG retrieval pipelines.
import re
from pathlib import Path
def chunk_document(text, chunk_size=500, overlap=100):
"""Split text into overlapping chunks suitable for RAG retrieval."""
# Normalize whitespace
text = re.sub(r'\s+', ' ', text).strip()
chunks = []
start = 0
while start < len(text):
end = min(s…
How to Mock OpenAI Tool Call Messages in Python
Create an assistant message with a function tool call in OpenAI's chat format, useful for testing and mocking.
from openai import OpenAI
def mock_tool_call(tool_name: str, arguments: dict) -> dict:
"""Simulate a tool call message in OpenAI style."""
return {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_" + "a1b2c3d4e5f6",
"type…
How to build a function calling schema dict in Python
Build an OpenAI-compatible function calling schema dictionary with a helper function that takes name, description, parameters, and required fields.
import json
from typing import Dict, Any, List, Optional
def build_function_schema(
name: str,
description: str,
parameters: Optional[Dict[str, Any]] = None,
required: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Build an OpenAI-compatible function calling schema dictionary."""
schema: …
Route Tool Call Name to Python Handler Dict
Routes a tool call name to the correct Python handler function using a dictionary lookup, returning an error for unknown tools.
def get_name():
return {"name": "Alice"}
def get_age():
return {"age": 30}
def get_email():
return {"email": "alice@example.com"}
handlers = {
"get_name": get_name,
"get_age": get_age,
"get_email": get_email,
}
def route(tool_call):
handler = handlers.get(tool_call["name"])
if handl…
Browse by section
Each section groups closely related Python snippets.
AI & LLM integration patterns — Python code examples
What you will find here
This page collects ai & llm integration patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.