context7
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseContext7
Context7
Overview
概述
Context7 is a service that provides LLMs with the latest library documentation. It prevents hallucinations from outdated training data or non-existent APIs, enabling retrieval of version-specific accurate documentation and code examples.
Context7是一项为大语言模型(LLMs)提供最新库文档的服务。它可以避免因过时训练数据或不存在的API产生的幻觉,支持检索特定版本的准确文档和代码示例。
When to Use
使用场景
Use this skill in the following cases:
- Explicit instruction: When the user instructs "use context7" or "check the latest documentation"
- Library usage questions: When asked about how to use a specific library's API, hooks, or functions
- Code example requests: When asked for code examples using a specific library
- Version-specific information: When library information for a specific version is needed
- Uncertain API information: When your knowledge might be outdated and latest information verification is needed
在以下场景中使用本Skill:
- 明确指令:当用户指示“use context7”或“查看最新文档”时
- 库使用问题:当被问及特定库的API、钩子(hooks)或函数的使用方法时
- 代码示例请求:当用户请求使用特定库的代码示例时
- 特定版本信息:当需要特定版本的库信息时
- 不确定的API信息:当你的知识可能过时,需要验证最新信息时
Workflow
工作流程
Step 1: Resolve Library ID
步骤1:解析库ID
First, obtain the Context7 ID for the target library.
API call:
bash
curl "https://context7.com/api/v2/libs/search?libraryName=LIBRARY_NAME&query=CONTEXT_QUERY" \
-H "Authorization: Bearer $CONTEXT7_API_KEY"Example:
bash
undefined首先,获取目标库的Context7 ID。
API调用:
bash
curl "https://context7.com/api/v2/libs/search?libraryName=LIBRARY_NAME&query=CONTEXT_QUERY" \
-H "Authorization: Bearer $CONTEXT7_API_KEY"示例:
bash
undefinedSearch for React library
搜索React库
curl "https://context7.com/api/v2/libs/search?libraryName=react&query=hooks"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
**Criteria for selecting from response:**
- `trustScore`: Trust score (higher is better)
- `totalSnippets`: Number of available documents (more means richer information)
- `versions`: Verify that the required version is includedcurl "https://context7.com/api/v2/libs/search?libraryName=react&query=hooks"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
**响应结果选择标准:**
- `trustScore`:信任分数(越高越好)
- `totalSnippets`:可用文档数量(越多表示信息越丰富)
- `versions`:验证是否包含所需版本Step 2: Retrieve Documentation
步骤2:检索文档
Use the resolved library ID to retrieve specific documentation.
API call:
bash
curl "https://context7.com/api/v2/context?libraryId=LIBRARY_ID&query=SPECIFIC_QUERY" \
-H "Authorization: Bearer $CONTEXT7_API_KEY"Example:
bash
undefined使用解析得到的库ID检索特定文档。
API调用:
bash
curl "https://context7.com/api/v2/context?libraryId=LIBRARY_ID&query=SPECIFIC_QUERY" \
-H "Authorization: Bearer $CONTEXT7_API_KEY"示例:
bash
undefinedRetrieve information about React's useEffect
检索React的useEffect相关信息
curl "https://context7.com/api/v2/context?libraryId=/facebook/react&query=useEffect cleanup function"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
**Query best practices:**
- Use specific queries ("useEffect cleanup function" rather than "hooks")
- Be clear about the purpose ("authentication middleware", "form validation", etc.)curl "https://context7.com/api/v2/context?libraryId=/facebook/react&query=useEffect cleanup function"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
-H "Authorization: Bearer $CONTEXT7_API_KEY"
**查询最佳实践:**
- 使用具体的查询词(比如“useEffect cleanup function”而非“hooks”)
- 明确查询目的(比如“认证中间件”、“表单验证”等)Step 3: Respond to User
步骤3:响应用户
Respond to the user's question based on the retrieved documentation.
Information to include in the response:
- Retrieved code examples
- API explanations
- Documentation source URLs (for reference)
根据检索到的文档响应用户的问题。
响应中需包含的信息:
- 检索到的代码示例
- API说明
- 文档来源URL(供参考)
Examples
示例
Example 1: React Hook Usage
示例1:React Hook使用
User: "How do I write a cleanup function with React's useEffect?"
Execution steps:
- Library search: ,
libraryName=reactquery=useEffect cleanup - Documentation retrieval: ,
libraryId=/facebook/reactquery=useEffect cleanup function - Respond with the latest code examples and best practices
用户:“如何使用React的useEffect编写清理函数?”
执行步骤:
- 库搜索:,
libraryName=reactquery=useEffect cleanup - 文档检索:,
libraryId=/facebook/reactquery=useEffect cleanup function - 返回最新的代码示例和最佳实践
Example 2: Next.js Routing
示例2:Next.js路由
User: "Use context7 to show me how to implement authentication middleware with Next.js App Router"
Execution steps:
- Library search: ,
libraryName=next.jsquery=middleware authentication - Documentation retrieval: ,
libraryId=/vercel/next.jsquery=middleware authentication route protection - Respond with the latest App Router compatible middleware implementation
用户:“Use context7展示如何用Next.js App Router实现认证中间件”
执行步骤:
- 库搜索:,
libraryName=next.jsquery=middleware authentication - 文档检索:,
libraryId=/vercel/next.jsquery=middleware authentication route protection - 返回与App Router兼容的最新中间件实现方案
Example 3: Multiple Libraries Combination
示例3:多库组合
User: "How to create a button component using TailwindCSS and shadcn/ui"
Execution steps:
- Resolve IDs for each library
- Retrieve documentation for both
- Respond with a combined implementation example
用户:“如何使用TailwindCSS和shadcn/ui创建按钮组件?”
执行步骤:
- 解析每个库的ID
- 检索两个库的文档
- 返回组合实现的示例
Error Handling
错误处理
| Error | Solution |
|---|---|
| 404 (Not Found) | Search again with a different library name (e.g., "nextjs" → "next.js") |
| 429 (Rate Limit) | Wait a moment and retry |
| Empty response | Retry with a more general query |
| 错误 | 解决方案 |
|---|---|
| 404 (未找到) | 使用不同的库名称重新搜索(例如“nextjs” → “next.js”) |
| 429 (请求频率限制) | 稍等片刻后重试 |
| 空响应 | 使用更通用的查询词重试 |
Resources
资源
For detailed API specifications, see .
references/api_reference.md- Endpoint details
- Response field descriptions
- Rate limit information
- Error code list
有关详细的API规范,请参阅。
references/api_reference.md- 端点详情
- 响应字段说明
- 请求频率限制信息
- 错误代码列表