In this blog post, we will show how to use Azure OpenAI with REST API and interact with an OpenAI model.
REST APIs, or Representational State Transfer APIs, serve as a bridge between OpenAI’s powerful AI models, like GPT-4, and various applications across the web.
These APIs facilitate seamless communication by allowing developers to send requests and receive responses using standard HTTP methods.
This architecture not only supports scalability by handling large volumes of requests but also ensures that developers can integrate OpenAI’s cutting-edge models into their own applications with minimal friction.
The following table shows how the relationships between functions and the REST API method.
Function | Method |
Retrieve | GET |
Create | POST |
Update | PUT |
Delete | DELETE |
JSON Rules
In the context of OpenAI’s REST APIs, JSON (JavaScript Object Notation) plays a crucial role in structuring the request body for efficient and effective communication between clients and the AI models.
JSON is a lightweight data-interchange format that is easy for humans to read and write, and simple for machines to parse and generate.
When developers craft a request to OpenAI’s APIs, such as when submitting text to the GPT-4 model, they typically encapsulate the necessary parameters in a JSON-formatted payload.
The following code shows an example of the most common JSON rules and objects.
{
"name": "John Doe", // This is a string
"age": 30, // This is a number
"isStudent": false, // This is a boolean
"subjects": ["Math", "Science"], // This is an array
"address": { // This is an object
"street": "123 Main St", // This is a string
"city": "Anytown", // This is a string
"postalCode": "12345" // This is a string
},
"explanation": "This JSON object includes examples of a string, number, boolean, array, and nested objects." // This is a string
}
Below is an example of a JSON object that you can directly use in Postman to make a request to the OpenAI GPT-4 API
{
"messages": [
{
"role": "user",
"content": "Can you explain the impact of quantum computing on encryption?"
}
],
"max_tokens": 150
}
Use Azure OpenAI With REST API
To use Azure OpenAI REST API with Postman, you will need the following.
- Azure OpenAI Resource (including API Key)
- Azure OpenAI deployment model (I recommend you keep the deployment name the same as the model name)
Once you have the above, create a REST API POST request and use the the following.
https://{{endpoint}}/openai/deployments/{{deployment name/chat/completions?api-version=2024-02-15-preview&Content-Type=application/json
In the Body tab, add the JSON code.
In the Authorization, add an API Key with the name api-key and the API Key from the Azure OpenAI resource.