Skip to main content

Learn Advanced Features

Explore Documentation

Build Real Applications

1. Chatbot

Build an intelligent chatbot:
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.megallm.io/v1",
    api_key="your-key"
)

def chatbot(user_message, history=[]):
    history.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="gpt-4",
        messages=history
    )

    assistant_message = response.choices[0].message.content
    history.append({"role": "assistant", "content": assistant_message})

    return assistant_message, history

2. Content Generator

Generate blog posts, emails, or social media content:
def generate_content(topic, content_type="blog"):
    prompts = {
        "blog": f"Write a comprehensive blog post about {topic}",
        "email": f"Write a professional email about {topic}",
        "tweet": f"Write an engaging tweet about {topic}"
    }

    response = client.chat.completions.create(
        model="claude-3.5-sonnet",
        messages=[{"role": "user", "content": prompts[content_type]}],
        temperature=0.7
    )

    return response.choices[0].message.content

3. Code Assistant

Build a coding helper:
def code_assistant(task, language="python"):
    prompt = f"Write {language} code to {task}. Include comments and error handling."

    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.2  # Lower temperature for more deterministic code
    )

    return response.choices[0].message.content

4. Data Analyzer

Analyze data and generate insights:
def analyze_data(data_description):
    prompt = f"""
    Analyze this data and provide insights:
    {data_description}

    Provide:
    1. Key findings
    2. Trends
    3. Recommendations
    """

    response = client.chat.completions.create(
        model="claude-opus-4-1-20250805",  # Best for analysis
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content

Best Practices

  • GPT-4: Best for complex reasoning
  • GPT-3.5 Turbo: Fast and cost-effective
  • Claude Opus: Excellent for analysis and long context
  • Claude Sonnet: Balanced performance
  • Gemini Pro: Strong multimodal capabilities
See Models Catalog for detailed comparisons.
  • Start with cheaper models for testing
  • Use max_tokens to limit response length
  • Cache responses when possible
  • Use streaming to improve perceived performance
  • Monitor usage in your dashboard
from openai import OpenAI, AuthenticationError, RateLimitError
import time

def make_request_with_retry(messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="gpt-4",
                messages=messages
            )
        except RateLimitError:
            if attempt < max_retries - 1:
                time.sleep(2 ** attempt)  # Exponential backoff
            else:
                raise
        except AuthenticationError:
            print("Invalid API key")
            raise
  • Be specific and clear
  • Provide examples when needed
  • Use system messages to set context
  • Break complex tasks into steps
  • Test different temperature settings
  • Keep track of conversation history
  • Limit history to avoid token limits
  • Summarize old messages if needed
  • Use prompt caching for repeated content

Production Considerations

Security

  • Store API keys in environment variables
  • Never commit keys to version control
  • Use different keys for dev/staging/production
  • Rotate keys regularly
  • Monitor usage for anomalies

Performance

  • Use streaming for better UX
  • Implement caching where appropriate
  • Add retry logic with exponential backoff
  • Monitor response times
  • Consider using webhooks for async operations

Monitoring

  • Track token usage
  • Monitor error rates
  • Log API requests (without sensitive data)
  • Set up alerts for quota limits
  • Review costs regularly

Scaling

  • Implement rate limiting
  • Use queues for high-volume requests
  • Cache common responses
  • Consider batching requests
  • Plan for failover strategies

Join the Community

Get Help

Most common questions are answered in our FAQ.
Comprehensive guides available in Developer Docs.
Email us at support@megallm.io for technical assistance.
Found a bug? Report it on GitHub.

Useful Resources

Ready to Build?

Start building your AI application today. If you need help, we’re here for you!