top of page

How to use the Azure OpenAI Service completion endpoint to generate text?

Cleverly crafting text that resonates with your audience has never been easier. Thanks to the Azure OpenAI Service, you can effortlessly generate high-quality content tailored to your needs.


In this article, we'll walk you through the process of using the Azure OpenAI Service completion endpoint to effortlessly generate text. You'll learn about completions, understand how to utilize the completion endpoint, follow step-by-step instructions for text generation, explore various types of prompts, and become aware of the limitations of the completion endpoint.


Let's explore the world of AI-powered text generation!


Table of Contents:
STEP 1: Create an Azure OpenAI Service Resource
STEP 2: Deploy a Model
STEP 3: Get an API Key
STEP 4: Create URL for Completion Endpoint
STEP 5: Make a request to Completion Endpoint


What is Completion?

A completion is a generated text that is a continuation of a given prompt. The completion endpoint is a RESTful API that allows you to request completions from the Azure OpenAI Service.


What is the Completion Endpoint in Azure OpenAI Service?

The completion endpoint in Azure OpenAI Service takes a prompt as input and returns a list of possible completions. The prompt can be anything from a few words to a paragraph. The completion endpoint uses a large language model to generate the completions. The language model is trained on a massive dataset of text and code.


The completion endpoint has a number of parameters that you can use to control the output. These parameters include:

  • Prompt: This is the text that you want the model to complete. It can be anything from a few words to a paragraph.

  • Max tokens: This is the maximum number of tokens that the model should return. A token is a word or punctuation mark. If the model generates more than the specified number of tokens, it will be truncated.

  • Temperature: This controls the creativity of the output. A higher temperature will result in more creative output, but it may also be less accurate. A lower temperature will result in more accurate output, but it may also be less creative.

  • Top tokens: This specifies the number of top-ranked completions that the API should return. The API will return the completions with the highest scores.

Here is an example of a completion request:

{   
    "prompt": "Write a poem about love",   
    "max_tokens": 100,   
    "temperature": 0.5,   
    "top_tokens": 3 
} 

This request would ask the model to generate a poem about love, with a maximum of 100 tokens, a temperature of 0.5, and the top 3 completions returned.


Also Read:


Steps to use the Azure OpenAI Service Completion Endpoint to Generate Text

Generating text with the Azure OpenAI Service completion endpoint is easy. Just follow these steps to tap into AI-powered content creation for tasks like automated responses, content generation, and more.


STEP 1: Create Azure OpenAI Service Resource

Below are the steps following which you can create a resource:


1. Sign in to the Azure portal. In the left-hand menu, click on “+ Create a resource”.


2. In the “Search the Marketplace” field, type “OpenAI” and select “OpenAI Service” from the dropdown menu. Click on “Create”.


How to use the Azure OpenAI Service completion endpoint to generate text?

3. Fill in the details such as subscription, resource group, region, and name. Click on “Review + Create” and then “Create”.


STEP 2: Deploy a Model

Before generating text, you have to deploy a mode. You can select any model available in Azure OpenAI Studio.

1. In your OpenAI Service Resource, Choose the subscription and the Azure OpenAI resource to work with, and select Use resource.


2. Under Management select Deployments.


3. Select Create new deployment and configure the following fields:

  • Select a model

  • Deployment name

  • Advanced options

3. Click on “Create”.


When the deployment completes, your model deployment status changes to succeeded.


STEP 3: Get the API key

Once the deployment is complete, go to the resource you just created.


Click on “Keys and Endpoint” on the left-hand menu.


Copy the value of “Key1”. This is your API key.


How to use the Azure OpenAI Service completion endpoint to generate text?

STEP 4: Create a URL for Completion Endpoint

To create a URL for the Completion Endpoint, follow these steps:


1. Identify your resource name: This is the name of your Azure OpenAI Resource. You would have chosen this when you created the resource.


2. Identify your deployment ID: This is the deployment name you chose when you deployed the model.


3. Identify your API version: This is the API version to use for this operation. It’s usually provided in the API documentation.


4. Create the URL: The URL for the completion endpoint will be in this format:

https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/completions?api-version={api-version}2.


Replace {your-resource-name}, {deployment-id}, and {api-version} with your actual resource name, deployment ID, and API version respectively.


For example, if

  • Your resource name is my-openai-resource,

  • Your deployment ID is my-deployment, and

  • Your API version is 2023-09-13,

then your URL will be:

https://my-openai-resource.openai.azure.com/openai/deployments/my-deployment/completions?api-version=2023-09-13.


STEP 5: Make a request to the Completion Endpoint

You can make a POST request to this URL to generate text. The body of your request should be a JSON object that includes a prompt and max_tokens.


Here’s an example of how you can do this in Python:

import requests 
import json  

# Define your API key, endpoint URL, headers, and data 
api_key = 'your-api-key' 
url = 'https://your-resource-name.openai.azure.com/openai/deployments/your-deployment-id/completions?api-version=2023-09-13' 
headers = {
    'Content-Type': 'application/json', 
    'Authorization': f'Bearer {api_key}'
} 
data = {
    'prompt': 'Translate the following English text to French: "{text}"', 
    'max_tokens': 60
}  

# Make a POST request 
response = requests.post(url, headers=headers, data=json.dumps(data))  

# Print the response
print(response.json()) 

In the above code:

  1. API Key and URL: You need to replace 'your-api-key', 'https://your-resource-name.openai.azure.com/openai/deployments/your-deployment-id/completions?api-version=2023-09-13' with your actual API key and the URL of your completion endpoint.

  2. Headers: The headers for the request are defined. The Content-Type is set to application/json indicating that the body of the request will be in JSON format. The Authorization header is set to Bearer {api_key}, where {api_key} is your actual API key.

  3. Data: The data for the request is defined in JSON format. The prompt is set to the text you want to translate, and max_tokens is set to limit the length of the generated text.

  4. POST Request: A POST request is made to the completion endpoint using requests.post(). The URL, headers, and data are passed as arguments.

  5. Response: The response from the server is printed out. This will be a JSON object that includes the generated text.

Make sure you have the requests library installed in your Python environment. If not installed, you can do so by running pip install requests.


Here is another example where you can make a request using openai library:

import openai

openai.api_type = "azure"
openai.api_key = "{your-api-key}"
openai.api_base = "https://{your-resource-name}.openai.azure.com"
openai.api_version = "{api-version}"

# create a chat completion
chat_completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
    ]
)
print(chat_completion.choices[0].message['content'])

This code uses the openai library, which is a Python client for the OpenAI API. This library provides a higher-level interface for interacting with the API, making it easier to work with. The openai.ChatCompletion.create() function is used to send a request to the API and generate a chat completion. The response from this function is an object that includes the generated text, which can be accessed directly.


Also Read:


Types of Prompt used with Azure OpenAI Service Completion Endpoint

These are the different types of prompts that can be used with the Azure OpenAI Service completion endpoint to generate text.


1. Design prompts

These are used to generate creative text formats, like poems, code, scripts, musical pieces, emails, letters, etc. For example, you could use a design prompt to generate a poem about love or a code snippet to implement a sorting algorithm.

# Generate a poem about love 
prompt = "Write me a poem about love"  

# Generate a code snippet to implement a sorting algorithm 
prompt = "Implement a sorting algorithm in Python"

2. Classify text

This is used to create a text classifier with the API. You provide a description of the task and provide a few examples. For example, if you want to create a text classifier that can classify the sentiment of text messages, you would provide the API with a description of the task and a few examples of text messages that are positive, negative, and neutral.

This is a text message sentiment classifier

Message: "I loved the new adventure movie!"
Sentiment: Positive

Message: "I hate it when my phone battery dies." 
Sentiment: Negative

Message: "My day has been 👍"
Sentiment: Positive

Message: "This is the link to the article"
Sentiment: Neutral

Message: "This new music video is unreal"
Sentiment:

Classify text can also be used to classify text into different categories. For example, text can be classified as news, fiction, or marketing. You could use a classify text prompt to classify a document as a news article or a blog post.

# Classify a document as news or fiction 
prompt = "Classify this document as news or fiction"  

# Classify a sentence as positive or negative 
prompt = "Classify this sentence as positive or negative"

Below are some of the additional things to keep in mind about classify text:

  • The accuracy of the text classifier will depend on the quality of the training data.

  • The text classifier may not be able to classify all text accurately, especially if the text is ambiguous or if the text is not in the same style as the training data.

  • It is important to test the text classifier before using it in production. This will help you to identify any potential errors in the classifier.


3. Trigger ideas

Trigger ideas are used to generate new ideas or versions of input. For example, you can use it to generate story ideas for a mystery novel, business plans, character descriptions, marketing slogans, and more.


Here is an example of how you could use trigger ideas to generate ideas for a mystery novel:

prompt = "Generate ideas for a mystery novel"  

# The API will generate a list of ideas, such as: 
# - A detective investigates a series of murders in a small town. 
# - A group of friends go on a camping trip and one of them disappears. 
# - A young woman moves into a new apartment and starts to experience strang

In the next demonstration, the API is used to create more examples of how to use virtual reality in the classroom.

Ideas involving education and virtual reality

1. Virtual Mars
Students get to explore Mars via virtual reality and go on missions to collect and catalog what they see.

2. 

The API is given a basic description of the list along with one list item, "Virtual Mars". The incomplete prompt of "2." is used to trigger a response from the API. The API interprets the incomplete entry as a request to generate similar items and add them to the list.


4. Conduct conversations

These are used to generate text that simulates a conversation between two people. The API can be used to create chatbots that can carry on conversations with humans. The API can be instructed to behave in a certain way, such as being helpful, creative, or sarcastic.


In this first example, the API is instructed to be helpful and creative. It answers the user's questions in a clear and concise way, and it also makes jokes and puns.

The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.

Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?
Human:

In this second example, the API is instructed to be sarcastic and reluctant to answer questions. It answers the user's questions, but it does so in a way that is meant to be humorous.

Cramer is a chatbot that reluctantly answers questions.

###
User: How many pounds are in a kilogram?
Cramer: This again? There are 2.2 pounds in a kilogram. Please make a note of this.
###
User: What does HTML stand for?
Cramer: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.
###
User: When did the first airplane fly?
Cramer: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they'd come and take me away.
###
User: Who was the first man in space?
Cramer:

The key to creating a good chatbot is to provide the API with clear instructions and examples. The more examples you provide, the better the API will be at understanding the role you want it to play.


Below are some of the additional things to keep in mind when creating a chatbot:

  • The chatbot should be consistent in its behavior. If the chatbot is sometimes helpful and sometimes sarcastic, the user will be confused.

  • The chatbot should be able to handle a variety of questions. The chatbot should not get stuck on one question or topic.

  • The chatbot should be able to learn and adapt. As the chatbot interacts with users, it should learn how to better answer questions and interact with people.


5. Transform text

These are used to transform text from one format to another, such as from plain text to code or from code to plain text. It can translate between languages, convert text to emoji, and summarize text.


Translate between languages

Consider the below example where API is used to translate English phrases into French.

English: I do not speak French. 
French: Je ne parle pas français. 

English: See you later! 
French: À tout à l'heure! 

English: Where is a good restaurant? 
French: Où est un bon restaurant?

The API already has a grasp of the French language, so it does not need to be taught the language. It just needs to be given enough examples to understand the request to translate from one language to another.


Convert text to emoji

Consider the below example where we will convert the name of a movie from text into emoji characters.

Carpool Time: 👨👴👩🚗🕒 
Robots in Cars: 🚗🤖 
Super Femme: 👸🏻👸🏼👸🏽👸🏾👸🏿 
Webs of the Spider: 🕸🕷🕸🕸🕷🕸 
The Three Bears: 🐻🐼🐻 
Mobster Family: 👨👩👧🕵🏻‍♂️👲💥

Summarize the text

Consider the below example where we will summarize a block of text. The API can grasp the context of the text and rephrase it in different ways. In this case, the API creates an explanation that is understandable by a primary-age child.

My ten-year-old asked me what this passage means:
"""
A neutron star is the collapsed core of a massive supergiant star, which had a total mass of between 10 and 25 solar masses, possibly more if the star was especially metal-rich.[1] Neutron stars are the smallest and densest stellar objects, excluding black holes and hypothetical white holes, quark stars, and strange stars.[2] Neutron stars have a radius on the order of 10 kilometres (6.2 mi) and a mass of about 1.4 solar masses.[3] They result from the supernova explosion of a massive star, combined with gravitational collapse, that compresses the core past white dwarf star density to that of atomic nuclei.
"""

I rephrased it for him, in plain language a ten-year-old can understand:
"""

This shows that the API has a deep grasp of language and can be used to perform a variety of tasks.


Below are some of the additional things to keep in mind about the API:

  • The API is still under development, so it may not be able to understand all requests perfectly.

  • The API is trained on a massive dataset of text and code, so it may be biased towards certain topics or viewpoints.

  • It is important to use the API responsibly and ethically.


6. Complete partial text and code inputs

These are used to complete partial text or code inputs. This can be useful if you want the API to pick up where you left off or if you want to get help with writing a specific type of text, such as code.


For example, you are writing a blog post about the benefits of exercise. You start writing a sentence about how exercise can help you lose weight, but you get stuck. You can use the API to complete the sentence by typing "exercise can help you lose weight by".

How exercise can help you lose weight

Consider another example where you are writing a code snippet to create a new JavaScript function. You start writing the function definition, but you forget how to close the function. You can use the API to complete the function definition by typing "function myFunction()".

function myFunction()
{

Below are some of the additional things to keep in mind about text completion:

  • Be as specific as possible in your prompt. The more specific you are, the more likely the API is to complete the text correctly.

  • Use keywords that are relevant to the topic of the text. The API will use these keywords to find the most relevant completions.

  • Use natural language. The API is trained on a massive dataset of text, so it is best to use natural language in your prompts.


7. Generate Factual Responses

These are used to generate factual responses to questions. This means that it can generate text that sounds like it is true, even if it is not.


There are a few things you can do to limit the likelihood of the API making up an answer:

  • Provide the API with factual information. This means giving the API examples of questions and answers that you know are true.

  • Set the temperature probability to a low value. The temperature probability is a setting that controls how creative the API is when generating text. A low temperature probability will make the API more likely to generate factual responses.

  • Use a question mark to indicate that you don't know the answer. This will teach the API that it should not make up an answer if it doesn't know the answer for sure.

For example, you could use a generate factual response prompt to answer a question about the weather or the capital of California.

Q: What is the capital of California? 
A: Sacramento.

Q: What's the weather in New York?
A: Mostly Sunny

The specific prompt that you use will depend on the task that you are trying to accomplish. For example, if you are trying to generate a poem, you would use a design prompt. If you are trying to classify text, you would use a classify text prompt.


When designing a prompt, it is important to be as specific as possible. The more specific the prompt, the more likely the model is to generate the desired output. For example, if you are trying to generate a poem about love, you would want to include keywords such as "love", "heart", and "romance" in the prompt.


Limitations of Completion Endpoint

Here are some of the limitations of the completion endpoint in Azure OpenAI Service:

  • The completion endpoint is a Machine Learning model, so it is not perfect. The model can sometimes generate incorrect or nonsensical text.

  • The completion endpoint is trained on a massive dataset of text and code, but it may not be familiar with all topics or viewpoints. If you give the completion endpoint a prompt that it is not familiar with, it may not be able to generate a good completion.

  • The completion endpoint is limited by the amount of data that it is trained on. If the model is not trained on enough data, it may not be able to generate accurate or creative text.

  • The completion endpoint is a cloud-based service, so it can be affected by network latency and availability. If the network is slow or unavailable, the completion endpoint may not be able to respond.

Despite these limitations, the completion endpoint in Azure OpenAI Service is a powerful tool that can be used for a variety of tasks. It is important to be aware of the limitations of the model and to use it accordingly.


Here are some tips for using the completion endpoint effectively in Azure OpenAI Service:

  • Use clear and concise prompts. The more clear and concise your prompt is, the more likely the model is to generate a good completion.

  • Avoid using prompts that are too complex or challenging. The model may not be able to generate a good completion if the prompt is too complex or challenging.

  • Be patient. The model may take some time to generate a good completion.

  • Try different parameters. The different parameters can affect the output of the model. Experiment with different parameters to see what works best for you.


Conclusion

In conclusion, the Azure OpenAI Service completion endpoint offers a versatile and powerful solution for text generation. Whether for automation, content creation, or creative exploration, you now have the tools and knowledge to make the most of this AI-driven capability. As you apply these techniques, you'll find new ways to enhance your projects and streamline your workflow.

Comentários


bottom of page