Reference library

AI & LLM integration patterns

Call LLM APIs, structure prompts, parse responses, and ship AI features safely.

4 matches
AI & LLM integration patterns easy

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.

jsonl audit llm
Python
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…
15 0 Open
AI & LLM integration patterns easy

How to Summarize Old Conversation Turns in Python

Compress old conversation turns into a brief summary while keeping recent turns intact for LLM context management.

llm context compression
Python
from datetime import datetime, timedelta


def summarize_old_turns(conversation, max_turns=5):
    """Compress turns older than max_turns into a brief summary."""
    if len(conversation) <= max_turns:
        return conversation, ""

    old_turns = conversation[:-max_turns]
    recent_turns = conversation[-max_turns…
14 0 Open
AI & LLM integration patterns easy

How to compute ROUGE recall in Python

Compute ROUGE recall by counting token overlap between a reference and candidate summary with pure Python.

rouge nlp evaluation
Python
def rouge_recall(reference, candidate):
    ref_tokens = reference.lower().split()
    cand_tokens = candidate.lower().split()

    ref_counts = {}
    for token in ref_tokens:
        ref_counts[token] = ref_counts.get(token, 0) + 1

    cand_counts = {}
    for token in cand_tokens:
        cand_counts[token] = cand…
12 0 Open
AI & LLM integration patterns easy

Prepare LLM prompt data with a Python helper class

A beginner-friendly Python class that collects records, converts them to JSON, and produces a quick summary for building LLM prompt context.

llm json prompt-engineering
Python
import json
from typing import Any, Dict, List

class DataHelper:
    """Simple helper to prepare data for LLM prompts."""
    
    def __init__(self):
        self.data = []
    
    def add(self, item: Dict[str, Any]) -> "DataHelper":
        self.data.append(item)
        return self
    
    def to_json(self) -> s…
16 0 Open

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.