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;
}