Skip to content

Examples

Andrew Lambert edited this page Feb 13, 2023 · 37 revisions

Examples

See also the OpenAI documentation examples page.

Correct text

This example asks the AI to correct an English sentence.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = "Correct this to standard English" ' natural language instructions to AI
  Dim prompt As String = "This ez uh test uv teh OpenAI AIP"
  Dim result As OpenAI.Response = OpenAI.Completion.Edit(prompt, instruction)
  Dim correction As String = result.GetResult()

Translate text

This example asks the AI to translate an English sentence into French.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = "Translate this into French: " + EndOfLine.UNIX + EndOfLine.UNIX
  Dim prompt As String = "Hello, I would like to purchase some matches"
  Dim result As OpenAI.Response = OpenAI.Completion.Create(instruction + prompt)
  Dim translation As String = result.GetResult()

Explain source code

This example asks the AI to explain a snippet of Python source code.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = """""""" + EndOfLine.UNIX + "Here's what the above class is doing:" + EndOfLine.UNIX + "1." + EndOfLine.UNIX + EndOfLine.UNIX
  Dim prompt As String = "print('Hello, world!')" + EndOfLine.UNIX + EndOfLine.UNIX
  Dim result As OpenAI.Response = OpenAI.Completion.Create(prompt + instruction, 1, OpenAI.Model.Lookup("code-davinci-002"))
  Dim explanation As String = result.GetResult()

Generate picture

This example asks the AI to generate a picture from a natural language prompt.

  OpenAI.APIKey = "YOUR API KEY"
  Dim result As OpenAI.Response = OpenAI.Image.Generate("A rodent of unusual size")
  Dim p As Picture = result.GetResult()

Edit a picture

This example asks the AI to edit a picture from a natural language prompt and a mask picture. The mask is a picture of the same dimensions as the original whose fully transparent areas (i.e. where alpha is zero) indicates where the original should be edited. If the original is already transparent in the target areas then the mask can be omitted.

  OpenAI.APIKey = "YOUR API KEY"
  Dim src As FolderItem = SpecialFolder.Desktop.Child("my portrait.png")
  Dim original As Picture = Picture.Open(src)
  src = SpecialFolder.Desktop.Child("my portrait mask.png")
  Dim mask As Picture = Picture.Open(src)
  ' let's add a NYC skyline as a backdrop to our portrait
  Dim img As OpenAI.Response = OpenAI.Image.Edit(original, "The New York City skyline", mask)
  Dim output As Picture = img.GetResult()

Check text for offensive content

This example asks the AI to check a comment for offensive content.

  OpenAI.APIKey = "YOUR API KEY"
  Dim result As OpenAI.Response = OpenAI.Moderation.Create("I will kill that bitch")
  Dim response As JSONItem = result.GetResult()
  If response.Value("flagged") = True Then
    Dim categories As JSONItem = response.Value("categories")
    Dim scores As JSONItem = response.Value("category_scores")
    ' etc
  End If

Sending a custom request

This example creates a new OpenAI.Request object and uses it to set the parameters of a custom request.

  OpenAI.APIKey = "YOUR API KEY"
  Dim request As New OpenAI.Request
  request.Prompt = "What is the airspeed velocity of an unladen European swallow?"
  request.Model = OpenAI.Model.Lookup("text-davinci-003")
  request.Temperature = 0.9
  request.NumberOfResults = 5
  request.BestOf = 10
  request.MaxTokens = 128
  
  Dim result As OpenAI.Response = OpenAI.Completion.Create(request)
  Dim choices() As String
  For i As Integer = 0 To result.ResultCount - 1
    choices.Append(result.GetResult(i))
  Next
Clone this wiki locally