OpenAI
Email checker with OpenAI SDK. Verify emails using GPT function calling.
Integrate EmailVerify directly with OpenAI's function calling API for GPT models.
Installation
npm install openaiBasic Setup
import OpenAI from 'openai';
const openai = new OpenAI();
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'verify_email',
description: 'Verify if an email address is valid and deliverable',
parameters: {
type: 'object',
properties: {
email: {
type: 'string',
description: 'The email address to verify',
},
},
required: ['email'],
},
},
},
];Implementation
import OpenAI from 'openai';
const openai = new OpenAI();
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'verify_email',
description: 'Verify if an email address is valid and deliverable',
parameters: {
type: 'object',
properties: {
email: {
type: 'string',
description: 'The email address to verify',
},
},
required: ['email'],
},
},
},
];
async function verifyEmail(email: string) {
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();
}
async function chat(userMessage: string) {
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: 'user', content: userMessage },
];
const response = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
tools,
});
const message = response.choices[0].message;
// Handle tool calls
if (message.tool_calls) {
for (const toolCall of message.tool_calls) {
if (toolCall.function.name === 'verify_email') {
const args = JSON.parse(toolCall.function.arguments);
const result = await verifyEmail(args.email);
messages.push(message);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result),
});
}
}
// Get final response
const finalResponse = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
});
return finalResponse.choices[0].message.content;
}
return message.content;
}
// Usage
const result = await chat('Is john@example.com a valid email?');
console.log(result);Multiple Tools
Add multiple email verification related tools:
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'verify_email',
description: 'Verify a single email address',
parameters: {
type: 'object',
properties: {
email: {
type: 'string',
description: 'The email address to verify',
},
},
required: ['email'],
},
},
},
{
type: 'function',
function: {
name: 'verify_emails_bulk',
description: 'Verify multiple email addresses at once',
parameters: {
type: 'object',
properties: {
emails: {
type: 'array',
items: { type: 'string' },
description: 'Array of email addresses to verify',
},
},
required: ['emails'],
},
},
},
{
type: 'function',
function: {
name: 'check_credits',
description: 'Check remaining verification credits',
parameters: {
type: 'object',
properties: {},
},
},
},
];With Streaming
async function chatStream(userMessage: string) {
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: 'user', content: userMessage },
];
const stream = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages,
tools,
stream: true,
});
let toolCalls: any[] = [];
for await (const chunk of stream) {
const delta = chunk.choices[0].delta;
if (delta.tool_calls) {
// Accumulate tool calls
for (const tc of delta.tool_calls) {
if (!toolCalls[tc.index]) {
toolCalls[tc.index] = { id: '', function: { name: '', arguments: '' } };
}
if (tc.id) toolCalls[tc.index].id = tc.id;
if (tc.function?.name) toolCalls[tc.index].function.name = tc.function.name;
if (tc.function?.arguments) toolCalls[tc.index].function.arguments += tc.function.arguments;
}
}
if (delta.content) {
process.stdout.write(delta.content);
}
}
// Handle tool calls if any
if (toolCalls.length > 0) {
// Process tool calls and continue conversation
// ...
}
}Python Example
from openai import OpenAI
import requests
import json
import os
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "verify_email",
"description": "Verify if an email address is valid and deliverable",
"parameters": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "The email address to verify"
}
},
"required": ["email"]
}
}
}
]
def verify_email(email: str) -> dict:
response = requests.post(
'https://api.emailverify.ai/v1/verify',
headers={
'Authorization': f'Bearer {os.environ["EMAILVERIFY_API_KEY"]}',
'Content-Type': 'application/json',
},
json={'email': email}
)
return response.json()
def chat(user_message: str) -> str:
messages = [{"role": "user", "content": user_message}]
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages,
tools=tools
)
message = response.choices[0].message
if message.tool_calls:
for tool_call in message.tool_calls:
if tool_call.function.name == "verify_email":
args = json.loads(tool_call.function.arguments)
result = verify_email(args["email"])
messages.append(message)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
final_response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages
)
return final_response.choices[0].message.content
return message.content
# Usage
result = chat("Is john@google.com a valid email?")
print(result)