Generating Code with Azure OpenAI Codex

Introduction

In the ever-evolving landscape of software development, developers constantly seek innovative tools and technologies to boost their productivity and efficiency. One such groundbreaking advancement is Azure OpenAI Codex, a powerful language model that has revolutionized how developers generate code. With Codex, developers can now leverage the power of artificial intelligence to generate code based on prompts, significantly reducing development time and effort. In this article, we will explore the capabilities of Azure OpenAI Codex and delve into the approach for generating code with this remarkable tool.

Approach

  1. Understanding the Power of Azure OpenAI Codex: Azure OpenAI Codex is built on OpenAI's GPT-3.5 architecture, which enables it to understand natural language prompts and generate human-like responses. It has been pre-trained on a vast corpus of text from the internet, including programming languages, documentation, and code repositories, making it a comprehensive source of knowledge for generating code. This immense training data empowers Codex to offer insightful suggestions, fix syntax errors, and even write complete code snippets based on the prompts it receives.

  2. Providing Clear and Contextual Prompts: To generate code effectively with Codex, providing clear and context-rich prompts is crucial. Developers can frame their prompts as specific questions, explain the desired functionality, or describe the problem they are trying to solve. By including relevant details, such as programming language, framework, or specific requirements, developers can guide Codex to generate more accurate and tailored code solutions.

  3. Iterative and Collaborative Development: Azure OpenAI Codex is designed to be a powerful assistant that collaborates with developers during the code-writing process. Instead of expecting a perfect output in a single attempt, developers can iteratively refine and improve the code generation by providing feedback to Codex. The model can learn from the feedback and adapt its responses accordingly, allowing developers to shape the code generation process to align with their requirements.

  4. Enforcing Safety and Best Practices: While Azure OpenAI Codex is capable of generating high-quality code, it is essential to validate the output and enforce coding best practices. Developers should review and verify the generated code for security vulnerabilities, performance considerations, and maintainability. By combining the power of Codex with human expertise, developers can leverage its assistance to enhance code quality and adhere to industry standards.

  5. Leveraging Integration and Customization: Azure OpenAI Codex provides seamless integration options that enable developers to incorporate it into their existing development workflows. It can be accessed through the OpenAI API, making it straightforward to integrate into various programming environments and tools. Moreover, developers can customize Codex's behavior by providing additional instructions or hints, further refining the generated code.

Development

Let's go over the steps to create an OpenAI account in Azure.

Search Azure OpenAI and create an account.

Generating Code with Azure OpenAI Codex

Choose the region and type the account name. Then click on Next until the final step and click on Create.

Generating Code with Azure OpenAI Codex

Once the account is created, you'll see the overview of your Azure OpenAI account. Click on Explore.

Generating Code with Azure OpenAI Codex

Another tab is opened; this is the Azure AI Studio. Click on Deployments from the left sidebar menu, then click on Create new deployment.

Generating Code with Azure OpenAI Codex

Since we will create a model to generate code, we can choose between code-davinci-002 and code-cushman-001 models. Both were the available Codex models at the time this article was written. After selecting the model, write a description and click on Create.

Generating Code with Azure OpenAI Codex

If you want to know more about the available models for deployment, check this documentation.

After you create the model, you'll see further details about it.

Generating Code with Azure OpenAI Codex

From the sidebar, go to Completions, then select the deployment you created earlier. On this page, you'll find a lot of examples to use here.

In this example, we're giving some tables names with their columns, and the task is to create a query to return all albums with more than 10 tracks. Notice that below the box prompt, you can apply a language format, for this example, SQL. The format isn't mandatory to apply.

Generating Code with Azure OpenAI Codex

After you write the prompt, click on Generate. The result will be displayed below the prompt.

Generating Code with Azure OpenAI Codex

Let's go over more examples.

Paste the following prompt to generate unit tests:

# Python 3
def sum_numbers(a, b):
  return a + b

# Unit test
def

The result is the following:

Generating Code with Azure OpenAI Codex

Next to the Deployments dropdown, there is another dropdown for Examples. Click on it and select Explain a SQL query.

Generating Code with Azure OpenAI Codex

The prompt and the result is the following:

Generating Code with Azure OpenAI Codex

As you have noticed, on the right are some parameters such as temperature, tokens, etc. The following table describes those parameters:

Parameter Type Default Description
Temperature number 1 Controls randomness. Lowering the temperature means that the model will produce more repetitive and deterministic responses. Increasing the temperature will result in more unexpected or creative responses. Try adjusting the temperature or Top P but not both.
Max length (tokens) integer 16 Set a limit on the number of tokens per model response. The API supports a maximum of 8000 tokens shared between the prompt (including system message, examples, message history, and user query) and the model's response. One token is roughly 4 characters for typical English text.
Stop sequences string or array null Make responses stop at a desired point, such as the end of a sentence or list. Specify up to four sequences where the model will stop generating further tokens in a response. The returned text won't contain the stop sequence.
Top probabilities number 1 Similar to temperature, this controls randomness but uses a different method. Lowering Top P will narrow the model's token selection to likelier tokens. Increasing Top P will let the model choose from tokens with both high and low likelihood. Try adjusting the temperature or Top P but not both.
Frequency penalty number 0 Reduce the chance of repeating a token proportionally based on how often it has appeared in the text so far. This decreases the likelihood of repeating the exact same text in response.
Presence penalty number 0 Reduce the chance of repeating any token that has appeared in the text at all so far. This increases the likelihood of introducing new topics in a response.
Best of integer 1 Generate multiple responses, and display only the one with the best total probability across all its tokens. The unused candidates will still incur usage costs, so use this parameter carefully and make sure to set the parameters for max response length and ending triggers as well. Note that streaming will only work when this is set to 1.

For further details about those parameters, visit this page.

Finally, let's test another example in Python:

#Note: The openai-python library support for Azure OpenAI is in preview.

import os
import openai

openai.api_type = "azure"
openai.api_base = "https://<openai_account_name>.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")


response = openai.Completion.create(
  engine = "Codex-Davinci",
  prompt = "# Write a python function to reverse a string. The function should be an optimal solution in terms of time and space complexity.\n# Example input to the function: abcd123\n# Example output to the function: 321dcba",
  temperature = 0.2,
  max_tokens = 150,
  top_p = 1,
  frequency_penalty = 0,
  presence_penalty = 0,
  best_of = 1,
  stop = ["#"])

This is the JSON response:

{
   "id": "cmpl-7VVfrxvTAd913vUKzrxlttYluzmbH",
   "object": "text_completion",
   "created": 1687744279,
   "model": "code-davinci-002",
   "choices": [
      {
         "text": "\n\ndef reverse_string(input_string):\n    return input_string[::-1]\n\nprint(reverse_string('abcd123'))",
         "index": 0,
         "finish_reason": "stop",
         "logprobs": null
      }
   ],
   "usage": {
      "completion_tokens": 36,
      "prompt_tokens": 46,
      "total_tokens": 82
   }
}

Conclusion

Azure OpenAI Codex has opened up a new realm of possibilities for developers by offering a powerful code generation tool. By understanding its capabilities and adopting an effective approach, developers can significantly accelerate their coding process, overcome challenges, and unleash their creativity. Whether prototyping, finding solutions, or simply enhancing productivity, Azure OpenAI Codex empowers developers to reach new heights in software development.

Thanks for reading

Thank you very much for reading; I hope you found this article interesting and may be useful in the future. If you have any questions or ideas you need to discuss, it will be a pleasure to collaborate and exchange knowledge.


Similar Articles