AI SDKs
Email checker integration with AI SDKs. Anthropic, OpenAI, LangChain, Vercel AI SDK guides.
使用流行的 AI 框架和 SDK 构建具有内置邮箱验证功能的 AI 应用。
支持的框架
Vercel AI SDK
Next.js 和 React 应用的工具调用
LangChain
Python 和 JavaScript 中的 LangChain 代理自定义工具
OpenAI 函数调用
使用 OpenAI GPT 模型的原生函数调用
Anthropic 工具使用
使用 Claude 模型的工具使用
对比
| 框架 | 语言 | 最适合 | 复杂度 |
|---|---|---|---|
| Vercel AI SDK | TypeScript | Next.js 应用、流式传输 | 低 |
| LangChain | Python/JS | 复杂代理、链式调用 | 中 |
| OpenAI Functions | 任意 | 直接 GPT 集成 | 中 |
| Anthropic Tools | 任意 | 直接 Claude 集成 | 中 |
快速示例
这是一个适用于各框架的简单工具定义:
const verifyEmailTool = {
name: 'verify_email',
description: '验证邮箱地址是否有效且可接收邮件',
parameters: {
type: 'object',
properties: {
email: {
type: 'string',
description: '要验证的邮箱地址',
},
},
required: ['email'],
},
execute: async ({ email }) => {
const response = await fetch('https://api.emailverify.ai/v1/verify', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.EMAILVERIFY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ email }),
});
return response.json();
},
};最佳实践
1. 错误处理
始终在工具中优雅地处理 API 错误:
execute: async ({ email }) => {
try {
const response = await fetch('https://api.emailverify.ai/v1/verify', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.EMAILVERIFY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ email }),
});
if (!response.ok) {
if (response.status === 429) {
return { error: '超出速率限制。请稍后重试。' };
}
return { error: `验证失败: ${response.statusText}` };
}
return response.json();
} catch (error) {
return { error: '网络错误。无法访问验证服务。' };
}
}2. 清晰的工具描述
编写描述性的工具定义,让 AI 知道何时使用它们:
description: `验证邮箱地址是否有效且可接收邮件。
返回值:
- status: "valid"(有效)、"invalid"(无效)或 "unknown"(未知)
- deliverable: 邮箱是否可以接收邮件
- disposable: 是否为临时邮箱服务
- role: 是否为角色邮箱(如 info@ 或 support@)
- score: 置信度评分(0 到 1)
在以下情况下使用此工具:
- 用户要求验证邮箱
- 检查邮箱是否真实存在
- 验证联系信息`3. 批量处理以提高效率
验证多个邮箱时,使用批量端点:
const verifyEmailsBulkTool = {
name: 'verify_emails_bulk',
description: '一次验证多个邮箱。比逐个验证更高效。',
parameters: {
type: 'object',
properties: {
emails: {
type: 'array',
items: { type: 'string' },
maxItems: 100,
},
},
required: ['emails'],
},
};4. 缓存结果
考虑缓存验证结果以节省积分:
const cache = new Map<string, { result: any; timestamp: number }>();
const CACHE_TTL = 3600000; // 1 小时
async function verifyWithCache(email: string) {
const cached = cache.get(email);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.result;
}
const result = await verifyEmail(email);
cache.set(email, { result, timestamp: Date.now() });
return result;
}