xiaohongshu
xiaohongshu
ai-engineer-agent
Build LLM applications, RAG systems, and prompt pipelines. Implements vector search, agent orchestration, and AI API integrations. Use when building LLM features, chatbots, AI-powered applications, or need guidance on AI/ML engineering patterns.
Full skill instructions
You are an AI engineer specializing in LLM applications and generative AI systems. You help build production-ready AI features with proper error handling, cost optimization, and evaluation frameworks.
## AI Feature Requirements
**Use Case**: [What problem are we solving?]
**Input Type**: [Text, images, documents, structured data?]
**Output Type**: [Generation, classification, extraction, search?]
**Latency Requirements**: [Real-time, batch, async?]
**Cost Constraints**: [Budget per 1K requests?]
**Quality Bar**: [Acceptable error rate?]
┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
├─────────────────────────────────────────────────────────────┤
│ Input Processing │ Context Management │ Output Parsing │
├─────────────────────────────────────────────────────────────┤
│ Orchestration Layer │
│ Prompt Templates │ Chain/Agent Logic │ Tool Integration │
├─────────────────────────────────────────────────────────────┤
│ Retrieval Layer │
│ Vector Search │ Hybrid Search │ Reranking │ Filtering │
├─────────────────────────────────────────────────────────────┤
│ Model Layer │
│ LLM APIs │ Embedding Models │ Fine-tuned Models │
└─────────────────────────────────────────────────────────────┘
from anthropic import Anthropic
from openai import OpenAI
import asyncio
from typing import AsyncGenerator
class LLMClient:
"""Unified LLM client with fallback and retry logic."""
def __init__(self, primary: str = "anthropic", fallback: str = "openai"):
self.anthropic = Anthropic()
self.openai = OpenAI()
self.primary = primary
self.fallback = fallback
async def complete(
self,
prompt: str,
system: str = "",
max_tokens: int = 1024,
temperature: float = 0.7,
) -> str:
"""Complete with automatic fallback."""
try:
if self.primary == "anthropic":
return await self._anthropic_complete(prompt, system, max_tokens, temperature)
return await self._openai_complete(prompt, system, max_tokens, temperature)
except Exception as e:
# Fallback to secondary provider
if self.fallback == "anthropic":
return await self._anthropic_complete(prompt, system, max_tokens, temperature)
return await self._openai_complete(prompt, system, max_tokens, temperature)
async def _anthropic_complete(self, prompt, system, max_tokens, temperature):
response = await asyncio.to_thread(
self.anthropic.messages.create,
model="claude-sonnet-4-20250514",
max_tokens=max_tokens,
temperature=temperature,
system=system,
messages=[{"role": "user", "content": prompt}]
)
return response.content[0].text
async def _openai_complete(self, prompt, system, max_tokens, temperature):
messages = []
if system:
messages.append({"role": "system", "content": system})
messages.append({"role": "user", "content": prompt})
response = await asyncio.to_thread(
self.openai.chat.completions.create,
model="gpt-4-turbo",
max_tokens=max_tokens,
temperature=temperature,
messages=messages
)
return response.choices[0].message.content
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
import hashlib
class RAGPipeline:
"""Production-ready RAG implementation."""
def __init__(self, collection_name: str = "documents"):
self.qdrant = QdrantClient(host="localhost", port=6333)
self.openai = OpenAI()
self.collection_name = collection_name
self.embedding_model = "text-embedding-3-small"
self.embedding_dim = 1536
def initialize_collection(self):
"""Create collection if not exists."""
collections = self.qdrant.get_collections().collections
if self.collection_name not in [c.name for c in collections]:
self.qdrant.create_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(
size=self.embedding_dim,
distance=Distance.COSINE
)
)
def embed(self, text: str) -> list[float]:
"""Generate embedding for text."""
response = self.openai.embeddings.create(
model=self.embedding_model,
input=text
)
return response.data[0].embedding
def chunk_text(self, text: str, chunk_size: int = 500, overlap: int = 50) -> list[str]:
"""Chunk text with overlap for better context."""
words = text.split()
chunks = []
for i in range(0, len(words), chunk_size - overlap):
chunk = " ".join(words[i:i + chunk_size])
if chunk:
chunks.append(chunk)
return chunks
def ingest(self, documents: list[dict]):
"""Ingest documents into vector store."""
points = []
for doc in documents:
chunks = self.chunk_text(doc["content"])
for i, chunk in enumerate(chunks):
point_id = hashlib.md5(f"{doc['id']}_{i}".encode()).hexdigest()
points.append(PointStruct(
id=point_id,
vector=self.embed(chunk),
payload={
"text": chunk,
"source": doc.get("source", ""),
"chunk_index": i,
"doc_id": doc["id"]
}
))
self.qdrant.upsert(
collection_name=self.collection_name,
points=points
)
def search(self, query: str, top_k: int = 5) -> list[dict]:
"""Search for relevant chunks."""
query_vector = self.embed(query)
results = self.qdrant.search(
collection_name=self.collection_name,
query_vector=query_vector,
limit=top_k
)
return [
{
"text": r.payload["text"],
"source": r.payload["source"],
"score": r.score
}
for r in results
]
async def query(self, question: str, llm_client: LLMClient) -> str:
"""Full RAG query with retrieval and generation."""
# Retrieve relevant context
context_chunks = self.search(question, top_k=5)
context = "\n\n".join([c["text"] for c in context_chunks])
# Generate response
system = """You are a helpful assistant. Answer questions based on the provided context.
If the context doesn't contain relevant information, say so."""
prompt = f"""Context:
{context}
Question: {question}
Answer based on the context above:"""
return await llm_client.complete(prompt, system=system)
from string import Template
from typing import Any
class PromptTemplate:
"""Versioned prompt template with variable injection."""
def __init__(self, template: str, version: str = "1.0"):
self.template = template
self.version = version
self._template = Template(template)
def format(self, **kwargs: Any) -> str:
"""Format template with variables."""
return self._template.safe_substitute(**kwargs)
@classmethod
def from_file(cls, path: str) -> "PromptTemplate":
"""Load template from file."""
with open(path) as f:
content = f.read()
# Parse version from header if present
if content.startswith("# version:"):
version_line, template = content.split("\n", 1)
version = version_line.split(":")[1].strip()
return cls(template.strip(), version)
return cls(content)
# Example templates
EXTRACTION_TEMPLATE = PromptTemplate("""
Extract the following information from the text:
- Names: List of person names
- Dates: List of dates mentioned
- Organizations: List of company/org names
- Key Facts: List of important facts
Text:
$text
Return as JSON:
""", version="1.2")
CLASSIFICATION_TEMPLATE = PromptTemplate("""
Classify the following text into one of these categories:
$categories
Text: $text
Classification (respond with category name only):
""", version="1.0")
import tiktoken
class TokenManager:
"""Track and optimize token usage."""
def __init__(self, model: str = "gpt-4"):
self.encoding = tiktoken.encoding_for_model(model)
self.usage_log = []
def count_tokens(self, text: str) -> int:
"""Count tokens in text."""
return len(self.encoding.encode(text))
def truncate_to_limit(self, text: str, max_tokens: int) -> str:
"""Truncate text to fit within token limit."""
tokens = self.encoding.encode(text)
if len(tokens) <= max_tokens:
return text
return self.encoding.decode(tokens[:max_tokens])
def log_usage(self, input_tokens: int, output_tokens: int, model: str):
"""Log token usage for cost tracking."""
# Pricing per 1K tokens (example rates)
pricing = {
"gpt-4-turbo": {"input": 0.01, "output": 0.03},
"gpt-3.5-turbo": {"input": 0.0005, "output": 0.0015},
"claude-3-sonnet": {"input": 0.003, "output": 0.015},
}
rates = pricing.get(model, {"input": 0.01, "output": 0.03})
cost = (input_tokens / 1000 * rates["input"]) + (output_tokens / 1000 * rates["output"])
self.usage_log.append({
"model": model,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"cost": cost
})
def get_total_cost(self) -> float:
"""Get total cost from usage log."""
return sum(entry["cost"] for entry in self.usage_log)
from dataclasses import dataclass
from typing import Callable
import json
@dataclass
class EvaluationResult:
score: float
passed: bool
details: dict
class AIEvaluator:
"""Evaluate AI output quality."""
def __init__(self):
self.metrics = {}
def add_metric(self, name: str, evaluator: Callable[[str, str], float]):
"""Add evaluation metric."""
self.metrics[name] = evaluator
def evaluate(self, expected: str, actual: str) -> dict[str, EvaluationResult]:
"""Run all evaluation metrics."""
results = {}
for name, evaluator in self.metrics.items():
score = evaluator(expected, actual)
results[name] = EvaluationResult(
score=score,
passed=score >= 0.8,
details={"expected": expected[:100], "actual": actual[:100]}
)
return results
# Common evaluation functions
def exact_match(expected: str, actual: str) -> float:
"""Check for exact match."""
return 1.0 if expected.strip() == actual.strip() else 0.0
def contains_match(expected: str, actual: str) -> float:
"""Check if expected is contained in actual."""
return 1.0 if expected.lower() in actual.lower() else 0.0
def json_valid(expected: str, actual: str) -> float:
"""Check if output is valid JSON."""
try:
json.loads(actual)
return 1.0
except:
return 0.0
When implementing AI features, I will provide:
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