Getting started with OpenAI Completions using .NET

Author: Luis Quintanilla
Translation: Alan Wang
Typesetting: Alan Wang

Welcome to the blog series on OpenAI and .NET!

If you're new here, check out our first article in which we introduced the series and showed you how to get started with OpenAI in .NET.

This article focuses on Completions. let's start!

What are Completions?

Completions are responses generated by models like GPT.
The types of responses you can generate include:

text

Enter
to translate "Hello" into Spanish.
Output
"Hola"

the code

Input
Creates a C# function that adds two integers
Output

int Add(int x, int y)
{
    
    
    return x + y
}

image

Input A cozy pug dog wrapped
in a blanket Output This article focuses on text and code completion.

insert image description here

How is Completion generated?

Generating completion requires several parts:

  • Model
  • user input (prompt)

insert image description here
You can think of a model as a stateful function. A model is a system used to algorithmically identify patterns in data. The power of a model depends on the data and algorithms used to build it. For more details on the different types of models and their capabilities, see the Azure OpenAI Service Models documentation .

The algorithms used by OpenAI models such as GPT are neural networks called transformers. More specifically, models like GPT are often referred to as Large Language Models (LLMs), due to their size (large) and the type of problems they are designed to solve (language).

The technical details of large language models are not within the scope of this article. However, if you want to learn more, check out the paper What ChatGPT is doing...and why it works and the paper Language Models as Few Instance Learners .

User input (also known as a prompt) is what instructs the model and provides instructions for what you want the model to output. For more precise results, hints include the following:

  • the context
  • task/question

To summarize for second graders:

Jupiter is the fifth closest planet to the sun and the largest planet in the solar system. It is a gas giant with a mass one-thousandth that of the sun but 2.5 times the mass of all the other planets in the solar system combined. One of the brightest objects visible to the naked eye in the night sky is Jupiter, known to ancient civilizations long before recorded history. Jupiter is named after the main god Jupiter in Roman mythology[19]. Viewed from Earth, Jupiter is bright enough that its reflected light can cast visible shadows [20], and is usually the third brightest natural object in the night sky after the Moon and Venus.

It can be split into:

  • Context: Jupiter is the fifth closest planet to the sun and the largest planet in the solar system. It is a gas giant with a mass one-thousandth that of the sun but 2.5 times the mass of all the other planets in the solar system combined. One of the brightest objects visible to the naked eye in the night sky is Jupiter, known to ancient civilizations long before recorded history. Jupiter is named after the main god Jupiter in Roman mythology[19]. Viewed from Earth, Jupiter is bright enough that its reflected light can cast visible shadows [20], and is usually the third brightest natural object in the night sky after the Moon and Venus.

  • Assignment/Questions: Summarize this content for 2nd graders:

The final completion result should be similar to:

Jupiter is the fifth closest planet to the sun and the largest planet in our solar system. It is very bright in the night sky and has been known since ancient times. It is named after Jupiter, the main god in Roman mythology. Usually it is the third brightest object in the night sky after the Moon and Venus.

The important part here is the task/question, as this is what guides the model to produce a specific type of output. For example, if I change the task/question to "How does Jupiter's mass compare to the Sun?", I can expect a completion like "Jupiter's mass is one-thousandth that of the Sun, or 0.001 solar mass."

As you can see, when you combine models like GPT with a well-formed hint, they provide an effective foundation for building all kinds of applications that use AI.

How much text can I provide in my prompt?

Your prompt size is measured in "tokens". Typically, GPT models will split words into "tokens". Common polysyllabic words are usually a token, while less common words are split by syllable. Each model has a token limit. For more details, check out the Azure OpenAI Service Model documentation .

To count the number of tokens in your prompt, use the Microsoft.ML.Tokenizers NuGet package.

See the tokenization example for more details.

How to start generating my completion?

Now that you know what completions are and how they are generated, it's time to start generating your own completions. Steps to get started:

  1. Register or request access to OpenAI or the Azure OpenAI service .
  2. Use your credentials to start experimenting with the OpenAI .NET samples .

Next step

In the next article, we'll discuss in more detail the topic of prompt engineering, the process of optimizing prompts to produce more precise answers.

we want to hear from you

Help us learn more about how you use AI in your applications. Please take a few minutes to complete this survey .

Are there any other topics you would like to learn more about? Let us know in the comments section.

Guess you like

Origin blog.csdn.net/MicrosoftReactor/article/details/131466588
Recommended