xiaohongshu
xiaohongshu
cloudflare-workers-ai
Cloudflare Workers AI essentials - text generation, embeddings, image generation, streaming. Trigger: When working with Workers AI, LLMs, text generation, embeddings, image generation.
Full skill instructions
# wrangler.toml
[ai]
binding = "AI"
export interface Env {
AI: Ai
}
import { createWorkersAI } from "workers-ai-provider"
import { generateText } from "ai"
type Env = { AI: Ai }
export default {
async fetch(req: Request, env: Env) {
const workersai = createWorkersAI({ binding: env.AI })
const { text } = await generateText({
model: workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
prompt: "Write a short poem about clouds"
})
return Response.json({ generatedText: text })
}
}
Popular models:
@cf/meta/llama-3.3-70b-instruct-fp8-fast - Llama 3.3 (large, fast)@cf/meta/llama-2-7b-chat-int8 - Llama 2 (smaller)@cf/mistral/mistral-7b-instruct-v0.1 - Mistral 7Bimport { streamText } from "ai"
import { Hono } from "hono"
const app = new Hono<{ Bindings: Env }>()
app.post("/chat", async (c) => {
const { messages } = await c.req.json()
const workersai = createWorkersAI({ binding: c.env.AI })
const result = await streamText({
model: workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
messages
})
return result.toTextStreamResponse({
headers: {
"Content-Type": "text/x-unknown",
"content-encoding": "identity",
"transfer-encoding": "chunked"
}
})
})
export default { fetch: app.fetch }
const systemPrompt = `You are a helpful financial advisor.
Provide accurate, concise advice about personal finance.`
const result = await streamText({
model: workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userMessage }
],
maxTokens: 1000,
temperature: 0.7
})
import { embed } from "ai"
const workersai = createWorkersAI({ binding: env.AI })
const { embedding } = await embed({
model: workersai.textEmbedding("@cf/baai/bge-base-en-v1.5"),
value: "Search engines use embeddings for semantic understanding"
})
// embedding is array of numbers [0.123, -0.456, ...]
console.log(embedding.length) // 768 dimensions
Popular embedding models:
@cf/baai/bge-base-en-v1.5 - 768 dimensions (recommended)@cf/baai/bge-small-en-v1.5 - 384 dimensions (faster)@cf/baai/bge-large-en-v1.5 - 1024 dimensions (more accurate)import { embedMany } from "ai"
const { embeddings } = await embedMany({
model: workersai.textEmbedding("@cf/baai/bge-base-en-v1.5"),
values: [
"First document",
"Second document",
"Third document"
]
})
// embeddings is array of arrays
console.log(embeddings.length) // 3
console.log(embeddings[0].length) // 768
import { generateImage } from "ai"
const workersai = createWorkersAI({ binding: env.AI })
const { image } = await generateImage({
model: workersai.image("@cf/black-forest-labs/flux-1-schnell"),
prompt: "A serene mountain landscape at sunset",
size: "1024x1024"
})
return new Response(image, {
headers: { "Content-Type": "image/png" }
})
Popular image models:
@cf/black-forest-labs/flux-1-schnell - FLUX.1 (fast, recommended)@cf/stabilityai/stable-diffusion-xl-base-1.0 - SDXL@cf/bytedance/stable-diffusion-xl-lightning - SDXL LightningSizes: "1024x1024", "1024x768", "768x1024"
import { Hono } from "hono"
import { createWorkersAI } from "workers-ai-provider"
import { streamText } from "ai"
const app = new Hono<{ Bindings: Env }>()
app.post("/api/chat", async (c) => {
const { messages } = await c.req.json()
if (!messages || !Array.isArray(messages)) {
return c.json({ error: "Invalid messages" }, 400)
}
const workersai = createWorkersAI({ binding: c.env.AI })
try {
const result = await streamText({
model: workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
messages,
maxTokens: 1000,
temperature: 0.7
})
return result.toTextStreamResponse({
headers: {
"Content-Type": "text/x-unknown",
"content-encoding": "identity",
"transfer-encoding": "chunked"
}
})
} catch (error) {
console.error("AI error:", error)
return c.json({ error: "Failed" }, 500)
}
})
export default { fetch: app.fetch }
app.post("/search", async (c) => {
const { query } = await c.req.json()
const workersai = createWorkersAI({ binding: c.env.AI })
// 1. Generate embedding for query
const { embedding: queryEmbedding } = await embed({
model: workersai.textEmbedding("@cf/baai/bge-base-en-v1.5"),
value: query
})
// 2. Search in vector database (e.g., Vectorize)
const results = await c.env.VECTORIZE.query(queryEmbedding, {
topK: 10
})
return c.json({ results })
})
app.post("/generate-image", async (c) => {
const { prompt } = await c.req.json()
if (!prompt || prompt.length < 3 || prompt.length > 1000) {
return c.json({ error: "Invalid prompt" }, 400)
}
const workersai = createWorkersAI({ binding: c.env.AI })
try {
const { image } = await generateImage({
model: workersai.image("@cf/black-forest-labs/flux-1-schnell"),
prompt,
size: "1024x1024"
})
// Upload to R2
const key = `images/${crypto.randomUUID()}.png`
await c.env.BUCKET.put(key, image, {
httpMetadata: { contentType: "image/png" }
})
return c.json({ success: true, key })
} catch (error) {
return c.json({ error: "Generation failed" }, 500)
}
})
// ✅ Stream for long responses
const result = await streamText({...})
return result.toTextStreamResponse()
// ❌ Wait for entire response (slow)
const { text } = await generateText({...})
return Response.json({ text })
// ✅ Choose right model size
// Small tasks -> smaller models
workersai("@cf/meta/llama-2-7b-chat-int8")
// Complex reasoning -> larger models
workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast")
// ✅ Cache embeddings
const cached = await cache.get(text)
if (cached) return JSON.parse(cached)
const { embedding } = await embed({...})
await cache.put(text, JSON.stringify(embedding))
// ✅ Batch embeddings
const { embeddings } = await embedMany({
model: workersai.textEmbedding("@cf/baai/bge-base-en-v1.5"),
values: documents
})
// ❌ One at a time (slow)
for (const doc of documents) {
await embed({ value: doc })
}
const result = await generateText({
model: workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
prompt: "Your prompt",
maxTokens: 500, // Max tokens to generate
temperature: 0.7, // Randomness (0-1)
topP: 0.9, // Nucleus sampling
frequencyPenalty: 0.5, // Reduce repetition
presencePenalty: 0.5, // Encourage diversity
stopSequences: ["\n\n", "END"]
})
❌ Not handling errors
const { text } = await generateText({...}) // May throw!
✅ Always use try-catch
try {
const { text } = await generateText({...})
} catch (error) {
return c.json({ error: "Failed" }, 500)
}
❌ Missing streaming headers
return result.toTextStreamResponse()
✅ Include required headers
return result.toTextStreamResponse({
headers: {
"Content-Type": "text/x-unknown",
"content-encoding": "identity",
"transfer-encoding": "chunked"
}
})
# List models
wrangler ai models list
# Test model
wrangler ai run @cf/meta/llama-3.3-70b-instruct-fp8-fast --prompt "Hello"
# Deploy
wrangler deploy
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience