Tl;dr A neutron star is the collapsed core of a supergiant star. These incredibly dense objects are incredibly fascinating due to their strange properties and their potential for phenomena such as extreme gravitational forces and a strong magnetic field.
The accuracy of the response can vary per model. The Davinci based model in this example is well-suited to this type of summarization, whereas a Codex based model wouldn't perform as well at this particular task.
I ran into an issue with the playground.
Clean up resources
If you want to clean up and remove an OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Portal
Azure CLI
Next steps
Learn more about how to generate the best completion in our How-to guide on completions.
For more examples check out the Azure OpenAI Samples GitHub repository.
Source code | Package (NuGeT) | Samples
Prerequisites
An Azure subscription - Create one for free
Access granted to the Azure OpenAI service in the desired Azure subscription.
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access.
The current version of .NET Core
An Azure OpenAI Service resource with the text-davinci-003
model deployed. For more information about model deployment, see the resource deployment guide.
I ran into an issue with the prerequisites.
Set up
Create a new .NET Core application
In a console window (such as cmd, PowerShell, or Bash), use the dotnet new
command to create a new console app with the name azure-openai-quickstart
. This command creates a simple "Hello World" project with a single C# source file: Program.cs.
dotnet new console -n azure-openai-quickstart
Change your directory to the newly created app folder. You can build the application with:
dotnet build
The build output should contain no warnings or errors.
Build succeeded.
0 Warning(s)
0 Error(s)
Install the OpenAI .NET client library with:
dotnet add package Azure.AI.OpenAI --prerelease
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you'll need an endpoint and a key.
Variable name
Value
ENDPOINT
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/
.
API-KEY
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
.
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
Create .NET application
From the project directory, open the program.cs file and replace with the following code:
using Azure;
using Azure.AI.OpenAI;
using static System.Environment;
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
string key = GetEnvironmentVariable("AZURE_OPENAI_KEY");
// Enter the deployment name you chose when you deployed the model.
string engine = "text-davinci-003";
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
string prompt = "When was Microsoft founded?";
Console.Write($"Input: {prompt}\n");
Response<Completions> completionsResponse =
await client.GetCompletionsAsync(engine, prompt);
string completion = completionsResponse.Value.Choices[0].Text;
Console.WriteLine($"Chatbot: {completion}");
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
dotnet run program.cs
Output
Input: When was Microsoft founded?
Chatbot:
Microsoft was founded on April 4, 1975
I ran into an issue when running the code sample.
Clean up resources
If you want to clean up and remove an OpenAI resource, you can delete the resource. Before deleting the resource you must first delete any deployed models.
Portal
Azure CLI
Next steps
For more examples check out the Azure OpenAI Samples GitHub repository
Source code | Artifact (Maven) | Samples
Prerequisites
An Azure subscription - Create one for free
Access granted to the Azure OpenAI service in the desired Azure subscription.
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access.
Java Development Kit (JDK) with version 8 or above.
An Azure OpenAI Service resource with either the gpt-35-turbo
or the gpt-4
1 models deployed. For more information about model deployment, see the resource deployment guide.
1 GPT-4 models are currently only available by request. Existing Azure OpenAI customers can apply for access by filling out this form.
I ran into an issue with the prerequisites.
Set up
Install Apache Maven. Then run mvn -v
to confirm successful installation. The README.txt
file from the installation has instructions on adding the Maven bin directory to your PATH variable. If you don't set this the mvn
command will instead need to run like c:\apache-maven-3.9.2-bin\apache-maven-3.9.2\mvn -v
.
Create the directory structure for your project.
mkdir "quickstart/src/main/java/com/azure/ai/openai/usage"
At the root of the quickstart directory, create a file named pom.xml
with the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.azure.ai.openai.usage.GetCompletionsSample</groupId>
<artifactId>quickstart-eclipse</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
</dependencies>
</project>
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name
Value
ENDPOINT
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/
.
API-KEY
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
.
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
Create a sample application
Create a new file named GetCompletionsSample.java
and place it in quickstart/src/main/java/com/azure/ai/openai/usage
folder. Copy the following code into the file.
package com.azure.ai.openai.usage;
import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.Choice;
import com.azure.ai.openai.models.Completions;
import com.azure.ai.openai.models.CompletionsOptions;
import com.azure.ai.openai.models.CompletionsUsage;
import com.azure.core.credential.AzureKeyCredential;
import java.util.ArrayList;
import java.util.List;
public class GetCompletionsSample {
public static void main(String[] args) {
String azureOpenaiKey = System.getenv("AZURE_OPENAI_KEY");;
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");;
String deploymentOrModelId = "text-davinci-003";
OpenAIClient client = new OpenAIClientBuilder()
.endpoint(endpoint)
.credential(new AzureKeyCredential(azureOpenaiKey))
.buildClient();
List<String> prompt = new ArrayList<>();
prompt.add("When was Microsoft founded?");
Completions completions = client.getCompletions(deploymentOrModelId, new CompletionsOptions(prompt));
System.out.printf("Model ID=%s is created at %d.%n", completions.getId(), completions.getCreated());
for (Choice choice : completions.getChoices()) {
System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
CompletionsUsage usage = completions.getUsage();
System.out.printf("Usage: number of prompt token is %d, "
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
From the top level quickstart directory where your pom.xml
is located run:
mvn compile
Now run the sample:
mvn exec:java -Dexec.mainClass="com.azure.ai.openai.usage.GetCompletionsSample"
Output
Model ID=cmpl-7JZRbWuEuHX8ozzG3BXC2v37q90mL is created at 1684898835.
Index: 0, Text:
Microsoft was founded on April 4, 1975..
Usage: number of prompt token is 5, number of completion token is 11, and number of total tokens in request and response is 16.
I ran into an issue when running the code sample.
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models.
Portal
Azure CLI
Next steps
For more examples, check out the Azure OpenAI Samples GitHub repository
Source code | Package (npm) | Samples
Prerequisites
An Azure subscription - Create one for free
Access granted to the Azure OpenAI service in the desired Azure subscription.
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access.
LTS versions of Node.js
An Azure OpenAI Service resource with the text-davinci-003 model deployed. For more information about model deployment, see the resource deployment guide.
I ran into an issue with the prerequisites.
Set up
Install the client library
Create a new directory and navigate into that directory.
Install the Azure OpenAI client library for JavaScript with npm:
npm install @azure/openai
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name
Value
ENDPOINT
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/
.
API-KEY
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
.
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
Create a sample application
Open a command prompt where you want the new project, and create a new file named Completion.js. Copy the following code into the ChatCompletion.js file.
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] ;
const azureApiKey = process.env["AZURE_OPENAI_KEY"] ;
const prompt = ["When was Microsoft founded?"];
async function main() {
console.log("== Get completions Sample ==");
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const deploymentId = "text-davinci-003";
const result = await client.getCompletions(deploymentId, prompt);
for (const choice of result.choices) {
console.log(choice.text);
main().catch((err) => {
console.error("The sample encountered an error:", err);
module.exports = { main };
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
node.exe Completion.js
Output
== Get completions Sample ==
Microsoft was founded on April 4, 1975.
I ran into an issue when running the code sample.
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models.
Portal
Azure CLI
Next steps
For more examples, check out the Azure OpenAI Samples GitHub repository
Library source code | Package (PyPi) |
Prerequisites
An Azure subscription - Create one for free
Access granted to Azure OpenAI in the desired Azure subscription
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.
Python 3.7.1 or later version
The following Python libraries: os, requests, json
An Azure OpenAI Service resource with a model deployed. For more information about model deployment, see the resource deployment guide.
I ran into an issue with the prerequisites.
Set up
Install the OpenAI Python client library with:
pip install openai
This library is maintained by OpenAI and is currently in preview. Refer to the release history or the version.py commit history to track the latest updates to the library.
Retrieve key and endpoint
To successfully make a call against the Azure OpenAI service, you'll need the following:
Variable name
Value
ENDPOINT
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/
.
API-KEY
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
.
DEPLOYMENT-NAME
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Deployments in the Azure portal or alternatively under Management > Deployments in Azure OpenAI Studio.
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
Create a new Python application
Create a new Python file called quickstart.py. Then open it up in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code. Modify the code to add your key, endpoint, and deployment name:
import os
import requests
import json
import openai
openai.api_key = os.getenv("AZURE_OPENAI_KEY")
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
openai.api_type = 'azure'
openai.api_version = '2023-05-15' # this may change in the future
deployment_name='REPLACE_WITH_YOUR_DEPLOYMENT_NAME' #This will correspond to the custom name you chose for your deployment when you deployed a model.
# Send a completion call to generate an answer
print('Sending a test completion job')
start_phrase = 'Write a tagline for an ice cream shop. '
response = openai.Completion.create(engine=deployment_name, prompt=start_phrase, max_tokens=10)
text = response['choices'][0]['text'].replace('\n', '').replace(' .', '.').strip()
print(start_phrase+text)
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
Run the application with the python
command on your quickstart file:
python quickstart.py
Output
The output will include response text following the Write a tagline for an ice cream shop.
prompt. Azure OpenAI returned The coldest ice cream in town!
in this example.
Sending a test completion job
Write a tagline for an ice cream shop. The coldest ice cream in town!
Run the code a few more times to see what other types of responses you get as the response won't always be the same.
I ran into an issue when running the code sample.
Understanding your results
Since our example of Write a tagline for an ice cream shop.
provides little context, it's normal for the model to not always return expected results. You can adjust the maximum number of tokens if the response seems unexpected or truncated.
Azure OpenAI also performs content moderation on the prompt inputs and generated outputs. The prompts or responses may be filtered if harmful content is detected. For more information, see the content filter article.
Clean up resources
If you want to clean up and remove an OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Portal
Azure CLI
Next steps
Learn more about how to generate the best completion in our How-to guide on completions.
For more examples check out the Azure OpenAI Samples GitHub repository.
Prerequisites
An Azure subscription - Create one for free
Access granted to Azure OpenAI in the desired Azure subscription
Currently, access to this service is granted only by application. You can apply for access to the Azure OpenAI service by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.
Python 3.7.1 or later version
The following Python libraries: os, requests, json
An Azure OpenAI resource with a model deployed. For more information about model deployment, see the resource deployment guide.
Set up
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you'll need the following:
Variable name
Value
ENDPOINT
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/
.
API-KEY
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
.
DEPLOYMENT-NAME
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Deployments in the Azure portal or alternatively under Management > Deployments in Azure OpenAI Studio.
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
echo export AZURE_OPENAI_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
Create a new Python application
Create a new Python file called quickstart.py. Then open it up in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code.
import os
import requests
import json
api_key = os.getenv("AZURE_OPENAI_KEY")
base_url = os.getenv("AZURE_OPENAI_ENDPOINT")
deployment_name ="REPLACE_WITH_YOUR_DEPLOYMENT_NAME_HERE"
url = base_url + "/openai/deployments/" + deployment_name + "/completions?api-version=2023-05-15"
prompt = "Once upon a time"
payload = {
"prompt":prompt
r = requests.post(url,
headers={
"api-key": api_key,
"Content-Type": "application/json"
json = payload
response = json.loads(r.text)
formatted_response = json.dumps(response, indent=4)
print(formatted_response)
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
Run the application with the python
command on your quickstart file:
python quickstart.py
Output
The output from the completions API will look as follows.
"id": "ID of your call",
"object": "text_completion",
"created": 1675444965,
"model": "text-davinci-002",
"choices": [
"text": " there lived in a little village a woman who was known as the meanest",
"index": 0,
"finish_reason": "length",
"logprobs": null
"usage": {
"completion_tokens": 16,
"prompt_tokens": 3,
"total_tokens": 19
I ran into an issue when running the code sample.
The Azure OpenAI Service also performs content moderation on the prompt inputs and generated outputs. The prompts or responses may be filtered if harmful content is detected. For more information, see the content filter article.
Clean up resources
If you want to clean up and remove an OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Portal
Azure CLI
Next steps
Learn more about how to generate the best completion in our How-to guide on completions.
For more examples check out the Azure OpenAI Samples GitHub repository.