1. Overview
  2. Introduction
  3. Integration with AI Services (Advanced)

Integration with AI Services (Advanced)

OpenAI

Python

import openai
import requests

def generate_and_send_email(openai_api_key, lemon_api_key, user_data):
    openai.api_key = openai_api_key
    
    # Generate content with OpenAI
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=f"Write an email to {user_data['name']} about their recent activity: {user_data['activity']}",
        max_tokens=200
    )
    email_content = response.choices[0].text.strip()

    # Send email using Lemon API
    response = requests.post(
        'https://app.xn--lemn-sqa.com/api/transactional/send',
        headers={
            'Content-Type': 'application/json',
            'X-Auth-APIKey': lemon_api_key
        },
        json={
            "fromname": "Your SaaS",
            "fromemail": "noreply@yoursaas.com",
            "to": user_data['email'],
            "subject": "Your Activity Update",
            "body": f"<html><body>{email_content}</body></html>"
        }
    )
    return response.json()

# Usage
result = generate_and_send_email(
    'your_openai_api_key',
    'your_lemon_api_key',
    {
        "name": "John",
        "email": "john@example.com",
        "activity": "Completed 5 workouts this week"
    }
)
print(result)

Anthropic (Claude)

JS

const axios = require('axios');

async function generateAndSendEmail(anthropicApiKey, lemonApiKey, userData) {
    // Generate content with Anthropic's Claude
    const anthropicResponse = await axios.post(
        'https://api.anthropic.com/v1/complete',
        {
            prompt: `Human: Write an email to ${userData.name} about their recent activity: ${userData.activity}\n\nAssistant:`,
            model: "claude-v1",
            max_tokens_to_sample: 200,
            stop_sequences: ["\n\nHuman:"]
        },
        {
            headers: {
                'Content-Type': 'application/json',
                'X-API-Key': anthropicApiKey
            }
        }
    );

    const emailContent = anthropicResponse.data.completion.trim();

    // Send email using Lemon API
    const lemonResponse = await axios.post(
        'https://app.xn--lemn-sqa.com/api/transactional/send',
        {
            fromname: "Your SaaS",
            fromemail: "noreply@yoursaas.com",
            to: userData.email,
            subject: "Your Activity Update",
            body: `<html><body>${emailContent}</body></html>`
        },
        {
            headers: {
                'Content-Type': 'application/json',
                'X-Auth-APIKey': lemonApiKey
            }
        }
    );

    return lemonResponse.data;
}

// Usage
generateAndSendEmail(
    'your_anthropic_api_key',
    'your_lemon_api_key',
    {
        name: "John",
        email: "john@example.com",
        activity: "Completed 5 workouts this week"
    }
)
.then(result => console.log(result))
.catch(error => console.error(error));

Google AI

Python

from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value
import requests

def generate_and_send_email(project_id, location, model_name, lemon_api_key, user_data):
    # Initialize Google AI Platform client
    aiplatform.init(project=project_id, location=location)

    # Generate content with Google AI
    model = aiplatform.Model(model_name=model_name)
    instance = json_format.ParseDict({"prompt": f"Write an email to {user_data['name']} about their recent activity: {user_data['activity']}"}, Value())
    parameters = json_format.ParseDict({"temperature": 0.2, "maxOutputTokens": 256, "topK": 40, "topP": 0.95}, Value())
    
    response = model.predict([instance], parameters=parameters)
    email_content = response.predictions[0]

    # Send email using Lemon API
    response = requests.post(
        'https://app.xn--lemn-sqa.com/api/transactional/send',
        headers={
            'Content-Type': 'application/json',
            'X-Auth-APIKey': lemon_api_key
        },
        json={
            "fromname": "Your SaaS",
            "fromemail": "noreply@yoursaas.com",
            "to": user_data['email'],
            "subject": "Your Activity Update",
            "body": f"<html><body>{email_content}</body></html>"
        }
    )
    return response.json()

# Usage
result = generate_and_send_email(
    'your-google-cloud-project-id',
    'us-central1',
    'text-bison@001',
    'your_lemon_api_key',
    {
        "name": "John",
        "email": "john@example.com",
        "activity": "Completed 5 workouts this week"
    }
)
print(result)

IBM Watson

Python

from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import requests

def generate_and_send_email(watson_api_key, watson_url, watson_assistant_id, lemon_api_key, user_data):
    # Initialize Watson Assistant
    authenticator = IAMAuthenticator(watson_api_key)
    assistant = AssistantV2(
        version='2021-06-14',
        authenticator=authenticator
    )
    assistant.set_service_url(watson_url)

    # Generate content with Watson
    response = assistant.message(
        assistant_id=watson_assistant_id,
        input={
            'text': f"Write an email to {user_data['name']} about their recent activity: {user_data['activity']}"
        }
    ).get_result()

    email_content = response['output']['generic'][0]['text']

    # Send email using Lemon API
    response = requests.post(
        'https://app.xn--lemn-sqa.com/api/transactional/send',
        headers={
            'Content-Type': 'application/json',
            'X-Auth-APIKey': lemon_api_key
        },
        json={
            "fromname": "Your SaaS",
            "fromemail": "noreply@yoursaas.com",
            "to": user_data['email'],
            "subject": "Your Activity Update",
            "body": f"<html><body>{email_content}</body></html>"
        }
    )
    return response.json()

# Usage
result = generate_and_send_email(
    'your_watson_api_key',
    'your_watson_url',
    'your_watson_assistant_id',
    'your_lemon_api_key',
    {
        "name": "John",
        "email": "john@example.com",
        "activity": "Completed 5 workouts this week"
    }
)
print(result)

Was this article helpful?
© 2024 Lemón SES