Reference library

AI & LLM integration patterns

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

3 matches
AI & LLM integration patterns easy

Cache LLM Completions by Hashing the Prompt in Python

A simple in-memory cache that stores LLM completions keyed by a SHA-256 hash of the prompt to avoid recomputing identical requests.

llm caching hashing
Python
import hashlib
import json

class PromptCache:
    def __init__(self):
        self.cache = {}

    def _hash_prompt(self, prompt: str) -> str:
        return hashlib.sha256(prompt.encode("utf-8")).hexdigest()

    def get(self, prompt: str) -> str | None:
        key = self._hash_prompt(prompt)
        return self.ca…
14 0 Open
AI & LLM integration patterns easy

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.

embeddings hashing numpy
Python
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…
13 0 Open
AI & LLM integration patterns medium

How to cache embeddings with a Python dict to avoid recomputation

Caches embeddings computed from text in a dictionary keyed by SHA-256 hash, returning cached results for repeated calls.

embedding cache dict
Python
import hashlib
import time


class EmbeddingCache:
    def __init__(self):
        self.cache = {}

    def _hash_text(self, text):
        return hashlib.sha256(text.encode()).hexdigest()

    def get_embedding(self, text, compute_func):
        key = self._hash_text(text)
        if key not in self.cache:
          …
15 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.