Skip to main content
This tutorial creates an OpenAI-compatible endpoint that works with any open-source model. Use existing OpenAI code with Cerebrium serverless functions by changing just two lines of code. To see the final code implementation, you can view it here

Cerebrium setup

Create a Cerebrium account by signing up here and follow the installation docs. Run the following command to create the Cerebrium starter project: cerebrium init 1-openai-compatible-endpoint. This creates two files:
  • main.py: The entrypoint file where application code lives
  • cerebrium.toml: A configuration file that contains all build and environment settings
Add the following pip packages and hardware requirements to your cerebrium.toml to create your deployment environment:
Define the imports and initialize the model. This example uses Meta’s Llama 3.1 model, which requires Hugging Face authorization. Add the HF token to secrets in the Cerebrium dashboard, then add this code to main.py:
Next, define the required output format for OpenAI endpoints using Pydantic:
The function:
  • Takes parameters through its signature, with optional and default values available
  • Automatically receives a unique run_id for each request
  • Processes the entire prompt through the model
  • Streams results when stream=True using async functionality
  • Returns the complete result at the end if streaming is disabled

Deploy & Inference

To deploy the model use the following command:
After deployment, a curl command like this appears:
In Cerebrium, each function name becomes an endpoint (ending with /run). While OpenAI-compatible endpoints typically end with /chat/completions, all Cerebrium endpoints are OpenAI-compatible. Call the endpoint as follows:
Set the base URL to the one from the deploy command (ending in /run). Use the JWT token from either the curl command or the Cerebrium dashboard’s API Keys section.