Skip to content
Posts en inglés. Usá el traductor del navegador para leerlos en tu idioma.

How to Migrate OpenAI API Calls to DigitalOcean Serverless Inference

Yammbo
· 10 min read
openai api migration llm api python sdk model access key chat completions
How to Migrate OpenAI API Calls to DigitalOcean Serverless Inference

The landscape of large language models (LLMs) is rapidly evolving, with various providers offering powerful inference capabilities. For developers who have built applications using the OpenAI API, migrating to an alternative service like DigitalOcean Serverless Inference can offer benefits such as access to a diverse model catalog, potentially optimized pricing, and a robust cloud infrastructure. DigitalOcean's Serverless Inference provides OpenAI-compatible API endpoints, meaning many existing OpenAI SDK workflows can be migrated with minimal code changes. This tutorial will walk you through the practical steps to reconfigure your Python application, specifically focusing on Chat Completions, to utilize DigitalOcean's serverless platform effectively.

Step 1: Obtain Your DigitalOcean Model Access Key

To secure your interactions with DigitalOcean's Serverless Inference, you'll need a unique authentication credential known as a "Model Access Key." This key is distinct from your general DigitalOcean API tokens and is specifically designed for accessing inference services. Unlike an OpenAI API key, which authenticates requests to OpenAI's ecosystem, this key authorizes your application to use DigitalOcean's hosted models.

Action:

  1. Log in to your DigitalOcean Control Panel.
  2. Navigate to the "Inference" section, typically found in the left-hand sidebar, and then select "Serverless Inference."
  3. Within the Serverless Inference dashboard, look for an option to "Create Model Access Key." Follow the prompts to generate a new key.
  4. Crucially, copy the generated key immediately. For security reasons, DigitalOcean usually displays this key only once upon creation. If you lose it, you'll need to generate a new one.

Why it matters: This Model Access Key functions as a bearer token, which is a standard method for authenticating API requests. It grants your application the necessary permissions to send requests to DigitalOcean's inference endpoints and utilize their hosted models. Storing this key securely is paramount. It's highly recommended to use environment variables or a dedicated secret management system rather than embedding the key directly in your application's source code.

Verification: After creation, you should have a unique alphanumeric string representing your MODEL_ACCESS_KEY. To store it as an environment variable in a Linux/macOS terminal for immediate use, you can run:

export MODEL_ACCESS_KEY="your_digitalocean_model_access_key_here"

For more permanent solutions, consider adding it to your shell's profile file (e.g., .bashrc, .zshrc) or using a .env file with a library like python-dotenv in your project.

Step 2: Configure Your Environment and OpenAI Python SDK

The OpenAI Python SDK is engineered for flexibility, allowing developers to direct API calls to custom endpoints by specifying a base_url. This feature is fundamental to migrating your application, as it enables you to seamlessly switch from OpenAI's default API servers to DigitalOcean's compatible inference endpoints. Alongside this, you'll configure the SDK to use your newly obtained DigitalOcean Model Access Key for authentication.

Action:

  1. First, ensure the OpenAI Python SDK is installed in your development environment. If it's not already, you can install it using pip:
    pip install openai
  2. In your Python application, import the os module (to access environment variables) and the OpenAI class from the openai library. When initializing the OpenAI client, you will provide two key parameters:
    • base_url: Set this to "https://inference.do-ai.run/v1", which is DigitalOcean's standard endpoint for OpenAI-compatible inference.
    • api_key: Retrieve your MODEL_ACCESS_KEY from your environment variables using os.getenv("MODEL_ACCESS_KEY").

Code Example:

import osfrom openai import OpenAI# Initialize the OpenAI client, directing requests to DigitalOcean's endpointclient = OpenAI(    base_url="https://inference.do-ai.run/v1",    api_key=os.getenv("MODEL_ACCESS_KEY"), # Authenticates with your DO Model Access Key)

Why it matters: The base_url parameter is a powerful abstraction that allows the SDK to function identically while routing requests to a different backend. This design minimizes code changes during migration. Using os.getenv() is a critical security practice, preventing sensitive credentials from being hardcoded into your application's source code, which could expose them in version control systems or during deployment.

Verification: Executing this snippet should successfully initialize the client object. Any immediate errors would likely indicate an issue with the SDK installation or Python environment setup. The true test of successful configuration will occur when you attempt an actual API call in the subsequent steps.

Step 3: Migrate Your Chat Completions API Call

For standard Chat Completions, the method signature within the OpenAI Python SDK remains consistent, which greatly simplifies the migration. The most significant change, apart from the client initialization, will be specifying the model ID. DigitalOcean hosts its own curated catalog of models, each with unique identifiers and characteristics, which differ from OpenAI's model names.

Action:

  1. Identify a suitable model: You need to select a model from DigitalOcean's Serverless Inference catalog that best fits your application's requirements. You can browse available models and their details directly within the DigitalOcean Control Panel under the Serverless Inference section, or by making a GET /v1/models API call to their endpoint. For demonstration purposes, we'll use llama3.3-70b-instruct, but you should choose one that aligns with your needs.
  2. Update the model parameter: Replace the OpenAI model ID (e.g., "gpt-4o", "gpt-3.5-turbo") in your client.chat.completions.create() call with the chosen DigitalOcean model ID.

Code Example (DigitalOcean Serverless Inference):

# Make a Chat Completions call using a DigitalOcean-supported modelresp = client.chat.comletions.create(    model="llama3.3-70b-instruct", # Example model from DigitalOcean's catalog    messages=[        {"role": "system", "content": "You are a helpful assistant."},        {"role": "user", "content": "Tell me a fun fact about octopuses."},    ],    temperature=0.7, # Example parameter, adjust as needed    max_tokens=256   # Example parameter)print(resp.choices[0].message.content)

Why it matters: While the API call's syntax is nearly identical, the model specified dictates which underlying LLM processes your request. Each model has been trained on different datasets, possesses varying context windows, and may excel at different types of tasks (e.g., code generation, creative writing, factual recall). Choosing the correct model ID ensures your request is routed to a valid and performant model within DigitalOcean's ecosystem. Additionally, remember that request parameters like temperature or max_tokens might have different optimal ranges or behaviors across models.

Verification: Run your Python script. A successful API call will print a generated text response to your console, indicating that the request was processed by the DigitalOcean-hosted model. If you encounter errors, double-check your model ID and ensure it is active and available in the DigitalOcean catalog.

Step 4: Implement Streaming Responses

Streaming responses are a fundamental feature for modern AI applications, significantly enhancing user experience by displaying generated content incrementally rather than waiting for the entire response. This is particularly valuable for long-form content generation or interactive chatbots. DigitalOcean Serverless Inference fully supports streaming, and the integration with the OpenAI Python SDK is seamless.

Action:

  1. To enable streaming, simply add the parameter stream=True to your client.chat.completions.create() call.
  2. The API call will then return an iterator. You'll need to loop through this iterator, processing each "chunk" of the response as it arrives. Each chunk typically contains a small portion of the generated content.

Code Example (Streaming):

# Make a streaming Chat Completions callstream = client.chat.completions.create(    model="llama3.3-70b-instruct",    messages=[{"role": "user", "content": "Write a haiku about Kubernetes."}],    stream=True,)print("Streaming response:")for chunk in stream:    if chunk.choices and chunk.choices[0].delta.content:        print(chunk.choices[0].delta.content, end="", flush=True)print("\nEnd of stream.")

Why it matters: Streaming dramatically improves the perceived responsiveness of your application. Instead of a noticeable delay while the model generates its full response, users see text appearing in real-time, making the interaction feel more dynamic and natural. This is a critical feature for building engaging conversational AI interfaces.

Verification: When you execute this code, observe your console. You should see the generated haiku about Kubernetes appear character by character or word by word, rather than being printed as a single block of text after a delay. This confirms that the streaming mechanism is correctly implemented and functioning.

Step 5: Understand Model Differences and Parameter Nuances

While DigitalOcean Serverless Inference strives for OpenAI API compatibility, it's crucial to understand that "compatibility" primarily refers to the API's structural interface. The underlying large language models and their specific implementations are distinct. This means that a direct code migration does not guarantee identical behavior, output quality, or full parameter support compared to your original OpenAI integration.

Key Considerations:

  • Model Capabilities and Performance: Different models excel at different tasks. A model like llama3.3-70b-instruct might have different strengths, weaknesses, context window limits, and latency characteristics compared to a gpt-4o model. Test your specific use cases thoroughly.
  • Parameter Behavior: While common parameters like temperature, max_tokens, and stop sequences are generally supported, their exact effect or optimal values might vary between models. More advanced or niche parameters available in one API might not be present or behave identically in another. Always refer to DigitalOcean's specific model documentation for details.
  • Output Quality and Style: The stylistic nuances, factual accuracy, and adherence to complex instructions can differ. What works perfectly with one model might require prompt engineering adjustments for another.
  • Tool Use and Function Calling: If your application relies on advanced features like tool use or function calling, verify their support and implementation details with DigitalOcean's specific models. These features often have model-specific interfaces.

Action:

  1. Consult DigitalOcean's Model Catalog: Regularly review the official DigitalOcean Serverless Inference documentation for detailed information on available models, their capabilities, limitations, and supported parameters. This is your primary source of truth.
  2. Implement Comprehensive Testing: Before deploying to production, conduct rigorous testing. This should include:
    • Unit and Integration Tests: Verify that your API calls function as expected.
    • Performance Benchmarking: Measure latency and throughput to ensure it meets your application's requirements.
    • Quality Assurance: Manually and programmatically evaluate the quality, relevance, and safety of the model's outputs for your specific application.
  3. Iterate on Prompt Engineering: Be prepared to adjust your prompts to optimize performance for the new model. What worked for one model might not yield the best results with another.

Why it matters: Understanding these differences is crucial for a successful migration. Assuming identical behavior can lead to unexpected results, degraded user experience, or even application failures. Proactive testing and continuous monitoring are essential to maintain the quality and reliability of your AI-powered features.

Verification: A successful verification involves confirming that the new model meets your application's functional and non-functional requirements, including output quality, performance, and reliability, through a robust testing suite.

Step 6: Making Direct HTTP Requests

While the OpenAI Python SDK provides a convenient abstraction, some developers or applications in other programming languages may prefer to interact with DigitalOcean Serverless Inference directly via raw HTTP requests. This method offers maximum control over the request and response lifecycle, adhering to the same OpenAI API specification for endpoints, authentication, and payload format.

Action:

  1. Construct the Request URL: The target endpoint for Chat Completions is https://inference.do-ai.run/v1/chat/completions.
  2. Set Authentication Header: Include an Authorization header with your MODEL_ACCESS_KEY prefixed by Bearer (e.g., "Authorization: Bearer YOUR_MODEL_ACCESS_KEY").
  3. Specify Content Type: Set the Content-Type header to application/json to indicate the format of your request body.
  4. Format Request Body: Create a JSON object for the request body, specifying the model and an array of messages, structured identically to the SDK examples. You can also include other parameters like temperature or max_completion_tokens.

Code Example (curl):

curl -X POST https://inference.do-ai.run/v1/chat/completions \  -H "Authorization: Bearer $MODEL_ACCESS_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "llama3.3-70b-instruct",    "messages": [{"role": "user", "content": "What is the capital of France?"}],    "temperature": 0.7,    "max_completion_tokens": 256  }'

Why it matters: Direct HTTP requests are invaluable for debugging, for use in environments where an SDK might not be available or preferred, or when building custom API clients in languages other than Python. It provides a clear understanding of the underlying communication protocol between your application and the inference service.

Verification: Execute the curl command in your terminal. A successful response will be a JSON object containing the model's completion, such as {"choices":[{"message":{"content":"Paris"}}], ...}. This confirms that your direct HTTP request is correctly formatted and authenticated.

Migrating your applications from the OpenAI API to DigitalOcean Serverless Inference can be a strategic move, offering flexibility and access to a different range of powerful models. By carefully adjusting your base URL, API key, and model identifiers, and thoroughly testing the new integration, you can ensure a smooth transition. For developers looking to build and host powerful web applications, Yammbo Web at https://web.yammbo.com provides an AI-powered website builder that simplifies deployment and management.