mcpField
A
ai-engineer

ai-engineer

github.com/htlin222开发7845 次安装3 周前更新
代码仓库

关于 ai-engineer

ai-engineer 是面向构建生产级 LLM 应用和 AI 系统的技能,适合开发者参考。内容涵盖 LLM API 集成、RAG、Agent、向量数据库和 Token 成本优化。

主要功能

  1. 01集成 LLM API
  2. 02生成结构化输出
  3. 03处理文档并构建 RAG
  4. 04设置向量存储与检索
  5. 05优化 Token 使用成本

适用场景

  1. 01为应用添加 AI 聊天功能
  2. 02为文档搭建 RAG 检索
  3. 03创建 AI Agent
  4. 04优化 LLM 请求成本

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/htlin222/dotfiles --skill ai-engineer

安装后在 Claude Code 中输入/ai-engineer确认已加载。

详细介绍

AI 工程

构建用于生产环境的大模型应用和 AI 系统。

适用场景

  • 集成大模型 API
  • 构建 RAG 系统
  • 创建 AI Agent
  • 配置向量数据库
  • 优化 token 使用

大模型集成

API 配置
from anthropic import Anthropic

client = Anthropic()

def chat(messages: list[dict], system: str = None) -> str:
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        system=system or "You are a helpful assistant.",
        messages=messages
    )
    return response.content[0].text

# With retry and error handling
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=10))
def safe_chat(messages, system=None):
    try:
        return chat(messages, system)
    except Exception as e:
        logger.error(f"LLM call failed: {e}")
        raise
结构化输出
import json

def extract_structured(text: str, schema: dict) -> dict:
    prompt = f"""Extract information from the text according to this schema:
{json.dumps(schema, indent=2)}

Text: {text}

Return valid JSON only."""

    response = chat([{"role": "user", "content": prompt}])
    return json.loads(response)

RAG 系统

文档处理
from langchain.text_splitter import RecursiveCharacterTextSplitter

def chunk_documents(docs: list[str], chunk_size=1000, overlap=200):
    splitter = RecursiveCharacterTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=overlap,
        separators=["\n\n", "\n", ". ", " "]
    )
    return splitter.split_documents(docs)
向量存储
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams

client = QdrantClient(":memory:")  # or url="http://localhost:6333"

# Create collection
client.create_collection(
    collection_name="docs",
    vectors_config=VectorParams(size=1536, distance=Distance.COSINE)
)

# Upsert vectors
client.upsert(
    collection_name="docs",
    points=[
        {"id": i, "vector": embed(chunk), "payload": {"text": chunk}}
        for i, chunk in enumerate(chunks)
    ]
)

# Search
results = client.search(
    collection_name="docs",
    query_vector=embed(query),
    limit=5
)
RAG 查询
def rag_query(question: str, top_k=5) -> str:
    # Retrieve relevant chunks
    results = client.search(
        collection_name="docs",
        query_vector=embed(question),
        limit=top_k
    )

    context = "\n\n".join([r.payload["text"] for r in results])

    prompt = f"""Answer based on the context below.

Context:
{context}

Question: {question}

Answer:"""

    return chat([{"role": "user", "content": prompt}])

成本优化

  • 缓存常见查询
  • 对简单任务使用较小的模型
  • 尽可能批量发送请求
  • 按功能跟踪 token 使用量
  • 合理设置 max_tokens

示例

输入:“为此应用添加 AI 聊天” **操作:**配置大模型客户端,创建聊天端点,添加错误处理

输入:“为文档构建 RAG” **操作:**对文档进行分块,创建嵌入,配置向量存储,实现搜索

评论

支持 Markdown