AI & GenAI
Production-ready snippets for LLMs, embeddings, RAG pipelines, and AI integrations.
20 snippets
Showing 20 of 20 snippets
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
RAG Pipeline (Retrieve + Augment + Generate)
Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.
Claude Messages API (Anthropic SDK)
Send messages to Claude using the official Anthropic SDK with system prompt and user turn.
OpenAI Tool Calling (Function Calling)
Define tools for GPT to call, parse the response, execute the function, and return results.
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
DALL·E 3 Image Generation
Generate images from a text prompt using the OpenAI DALL·E 3 API and return a URL.
OpenAI Structured Output with Zod
Force GPT-4o to return valid JSON matching a Zod schema using response_format structured output.
Content Moderation with OpenAI
Check user input for harmful content using the OpenAI Moderation API before processing.
Next.js AI Streaming Route Handler
Stream OpenAI responses from a Next.js App Router route handler using the Vercel AI SDK.
LangChain RAG Chain Pipeline
Build a retrieval-augmented generation chain with LangChain using vector store retrieval and prompt templates.
OpenAI Assistants API with Threads
Create persistent conversation threads with OpenAI Assistants API for stateful multi-turn interactions.
Pinecone Vector Store Operations
Store and query vector embeddings with Pinecone for semantic search and similarity matching.
Few-Shot Prompt Template
Build structured few-shot prompts with examples, system instructions, and output format constraints.
AI Agent Loop with Tool Calling
Implement an autonomous agent loop that plans, selects tools, executes actions, and observes results.
Token Counter with Tiktoken
Count tokens and estimate costs for OpenAI API calls using the tiktoken tokenizer library.
Hugging Face Inference API
Run ML models via the Hugging Face Inference API for text generation, classification, and embeddings.
Whisper Audio Transcription
Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.
OpenAI Text-to-Speech
Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.
AI Guardrails & Safety Pattern
Implement input/output guardrails for LLM applications with content filtering and response validation.