OpenAI
The following demonstrates how to locally run an API Runtime instance to run inference with OpenAI models. It also illustrates the different ways it can be used.
To get up and running we need to pull the runtime Docker image. To pull the docker image, you must be authenticated. Check our installation tutorial to see how you can authenticate with Docker CLI.
docker pull \
europe-west2-docker.pkg.dev/seldon-registry/llm/mlserver-llm-api:0.7.0Before we can start the runtime we need to create the model-settings.json which will tell it what model to run:
!cat models/openai-chat-completions/model-settings.json{
"name": "openai-chat-completions",
"implementation": "mlserver_llm_api.LLMRuntime",
"parameters": {
"extra": {
"provider_id": "openai",
"config": {
"model_id": "gpt-3.5-turbo",
"model_type": "chat.completions"
}
}
}
}In the above settings, the runtime config is specified in the parameters JSON field:
the choice of
provider_id- currently, the providers supported are"openai"and"gemini".we've chosen the
gpt-3.5-turbomodel and thechat.completionsAPI.
Starting the Runtime
Finally, to start the server run:
Sending Requests
To send our first request to the chat.completions endpoint that we are now serving via mlserver, we use the following:
Note that we've sent three tensors: a "role", a "content", and a "type" tensor. The "role" tensors tell the model who is speaking. In this case, it includes a "system" role and a "user" role. The "system" role is used to dictate the context of the interaction and the "user" role indicates that the matching content is sent by a user. In the above the system content is: "You are a helpful assistant" and the user content is "Hello from MLServer". The "type" tensor indicates that the content we sent is a text.
The endpoint responds with its own "role", "content" and "type" tensors. Its "role" is given as "assistant" and the "content" it returns is "Hello! How can I assist you today?". As well as this the server returns the full response received from OpenAI via the "output_all" tensor.
Requests with Parameters
As well as this we can add parameters to the request, to specify the number of generations and temperature, etc. For a list of all available parameters see the OpenAI documentation for their API. The following sets the temperature, the maximum number of tokens to generate and the number of generations to return:
Note that if you are sending a single message, you must not encode the content and type as json.
Adding prompts
Prompting is a pivotal part of using large language models. It allows developers to embed messages sent by a user within other textual contexts that provide more information about the task you want the model to perform. For instance, we use the following prompt to give the model more context about the kind of question that the model is expected to answer:
In the above the content sent by the user can be inserted in the {question} variable. A developer should specify what content to insert thereby giving that tensor the name "question". To start with we need to create a new model-settings.json file. This will be the same as the previous one but in addition it specifies the prompt tempalte to be used through prompt_utils settings.
We can test this using the following:
Note that we sent a single tensor named "question", which was the inserted content.
Embedding text
The OpenAI runtime also provides an embedding API that allows you to encode a body of text into a high-dimensional vector representation. Developers can then use this to perform vector searches over similarly encoded text. Here is an example of a model-settings.json:
The above output is limited because the embedding vectors are quite long, you can see that there are two and each is length 1536. These can be used to search similar vectorized text corpora for similar content.
Image Generation
The OpenAI runtime also allows access to the Dall-E image generations endpoint, the model-settings.json is similar to the previous cases.
When calling this endpoint we send a prompt tensor. Let's try and depict something nice and cheery!
The images are returned as a URL to their location that can be used to download them.

Azure Deployments
Our runtime can also be set up to integrate with OpenAI deployments on Azure. To set up such an Azure OpenAI deployment see here.
The following is an example model-settings.json file.
In the above YAML definition
api_typeis "azure" or "azure_ad"azure_endpointis the deployment endpointmodel_idis deployment nameapi_versionis "2024-08-01-preview" and could change in the future

You can call the models in azure as before - everything else stays the same.
Deploying on Seldon Core 2
We will now demostrate how do deploy the chat completions model on Seldon Core 2. All the other models can be deployed with the same steps.
While the runtime image can be used as a stand alone server in most cases you'll want to deploy it as part of a Kubernetes cluster. This section assumes the user has a Kubernetes cluster running with Seldon Core 2 installed in the seldon namespace. In order to start serving OpenAI models you will have to first create a secret for the OpenAI API key and deploy the API Runtime server. Please check our installation tutorial to see how you can do so.
To deploy the chat completions model, we will need to create the associated manifest file.
To load the model in Seldon Core 2, run:
Before sending the actual request, we need to get the mesh ip. The following util function will help you retrieve the correct ip:
As before, we can now send a request to the model:
You now have a deployed model in Seldon Core 2, ready and available for requests! To unload the model, you can run the following command:
Last updated
Was this helpful?