DEV Community

Cover image for Building the Gemini API: A Deep Dive into CI/CD Pipelines and API Testing
Santosh khanal
Santosh khanal

Posted on

Building the Gemini API: A Deep Dive into CI/CD Pipelines and API Testing

Introduction

In this blog, I’ll walk you through the development of the Gemini API, its features, and the CI/CD pipeline I implemented using GitHub Actions and Docker. From testing to deployment, you’ll get a comprehensive look at how automation enhances the development process.

The Gemini API: A Gateway to Advanced Language Models

The Gemini API, a powerful language model developed by Google AI, serves as a versatile tool for a wide range of natural language processing tasks. This API empowers developers to seamlessly integrate sophisticated language capabilities into their applications, enabling tasks like text generation, translation, summarization, and code generation.

The Role of CI/CD in Modern Software Development

In today's fast-paced software development landscape, Continuous Integration and Continuous Delivery (CI/CD) have become indispensable practices. CI/CD pipelines automate the building, testing, and deployment of software, ensuring rapid delivery of high-quality applications. By streamlining development processes, CI/CD significantly reduces the time and effort required to release new features and updates.

Setting Up the Gemini API
The Gemini API operates on the principle of prompt-based interaction. Users provide textual prompts, and the API processes these inputs to generate relevant text outputs.

How Prompts Are Handled
At the core of the Gemini API is its ability to process user prompts. A prompt represents the user's input, and the API generates a meaningful response based on this input. The main function, generate_content, ensures accurate and context-aware responses.

Here’s a simplified version of the generate_content function:

def generate_content(prompt: str, instructions: str) -> str:
    """
    Generates content based on a user prompt and custom instructions.

    Args:
        prompt (str): The user's input.
        instructions (str): Instructions to customize the response.

    Returns:
        str: The generated content.
    """
    try:
        # Simulate processing the prompt (replace with actual model logic)
        response = f"Processed Prompt: '{prompt}' with Instructions: '{instructions}'"
        return response
    except Exception as e:
        return f"Error generating content: {str(e)}"


Enter fullscreen mode Exit fullscreen mode

Example Usage:

prompt = "Tell me about Python programming."
instructions = "Use a conversational tone."
print(generate_content(prompt, instructions))
# Output: Processed Prompt: 'Tell me about Python programming.' with Instructions: 'Use a conversational tone.'

Enter fullscreen mode Exit fullscreen mode

Custom Instructions and Their Importance

Custom instructions allow users to tailor the API’s response to meet their specific needs. This feature is particularly useful for applications requiring a personalized touch, such as chatbots, educational tools, or creative content generators.

Example Scenarios:
Formal Tone: "Explain recursion in programming using formal language."
Creative Style: "Write a poem about autumn."
By incorporating instructions, the API adapts to diverse use cases, enhancing its usability and flexibility.

Key Features of Our Implementation:
-Robust Error Handling: Our implementation includes robust error handling mechanisms to gracefully handle unexpected inputs or API errors.
-Asynchronous Processing: We leverage asynchronous programming techniques to optimize performance and enable concurrent processing of multiple requests.
-Caching: We implement caching strategies to reduce API call frequency and improve response times, especially for frequently used prompts.
-Security Measures: We prioritize security by implementing measures to protect sensitive information and prevent unauthorized access.

How it works

  • Users submit a POST request containing a prompt and optional instructions.
  • The system processes this input, adjusting it based on specific formatting or style requirements.
  • The final output is returned as a JSON response, suitable for seamless integration with various applications.

Gemini API with Flask
Flask is a lightweight and flexible Python web framework that makes it easy to build and deploy APIs. The Gemini API leverages Flask to expose its functionality via HTTP endpoints, allowing clients to interact with it seamlessly. Here’s how the integration works:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/generate', methods=['POST'])
def generate():
    data = request.get_json()

    # Validate input
    prompt = data.get('prompt')
    instructions = data.get('instructions', "Default Instructions")

    if not prompt:
        return jsonify({"error": "Prompt is required"}), 400

    # Generate response
    response = generate_content(prompt, instructions)
    return jsonify({"result": response})
Enter fullscreen mode Exit fullscreen mode

Integrating the Gemini API with Flask provides a robust and scalable framework for building APIs while maintaining simplicity and flexibility.

Testing the Gemini API
Testing ensures that the Gemini API is reliable, functional, and ready for deployment. This involves writing and running unit tests and integration tests to validate the API’s components and endpoints.

Unit Tests

These tests focus on individual functions or components, such as the generate_content function. Unit tests ensure that each function behaves as expected under various conditions.
Code Example: Unit Tests

import pytest
from app import generate_content

def test_generate_content_with_valid_input():
    prompt = "What is Python?"
    instructions = "Explain briefly."
    result = generate_content(prompt, instructions)
    assert result == "Processed Prompt: 'What is Python?' with Instructions: 'Explain briefly.'"

def test_generate_content_with_empty_prompt():
    prompt = ""
    instructions = "Use a formal tone."
    result = generate_content(prompt, instructions)
    assert result == "Processed Prompt: '' with Instructions: 'Use a formal tone.'"

def test_generate_content_with_missing_instructions():
    prompt = "What is Flask?"
    instructions = None
    result = generate_content(prompt, "Default Instructions")
    assert result == "Processed Prompt: 'What is Flask?' with Instructions: 'Default Instructions'"


Enter fullscreen mode Exit fullscreen mode

Integration Tests

These tests validate the API as a whole, ensuring that all components work together seamlessly. They involve making HTTP requests to the API endpoints and verifying the responses.
Integration tests use Flask’s test client to simulate HTTP requests to the API’s /generate endpoint.

Code Example: Integration Tests

from app import app
import pytest

@pytest.fixture
def client():
    with app.test_client() as client:
        yield client

def test_generate_endpoint_with_valid_data(client):
    response = client.post('/generate', json={
        "prompt": "What is machine learning?",
        "instructions": "Explain in simple terms."
    })
    assert response.status_code == 200
    data = response.get_json()
    assert "result" in data
    assert "Processed Prompt: 'What is machine learning?'" in data["result"]

def test_generate_endpoint_missing_prompt(client):
    response = client.post('/generate', json={
        "instructions": "Explain briefly."
    })
    assert response.status_code == 400
    data = response.get_json()
    assert "error" in data
    assert data["error"] == "Prompt is required"


Enter fullscreen mode Exit fullscreen mode

Setting Up CI/CD with Github Actions

Image description

Continuous Integration and Continuous Deployment (CI/CD) automate the process of testing, building, and deploying code changes, ensuring faster and more reliable delivery of software updates. In the Gemini API project, GitHub Actions is used to create a robust CI/CD pipeline. GitHub Actions is a powerful automation tool that integrates seamlessly with your repository.

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # Checkout code
      - name: Checkout code
        uses: actions/checkout@v3

      # Set up Python
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      # Install dependencies
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      # Run tests
      - name: Run tests
        run: pytest

  build:
    runs-on: ubuntu-latest
    needs: test
    steps:
      # Checkout code
      - name: Checkout code
        uses: actions/checkout@v3

      # Build Docker image
      - name: Build Docker image
        run: |
          docker build -t gemini-api:latest .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      # Checkout code
      - name: Checkout code
        uses: actions/checkout@v3

      # Deploy to production (example with SSH)
      - name: Deploy to Production
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
          SERVER_IP: ${{ secrets.SERVER_IP }}
        run: |
          ssh -i $SSH_KEY user@$SERVER_IP "docker pull gemini-api:latest && docker run -d -p 5000:5000 gemini-api:latest"


Enter fullscreen mode Exit fullscreen mode

Workflow Breakdown

Triggers
The workflow triggers on pushes or pull requests to the main branch.
Jobs
Test: Runs unit and integration tests using pytest.
Build: Builds a Docker image of the API after the tests pass.
Deploy: Deploys the Docker image to a production server via SSH.
Secrets
Use GitHub Secrets to securely store sensitive data like SSH_KEY or SERVER_IP.

Setting up a CI/CD pipeline involves configuring automation for building Docker images, running tests, and deploying to production. This ensures that every code change is validated and deployed efficiently without manual intervention.

Benefits of CI/CD Automation

Speed and Efficiency:
Automates repetitive tasks, reducing time to deploy updates.

Consistency:
Every code change follows the same steps, minimizing human error.

Early Bug Detection:
Running tests automatically detects issues before deployment.

Developer Focus:
Developers spend less time on manual tasks and more on building features.

Seamless Rollbacks:
Issues in the pipeline prevent faulty code from reaching production, and prior stable builds can be redeployed quickly.

Challenges and Solutions in the Gemini API Project

1: Debugging Failing Tests

Some tests failed intermittently, especially integration tests involving the Flask routes. These failures were difficult to debug as they only occurred in the CI pipeline, not locally.

Cause:

  • Environment inconsistencies between local and CI environments.
  • Missing dependencies or configurations in the test environment.

Solution:

  • Environment Standardization:
  • Used Docker for local development and ensured the same Docker image was used in the CI pipeline.
  • Enhanced Logging:
  • Added detailed logging to both the application and the tests to pinpoint failures.
  • Test Isolation:
  • Rewrote tests to isolate dependencies, mocking external services or APIs where necessary

2: Managing Secrets Securely

Storing sensitive information such as SSH keys and server IPs for deployment posed a security risk.

Solution:

  • GitHub Secrets: Leveraged GitHub Secrets to securely store and access sensitive data in workflows.
  • Environment Variables: Used environment variables within the deployment server to store sensitive configuration data, avoiding hardcoding them in the application.
  • Access Control: Limited access to the production server to authorized users and roles, ensuring secure deployment.

3: Docker Image Optimization

The Docker image built for the API was large and took a long time to build and deploy.

Solution:

  1. Multi-Stage Builds:
    Used multi-stage builds in the Dockerfile to minimize the
    final image size.

  2. Layer Caching:
    Optimized the Dockerfile to take advantage of layer caching
    by placing COPY requirements.txt and pip install early in
    the file.

4: Deployment Rollbacks

If a deployment failed or introduced a bug, rolling back to the previous stable version was time-consuming.

Solution:

  1. Tagging Docker Images:
    Tagged Docker images with both latest and version numbers,
    allowing easy rollback to a specific version.

  2. Rollback Script:
    Automated rollbacks using a script to redeploy the previous
    image.

Conclusion

The Gemini API Project has been a comprehensive journey through designing, developing, testing, and deploying a robust API powered by a CI/CD pipeline. This project not only showcased the technical skills required for API development but also emphasized the importance of automation and continuous improvement in modern software engineering.

Top comments (0)