🤖

AI & GenAI

Production-ready snippets for LLMs, embeddings, RAG pipelines, and AI integrations.

20 snippets

Showing 20 of 20 snippets

typescriptintermediate

OpenAI Chat Completion with Streaming

Stream GPT responses token-by-token using the OpenAI SDK with async iteration.

#openai#streaming
typescriptbeginner

Generate Text Embeddings with OpenAI

Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.

#openai#embeddings
typescriptadvanced

RAG Pipeline (Retrieve + Augment + Generate)

Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.

#rag#embeddings
typescriptbeginner

Claude Messages API (Anthropic SDK)

Send messages to Claude using the official Anthropic SDK with system prompt and user turn.

#anthropic#claude
typescriptintermediate

OpenAI Tool Calling (Function Calling)

Define tools for GPT to call, parse the response, execute the function, and return results.

#openai#tool-calling
pythonbeginner

LangChain Prompt Chain (Python)

Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.

#langchain#openai
typescriptbeginner

DALL·E 3 Image Generation

Generate images from a text prompt using the OpenAI DALL·E 3 API and return a URL.

#openai#dall-e
typescriptintermediate

OpenAI Structured Output with Zod

Force GPT-4o to return valid JSON matching a Zod schema using response_format structured output.

#openai#zod
typescriptbeginner

Content Moderation with OpenAI

Check user input for harmful content using the OpenAI Moderation API before processing.

#openai#moderation
typescriptintermediate

Next.js AI Streaming Route Handler

Stream OpenAI responses from a Next.js App Router route handler using the Vercel AI SDK.

#nextjs#openai
typescriptadvanced

LangChain RAG Chain Pipeline

Build a retrieval-augmented generation chain with LangChain using vector store retrieval and prompt templates.

#langchain#rag
typescriptintermediate

OpenAI Assistants API with Threads

Create persistent conversation threads with OpenAI Assistants API for stateful multi-turn interactions.

#openai#assistants
typescriptintermediate

Pinecone Vector Store Operations

Store and query vector embeddings with Pinecone for semantic search and similarity matching.

#pinecone#vector-store
typescriptbeginner

Few-Shot Prompt Template

Build structured few-shot prompts with examples, system instructions, and output format constraints.

#prompts#few-shot
typescriptadvanced

AI Agent Loop with Tool Calling

Implement an autonomous agent loop that plans, selects tools, executes actions, and observes results.

#agents#tool-calling
pythonbeginner

Token Counter with Tiktoken

Count tokens and estimate costs for OpenAI API calls using the tiktoken tokenizer library.

#tokens#tiktoken
pythonintermediate

Hugging Face Inference API

Run ML models via the Hugging Face Inference API for text generation, classification, and embeddings.

#huggingface#inference
typescriptintermediate

Whisper Audio Transcription

Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.

#whisper#transcription
typescriptbeginner

OpenAI Text-to-Speech

Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.

#tts#speech
typescriptadvanced

AI Guardrails & Safety Pattern

Implement input/output guardrails for LLM applications with content filtering and response validation.

#guardrails#safety