Loading...
Loading...
Compare original and translation side by side
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarize this article in 3 sentences: [text]"}
],
temperature=0 # Deterministic output
)
print(response.choices[0].message.content)import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const schema = z.object({
name: z.string(),
sentiment: z.enum(['positive', 'negative', 'neutral']),
});
const { object } = await generateObject({
model: openai('gpt-4'),
schema,
prompt: 'Extract sentiment from: "This product is amazing!"',
});from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarize this article in 3 sentences: [text]"}
],
temperature=0 # Deterministic output
)
print(response.choices[0].message.content)import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const schema = z.object({
name: z.string(),
sentiment: z.enum(['positive', 'negative', 'neutral']),
});
const { object } = await generateObject({
model: openai('gpt-4'),
schema,
prompt: 'Extract sentiment from: "This product is amazing!"',
});| Goal | Technique | Token Cost | Reliability | Use Case |
|---|---|---|---|---|
| Simple, well-defined task | Zero-Shot | ⭐⭐⭐⭐⭐ Minimal | ⭐⭐⭐ Medium | Translation, simple summarization |
| Specific format/style | Few-Shot | ⭐⭐⭐ Medium | ⭐⭐⭐⭐ High | Classification, entity extraction |
| Complex reasoning | Chain-of-Thought | ⭐⭐ Higher | ⭐⭐⭐⭐⭐ Very High | Math, logic, multi-hop QA |
| Structured data output | JSON Mode / Tools | ⭐⭐⭐⭐ Low-Med | ⭐⭐⭐⭐⭐ Very High | API responses, data extraction |
| Multi-step workflows | Prompt Chaining | ⭐⭐⭐ Medium | ⭐⭐⭐⭐ High | Pipelines, complex tasks |
| Knowledge retrieval | RAG | ⭐⭐ Higher | ⭐⭐⭐⭐ High | QA over documents |
| Agent behaviors | ReAct (Tool Use) | ⭐ Highest | ⭐⭐⭐ Medium | Multi-tool, complex tasks |
START
├─ Need structured JSON? → Use JSON Mode / Tool Calling (references/structured-outputs.md)
├─ Complex reasoning required? → Use Chain-of-Thought (references/chain-of-thought.md)
├─ Specific format/style needed? → Use Few-Shot Learning (references/few-shot-learning.md)
├─ Knowledge from documents? → Use RAG (references/rag-patterns.md)
├─ Multi-step workflow? → Use Prompt Chaining (references/prompt-chaining.md)
├─ Agent with tools? → Use Tool Use / ReAct (references/tool-use-guide.md)
└─ Simple task → Use Zero-Shot (references/zero-shot-patterns.md)| 目标 | 技术 | Token成本 | 可靠性 | 适用场景 |
|---|---|---|---|---|
| 简单、定义明确的任务 | 零样本 | ⭐⭐⭐⭐⭐ 最低 | ⭐⭐⭐ 中等 | 翻译、简单摘要 |
| 特定格式/风格 | 少样本 | ⭐⭐⭐ 中等 | ⭐⭐⭐⭐ 高 | 分类、实体提取 |
| 复杂推理 | 思维链 | ⭐⭐ 较高 | ⭐⭐⭐⭐⭐ 极高 | 数学、逻辑、多跳问答 |
| 结构化数据输出 | JSON模式/工具调用 | ⭐⭐⭐⭐ 低-中等 | ⭐⭐⭐⭐⭐ 极高 | API响应、数据提取 |
| 多步骤工作流 | 提示词链式调用 | ⭐⭐⭐ 中等 | ⭐⭐⭐⭐ 高 | 流水线、复杂任务 |
| 知识检索 | RAG | ⭐⭐ 较高 | ⭐⭐⭐⭐ 高 | 基于文档的问答 |
| Agent行为 | ReAct(工具调用) | ⭐ 最高 | ⭐⭐⭐ 中等 | 多工具、复杂任务 |
START
├─ 需要结构化JSON? → 使用JSON模式/工具调用(参考references/structured-outputs.md)
├─ 需要复杂推理? → 使用思维链(参考references/chain-of-thought.md)
├─ 需要特定格式/风格? → 使用少样本学习(参考references/few-shot-learning.md)
├─ 需要文档知识? → 使用RAG(参考references/rag-patterns.md)
├─ 多步骤工作流? → 使用提示词链式调用(参考references/prompt-chaining.md)
├─ 带工具的Agent? → 使用工具调用/ReAct(参考references/tool-use-guide.md)
└─ 简单任务 → 使用零样本(参考references/zero-shot-patterns.md)temperature=0prompt = """
Summarize the following customer review in 2 sentences, focusing on key concerns:
Review: [customer feedback text]
Summary:
"""references/zero-shot-patterns.mdtemperature=0prompt = """
Summarize the following customer review in 2 sentences, focusing on key concerns:
Review: [customer feedback text]
Summary:
"""references/zero-shot-patterns.mdprompt = """
Solve this problem step by step:
A train leaves Station A at 2 PM going 60 mph.
Another leaves Station B at 3 PM going 80 mph.
Stations are 300 miles apart. When do they meet?
Let's think through this step by step:
"""references/chain-of-thought.mdprompt = """
Solve this problem step by step:
A train leaves Station A at 2 PM going 60 mph.
Another leaves Station B at 3 PM going 80 mph.
Stations are 300 miles apart. When do they meet?
Let's think through this step by step:
"""references/chain-of-thought.mdprompt = """
Classify sentiment of movie reviews.
Examples:
Review: "Absolutely fantastic! Loved every minute."
Sentiment: positive
Review: "Waste of time. Terrible acting."
Sentiment: negative
Review: "It was okay, nothing special."
Sentiment: neutral
Review: "{new_review}"
Sentiment:
"""references/few-shot-learning.mdprompt = """
Classify sentiment of movie reviews.
Examples:
Review: "Absolutely fantastic! Loved every minute."
Sentiment: positive
Review: "Waste of time. Terrible acting."
Sentiment: negative
Review: "It was okay, nothing special."
Sentiment: neutral
Review: "{new_review}"
Sentiment:
"""references/few-shot-learning.mdfrom openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Extract user data as JSON."},
{"role": "user", "content": "From bio: 'Sarah, 28, sarah@example.com'"}
],
response_format={"type": "json_object"}
)import anthropic
client = anthropic.Anthropic()
tools = [{
"name": "record_data",
"description": "Record structured user information",
"input_schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}
}]
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Extract: 'Sarah, 28'"}]
)import { generateObject } from 'ai';
import { z } from 'zod';
const schema = z.object({
name: z.string(),
age: z.number(),
});
const { object } = await generateObject({
model: openai('gpt-4'),
schema,
prompt: 'Extract: "Sarah, 28"',
});references/structured-outputs.mdfrom openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Extract user data as JSON."},
{"role": "user", "content": "From bio: 'Sarah, 28, sarah@example.com'"}
],
response_format={"type": "json_object"}
)import anthropic
client = anthropic.Anthropic()
tools = [{
"name": "record_data",
"description": "Record structured user information",
"input_schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}
}]
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Extract: 'Sarah, 28'"}]
)import { generateObject } from 'ai';
import { z } from 'zod';
const schema = z.object({
name: z.string(),
age: z.number(),
});
const { object } = await generateObject({
model: openai('gpt-4'),
schema,
prompt: 'Extract: "Sarah, 28"',
});references/structured-outputs.md1. Role/Persona
2. Capabilities and knowledge domain
3. Behavior guidelines
4. Output format constraints
5. Safety/ethical boundariessystem_prompt = """
You are a senior software engineer conducting code reviews.
Expertise:
- Python best practices (PEP 8, type hints)
- Security vulnerabilities (SQL injection, XSS)
- Performance optimization
Review style:
- Constructive and educational
- Prioritize: Critical > Major > Minor
Output format:1. 角色/人设
2. 能力与知识领域
3. 行为准则
4. 输出格式约束
5. 安全/伦理边界system_prompt = """
You are a senior software engineer conducting code reviews.
Expertise:
- Python best practices (PEP 8, type hints)
- Security vulnerabilities (SQL injection, XSS)
- Performance optimization
Review style:
- Constructive and educational
- Prioritize: Critical > Major > Minor
Output format:
**Anthropic Claude with XML tags:**
```python
system_prompt = """
<capabilities>
- Answer product questions
- Troubleshoot common issues
</capabilities>
<guidelines>
- Use simple, non-technical language
- Escalate refund requests to humans
</guidelines>
"""
**Anthropic Claude结合XML标签:**
```python
system_prompt = """
<capabilities>
- Answer product questions
- Troubleshoot common issues
</capabilities>
<guidelines>
- Use simple, non-technical language
- Escalate refund requests to humans
</guidelines>
"""tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
}]
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools,
tool_choice="auto"
)undefinedtools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
}]
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools,
tool_choice="auto"
)undefined
See `references/tool-use-guide.md` for multi-tool workflows and ReAct patterns.
详见`references/tool-use-guide.md`获取多工具工作流及ReAct模式。from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
summarize_prompt = ChatPromptTemplate.from_template(
"Summarize: {article}"
)
title_prompt = ChatPromptTemplate.from_template(
"Create title for: {summary}"
)
llm = ChatOpenAI(model="gpt-4")
chain = summarize_prompt | llm | title_prompt | llm
result = chain.invoke({"article": "..."})undefinedfrom langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
summarize_prompt = ChatPromptTemplate.from_template(
"Summarize: {article}"
)
title_prompt = ChatPromptTemplate.from_template(
"Create title for: {summary}"
)
llm = ChatOpenAI(model="gpt-4")
chain = summarize_prompt | llm | title_prompt | llm
result = chain.invoke({"article": "..."})undefined
See `references/prompt-chaining.md` for LangChain, LlamaIndex, and DSPy patterns.
详见`references/prompt-chaining.md`获取LangChain、LlamaIndex及DSPy模式。pip install langchain langchain-openai langchain-anthropic/langchain-ai/langchainpip install llama-index/run-llama/llama_indexpip install dspy-aistanfordnlp/dspypip install openai/openai/openai-pythonpip install anthropic/anthropics/anthropic-sdk-pythonpip install langchain langchain-openai langchain-anthropic/langchain-ai/langchainpip install llama-index/run-llama/llama_indexpip install dspy-aistanfordnlp/dspypip install openai/openai/openai-pythonpip install anthropic/anthropics/anthropic-sdk-pythonnpm install ai @ai-sdk/openai @ai-sdk/anthropicnpm install langchain @langchain/openai/langchain-ai/langchainjsnpm install openainpm install @anthropic-ai/sdk| Library | Complexity | Multi-Provider | Best For |
|---|---|---|---|
| LangChain | High | ✅ | Complex workflows, RAG |
| LlamaIndex | Medium | ✅ | Data-centric RAG |
| DSPy | High | ✅ | Research, optimization |
| Vercel AI SDK | Low-Medium | ✅ | React/Next.js apps |
| Provider SDKs | Low | ❌ | Single-provider apps |
npm install ai @ai-sdk/openai @ai-sdk/anthropicnpm install langchain @langchain/openai/langchain-ai/langchainjsnpm install openainpm install @anthropic-ai/sdk| 库 | 复杂度 | 多提供商支持 | 最佳适用场景 |
|---|---|---|---|
| LangChain | 高 | ✅ | 复杂工作流、RAG |
| LlamaIndex | 中 | ✅ | 数据中心型RAG |
| DSPy | 高 | ✅ | 研究、优化 |
| Vercel AI SDK | 低-中 | ✅ | React/Next.js应用 |
| 提供商SDK | 低 | ❌ | 单提供商应用 |
PROMPTS = {
"v1.0": {
"system": "You are a helpful assistant.",
"version": "2025-01-15",
"notes": "Initial version"
},
"v1.1": {
"system": "You are a helpful assistant. Always cite sources.",
"version": "2025-02-01",
"notes": "Reduced hallucination"
}
}PROMPTS = {
"v1.0": {
"system": "You are a helpful assistant.",
"version": "2025-01-15",
"notes": "Initial version"
},
"v1.1": {
"system": "You are a helpful assistant. Always cite sources.",
"version": "2025-02-01",
"notes": "Reduced hallucination"
}
}def tracked_completion(prompt, model):
response = client.messages.create(model=model, ...)
usage = response.usage
cost = calculate_cost(usage.input_tokens, usage.output_tokens, model)
log_metrics({
"input_tokens": usage.input_tokens,
"output_tokens": usage.output_tokens,
"cost_usd": cost,
"timestamp": datetime.now()
})
return responsedef tracked_completion(prompt, model):
response = client.messages.create(model=model, ...)
usage = response.usage
cost = calculate_cost(usage.input_tokens, usage.output_tokens, model)
log_metrics({
"input_tokens": usage.input_tokens,
"output_tokens": usage.output_tokens,
"cost_usd": cost,
"timestamp": datetime.now()
})
return responsefrom tenacity import retry, stop_after_attempt, wait_exponential
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=10)
)
def robust_completion(prompt):
try:
return client.messages.create(...)
except anthropic.RateLimitError:
raise # Retry
except anthropic.APIError as e:
return fallback_completion(prompt)from tenacity import retry, stop_after_attempt, wait_exponential
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=10)
)
def robust_completion(prompt):
try:
return client.messages.create(...)
except anthropic.RateLimitError:
raise # 重试
except anthropic.APIError as e:
return fallback_completion(prompt)def sanitize_user_input(text: str) -> str:
dangerous = [
"ignore previous instructions",
"ignore all instructions",
"you are now",
]
cleaned = text.lower()
for pattern in dangerous:
if pattern in cleaned:
raise ValueError("Potential injection detected")
return textdef sanitize_user_input(text: str) -> str:
dangerous = [
"ignore previous instructions",
"ignore all instructions",
"you are now",
]
cleaned = text.lower()
for pattern in dangerous:
if pattern in cleaned:
raise ValueError("Potential injection detected")
return texttest_cases = [
{
"input": "What is 2+2?",
"expected_contains": "4",
"should_not_contain": ["5", "incorrect"]
}
]
def test_prompt_quality(case):
output = generate_response(case["input"])
assert case["expected_contains"] in output
for phrase in case["should_not_contain"]:
assert phrase not in output.lower()scripts/prompt-validator.pyscripts/ab-test-runner.pytest_cases = [
{
"input": "What is 2+2?",
"expected_contains": "4",
"should_not_contain": ["5", "incorrect"]
}
]
def test_prompt_quality(case):
output = generate_response(case["input"])
assert case["expected_contains"] in output
for phrase in case["should_not_contain"]:
assert phrase not in output.lower()scripts/prompt-validator.pyscripts/ab-test-runner.py<thinking>references/multi-model-portability.md<thinking>references/multi-model-portability.mdundefinedundefined
**2. Prompt injection vulnerability**
```python
**2. 提示词注入漏洞**
```python
**3. Wrong temperature for task**
```python
**3. 任务温度设置错误**
```python
**4. Not validating structured outputs**
```python
**4. 未验证结构化输出**
```pythonundefinedundefinedexamples/openai-examples.pyexamples/anthropic-examples.pyexamples/langchain-examples.pyexamples/rag-complete-example.pyexamples/vercel-ai-examples.tsexamples/openai-examples.pyexamples/anthropic-examples.pyexamples/langchain-examples.pyexamples/rag-complete-example.pyexamples/vercel-ai-examples.tsscripts/prompt-validator.pyscripts/token-counter.pyscripts/template-generator.pyscripts/ab-test-runner.pyscripts/prompt-validator.pyscripts/token-counter.pyscripts/template-generator.pyscripts/ab-test-runner.pyreferences/zero-shot-patterns.mdreferences/chain-of-thought.mdreferences/few-shot-learning.mdreferences/structured-outputs.mdreferences/tool-use-guide.mdreferences/prompt-chaining.mdreferences/rag-patterns.mdreferences/multi-model-portability.mdreferences/zero-shot-patterns.mdreferences/chain-of-thought.mdreferences/few-shot-learning.mdreferences/structured-outputs.mdreferences/tool-use-guide.mdreferences/prompt-chaining.mdreferences/rag-patterns.mdreferences/multi-model-portability.mdbuilding-ai-chatllm-evaluationmodel-servingapi-patternsdocumentation-generationbuilding-ai-chatllm-evaluationmodel-servingapi-patternsdocumentation-generation