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 Accumulate Streamed Tokens into a Final String in Python
Accumulate a stream of tokens into a single final string by concatenating each token in sequence.
def accumulate_tokens(tokens):
"""Accumulate a stream of tokens into a single final string."""
result = ""
for token in tokens:
result += token
return result
if __name__ == "__main__":
token_stream = ["Hello", ", ", "world", "!", " This ", "is ", "accumulated."]
final_string = accumul…
How to Append Few-Shot Examples to a Prompt in Python
This code builds a complete LLM prompt by appending few-shot examples in alternating user/assistant format using a simple loop.
def append_few_shot_examples(prompt: str, examples: list[tuple[str, str]], separator: str = "\n\n") -> str:
"""Append few-shot examples to a prompt in alternating user/assistant format."""
full_prompt = prompt
for user_input, assistant_output in examples:
full_prompt = f"{full_prompt}{separator}Use…
How to Batch Embed a List of Strings in Python
Batch embed a list of strings into deterministic pseudo-random vectors using a mock encoder class.
class MockEncoder:
def __init__(self, dim=8, seed=42):
self.dim = dim
self.seed = seed
def embed(self, text):
# Deterministic pseudo-random embedding based on text content
hash_val = hash(text)
import random
rng = random.Random(hash_val + self.seed)
retu…
How to Build a Prompt Template with Variable Slots in Python
Create a reusable LLM prompt template with named variable slots using Python's string.Template class and fill them with render() calls.
from string import Template
class PromptTemplate:
def __init__(self, template_text):
self.template = Template(template_text)
def render(self, **kwargs):
return self.template.substitute(**kwargs)
if __name__ == "__main__":
template = PromptTemplate(
"You are a helpful assistant …
How to Build a Simple Semantic Cache for Similar Prompts in Python
Mock a semantic cache that finds the closest matching prompt using word-overlap similarity and returns cached results above a threshold.
prompt_cache = [
"What is the capital of France?",
"How does recursion work?",
"Best practices for Python logging?",
"Explain binary search in one line.",
"How to reverse a string in Python?"
]
def normalize(text):
return " ".join(text.lower().split())
def similarity(a, b):
a_words = set(…
How to Build a System-User-Assistant Message List in Python
Use dataclasses to model a chat conversation and build the system/user/assistant message list expected by LLM APIs.
from dataclasses import dataclass, field
from typing import List
@dataclass
class Message:
role: str
content: str
@dataclass
class Conversation:
messages: List[Message] = field(default_factory=list)
def add_system(self, content: str) -> None:
self.messages.append(Message(role="system", con…
How to Build a Zero-Shot Classification Prompt in Python
Creates a prompt for zero-shot text classification by pairing input text with candidate labels and a hypothesis template.
from typing import Dict, List
def build_zero_shot_prompt(
text: str,
candidate_labels: List[str],
hypothesis_template: str = "This is about {}.",
) -> Dict[str, List[str]]:
"""Build a prompt ready for zero-shot classification."""
return {
"sequences": text,
"candidate_labels": can…
How to Build an Agent Loop with Plan, Act, Observe in Python
Implements a simple plan-act-observe loop that an AI agent uses to iteratively complete a task in an environment while storing observations in memory.
class Agent:
def __init__(self, name):
self.name = name
self.memory = {}
def plan(self, task):
return f"Plan for {task}: step 1, step 2, step 3"
def act(self, plan, environment):
return f"Executing {plan} in {environment}"
def observe(self, action_result):
sel…
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 Build an In-Memory Vector Store in Python
Build a lightweight in-memory vector store using a Python dict and cosine similarity for fast nearest-neighbor searches.
import math
from typing import Dict, List, Optional
class InMemoryVectorStore:
def __init__(self) -> None:
self.vectors: Dict[str, List[float]] = {}
self.index: Dict[str, List[str]] = {} # query -> list of ids sorted by similarity
def add(self, vector_id: str, vector: List[float]) -> None:
…
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 Compute a Mock BLEU Score with n-gram Overlap in Python
Evaluate text similarity with a simplified BLEU score using word-level n-gram precision and a brevity penalty.
from collections import Counter
def bleu_score(reference, candidate, n=2):
"""
Compute a simplified BLEU score with n-gram precision and brevity penalty.
Mock demo using word-level n-grams.
"""
ref_tokens = reference.lower().split()
cand_tokens = candidate.lower().split()
# Compute n-…
How to Convert Data to JSON and Back in Python
Convert a Python dict into a JSON string with indentation, then parse it back into a dict, demonstrating a common round-trip conversion for beginners.
import json
from datetime import datetime
def convert_data(data):
"""Convert a dict into a JSON string and back to dict."""
json_str = json.dumps(data, indent=2)
parsed = json.loads(json_str)
return json_str, parsed
def main():
sample_data = {
"user": "alice",
"message": "hello",
…
How to Create a Mock LLM Judge Rubric Score in Python
Scores a response against a rubric by counting keyword matches, returning total, percentage, and per-criterion feedback.
def judge_score(response, rubric):
"""Mock LLM judge that scores a response against a rubric."""
total = 0
max_total = 0
feedback = []
for criterion, rubric_item in rubric.items():
max_points = rubric_item["max"]
description = rubric_item["description"]
# Simple mock scori…
How to Create a Mock Text Embedding with Hash in Python
Generate deterministic mock text embeddings using SHA-256 hashing and numpy, producing normalized vectors for similarity testing without an LLM.
import hashlib
import numpy as np
def mock_embed(text: str, dim: int = 10, seed: int = 42) -> np.ndarray:
"""Generate a deterministic mock embedding using a hash function.
Args:
text: Input text to embed
dim: Dimension of the output vector
seed: Seed for reproducibility
R…
How to Create a Simple Data Helper in Python for LLM Projects
Create a beginner-friendly Python class that stores, filters, and serializes data records for AI/LLM workflows.
import json
from typing import Any, Dict, List, Optional
class DataHelper:
"""Simple helper for beginners to manage data in AI/LLM projects."""
def __init__(self, data: Optional[List[Dict[str, Any]]] = None) -> None:
self.data: List[Dict[str, Any]] = data or []
def add_item(self, item: Dict[str…
How to Estimate Token Count in Python
Estimates tokens in a text string using a whitespace and punctuation heuristic without external libraries.
def estimate_tokens(text: str) -> int:
"""Estimate token count using whitespace and punctuation heuristics."""
if not text:
return 0
words = text.split()
total_punctuation = sum(1 for char in text if char in ".,!?;:")
special_tokens = sum(1 for char in text if char in "\n\t")
# Rough …
How to Filter Blocked Words in Python
Scans input text against a moderation blocklist, returning blocked terms and their counts.
MODERATION_BLOCKLIST = {"spam", "scam", "fraud", "phishing", "malware", "abuse"}
def scan_text(text: str) -> dict:
normalized = text.lower()
words = normalized.replace(".", " ").replace(",", " ").replace("!", " ").replace("?", " ").split()
found_terms = []
for word in words:
if word in MO…
How to Filter Toxic Keywords in Python
Filter toxic keywords from text by replacing each occurrence with asterisks, useful as a basic guardrail for LLM inputs.
TOXIC_KEYWORDS = ["insult", "threat", "hate", "violence", "spam"]
def guardrails_filter(text: str, keywords: list[str] | None = None) -> str:
"""Filter out toxic keywords from the given text.
Args:
text: The input text to filter.
keywords: Optional keyword list. Defaults to TOXIC_KEYWORDS.
…
How to Keep Last K Turns in a Memory Buffer in Python
A TurnBuffer class using deque with maxlen to keep only the most recent k conversation turns in memory for LLM context.
from collections import deque
class TurnBuffer:
def __init__(self, k):
self.k = k
self.turns = deque(maxlen=k)
def add(self, turn):
self.turns.append(turn)
def last_k(self):
return list(self.turns)
if __name__ == "__main__":
buffer = TurnBuffer(3)
buffer.add("tu…
How to Log Prompts and Completions as JSONL Audit Files in Python
Read a JSONL file of LLM prompt–completion pairs, compute totals and averages, then write an audit summary with timestamps.
import json
from pathlib import Path
from datetime import datetime
def audit_jsonl(filepath):
logs = []
with open(filepath, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
entry = json.loads(line)
logs.ap…
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 Mock an LLM Client in Python
Create a simple mock LLM client that returns a canned completion for testing or development without a real API.
from dataclasses import dataclass
@dataclass
class MockLLMClient:
canned_response: str = "This is a canned completion."
def complete(self, prompt: str) -> str:
return f"{self.canned_response} [to: {prompt[:20]}]"
if __name__ == "__main__":
client = MockLLMClient()
result = client.complete(…
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.