This article provides an overview of the Azure OpenAI modules, explicitly focusing on the Davinci module. Sample code written in C# is included to demonstrate its usage. We will explore the other modules in more detail in an upcoming blog post.
Azure OpenAI offers a range of models categorized by their intended task and capability. These models are grouped into different families, each associated with a specific set of tasks. However, not all models are available in all regions at present. The below describes the model families that are currently available.
There are three sets of models offered by OpenAI. Below is just an overview of the module information.
- GPT-3 for natural language understanding and generation
- Codex for code understanding and generation (including translation from natural language to code), and Embeddings for utilizing and understanding semantic representations of text data.
- The Embeddings models come in three families for different functions: similarity, text search, and code search.
Let's start in detail GPT-3 Module
GPT-3 Module
The GPT-3 models are capable of natural language understanding and generation and are available in four different capabilities, each with varying levels of power and speed suited to specific tasks. The most capable model is Davinci, while Ada is the fastest. The following list shows the latest versions of the GPT-3 models ordered by increasing capability:
- text-ada-001
- text-babbage-001
- text-curie-001
- text-davinci-003
Davinici
Davinci is the most potent GPT-3 model and can perform all the tasks of other models with less instruction. It's the best choice for applications that require a deep understanding of content, such as summarizing specific audiences and generating creative content. However, Davinci requires more computing resources, making it slower and more expensive than other models.
Davinci also excels in understanding the intent of the text, solving various logic problems, and explaining character motives. It has successfully tackled some of the most challenging AI problems, especially those involving cause and effect.
Davinci is recommended for use in complex intent, cause-and-effect analysis, and summarization for audiences.
How to Create an Azure OpenAI Deployment and Test a Model: A Step-by-Step Guide
To start this sample, you need to create an Azure OpenAI deployment. This section focuses on how to create an Azure OpenAI deployment.
- After creating the resource, go to "Model deployments" and create a new deployment.
- Provide a name for the Model deployment and select the model you wish to test.
- For this example, we have chosen "text-davinci-002".
Example
This C# code example showcases the Azure OpenAI GPT-3 Davinci model's capacity to handle complex intent. The code generates an email text that follows up with a potential client after a meeting. As a sales representative for a software company, the email should explain the advantages of the company's software, address any concerns the client may have raised, and invite them to a demo of the software.
To use C# language, I have created a Console application using Visual Studio 2022.
The code initializes a string variable "inputMessage" with a message prompt for the OpenAI GPT-3 model. The message prompt includes a scenario where a sales representative for a software company needs to write an email to a potential client explaining the benefits of the company's software, addressing any concerns the client may have expressed, and inviting them to a demo of the software.
The code then calls the "ConnectAzureOpenAI" method with the "Url" and "key" variables as parameters to send the prompt to the OpenAI GPT-3 model and obtain a response. The response is stored in the "result" variable using the "await" keyword to wait for the response.
static async Task Main(string[] args) {
Console.WriteLine("Welcome to Azure OpenAI Davinci Example");
var inputMessage = "You are a sales representative for a software company. You have just finished a meeting with a potential client, and you need to follow up with them via email." + " Write an email to the client that explains the benefits of your company's software, addresses any concerns the client may have expressed, and invites them to a demo of the software..";
var result = await ConnectAzureOpenAI(Url, key, inputMessage);
Console.WriteLine(result);
Console.Read();
}
The code initializes a JSON object named "jsonContent" with various properties, including "prompt", "max_tokens", "temperature", "frequency_penalty", "presence_penalty", "top_p", and "best_of". These properties are used to configure parameters for the OpenAI GPT-3 model.
The code then creates an HTTP POST request message and sets the request URL. It creates a new string content object "prompt" using the serialized JSON object "jsonContent" as the content, with UTF8 encoding and "application/json" media type. The "prompt" content is then set as the content of the request message.
using
var client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("api-key", key);
var jsonContent = new {
prompt = message,
max_tokens = 1024,
temperature = 0.7,
frequency_penalty = 0.5,
presence_penalty = 0,
top_p = 0.5,
best_of = 1
};
var msg = new HttpRequestMessage(HttpMethod.Post, url);
var prompt = new StringContent(JsonConvert.SerializeObject(jsonContent), Encoding.UTF8, "application/json");
msg.Content = prompt;
The code sends the HTTP POST request message to the OpenAI GPT-3 model via the HttpClient instance "client". It uses the "await" keyword to wait for the response and the "ConfigureAwait(false)" method to ensure that the continuation does not have to run in the same context as the previous code.
If the response status code indicates success, the code reads the response content as a string and parses it into a JObject. It then checks if the "choices" key exists in the result and has at least one value. If so, it extracts the summary text from the first choice in the "choices" array, stored in the "summary" variable.
var res = await client.SendAsync(msg).ConfigureAwait(false);
if (res.IsSuccessStatusCode) {
// Read the response content as a string
var stringResult = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
// Parse the string result as a JObject
var result = JObject.Parse(stringResult);
// If the "choices" key exists in the result and it has at least one value
if (result["choices"] != null && result["choices"].Any()) {
// Extract the summary text from the first choice in the "choices" array
summary = result["choices"][0]["text"].ToString();
}
}
Here is an example of how each request to Azure OpenAI will produce a different output.